Skip to content

Commit

Permalink
extract TYPE_SUPPORTED, replace PICKER_TEMPLATE with DOM.create
Browse files Browse the repository at this point in the history
  • Loading branch information
chemerisuk committed Nov 17, 2018
1 parent c5c4264 commit 8c0ba94
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
}
return false;
}());
const TYPE_SUPPORTED = (function() {
// use a stronger type support detection that handles old WebKit browsers:
// http://www.quirksmode.org/blog/archives/2015/03/better_modern_i.html
return DOM.create("<input type='date'>").value("_").value() !== "_";
}());

var HTML = DOM.get("documentElement"),
ampm = (pos, neg) => HTML.lang === "en-US" ? pos : neg,
Expand Down Expand Up @@ -55,9 +60,6 @@
return date.toUTCString().split(" ").slice(2, 4).join(" ");
}

const PICKER_TEMPLATE = DOM.create(html`
<dateinput-picker tabindex="-1"></dateinput-picker>`);

const PICKER_BODY_HTML = html`
<style>
body {
Expand Down Expand Up @@ -156,7 +158,7 @@ table+table[aria-hidden=true] {
this._svgTextFont = this.css("font");
this._svgTextOffset = ["padding-left", "border-left-width", "text-indent"].map(p => parseFloat(this.css(p))).reduce((a, b) => a + b);

const picker = PICKER_TEMPLATE.clone(true);
const picker = DOM.create("<dateinput-picker>");
const object = DOM.create("<object>")[0];
object.type = "text/html";
object.width = "100%";
Expand All @@ -167,6 +169,7 @@ table+table[aria-hidden=true] {
object.data = "about:blank";
}

picker.set("tabindex", -1);
picker.css("z-index", 1 + (this.css("z-index") | 0));

this.before(picker.append(DOM.constructor(object)).hide());
Expand All @@ -181,20 +184,14 @@ table+table[aria-hidden=true] {

if (polyfillType === "none") return true;

if (!polyfillType || polyfillType !== deviceType && polyfillType !== "all") {
// use a stronger type support detection that handles old WebKit browsers:
// http://www.quirksmode.org/blog/archives/2015/03/better_modern_i.html
if (this[0].type === "date") return true;
// persist current value to restore it later
this.set("defaultValue", this.value());
// if browser allows invalid value then it doesn't support the feature
return this.value("_").value() !== "_";
} else {
// remove native control
if (polyfillType && (polyfillType === deviceType || polyfillType === "all")) {
// remove native browser implementation
this.set("type", "text");
// force applying the polyfill
return false;
}

return TYPE_SUPPORTED;
},
_initPicker(object, picker) {
const pickerRoot = DOM.constructor(object.contentDocument);
Expand Down

0 comments on commit 8c0ba94

Please sign in to comment.