forked from brianblakely/nodep-date-input-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 66
/
input.js
159 lines (135 loc) · 4.36 KB
/
input.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
import thePicker from './picker.js';
import locales from './locales.js';
import dateFormat from './dateformat.js';
export default class Input {
constructor(input) {
this.element = input;
this.element.setAttribute(`data-has-picker`, ``);
this.locale =
this.element.getAttribute(`lang`)
|| document.body.getAttribute(`lang`)
|| `en`;
this.format = this.element.getAttribute('date-format')
|| document.body.getAttribute('date-format')
|| this.element.getAttribute(`data-date-format`)
|| document.body.getAttribute(`data-date-format`)
|| `yyyy-mm-dd`;
this.localeText = this.getLocaleText();
Object.defineProperties(
this.element,
{
'valueAsDate': {
get: () => {
if(!this.element.value) {
return null;
}
const format = this.format || 'yyyy-mm-dd';
const parts = this.element.value.match(/(\d+)/g);
let i = 0, fmt = {};
format.replace(/(yyyy|dd|mm)/g, part=> {
fmt[part] = i++;
});
return new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]);
},
set: val => {
this.element.value = dateFormat(val, this.format);
}
},
'valueAsNumber': {
get: ()=> {
if(!this.element.value) {
return NaN;
}
return this.element.valueAsDate.valueOf();
},
set: val=> {
this.element.valueAsDate = new Date(val);
}
}
}
);
// Open the picker when the input get focus,
// also on various click events to capture it in all corner cases.
const showPicker = (e) => {
const elm = this.element;
elm.locale = this.localeText;
const didAttach = thePicker.attachTo(elm);
};
this.element.addEventListener(`focus`, showPicker);
this.element.addEventListener(`mouseup`, showPicker);
// Update the picker if the date changed manually in the input.
this.element.addEventListener(`keydown`, e => {
const date = new Date();
switch(e.keyCode) {
case 9:
case 27:
thePicker.hide();
break;
case 38:
if(this.element.valueAsDate) {
date.setDate(this.element.valueAsDate.getDate() + 1);
this.element.valueAsDate = date;
thePicker.pingInput();
}
break;
case 40:
if(this.element.valueAsDate) {
date.setDate(this.element.valueAsDate.getDate() - 1);
this.element.valueAsDate = date;
thePicker.pingInput();
}
break;
default:
break;
}
thePicker.sync();
});
this.element.addEventListener(`keyup`, e => {
thePicker.sync();
});
}
getLocaleText() {
const locale = this.locale.toLowerCase();
for(const localeSet in locales) {
const localeList = localeSet.split(`_`);
localeList.map(el=>el.toLowerCase());
if(
!!~localeList.indexOf(locale)
|| !!~localeList.indexOf(locale.substr(0,2))
) {
return locales[localeSet];
}
}
}
// Return false if the browser does not support input[type="date"].
static supportsDateInput() {
const input = document.createElement(`input`);
input.setAttribute(`type`, `date`);
const notADateValue = `not-a-date`;
input.setAttribute(`value`, notADateValue);
return !(input.value === notADateValue);
}
// Will add the Picker to all inputs in the page.
static addPickerToDateInputs() {
// Get and loop all the input[type="date"]s in the page that do not have `[data-has-picker]` yet.
const dateInputs = document.querySelectorAll(`input[type="date"]:not([data-has-picker])`);
const length = dateInputs.length;
if(!length) {
return false;
}
for(let i = 0; i < length; ++i) {
new Input(dateInputs[i]);
}
}
static addPickerToOtherInputs() {
// Get and loop all the input[type="text"] class date-polyfill in the page that do not have `[data-has-picker]` yet.
const dateInputs = document.querySelectorAll(`input[type="text"].date-polyfill:not([data-has-picker])`);
const length = dateInputs.length;
if(!length) {
return false;
}
for(let i = 0; i < length; ++i) {
new Input(dateInputs[i]);
}
}
}