forked from brianblakely/nodep-date-input-polyfill
/
iife.html
153 lines (143 loc) · 5.4 KB
/
iife.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>nodep Polyfill Test</title>
<meta name="description" content="nodep Polyfill Test">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<script src="dist/esm-date-input-polyfill.js" ></script>
</head>
<body>
<h2>Using IIFE</h2>
<form>
<div data-force-date-input-polyfill>
<fieldset>
<legend>Empty Date No Min/Max</legend>
<p>
<label>Date:
<input type="date" id="simple-date">
</label>
</p>
<p>
<label>value:
<output class="date-value" for="simple-date"></output>
</label>
</p>
<p>
<label>valueAsDate:
<output class="value-as-date" for="simple-date"></output>
</label>
</p>
</fieldset>
<fieldset>
<legend>With Value and Min/Max Attributes</legend>
<p>
<label>Date:
<input type="date" id="value-attr-date" value="2020-04-05" min="2020-04-03" max="2020-04-19" lang="de-DE">
</label>
This should have the date value set to <span class="iso-date date-value">2020-04-05</span>
and only allow a minimum date of <span class="iso-date date-min">2020-04-03</span>
and maximum <span class="iso-date date-max">2020-04-19</span>.
</p>
<p>
<label>value:
<output class="date-value" for="value-attr-date"></output>
</label>
</p>
<p>
<label>valueAsDate:
<output class="value-as-date" for="value-attr-date"></output>
</label>
</p>
</fieldset>
<fieldset>
<legend>Dynamically Created</legend>
<p>
<label id="dynamic-create-date">Date:
</label>
</p>
<p>
<label>value:
<output class="date-value" for="dynamic-date"></output>
</label>
</p>
<p>
<label>valueAsDate:
<output class="value-as-date" for="dynamic-date"></output>
</label>
</p>
</fieldset>
</div>
<fieldset>
<legend>native date input (if supported):</legend>
<p>
<label>Date:
<input type="date" id="native-date" value="2020-04-05" min="2020-04-03" max="2020-04-19" lang="de-DE" >
</label>
This should have the date value set to <span class="iso-date date-value">2020-04-05</span>
and only allow a minimum date of <span class="iso-date date-min">2020-04-03</span>
and maximum <span class="iso-date date-max">2020-04-19</span>.
</p>
<p>
<label>value:
<output class="date-value" for="native-date"></output>
</label>
</p>
<p>
<label>valueAsDate:
<output class="value-as-date" for="native-date"></output>
</label>
</p>
</fieldset>
<input type="submit">
</form>
<script>
(function() {
// setting the value min & max to be around the current date, purely so that the style of the current date
// can be displayed, plus the today button can be used.
setDateAttributes();
dateInputPolyfill.polyfillIfRequired({watchForInsert: true, forcePolyfill: true});
var outputs = document.getElementsByTagName('output');
outputs.forEach(watchDates);
// dynamically create date element
setTimeout(function() {
var inpt = document.createElement("input");
inpt.setAttribute("type", "date");
inpt.setAttribute("id", "dynamic-date");
document.getElementById("dynamic-create-date").appendChild(inpt);
Array.prototype.filter.call(outputs, function(o) { return o.getAttribute('for') === 'dynamic-date' }).forEach(watchDates);
}, 500);
function watchDates(o) {
var outputFor = document.getElementById(o.getAttribute('for'));
if (outputFor) {
var onInput = o.className === 'date-value'
? function(el) { o.innerHTML = el.target.value; }
: function(el) { o.innerHTML = String(el.target.valueAsDate); };
outputFor.addEventListener('input', onInput);
}
}
function setDateAttributes() {
const d = new Date();
const asIso = function(dt) { return dt.toISOString().slice(0, 10) };
d.setDate(d.getDate() - 2);
updateDateAttrAndDescriptions('value', asIso(d));
d.setDate(d.getDate() - 4);
updateDateAttrAndDescriptions('min', asIso(d));
d.setDate(d.getDate() + 15);
updateDateAttrAndDescriptions('max', asIso(d));
}
function updateDateAttrAndDescriptions(attrName, value) {
querySelectorForEach('input[' + attrName + ']', function(el) {
el.setAttribute(attrName, value);
});
querySelectorForEach('span.date-' + attrName, function(el) {
el.innerHTML = value;
});
}
function querySelectorForEach(selector, delegate) {
Array.prototype.forEach.call(document.querySelectorAll(selector), delegate);
}
})();
</script>
</body>
</html>