forked from Pascalmh/date-input-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picker.js
334 lines (282 loc) · 9.12 KB
/
picker.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
class Picker {
constructor() {
// This is a singleton.
if(window.thePicker) {
return window.thePicker;
}
this.date = new Date();
this.input = null;
this.isOpen = false;
// The picker element. Unique tag name attempts to protect against
// generic selectors.
this.container = document.createElement(`date-input-polyfill`);
// Add controls.
// Year picker.
this.year = document.createElement(`select`);
Picker.createRangeSelect(
this.year,
1890, // oldest person alive born in 1894
this.date.getFullYear() + 20
);
this.year.className = `yearSelect`;
this.year.addEventListener(`change`, ()=> {
this.date.setYear(this.year.value);
this.refreshDaysMatrix();
});
const yearWrapper = document.createElement(`span`);
yearWrapper.className = `yearSelect-wrapper`;
yearWrapper.appendChild(this.year);
this.container.appendChild(yearWrapper);
// Month picker.
this.month = document.createElement(`select`);
this.month.className = `monthSelect`;
this.month.addEventListener(`change`, ()=> {
this.date.setMonth(this.month.value);
this.refreshDaysMatrix();
});
const monthWrapper = document.createElement(`span`);
monthWrapper.className = `monthSelect-wrapper`;
monthWrapper.appendChild(this.month);
this.container.appendChild(monthWrapper);
// Today button.
this.today = document.createElement(`button`);
this.today.textContent = `Today`;
this.today.addEventListener(`click`, ()=> {
const today = new Date();
this.date = new Date(
`${
today.getFullYear()
}/${
`0${today.getMonth()+1}`.slice(-2)
}/${
`0${today.getDate()}`.slice(-2)
}`
);
this.setInput();
});
this.container.appendChild(this.today);
// Setup unchanging DOM for days matrix.
const daysMatrix = document.createElement(`table`);
this.daysHead = document.createElement(`thead`);
this.days = document.createElement(`tbody`);
// THIS IS THE BIG PART.
// When the user clicks a day, set that day as the date.
// Uses event delegation.
this.days.addEventListener(`click`, e=> {
const tgt = e.target;
if(!tgt.hasAttribute(`data-day`)) {
return false;
}
const curSel = this.days.querySelector(`[data-selected]`);
if(curSel) {
curSel.removeAttribute(`data-selected`);
}
tgt.setAttribute(`data-selected`, ``);
this.date.setDate(parseInt(tgt.textContent));
this.setInput();
});
daysMatrix.appendChild(this.daysHead);
daysMatrix.appendChild(this.days);
this.container.appendChild(daysMatrix);
this.hide();
document.body.appendChild(this.container);
this.removeClickOut = e => {
if(this.isOpen) {
let el = e.target;
let isPicker = el === this.container || el === this.input;
while(!isPicker && (el = el.parentNode)) {
isPicker = el === this.container;
}
((e.target.getAttribute(`type`) !== `date` && !isPicker) || !isPicker)
&& this.hide();
}
};
this.removeBlur = e => {
if(this.isOpen) {
this.hide();
}
};
}
// Hide.
hide() {
this.container.setAttribute(`data-open`, this.isOpen = false);
// Close the picker when clicking outside of a date input or picker.
if(this.input) { this.input.blur() }
document.removeEventListener(`mousedown`, this.removeClickOut);
document.removeEventListener(`touchstart`, this.removeClickOut);
}
// Show.
show() {
this.container.setAttribute(`data-open`, this.isOpen = true);
// Close the picker when clicking outside of a date input or picker.
setTimeout(()=>{
document.addEventListener(`mousedown`, this.removeClickOut);
document.addEventListener(`touchstart`, this.removeClickOut);
}, 500);
// when used in a single-page app or otherwise,
// hide datepicker when the browser's back button is pressed
window.onpopstate = () => {
this.hide();
}
}
// Position picker below element. Align to element's right edge.
goto(element) {
const rekt = element.getBoundingClientRect();
this.container.style.top = `${
rekt.top + rekt.height
+ (document.documentElement.scrollTop || document.body.scrollTop)
+ 3
}px`;
const contRekt = this.container.getBoundingClientRect();
const width = contRekt.width ? contRekt.width : 280;
const classWithOutPos = () => {
return this.container.className
.replace(`polyfill-left-aligned`, ``)
.replace(`polyfill-right-aligned`, ``)
.replace(/\s+/g,` `).trim();
};
let base = rekt.right - width;
if(rekt.right < width) {
base = rekt.left;
this.container.className = `${classWithOutPos()} polyfill-left-aligned`;
} else {
this.container.className = `${classWithOutPos()} polyfill-right-aligned`;
}
this.container.style.left = `${
base
+ (document.documentElement.scrollLeft || document.body.scrollLeft)
}px`;
this.show();
}
// Initiate I/O with given date input.
attachTo(input) {
if(input === this.input && this.isOpen) {
return false;
}
this.input = input;
this.refreshLocale();
this.sync();
this.goto(this.input);
return true;
}
// Match picker date with input date.
sync() {
// fixes bug where an empty calendar appears if year is missing from keyboard input
if (!isNaN(Date.parse(this.input.valueAsDate))) {
this.date = Picker.absoluteDate(this.input.valueAsDate);
} else {
this.date = new Date();
}
this.year.value = this.date.getFullYear();
this.month.value = this.date.getMonth();
this.refreshDaysMatrix();
}
// Match input date with picker date.
setInput() {
this.input.valueAsDate = this.date;
this.input.focus();
setTimeout(()=> { // IE wouldn't hide, so in a timeout you go.
this.hide();
}, 100);
this.pingInput();
}
refreshLocale() {
if(this.locale === this.input.locale) {
return false;
}
this.locale = this.input.locale;
this.today.textContent = this.locale.today || `Today`;
const daysHeadHTML = [`<tr>`];
for(let i = 0, len = this.locale.days.length; i < len; ++i) {
daysHeadHTML.push(`<th scope="col">${this.locale.days[i]}</th>`);
}
this.daysHead.innerHTML = daysHeadHTML.join(``);
Picker.createRangeSelect(
this.month,
0,
11,
this.locale.months
);
}
refreshDaysMatrix() {
this.refreshLocale();
// Determine days for this month and year,
// as well as on which weekdays they lie.
const year = this.date.getFullYear(); // Get the year (2016).
const month = this.date.getMonth(); // Get the month number (0-11).
const startDay = new Date(year, month, 1).getDay(); // First weekday of month (0-6).
const maxDays = new Date(
this.date.getFullYear(),
month + 1,
0
).getDate(); // Get days in month (1-31).
// The input's current date.
const selDate = Picker.absoluteDate(this.input.valueAsDate) || false;
// Are we in the input's currently-selected month and year?
const selMatrix =
selDate
&& year === selDate.getFullYear()
&& month === selDate.getMonth();
// Populate days matrix.
const matrixHTML = [];
for(let i = 0; i < maxDays + startDay; ++i) {
// Add a row every 7 days.
if(i % 7 === 0) {
matrixHTML.push(`
${i !== 0 ? `</tr>` : ``}
<tr>
`);
}
// Add new column.
// If no days from this month in this column, it will be empty.
if(i + 1 <= startDay) {
matrixHTML.push(`<td></td>`);
continue;
}
// Populate day number.
const dayNum = i + 1 - startDay;
const selected = selMatrix && selDate.getDate() === dayNum;
matrixHTML.push(
`<td data-day ${selected ? `data-selected` : ``}>
${dayNum}
</td>`
);
}
this.days.innerHTML = matrixHTML.join(``);
}
pingInput() {
// Dispatch DOM events to the input.
let inputEvent;
let changeEvent;
// Modern event creation.
try {
inputEvent = new Event(`input`);
changeEvent = new Event(`change`);
}
// Old-fashioned way.
catch(e) {
inputEvent = document.createEvent(`KeyboardEvent`);
inputEvent.initEvent(`input`, true, false);
changeEvent = document.createEvent(`KeyboardEvent`);
changeEvent.initEvent(`change`, true, false);
}
this.input.dispatchEvent(inputEvent);
this.input.dispatchEvent(changeEvent);
}
static createRangeSelect(theSelect, min, max, namesArray) {
theSelect.innerHTML = ``;
for(let i = min; i <= max; ++i) {
const aOption = document.createElement(`option`);
theSelect.appendChild(aOption);
const theText = namesArray ? namesArray[i - min] : i;
aOption.text = theText;
aOption.value = i;
}
return theSelect;
}
static absoluteDate(date) {
return date && new Date(date.getTime() + date.getTimezoneOffset()*60*1000);
}
}
window.thePicker = new Picker();
export default window.thePicker;