-
Notifications
You must be signed in to change notification settings - Fork 13
/
px-datetime-validate.html
369 lines (346 loc) · 14.4 KB
/
px-datetime-validate.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../app-localize-behavior/app-localize-behavior.html"/>
<script>
(function() {
var PxDatetimeBehavior = window.PxDatetimeBehavior = (window.PxDatetimeBehavior || {});
/**
* Collection of validation scripts for px-datetime components
* Dependencies: momentjs
*
* @polymerBehavior PxDatetimeBehavior.Validate
*/
PxDatetimeBehavior.Validate = [Polymer.AppLocalizeBehavior, {
properties: {
/**
* Boolean stating if the current component is valid. Gets updated in '_validateInput'
*
* @private
*/
isValid: {
type: Boolean,
value: true,
notify: true
},
/**
* String used to describe the current invalid state
*/
validationErrorMessage: {
type: String,
value: 'invalid'
},
/**
* set a default for localizing
*/
language: {
type: String,
value: 'en'
},
/**
* Use the key for localization if value for language is missing. Should
* always be true for px components
*/
useKeyIfMissing: {
type: Boolean,
value: true
},
/**
* used to pass in strings for localization
*/
resources: {
type: Object,
value: function () {
return {
'en': {
'Future dates are not allowed': 'Future dates are not allowed',
'Past dates are not allowed': 'Past dates are not allowed',
'Year': 'Year',
'Month': 'Month',
'Day': 'Day',
'Hour': 'Hour',
'Minute': 'Minute',
'Second': 'Second',
'Millisecond': 'Millisecond',
'is not valid': 'is not valid',
'This hour is not within a 12 hour clock': 'This hour is not within a 12 hour clock'
}
};
}
}
},
observers: ['_updateValidationEvent(validationErrorMessage)'],
/**
* Validation function for 'px-datetime-entry'
*
* @event px-moment-valid
* @param {element} this
* @event px-moment-invalid
* @param {element} this
*/
_validateInput: function (format, tz) {
var inputArr = Polymer.dom(this.root).querySelectorAll('.cell'),
inputStr = this._entryInputString(inputArr);
if (inputStr) {
var inputMoment = Px.moment.tz(inputStr, format, tz);
if (inputMoment.isValid()) {
this.set('isValid', true);
this.toggleClass('validation-error', false, this.$.wrapper);
this.fire('px-moment-valid', { "element": this });
}
else {
this.set('validationErrorMessage', this._determineValidationMessage(inputMoment, new Object()));
this.set('isValid', false);
this.toggleClass('validation-error', true, this.$.wrapper);
this.fire('px-moment-invalid', { "element": this });
}
}
},
/**
* Validate a complete field
* called in px-datetime-field and px-datetime-range-field
*
* @param {String} funcOrigin - The function that called validation. Used for `_handleIncompleteEntries`
* @return validation result:
* - MomentObj - The validated new moment obj
* - False - If the new moment is invalid
* - FieldSame - If the new moment is the same as the old moment
* - FieldBlank - If the visible field(s) are blank
*/
_validateField: function (funcOrigin) {
var fieldNodes = new Object(),
dateEntry = Polymer.dom(this.root).querySelector('#date'),
timeEntry = Polymer.dom(this.root).querySelector('#time');
if (dateEntry !== null) { fieldNodes.dateCells = Polymer.dom(dateEntry.root).querySelectorAll('.cell') };
if (timeEntry !== null) { fieldNodes.timeCells = Polymer.dom(timeEntry.root).querySelectorAll('.cell') };
var dateString = (dateEntry !== null) ? this._entryInputString(fieldNodes.dateCells) : '',
timeString = (timeEntry !== null) ? this._entryInputString(fieldNodes.timeCells) : '';
// If the timeString is incomplete when only the time entry is showing or
// if the dateString is incomplete when only the date entry is showing or
// if either dateString or timeSting are incomplete when both entries are showing
// run `_handleIncompleteEntries()`
if ( (this.hideDate && !timeString) || (this.hideTime && !dateString) || ((!this.hideDate && !this.hideTime) && (!dateString || !timeString)) ) {
var entry = this.hideDate ? timeEntry : dateEntry;
return this._handleIncompleteEntries(funcOrigin, entry, fieldNodes);
}
//Create a string from all of the entries put together
var dateTimeString = dateString.trim() + " " + timeString.trim();
dateTimeString = dateTimeString.split('\xa0').join(' '); //added to be able to use Moment strict parsing
var dateFormat = this.hideDate ? '' : this.dateFormat,
timeFormat = this.hideTime ? '' : this.timeFormat,
dateTimeFormat = dateFormat + " " + timeFormat,
dateTimeMoment = Px.moment.tz(dateTimeString, dateTimeFormat, this.timeZone);
if(this.momentObj) {
//preserve parts of the momentObj that are not displayed
if (!/Y/.test(dateTimeFormat)) { dateTimeMoment.year(this.momentObj.year()); }
if (!/M/.test(dateTimeFormat)) { dateTimeMoment.month(this.momentObj.month()); }
if (!/D/.test(dateTimeFormat)) { dateTimeMoment.date(this.momentObj.date()); }
if (!/[Hhk]/.test(dateTimeFormat)) { dateTimeMoment.hour(this.momentObj.hour()); }
if (!/m/.test(dateTimeFormat)) { dateTimeMoment.minute(this.momentObj.minute()); }
if (!/s/.test(dateTimeFormat)) { dateTimeMoment.second(this.momentObj.second()); }
if (!/S/.test(dateTimeFormat)) { dateTimeMoment.millisecond(this.momentObj.millisecond()); }
}
return this._validateMomentObj(dateTimeMoment);
},
/**
* Validate a moment object
* called in from _validateField and px-datetime-field
*
* @param {Object} momentObj - Moment object getting validated
* @return validation result:
* - MomentObj - The validated new moment obj
* - False - If the new moment is invalid
* - FieldSame - If the new moment is the same as the old moment
*/
_validateMomentObj: function (validatingMomentObj) {
//If null is passed in clear the validation errors
if (validatingMomentObj === null ) {
this._validField(validatingMomentObj);
return;
}
var invalidObj = new Object();
invalidObj.futureSelection = this.blockFutureDates && validatingMomentObj.isAfter(Px.moment.tz(Px.moment(), this.timeZone));
invalidObj.pastSelection = this.blockPastDates && validatingMomentObj.isBefore(Px.moment.tz(Px.moment(), this.timeZone));
invalidObj.pastMaxSelection = this.maxDate && validatingMomentObj.isAfter(this.maxDate);
invalidObj.beforeMinSelection = this.minDate && validatingMomentObj.isBefore(this.minDate);
if ((validatingMomentObj.isValid() && !invalidObj.futureSelection && !invalidObj.pastSelection && !invalidObj.pastMaxSelection && !invalidObj.beforeMinSelection)) {
this._validField(validatingMomentObj);
//if the momentObj is null or not that same as this.momentObj return validatingMomentObj
if (!this.momentObj || !this.momentObj.isSame(validatingMomentObj)) {
return validatingMomentObj;
}
else if (this.momentObj.isSame(validatingMomentObj)) {
return 'FieldSame';
}
}
else {
this._invalidField(validatingMomentObj, invalidObj);
return false;
}
},
/**
* Reset field to valid
*
* @param {Object} submittedMoment - valid momentObj
*/
_validField: function (submittedMoment) {
if (!this.hideDate) {
var dateEntry = Polymer.dom(this.root).querySelector('#date');
if(dateEntry !== null){dateEntry.toggleClass('validation-error', false, dateEntry.$.wrapper);}
}
if (!this.hideTime) {
var timeEntry = Polymer.dom(this.root).querySelector('#time');
if(timeEntry !== null){timeEntry.toggleClass('validation-error', false, timeEntry.$.wrapper);}
}
this.set('fieldIsValid', true);
this.fire('px-moment-valid', { "element": this });
},
/**
* Set field to invalid
* Set validation Error Message, turn on validation error, set isValid to false, and fire event.
*
* @param {Object} submittedMoment - Invalid momentObj
* @param {Object} invalidObj - Stores the different invalid
*/
_invalidField: function (submittedMoment, invalidObj) {
this.set('validationErrorMessage', this._determineValidationMessage(submittedMoment, invalidObj));
this.set('fieldIsValid', false);
this.toggleClass('validation-error', true, this.$.fieldWrapper);
this.fire('px-moment-invalid', { "element": this });
},
/**
* loop through array of the entry cells and return a string of the value
*
* @param {Object} cells - Date or time cell nodes
*/
_entryInputString: function (cells) {
var outputStr = ""
for (var i = 0; i < cells.length; i++) {
var value = Polymer.dom(cells[i].root).querySelector('.datetime-entry-input').value;
if (value === '') {
return '';
}
if (cells[i].momentFormat !== "Z") {
outputStr = outputStr + value + cells[i].symbol;
}
}
return outputStr;
},
/**
* Returns a meaningful validation message
*
* @param {Object} submittedMoment - Invalid momentObj
* @param {Object} invalidObj - Stores the different invalid
*/
_determineValidationMessage: function (submittedMoment, invalidObj) {
if (!submittedMoment) { return this.localize('Incomplete'); }
if (invalidObj.futureSelection) { return this.localize('Future dates are not allowed'); }
else if (invalidObj.pastSelection) { return this.localize('Past dates are not allowed'); }
else if (invalidObj.pastMaxSelection) { return this.localize('Date is past max date'); }
else if (invalidObj.beforeMinSelection) { return this.localize('Date is before min date'); }
else {
var invalidAt = submittedMoment.invalidAt();
if (invalidAt !== -1) {
switch (invalidAt) {
case 0:
return this.localize('Year') + ' ' + submittedMoment._a[invalidAt] + ' ' + this.localize('is not valid');
break;
case 1:
return this.localize('Month') + ' ' + (submittedMoment._a[invalidAt] + 1) + ' ' + this.localize('is not valid');
break;
case 2:
return this.localize('Day') + ' ' + submittedMoment._a[invalidAt] + ' ' + this.localize('is not valid');
break;
case 3:
return this.localize('Hour') + ' ' + submittedMoment._a[invalidAt] + ' ' + this.localize('is not valid');
break;
case 4:
return this.localize('Minute') + ' ' + submittedMoment._a[invalidAt] + ' ' + this.localize('is not valid');
break;
case 5:
return this.localize('Second') + ' ' + submittedMoment._a[invalidAt] + ' ' + this.localize('is not valid');
break;
case 6:
return this.localize('Millisecond') + ' ' + submittedMoment._a[invalidAt] + ' ' + this.localize('is not valid');
break;
}
} else if (submittedMoment.parsingFlags().bigHour === true) {
return this.localize('This hour is not within a 12 hour clock');
}
}
},
/**
* Fires when 'validationErrorMessage' is changed
*
* @event px-validation-message
* @param {string} validationErrorMessage - Values validationErrorMessage
*/
_updateValidationEvent: function (validationErrorMessage) {
if (validationErrorMessage !== undefined) {
this.fire('px-validation-message', { 'validationErrorMessage': this.validationErrorMessage })
}
},
/**
* Sets the button state based on if validation has been passed or not. This in turn enables or disables the Submit button.
*
* @param {Boolean} state - Where the validation has passed or failed
*/
_submitButtonState: function (state) {
this.set('isSubmitButtonValid', state);
},
/**
* If required any incomplete momentObj is invalid
* If not required determine if the field is blank
*
* @param {String} funcOrigin - The function that called validation
* @param {Element} entry - px-datetime-entry date
* @param {Object} fieldNodes - Object of date and time cell nodes
*/
_handleIncompleteEntries: function (funcOrigin, entry, fieldNodes) {
if (funcOrigin === "_momentChanged") { return false };
if (!this.required) {
var dateEntryBlank = this.hideDate ? true : this._loopOverCellValues(fieldNodes.dateCells),
timeEntryBlank = this.hideTime ? true : this._loopOverCellValues(fieldNodes.timeCells);
if (dateEntryBlank && timeEntryBlank) {
entry.toggleClass('validation-error', false, entry.$.fieldWrapper);
entry.set('isValid', true);
return "FieldBlank";
}
this._invalidField("", new Object());
}
else {
this._invalidField("", new Object());
return false;
}
},
/**
* Checking to see if all cells are empty
* If a cell has a value return false
*
* @param {Object} cells - Date or time cell nodes
*/
_loopOverCellValues: function (cells) {
var isBlank = true;
if (cells && cells.length) {
for (i = 0; i < cells.length; i++) {
var cellInput = Polymer.dom(cells[i].root).querySelectorAll('.datetime-entry-input');
if (cellInput[0].value !== "") {
isBlank = false;
break;
}
};
}
return isBlank;
}
}];
})();
</script>