-
Notifications
You must be signed in to change notification settings - Fork 13
/
px-datetime-entry.html
502 lines (475 loc) · 16.2 KB
/
px-datetime-entry.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<!--
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="../polymer/polymer.html"/>
<link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html"/>
<link rel="import" href="px-datetime-validate.html"/>
<link rel="import" href="px-datetime-behavior.html"/>
<link rel="import" href="px-datetime-entry-cell.html"/>
<link rel="import" href="../px-icon-set/px-icon-set-utility.html"/>
<link rel="import" href="../px-icon-set/px-icon.html"/>
<link rel="import" href="../px-dropdown/px-dropdown.html"/>
<link rel="import" href="css/px-datetime-entry-styles.html"/>
<link rel="import" href="../px-overlay/px-overlay-behavior.html"/>
<!--
Range field component which displays a time or date and font awesome icon.
### Usage
<px-datetime-entry
moment="{{...}}"
date-or-time="Date"
moment-format="MM/DD/YYYY">
</px-datetime-entry>
#### Styling
The following custom property is available for styling. Please also refer to px-forms-design for additional variables that can be used to customize the field's appearance.
Custom property | Description
:----------------|:-------------
`--px-datetime-read-only-text-color` | Text color for the text timezones
`--px-datetime-entry-selected` | Text highlight color when a cell is selected
`--px-datetime-icon-color` | Color of the icons
`--px-validation-error-text-color` | Color of invalid text
@element px-datetime-entry
@homepage index.html
@demo index.html
-->
<dom-module id="px-datetime-entry">
<template>
<style include="px-datetime-entry-styles"></style>
<div id="wrapper" class="flex flex--middle">
<div class="flex flex--middle dt-flex--no-shrink">
<button class$="{{_getIconClass(hideIcon)}} btn btn--bare dt-icon-button" on-tap="_iconClicked">
<span class="a11y">{{dateOrTime}}</span>
<template is="dom-if" if="{{!hideIcon}}">
<px-icon
id="icon"
class="dt-icon"
icon="[[_setIcon(dateOrTime)]]">
</px-icon>
</template>
</button>
<template is="dom-repeat" items="{{_cellFormatArray}}">
<px-datetime-entry-cell
id="cell{{index}}"
class="cell"
order='{{index}}'
moment-obj="[[momentObj]]"
moment-format='[[item]]'
symbol=[[_returnSymbol(index,_symbolCharArray)]]
time-zone="[[timeZone]]"
is-selected="{{isSelected}}"
resources="[[resources]]"
language="[[language]]"
formats="[[formats]]">
</px-datetime-entry-cell>
<template is="dom-if" if={{_isSymbol(index,_symbolCharArray)}}>
<div class="dt-entry-symbol">
{{_returnSymbol(index,_symbolCharArray)}}
</div>
</template>
</template>
</div>
<div class$="{{_getTimeZoneTextClass(showTimeZone, isValid)}}">
<template is="dom-if" if="{{_showTimeZoneDropdown(showTimeZone)}}">
<px-dropdown
id="dropdown"
class="dropdown"
display-value="[[timeZone]]"
button-style="bare"
search-mode
disable-clear
items="[[_getTimeZoneList(showTimeZone)]]"
on-focus="_handleDropdownFocus"
on-blur="_handleDropdownBlur"
hoist="[[hoist]]"
container-type="[[containerType]]">
</px-dropdown>
</template>
<template is="dom-if" if="{{_showTimeZoneText(showTimeZone)}}">
<span id="timeZoneText" class="dt-text-input--non-editable">{{_getTimeZoneText(timeZone, showTimeZone)}}</span>
</template>
</div>
</div>
</template>
</dom-module>
<script>
Polymer({
is: 'px-datetime-entry',
behaviors: [
PxDatetimeBehavior.SingleMoment,
PxDatetimeBehavior.Validate,
PxOverlayBehavior.sharedProperties
],
properties: {
/**
* Date or Time icon/text
*
* Format is 'Date' or 'Time'
*/
dateOrTime: {
type: String,
value: 'Date'
},
/**
* Moment format string for the format to display/validate this field against
* (see http://momentjs.com/docs/#/parsing/string-format/)
*/
momentFormat: {
type: String,
value: 'YYYY/MM/DD',
observer: '_formatChanged'
},
/**
* Whether to hide the calendar or clock icon
*/
hideIcon: {
type: Boolean,
value: false
},
/**
* An array of tokens (e.g. ["YY","MM", "DD"])
*/
_cellFormatArray:{
type: Array,
notify: true
},
/**
* An array of the separator characters (e.g. ["/","/"])
*/
_symbolCharArray:{
type: Array,
notify: true,
value: function(){
return []
}
},
/**
* Determines how to display the timezone:
* - 'dropdown': shows the timezone as a dropdown which the user can use to select a different timezone. Only contains a subset of all timezones.
* **NOTE: this collection of timezones uses the POSIX convention for timezone offset, meaning that positive values correspond to timezones WEST of GMT and negative values correspond to timezones EAST of GMT.**
* - 'extendedDropdown': shows the timezone as a dropdown which the user can use to select a different timezone. Contains all existing timezones (588)
* - 'text': shows the timezone as text, non editable
* - 'abbreviatedText': shows the timezone as an abbreviated text, non editable (e.g. UTC, PST, EST, etc)
*/
showTimeZone: {
type: String,
value: ''
},
/**
* True if any cell is currently selected.
*/
isSelected: {
type: Boolean,
notify: true
},
/**
* Specifies if the dropdown content should get hoisted to a container in order to escape its current stacking context
*/
hoist: {
type: Boolean,
value: false
}
},
listeners: {
'px-entry-cell-move': '_entryCellMove',
'px-cell-blurred': '_handleBlur',
'px-request-datetime-entry-copy': '_handleCopy',
'px-request-datetime-entry-paste': '_handlePaste',
'px-dropdown-selection-changed': '_updateTimeZone'
},
/**
* calls `_formatChanged` if `momentFormat` is defined
*/
attached: function(){
if (typeof this.momentFormat === 'string') {
this._formatChanged(this.momentFormat);
}
},
/**
* Clears the input value for each entry-cell input field.
*/
clear: function() {
var cells = Polymer.dom(this.root).querySelectorAll('.cell');
cells.forEach(function(cell) {
cell.clear();
});
},
/**
* Resets the input value for each entry-cell input field to the current momentObj.
*/
setValueFromMoment: function() {
var cells = Polymer.dom(this.root).querySelectorAll('.cell');
cells.forEach(function(cell) {
cell.setValueFromMoment();
});
},
/**
* Runs when `momentFormat` is changed
* Updates `_cellFormatArray` and `_symbolCharArray` if one of them as changed
*
* This will also always size the symbols, regardless if the format changes - to ensure that
* the symbols are sized properly during all render steps
*/
_formatChanged: function(newMomentFormat) {
if(newMomentFormat !== undefined) {
var format = this._splitFormat(newMomentFormat);
if (format._cellFormatArray !== this._cellFormatArray || format._symbolCharArray !== this._symbolCharArray) {
this._cellFormatArray = format._cellFormatArray;
this._symbolCharArray = format._symbolCharArray;
this.notifyPath('_cellFormatArray',this._cellFormatArray);
this.notifyPath('_symbolCharArray',this._symbolCharArray);
this._sizeSymbols();
}
}
},
/**
* create a dummy canvas to measure string and make size the input correctly
*/
_sizeSymbols: function() {
var style = window.getComputedStyle(this, null),
fontSize = style.getPropertyValue('font-size'),
fontFamily = style.getPropertyValue('font-family'),
c = document.createElement('canvas'),
ctx=c.getContext('2d'),
symbols = Polymer.dom(this.root).querySelectorAll('.dt-entry-symbol'),
length;
ctx.font = fontSize + " " + fontFamily;
symbols.forEach(function(symbol, index) {
length = ctx.measureText(symbol.innerHTML.trim().replace(' ',' ')).width;
length = Math.ceil(length);
symbol.style['width'] = length + 'px';
});
},
/**
* Regex that splits the `momentFormat` into two arrays. One for the tokens and one for the separators
*/
_splitFormat:function(momentFormat){
var re = /[MDYAaHhkmsSZXx]+/g;
var reSymbol = /\W+/g;
return {
_cellFormatArray: momentFormat.match(re),
_symbolCharArray: momentFormat.match(reSymbol)
};
},
/**
* Helper function for a dom-if
* Determines if the `_symbolCharArray` of `index` is null or undefined
*/
_isSymbol: function(i, _symbolCharArray){
if(_symbolCharArray === null || typeof(_symbolCharArray[i]) === 'undefined'){
return false
}
return true
},
/**
* return a string of the separation symbols
*/
_returnSymbol: function(i, _symbolCharArray){
if(_symbolCharArray === null || typeof(_symbolCharArray[i]) === 'undefined'){
return ''
}
return _symbolCharArray[i].split(' ').join('\xa0');
},
/**
* helper to determine whether to diplay the calendar or clock icon
*/
_setIcon: function(){
if (this.dateOrTime.toLowerCase() === 'date') {
return 'px-utl:calendar'
} else {
return 'px-utl:clock'
}
},
/**
* handle arrow keys switching cells or firing events to above components
* @event px-next-field
* @param {string} dateOrTime - Values can be "Date" or "Time"
*
* @event px-previous-field
* @param {string} dateOrTime - Values can be "Date" or "Time"
*/
_entryCellMove: function(evt){
var ne = Polymer.dom(evt);
var entryCellOrder = ne.rootTarget.order;
if(entryCellOrder === this._cellFormatArray.length - 1 && evt.detail.dir === 1){
this.fire('px-next-field', {dateOrTime: this.dateOrTime});
} else if(entryCellOrder === 0 && evt.detail.dir === -1){
this.fire('px-previous-field', {dateOrTime: this.dateOrTime});
} else {
this._focusCell(parseInt(entryCellOrder) + evt.detail.dir);
}
},
/**
*
*/
_focusCell: function(cellNumber) {
var elem = this.$$("#cell" + cellNumber);
elem.$$('.datetime-entry-input').focus();
},
/**
*
*/
_focusLastCell: function() {
this._focusCell(this._cellFormatArray.length - 1);
},
/**
* when `px-cell-blurred` is fired run `_validateInput`
*/
_handleBlur: function(){
this._validateInput(this.momentFormat, this.timeZone);
},
/**
* Catches the `on-focus-changed` from `px-dropdown` and fires this up for `px-datetime-field`
* @private
* @event px-dropdown-focused
*/
_handleDropdownFocus: function(){
this.fire('px-dropdown-focused', {}, {cancelable:true})
},
/**
* Catches the `on-blur-changed` from `px-dropdown` and fires this up for `px-datetime-field`
* @private
* @event px-dropdown-blured
*/
_handleDropdownBlur: function(){
this.fire('px-dropdown-blurred', {}, {cancelable:true})
},
/**
* used in a dom-if
* determines whether to display the `text` or `abbreviatedText` option
*/
_showTimeZoneText: function(showTimeZone) {
return showTimeZone === "text" || showTimeZone === "abbreviatedText";
},
/**
* used in a dom-if
* determines whether to display the `dropdown` or `extendedDropdown` option
*/
_showTimeZoneDropdown: function(showTimeZone) {
return showTimeZone === "dropdown" || showTimeZone === "extendedDropdown";
},
/**
* Create the `extendedDropdown` and `dropdown` items for `px-dropdown`
*
* If showTimeZone is not `extendedDropdown` or `dropdown` return and empty array.
* Because of a timing issue `_getTimeZoneList` can be called before `_showTimeZoneDropdown`
* causing an issue if the dropdown tries to redraw without `items` being defined.
*/
_getTimeZoneList: function(showTimeZone) {
if(showTimeZone === "extendedDropdown") {
return Px.moment.tz.names();
} else if (showTimeZone === "dropdown"){
return [
"Etc/GMT-14",
"Etc/GMT-13",
"Etc/GMT-12",
"Etc/GMT-11",
"Etc/GMT-10",
"Etc/GMT-9",
"Etc/GMT-8",
"Etc/GMT-7",
"Etc/GMT-6",
"Etc/GMT-5",
"Etc/GMT-4",
"Etc/GMT-3",
"Etc/GMT-2",
"Etc/GMT-1",
"Etc/GMT-0",
"Etc/GMT",
"Etc/GMT0",
"Etc/GMT+0",
"Etc/GMT+1",
"Etc/GMT+2",
"Etc/GMT+3",
"Etc/GMT+4",
"Etc/GMT+5",
"Etc/GMT+6",
"Etc/GMT+7",
"Etc/GMT+8",
"Etc/GMT+9",
"Etc/GMT+10",
"Etc/GMT+11",
"Etc/GMT+12",
"UCT",
"UTC"
];
}
return [];
},
/**
* when `px-dropdown-selection-changed` is fired update `timeZone`
*/
_updateTimeZone: function(e) {
this.set('timeZone', e.detail.val);
},
/**
*
*/
_handleCopy: function(e) {
e.detail.setData('Text', Px.moment.tz(this.momentObj, this.timeZone).format(this.momentFormat));
e.stopPropagation();
},
/**
*
*/
_handlePaste: function(e) {
//try parsing what's been pasted...
var data = e.detail.getData('Text'),
newDate = Px.moment(data, this.momentFormat);
if(newDate.isValid()) {
//if this comes from another datetime-entry make sure it is the same type
//otherwise just allow. This is to avoid changing the date when parsing a time
/*if(!dateOrTime || (dateOrTime === this.dateOrTime)) {
}*/
this.momentObj = newDate;
}else {
console.log("tried to paste non valid date format: " + data + ". Expecting: " + this.momentFormat);
}
e.stopPropagation();
},
/**
* Determines what classes should show on the label icon
*/
_getIconClass: function(hideIcon){
return !hideIcon ? "u-mr--" : "";
},
/**
*/
_getTimeZoneText: function(timeZone, showTimeZone) {
if(timeZone !== undefined && showTimeZone === "abbreviatedText") {
tempMomentObj = this.momentObj ? this.momentObj : Px.moment();
return Px.moment.tz.zone(timeZone).abbr(tempMomentObj);
} else if (showTimeZone === "text"){
return timeZone;
}
},
/**
* css helper. If a dropdown is showing add a margin-left of 10px;
*/
_getTimeZoneTextClass: function (showTimeZone, isValid) {
var classList = "dt-flex--no-shrink ";
if (showTimeZone === "text" || showTimeZone === "abbreviatedText") {
classList += "u-ml- ";
}else if (showTimeZone === "dropdown" || showTimeZone === "extendedDropdown") {
classList += "u-ml-- ";
}
return classList;
},
/**
* This event is fired when the calendar or clock icon has been clicked
*
* @event px-datetime-entry-icon-clicked
* @param {string} dateOrTime - Values can be "Date" or "Time"
*/
_iconClicked: function(evt) {
this.fire('px-datetime-entry-icon-clicked', {dateOrTime: this.dateOrTime});
}
});
</script>