@progress/kendo-dateinputs-common 0.1.0 → 0.2.0-dev.202301061353

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +34 -3
  2. package/dist/cdn/js/kendo-dateinputs-common.js +1 -0
  3. package/dist/cdn/main.js +1 -1
  4. package/dist/es/common/constants.js +6 -0
  5. package/dist/es/common/dateobject.js +1159 -0
  6. package/dist/es/common/key.js +16 -0
  7. package/dist/es/common/keycode.js +16 -0
  8. package/dist/es/common/mask.js +8 -0
  9. package/dist/es/common/observable.js +32 -0
  10. package/dist/es/common/utils.js +128 -0
  11. package/dist/es/dateinput/dateinput.js +1011 -0
  12. package/dist/es/dateinput/interaction-mode.js +6 -0
  13. package/dist/es/dateinput/utils.js +93 -0
  14. package/dist/es/main.js +1 -1
  15. package/dist/es2015/common/constants.js +6 -0
  16. package/dist/es2015/common/dateobject.js +1137 -0
  17. package/dist/es2015/common/key.js +16 -0
  18. package/dist/es2015/common/keycode.js +16 -0
  19. package/dist/es2015/common/mask.js +6 -0
  20. package/dist/es2015/common/observable.js +29 -0
  21. package/dist/es2015/common/utils.js +117 -0
  22. package/dist/es2015/dateinput/dateinput.js +969 -0
  23. package/dist/es2015/dateinput/interaction-mode.js +6 -0
  24. package/dist/es2015/dateinput/utils.js +92 -0
  25. package/dist/es2015/main.js +1 -1
  26. package/dist/npm/common/constants.d.ts +6 -0
  27. package/dist/npm/common/constants.js +8 -0
  28. package/dist/npm/common/dateobject.d.ts +172 -0
  29. package/dist/npm/common/dateobject.js +1161 -0
  30. package/dist/npm/common/key.d.ts +16 -0
  31. package/dist/npm/common/key.js +18 -0
  32. package/dist/npm/common/keycode.d.ts +16 -0
  33. package/dist/npm/common/keycode.js +18 -0
  34. package/dist/npm/common/mask.d.ts +4 -0
  35. package/dist/npm/common/mask.js +10 -0
  36. package/dist/npm/common/observable.d.ts +9 -0
  37. package/dist/npm/common/observable.js +34 -0
  38. package/dist/npm/common/utils.d.ts +60 -0
  39. package/dist/npm/common/utils.js +130 -0
  40. package/dist/npm/dateinput/dateinput.d.ts +204 -0
  41. package/dist/npm/dateinput/dateinput.js +1013 -0
  42. package/dist/npm/dateinput/interaction-mode.d.ts +5 -0
  43. package/dist/npm/dateinput/interaction-mode.js +8 -0
  44. package/dist/npm/dateinput/utils.d.ts +27 -0
  45. package/dist/npm/dateinput/utils.js +95 -0
  46. package/dist/npm/main.d.ts +1 -1
  47. package/dist/npm/main.js +2 -2
  48. package/dist/systemjs/kendo-dateinputs-common.js +1 -0
  49. package/package.json +10 -8
  50. package/dist/cdn/js/kendo-typescript-package-base.js +0 -1
  51. package/dist/es/my-class.js +0 -15
  52. package/dist/es2015/my-class.js +0 -11
  53. package/dist/npm/my-class.d.ts +0 -9
  54. package/dist/npm/my-class.js +0 -17
  55. package/dist/systemjs/kendo-typescript-package-base.js +0 -1
@@ -0,0 +1,1011 @@
1
+ import * as tslib_1 from "tslib";
2
+ var _a;
3
+ import { DateObject } from '../common/dateobject';
4
+ import { approximateStringMatching } from './utils';
5
+ import { KeyCode } from '../common/keycode';
6
+ import { Key } from '../common/key';
7
+ import { extend, isPresent, isDocumentAvailable, millisecondDigitsInFormat, millisecondStepFor, isDate } from '../common/utils';
8
+ import { Observable } from '../common/observable';
9
+ import { DateInputInteractionMode } from './interaction-mode';
10
+ import { isEqual, cloneDate } from '@progress/kendo-date-math';
11
+ import { Constants } from '../common/constants';
12
+ var DEFAULT_SEGMENT_STEP = 1;
13
+ var DRAG_START = "dragStart";
14
+ var DROP = "drop";
15
+ var TOUCH_START = "touchstart";
16
+ var MOUSE_DOWN = "mousedown";
17
+ var MOUSE_UP = "mouseup";
18
+ var CLICK = "click";
19
+ var INPUT = "input";
20
+ var KEY_DOWN = "keydown";
21
+ var FOCUS = "focus";
22
+ var BLUR = "blur";
23
+ var PASTE = "paste";
24
+ var MOUSE_SCROLL = "DOMMouseScroll";
25
+ var MOUSE_WHEEL = "mousewheel";
26
+ var VALUE_CHANGE = "valueChange";
27
+ var INPUT_END = "inputEnd";
28
+ var BLUR_END = "blurEnd";
29
+ var FOCUS_END = "focusEnd";
30
+ var CHANGE = "change";
31
+ var defaultDateInputOptions = {
32
+ format: "d",
33
+ allowNulls: false,
34
+ placeholder: null,
35
+ cycleTime: true,
36
+ locale: null,
37
+ steps: {
38
+ millisecond: DEFAULT_SEGMENT_STEP,
39
+ second: DEFAULT_SEGMENT_STEP,
40
+ minute: DEFAULT_SEGMENT_STEP,
41
+ hour: DEFAULT_SEGMENT_STEP,
42
+ day: DEFAULT_SEGMENT_STEP,
43
+ month: DEFAULT_SEGMENT_STEP,
44
+ year: DEFAULT_SEGMENT_STEP
45
+ },
46
+ formatPlaceholder: null,
47
+ events: (_a = {},
48
+ _a[VALUE_CHANGE] = null,
49
+ _a[INPUT] = null,
50
+ _a[INPUT_END] = null,
51
+ _a[FOCUS] = null,
52
+ _a[FOCUS_END] = null,
53
+ _a[BLUR] = null,
54
+ _a[BLUR_END] = null,
55
+ _a[KEY_DOWN] = null,
56
+ _a[MOUSE_WHEEL] = null,
57
+ _a[CHANGE] = null,
58
+ _a),
59
+ selectNearestSegmentOnFocus: false,
60
+ enableMouseWheel: false,
61
+ allowCaretMode: false,
62
+ autoSwitchParts: true,
63
+ autoSwitchKeys: [],
64
+ twoDigitYearMax: Constants.twoDigitYearMax,
65
+ autoCorrectParts: true
66
+ };
67
+ var DateInput = /** @class */ (function (_super) {
68
+ tslib_1.__extends(DateInput, _super);
69
+ function DateInput(element, options) {
70
+ var _this = _super.call(this, options) || this;
71
+ _this.dateObject = null;
72
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
73
+ // @ts-ignore
74
+ _this.currentText = '';
75
+ _this.currentFormat = '';
76
+ _this.interactionMode = DateInputInteractionMode.None;
77
+ _this.localeId = Constants.defaultLocaleId;
78
+ _this.init(element, options);
79
+ return _this;
80
+ }
81
+ Object.defineProperty(DateInput.prototype, "value", {
82
+ get: function () {
83
+ return this.dateObject && this.dateObject.getValue();
84
+ },
85
+ enumerable: true,
86
+ configurable: true
87
+ });
88
+ DateInput.prototype.init = function (element, options) {
89
+ var dateValue = isDate(this.options.value) ? cloneDate(this.options.value) : new Date(options.formattedValue);
90
+ if (!isDate(dateValue)) {
91
+ dateValue = null;
92
+ }
93
+ this.element = element;
94
+ // this.element._kendoWidget = this;
95
+ this.options = extend({}, defaultDateInputOptions, options);
96
+ this.intl = this.options.intlService;
97
+ this.formatPlaceholder = this.options.formatPlaceholder ? this.options.formatPlaceholder : 'formatPattern';
98
+ this.dateObject = this.createDateObject();
99
+ this.dateObject.setValue(dateValue);
100
+ this.setTextAndFormat();
101
+ this.bindEvents();
102
+ this.resetSegmentValue = true;
103
+ this.forceUpdate();
104
+ };
105
+ DateInput.prototype.destroy = function () {
106
+ this.unbindEvents();
107
+ this.dateObject = null;
108
+ _super.prototype.destroy.call(this);
109
+ };
110
+ DateInput.prototype.bindEvents = function () {
111
+ this.onElementDragStart = this.onElementDragStart.bind(this);
112
+ this.element.addEventListener(DRAG_START, this.onElementDragStart);
113
+ this.onElementDrop = this.onElementDrop.bind(this);
114
+ this.element.addEventListener(DROP, this.onElementDrop);
115
+ this.onElementClick = this.onElementClick.bind(this);
116
+ this.element.addEventListener(CLICK, this.onElementClick);
117
+ this.onElementMouseDown = this.onElementMouseDown.bind(this);
118
+ this.element.addEventListener(MOUSE_DOWN, this.onElementMouseDown);
119
+ this.element.addEventListener(TOUCH_START, this.onElementMouseDown);
120
+ this.onElementMouseUp = this.onElementMouseUp.bind(this);
121
+ this.element.addEventListener(MOUSE_UP, this.onElementMouseUp);
122
+ this.onElementInput = this.onElementInput.bind(this);
123
+ this.element.addEventListener(INPUT, this.onElementInput);
124
+ this.onElementKeyDown = this.onElementKeyDown.bind(this);
125
+ this.element.addEventListener(KEY_DOWN, this.onElementKeyDown);
126
+ this.onElementFocus = this.onElementFocus.bind(this);
127
+ this.element.addEventListener(FOCUS, this.onElementFocus);
128
+ this.onElementBlur = this.onElementBlur.bind(this);
129
+ this.element.addEventListener(BLUR, this.onElementBlur);
130
+ this.onElementChange = this.onElementChange.bind(this);
131
+ this.element.addEventListener(CHANGE, this.onElementChange);
132
+ this.onElementPaste = this.onElementPaste.bind(this);
133
+ this.element.addEventListener(PASTE, this.onElementPaste);
134
+ this.onElementMouseWheel = this.onElementMouseWheel.bind(this);
135
+ this.element.addEventListener(MOUSE_SCROLL, this.onElementMouseWheel);
136
+ this.element.addEventListener(MOUSE_WHEEL, this.onElementMouseWheel);
137
+ };
138
+ DateInput.prototype.unbindEvents = function () {
139
+ this.element.removeEventListener(DRAG_START, this.onElementDragStart);
140
+ this.element.removeEventListener(DROP, this.onElementDrop);
141
+ this.element.removeEventListener(TOUCH_START, this.onElementMouseDown);
142
+ this.element.removeEventListener(MOUSE_DOWN, this.onElementMouseDown);
143
+ this.element.removeEventListener(MOUSE_UP, this.onElementMouseUp);
144
+ this.element.removeEventListener(CLICK, this.onElementClick);
145
+ this.element.removeEventListener(INPUT, this.onElementInput);
146
+ this.element.removeEventListener(KEY_DOWN, this.onElementKeyDown);
147
+ this.element.removeEventListener(FOCUS, this.onElementFocus);
148
+ this.element.removeEventListener(BLUR, this.onElementBlur);
149
+ this.element.removeEventListener(CHANGE, this.onElementChange);
150
+ this.element.removeEventListener(PASTE, this.onElementPaste);
151
+ this.element.removeEventListener(MOUSE_SCROLL, this.onElementMouseWheel);
152
+ this.element.removeEventListener(MOUSE_WHEEL, this.onElementMouseWheel);
153
+ };
154
+ DateInput.prototype.setOptions = function (options, refresh) {
155
+ if (refresh === void 0) { refresh = false; }
156
+ this.options = extend(this.options, options);
157
+ if (refresh) {
158
+ this.destroy();
159
+ this.init(this.element, options);
160
+ }
161
+ };
162
+ /**
163
+ * @hidden
164
+ */
165
+ DateInput.prototype.resetLocale = function () {
166
+ this.unbindEvents();
167
+ this.init(this.element, this.options);
168
+ };
169
+ /**
170
+ * @hidden
171
+ */
172
+ DateInput.prototype.isInCaretMode = function () {
173
+ return this.interactionMode === DateInputInteractionMode.Caret;
174
+ };
175
+ DateInput.prototype.focus = function () {
176
+ this.element.focus();
177
+ if (this.options.selectNearestSegmentOnFocus) {
178
+ this.selectNearestSegment(0);
179
+ }
180
+ };
181
+ /**
182
+ * @hidden
183
+ */
184
+ DateInput.prototype.onElementDragStart = function (e) {
185
+ e.preventDefault();
186
+ };
187
+ /**
188
+ * @hidden
189
+ */
190
+ DateInput.prototype.onElementDrop = function (e) {
191
+ e.preventDefault();
192
+ };
193
+ /**
194
+ * @hidden
195
+ */
196
+ DateInput.prototype.onElementMouseDown = function () {
197
+ this.mouseDownStarted = true;
198
+ this.focusedPriorToMouseDown = this.isActive;
199
+ };
200
+ /**
201
+ * @hidden
202
+ */
203
+ DateInput.prototype.onElementMouseUp = function (e) {
204
+ this.mouseDownStarted = false;
205
+ e.preventDefault();
206
+ };
207
+ /**
208
+ * @hidden
209
+ */
210
+ DateInput.prototype.onElementClick = function (e) {
211
+ this.mouseDownStarted = false;
212
+ var selection = this.selection;
213
+ if (this.isInCaretMode()) {
214
+ // explicitly refresh the input element value
215
+ // caret mode can change the number of symbols in the element
216
+ // thus clicking on a segment can result in incorrect selection
217
+ this.forceUpdate();
218
+ }
219
+ if (e.detail === 3) {
220
+ // when 3 clicks occur, leave the native event to handle the change
221
+ // this results in selecting the whole element value
222
+ }
223
+ else {
224
+ if (this.isActive && this.options.selectNearestSegmentOnFocus) {
225
+ var selectionPresent = this.element.selectionStart !== this.element.selectionEnd;
226
+ var placeholderToggled = isPresent(this.options.placeholder) &&
227
+ !this.dateObject.hasValue() &&
228
+ !this.focusedPriorToMouseDown;
229
+ // focus first segment if the user hasn't selected something during mousedown and if the placeholder was just toggled
230
+ var selectFirstSegment = !selectionPresent && placeholderToggled;
231
+ var index = selectFirstSegment ? 0 : this.caret()[0];
232
+ this.selectNearestSegment(index);
233
+ }
234
+ else {
235
+ this.setSelection(this.selectionByIndex(selection.start));
236
+ }
237
+ }
238
+ };
239
+ /**
240
+ * @hidden
241
+ */
242
+ DateInput.prototype.onElementInput = function (e) {
243
+ this.triggerInput({ event: e });
244
+ var keyDownEvent = this.keyDownEvent || {};
245
+ var isBackspaceKey = keyDownEvent.keyCode === KeyCode.BACKSPACE || keyDownEvent.key === Key.BACKSPACE;
246
+ var isDeleteKey = keyDownEvent.keyCode === KeyCode.DELETE || keyDownEvent.key === Key.DELETE;
247
+ if (!this.element || !this.dateObject) {
248
+ return;
249
+ }
250
+ if (this.isPasteInProgress) {
251
+ if (this.options.allowCaretMode) {
252
+ // pasting should leave the input with caret
253
+ // thus allow direct input instead of selection mode
254
+ this.resetSegmentValue = false;
255
+ }
256
+ this.updateOnPaste(e);
257
+ this.isPasteInProgress = false;
258
+ return;
259
+ }
260
+ var originalInteractionMode = this.interactionMode;
261
+ if (this.options.allowCaretMode &&
262
+ originalInteractionMode !== DateInputInteractionMode.Caret &&
263
+ !isDeleteKey && !isBackspaceKey) {
264
+ this.resetSegmentValue = true;
265
+ }
266
+ if (this.options.allowCaretMode) {
267
+ this.interactionMode = DateInputInteractionMode.Caret;
268
+ }
269
+ else {
270
+ this.interactionMode = DateInputInteractionMode.Selection;
271
+ }
272
+ var hasCaret = this.isInCaretMode();
273
+ if (hasCaret && this.keyDownEvent.key === Key.SPACE) {
274
+ // do not allow custom "holes" in the date segments
275
+ this.setPreviousInputEventState(this.keyDownEvent);
276
+ return;
277
+ }
278
+ var oldDateObjectValue = this.dateObject && this.dateObject.getValue();
279
+ var _a = this.dateObject.getTextAndFormat(), currentText = _a.text, currentFormat = _a.format;
280
+ this.currentFormat = currentFormat;
281
+ var text = hasCaret && (isBackspaceKey || isDeleteKey) ? this.previousElementValue : currentText;
282
+ var diff = approximateStringMatching({
283
+ oldText: text,
284
+ newText: this.element.value,
285
+ formatPattern: this.currentFormat,
286
+ selectionStart: this.selection.start,
287
+ isInCaretMode: hasCaret,
288
+ keyEvent: this.keyDownEvent
289
+ });
290
+ if (hasCaret && (!diff || diff.length === 0)) {
291
+ this.setPreviousInputEventState(this.keyDownEvent);
292
+ return;
293
+ }
294
+ else if (hasCaret && diff.length === 1) {
295
+ if (!diff[0] || !diff[0][0]) {
296
+ this.setPreviousInputEventState(this.keyDownEvent);
297
+ return;
298
+ }
299
+ else if (hasCaret && diff[0] &&
300
+ (diff[0][0] === Constants.formatSeparator || diff[0][1] === Constants.formatSeparator)) {
301
+ this.setPreviousInputEventState(this.keyDownEvent);
302
+ return;
303
+ }
304
+ }
305
+ var navigationOnly = (diff.length === 1 && diff[0][1] === Constants.formatSeparator);
306
+ var parsePartsResults = [];
307
+ var switchPart = false;
308
+ if (!navigationOnly) {
309
+ for (var i = 0; i < diff.length; i++) {
310
+ var parsePartResult = this.dateObject.parsePart({
311
+ symbol: diff[i][0],
312
+ currentChar: diff[i][1],
313
+ resetSegmentValue: this.resetSegmentValue,
314
+ cycleSegmentValue: !this.isInCaretMode(),
315
+ rawTextValue: this.element.value,
316
+ isDeleting: isBackspaceKey || isDeleteKey
317
+ });
318
+ parsePartsResults.push(parsePartResult);
319
+ switchPart = parsePartResult.switchToNext;
320
+ }
321
+ }
322
+ if (!this.options.autoSwitchParts) {
323
+ switchPart = false;
324
+ }
325
+ this.resetSegmentValue = false;
326
+ var lastParseResult = parsePartsResults[parsePartsResults.length - 1];
327
+ var lastParseResultHasNoValue = lastParseResult && !lastParseResult.value;
328
+ var parsingFailedOnDelete = (hasCaret && (isBackspaceKey || isDeleteKey) && lastParseResultHasNoValue);
329
+ var resetPart = lastParseResult ? lastParseResult.resetPart : false;
330
+ var hasDateValueChanged = !isEqual(oldDateObjectValue, this.dateObject.value);
331
+ var symbolForSelection;
332
+ var currentSelection = this.selection;
333
+ if (!hasCaret || parsingFailedOnDelete || resetPart) {
334
+ if (resetPart) {
335
+ symbolForSelection = this.currentFormat[currentSelection.start];
336
+ }
337
+ this.forceUpdate();
338
+ }
339
+ if (diff.length && diff[0][0] !== Constants.formatSeparator) {
340
+ if (!hasCaret || parsingFailedOnDelete || resetPart) {
341
+ if (symbolForSelection) {
342
+ this.setSelection(this.selectionBySymbol(symbolForSelection));
343
+ }
344
+ else {
345
+ if (!this.options.autoSwitchParts && diff[0][1] === Constants.formatSeparator) {
346
+ // do not change the selection when a separator is pressed
347
+ // this should happen only if autoSwitchKeys contains the separator explicitly
348
+ }
349
+ else {
350
+ this.setSelection(this.selectionBySymbol(diff[0][0]));
351
+ }
352
+ }
353
+ }
354
+ }
355
+ if (!switchPart && hasCaret && !isBackspaceKey && !isDeleteKey && !resetPart && lastParseResultHasNoValue) {
356
+ if (hasDateValueChanged) {
357
+ if (this.currentFormat.length === this.elementValue.length) {
358
+ // if a full date is entered, do not reset it
359
+ }
360
+ else {
361
+ // the input is not complete, not parsable or not updatable
362
+ if (hasCaret && originalInteractionMode !== DateInputInteractionMode.Caret && hasDateValueChanged) {
363
+ }
364
+ else if (hasCaret && originalInteractionMode !== DateInputInteractionMode.Caret) {
365
+ symbolForSelection = this.currentFormat[currentSelection.start];
366
+ this.forceUpdate();
367
+ this.setSelection(this.selectionBySymbol(symbolForSelection));
368
+ }
369
+ else {
370
+ this.setPreviousInputEventState(this.keyDownEvent);
371
+ }
372
+ }
373
+ }
374
+ }
375
+ else if (this.options.autoSwitchParts && (switchPart || navigationOnly)) {
376
+ if (!hasCaret) {
377
+ this.switchDateSegment(1);
378
+ }
379
+ }
380
+ this.tryTriggerValueChange({
381
+ oldValue: oldDateObjectValue,
382
+ event: e
383
+ });
384
+ this.triggerInputEnd({ event: e });
385
+ };
386
+ /**
387
+ * @hidden
388
+ */
389
+ DateInput.prototype.onElementFocus = function (e) {
390
+ if (this.triggerFocus({ event: e })) {
391
+ return;
392
+ }
393
+ this.isActive = true;
394
+ this.interactionMode = DateInputInteractionMode.None;
395
+ this.refreshElementValue();
396
+ if (!this.mouseDownStarted) {
397
+ this.caret(0, this.elementValue.length);
398
+ }
399
+ this.mouseDownStarted = false;
400
+ this.triggerFocusEnd({ event: e });
401
+ };
402
+ /**
403
+ * @hidden
404
+ */
405
+ DateInput.prototype.onElementBlur = function (e) {
406
+ this.resetSegmentValue = true;
407
+ this.isActive = false;
408
+ if (this.triggerBlur({ event: e })) {
409
+ return;
410
+ }
411
+ this.interactionMode = DateInputInteractionMode.None;
412
+ this.refreshElementValue();
413
+ this.triggerBlurEnd({ event: e });
414
+ };
415
+ /**
416
+ * @hidden
417
+ */
418
+ DateInput.prototype.onElementChange = function (e) {
419
+ this.triggerChange({ event: e });
420
+ };
421
+ /**
422
+ * @hidden
423
+ */
424
+ DateInput.prototype.onElementKeyDown = function (e) {
425
+ if (this.triggerKeyDown({ event: e })) {
426
+ return;
427
+ }
428
+ this.keyDownEvent = e;
429
+ this.previousElementValue = this.element.value;
430
+ var autoSwitchKeys = (this.options.autoSwitchKeys || [])
431
+ .map(function (x) { return x.toString().toLowerCase().trim(); });
432
+ if (autoSwitchKeys.indexOf(e.keyCode.toString()) >= 0 ||
433
+ autoSwitchKeys.indexOf(e.keyCode) >= 0 ||
434
+ autoSwitchKeys.indexOf(e.key.toLowerCase().trim()) >= 0) {
435
+ var isTabKey = autoSwitchKeys.indexOf(Key.TAB.toLowerCase().trim()) >= 0 ||
436
+ autoSwitchKeys.indexOf(KeyCode.TAB) >= 0 ||
437
+ autoSwitchKeys.indexOf(KeyCode.TAB.toString()) >= 0;
438
+ if (isTabKey) {
439
+ var _a = this.selection, selectionStart = _a.start, selectionEnd = _a.end;
440
+ if (e.shiftKey && isTabKey) {
441
+ this.switchDateSegment(-1);
442
+ }
443
+ else {
444
+ this.switchDateSegment(1);
445
+ }
446
+ if (selectionStart !== this.selection.start || selectionEnd !== this.selection.end) {
447
+ // when the selection changes, prevent the default Tab behavior
448
+ e.preventDefault();
449
+ return;
450
+ }
451
+ }
452
+ else {
453
+ // do not allow the "input" event to be triggered
454
+ e.preventDefault();
455
+ this.switchDateSegment(1);
456
+ return;
457
+ }
458
+ }
459
+ var symbol = this.currentFormat[this.selection.start];
460
+ var step = this.getStepFromSymbol(symbol);
461
+ var shouldPreventDefault = false;
462
+ if (e.altKey || e.ctrlKey || e.metaKey || e.keyCode === KeyCode.TAB) {
463
+ return;
464
+ }
465
+ switch (e.keyCode) {
466
+ case KeyCode.ARROW_LEFT:
467
+ this.switchDateSegment(-1);
468
+ shouldPreventDefault = true;
469
+ break;
470
+ case KeyCode.ARROW_UP:
471
+ this.modifyDateSegmentValue(step, symbol, event);
472
+ shouldPreventDefault = true;
473
+ break;
474
+ case KeyCode.ARROW_RIGHT:
475
+ this.switchDateSegment(1);
476
+ shouldPreventDefault = true;
477
+ break;
478
+ case KeyCode.ARROW_DOWN:
479
+ this.modifyDateSegmentValue(-step, symbol, event);
480
+ shouldPreventDefault = true;
481
+ break;
482
+ case KeyCode.ENTER:
483
+ // todo: handle "change" event
484
+ break;
485
+ case KeyCode.DELETE:
486
+ case KeyCode.BACKSPACE:
487
+ if (this.options.allowNulls) {
488
+ var oldValue = this.dateObject.value;
489
+ this.dateObject.setValue(null);
490
+ this.forceUpdate();
491
+ this.tryTriggerValueChange({
492
+ oldValue: oldValue,
493
+ event: e
494
+ });
495
+ }
496
+ shouldPreventDefault = true;
497
+ if (e.keyCode === KeyCode.BACKSPACE &&
498
+ ((this.options.autoSwitchKeys || []).indexOf(KeyCode.BACKSPACE) >= 0 ||
499
+ (this.options.autoSwitchKeys || []).indexOf(Key.BACKSPACE) >= 0)) {
500
+ this.switchDateSegment(-1);
501
+ }
502
+ return;
503
+ case Key.HOME:
504
+ this.selectNearestSegment(0);
505
+ break;
506
+ case Key.END:
507
+ this.selectNearestSegment(this.elementValue.length);
508
+ break;
509
+ default:
510
+ // allow the "input" event to handle the change
511
+ return;
512
+ }
513
+ if (shouldPreventDefault) {
514
+ e.preventDefault();
515
+ }
516
+ };
517
+ /**
518
+ * @hidden
519
+ */
520
+ DateInput.prototype.onElementPaste = function () {
521
+ this.isPasteInProgress = true;
522
+ };
523
+ /**
524
+ * @hidden
525
+ */
526
+ DateInput.prototype.onElementMouseWheel = function (e) {
527
+ if (!this.options.enableMouseWheel || this.triggerMouseWheel({ event: e })) {
528
+ return;
529
+ }
530
+ if (!this.isActive) {
531
+ return;
532
+ }
533
+ var event = e;
534
+ if (event.shiftKey) {
535
+ this.switchDateSegment((event.wheelDelta || -event.detail) > 0 ? -1 : 1);
536
+ }
537
+ else {
538
+ this.modifyDateSegmentValue((event.wheelDelta || -event.detail) > 0 ? 1 : -1);
539
+ }
540
+ event.returnValue = false;
541
+ if (event.preventDefault) {
542
+ event.preventDefault();
543
+ }
544
+ if (event.stopPropagation) {
545
+ event.stopPropagation();
546
+ }
547
+ };
548
+ DateInput.prototype.updateOnPaste = function (e) {
549
+ var value = this.intl.parseDate(this.elementValue, this.inputFormat) || this.value;
550
+ if (isPresent(value) && this.dateObject.shouldNormalizeCentury()) {
551
+ value = this.dateObject.normalizeCentury(value);
552
+ }
553
+ var oldDateObjectValue = this.dateObject && this.dateObject.getValue();
554
+ this.writeValue(value);
555
+ this.tryTriggerValueChange({
556
+ oldValue: oldDateObjectValue,
557
+ event: e
558
+ });
559
+ };
560
+ Object.defineProperty(DateInput.prototype, "elementValue", {
561
+ get: function () {
562
+ return (this.element || {}).value || '';
563
+ },
564
+ enumerable: true,
565
+ configurable: true
566
+ });
567
+ Object.defineProperty(DateInput.prototype, "inputFormat", {
568
+ get: function () {
569
+ if (!this.options.format) {
570
+ return Constants.defaultDateFormat;
571
+ }
572
+ if (typeof this.options.format === 'string') {
573
+ return this.options.format;
574
+ }
575
+ else {
576
+ return this.options.format.inputFormat;
577
+ }
578
+ },
579
+ enumerable: true,
580
+ configurable: true
581
+ });
582
+ Object.defineProperty(DateInput.prototype, "displayFormat", {
583
+ get: function () {
584
+ if (!this.options.format) {
585
+ return Constants.defaultDateFormat;
586
+ }
587
+ if (typeof this.options.format === 'string') {
588
+ return this.options.format;
589
+ }
590
+ else {
591
+ return this.options.format.displayFormat;
592
+ }
593
+ },
594
+ enumerable: true,
595
+ configurable: true
596
+ });
597
+ Object.defineProperty(DateInput.prototype, "selection", {
598
+ get: function () {
599
+ var returnValue = { start: 0, end: 0 };
600
+ if (this.element !== null && this.element.selectionStart !== undefined) {
601
+ returnValue = {
602
+ start: this.element.selectionStart,
603
+ end: this.element.selectionEnd
604
+ };
605
+ }
606
+ return returnValue;
607
+ },
608
+ enumerable: true,
609
+ configurable: true
610
+ });
611
+ DateInput.prototype.setSelection = function (selection) {
612
+ // this._lastSelectedSymbol = this.currentFormat[selection.start];
613
+ if (this.element && document.activeElement === this.element) {
614
+ this.element.setSelectionRange(selection.start, selection.end);
615
+ }
616
+ };
617
+ /**
618
+ * @hidden
619
+ */
620
+ DateInput.prototype.selectionBySymbol = function (symbol) {
621
+ var start = -1;
622
+ var end = 0;
623
+ for (var i = 0; i < this.currentFormat.length; i++) {
624
+ if (this.currentFormat[i] === symbol) {
625
+ end = i + 1;
626
+ if (start === -1) {
627
+ start = i;
628
+ }
629
+ }
630
+ }
631
+ if (start < 0) {
632
+ start = 0;
633
+ }
634
+ return { start: start, end: end };
635
+ };
636
+ /**
637
+ * @hidden
638
+ */
639
+ DateInput.prototype.selectionByIndex = function (index) {
640
+ var selection = { start: index, end: index };
641
+ for (var i = index, j = index - 1; i < this.currentFormat.length || j >= 0; i++, j--) {
642
+ if (i < this.currentFormat.length && this.currentFormat[i] !== Constants.formatSeparator) {
643
+ selection = this.selectionBySymbol(this.currentFormat[i]);
644
+ break;
645
+ }
646
+ if (j >= 0 && this.currentFormat[j] !== Constants.formatSeparator) {
647
+ selection = this.selectionBySymbol(this.currentFormat[j]);
648
+ break;
649
+ }
650
+ }
651
+ return selection;
652
+ };
653
+ DateInput.prototype.switchDateSegment = function (offset) {
654
+ var selection = this.selection;
655
+ if (this.isInCaretMode()) {
656
+ var start = selection.start;
657
+ var closestNonSeparatorSymbol = this.currentFormat[start];
658
+ for (var i = start; i >= 0; i--) {
659
+ closestNonSeparatorSymbol = this.currentFormat[i];
660
+ if (closestNonSeparatorSymbol !== Constants.formatSeparator) {
661
+ start = i;
662
+ break;
663
+ }
664
+ }
665
+ var symbol = void 0;
666
+ for (var i = start; i < this.currentFormat.length; i++) {
667
+ symbol = this.currentFormat[i];
668
+ if (symbol !== Constants.formatSeparator) {
669
+ break;
670
+ }
671
+ }
672
+ if (symbol) {
673
+ this.forceUpdate();
674
+ this.setSelection(this.selectionBySymbol(symbol));
675
+ }
676
+ }
677
+ var _a = this.selection, selectionStart = _a.start, selectionEnd = _a.end;
678
+ if (selectionStart < selectionEnd &&
679
+ this.currentFormat[selectionStart] !== this.currentFormat[selectionEnd - 1]) {
680
+ this.setSelection(this.selectionByIndex(offset > 0 ? selectionStart : selectionEnd - 1));
681
+ this.resetSegmentValue = true;
682
+ return;
683
+ }
684
+ var previousFormatSymbol = this.currentFormat[selectionStart];
685
+ var a = selectionStart + offset;
686
+ while (a > 0 && a < this.currentFormat.length) {
687
+ if (this.currentFormat[a] !== previousFormatSymbol &&
688
+ this.currentFormat[a] !== Constants.formatSeparator) {
689
+ break;
690
+ }
691
+ a += offset;
692
+ }
693
+ if (this.currentFormat[a] === Constants.formatSeparator) {
694
+ // no known symbol is found
695
+ return;
696
+ }
697
+ var b = a;
698
+ while (b >= 0 && b < this.currentFormat.length) {
699
+ if (this.currentFormat[b] !== this.currentFormat[a]) {
700
+ break;
701
+ }
702
+ b += offset;
703
+ }
704
+ if (a > b && (b + 1 !== selectionStart || a + 1 !== selectionEnd)) {
705
+ this.setSelection({ start: b + 1, end: a + 1 });
706
+ this.resetSegmentValue = true;
707
+ }
708
+ else if (a < b && (a !== selectionStart || b !== selectionEnd)) {
709
+ this.setSelection({ start: a, end: b });
710
+ this.resetSegmentValue = true;
711
+ }
712
+ };
713
+ DateInput.prototype.modifyDateSegmentValue = function (offset, symbol, event) {
714
+ if (symbol === void 0) { symbol = ""; }
715
+ if (event === void 0) { event = {}; }
716
+ if (!this.dateObject) {
717
+ return;
718
+ }
719
+ var oldValue = this.value;
720
+ var step = DEFAULT_SEGMENT_STEP * offset;
721
+ var caret = this.caret();
722
+ symbol = symbol || this.currentFormat[caret[0]];
723
+ if (symbol === "S" && !this.options.steps.millisecond) {
724
+ var msDigits = millisecondDigitsInFormat(this.inputFormat);
725
+ step = millisecondStepFor(msDigits);
726
+ }
727
+ this.dateObject.modifyPart(symbol, step);
728
+ this.tryTriggerValueChange({
729
+ oldValue: oldValue,
730
+ event: event
731
+ });
732
+ this.forceUpdate();
733
+ this.setSelection(this.selectionBySymbol(symbol));
734
+ };
735
+ /**
736
+ * @hidden
737
+ */
738
+ DateInput.prototype.tryTriggerValueChange = function (args) {
739
+ if (args === void 0) { args = { oldValue: null, event: {} }; }
740
+ if (!isEqual(this.value, args.oldValue)) {
741
+ return this.triggerValueChange(args);
742
+ }
743
+ };
744
+ /**
745
+ * @hidden
746
+ */
747
+ DateInput.prototype.triggerValueChange = function (args) {
748
+ if (args === void 0) { args = { oldValue: null, event: {} }; }
749
+ return this.trigger(VALUE_CHANGE, extend(args, {
750
+ value: this.value
751
+ }));
752
+ };
753
+ /**
754
+ * @hidden
755
+ */
756
+ DateInput.prototype.triggerInput = function (args) {
757
+ if (args === void 0) { args = { event: {} }; }
758
+ return this.trigger(INPUT, extend(args, {
759
+ value: this.value
760
+ }));
761
+ };
762
+ /**
763
+ * @hidden
764
+ */
765
+ DateInput.prototype.triggerInputEnd = function (args) {
766
+ if (args === void 0) { args = { event: {} }; }
767
+ return this.trigger(INPUT_END, extend(args, {
768
+ value: this.value
769
+ }));
770
+ };
771
+ /**
772
+ * @hidden
773
+ */
774
+ DateInput.prototype.triggerFocus = function (args) {
775
+ if (args === void 0) { args = { event: {} }; }
776
+ return this.trigger(FOCUS, extend({}, args));
777
+ };
778
+ /**
779
+ * @hidden
780
+ */
781
+ DateInput.prototype.triggerFocusEnd = function (args) {
782
+ if (args === void 0) { args = { event: {} }; }
783
+ return this.trigger(FOCUS_END, extend({}, args));
784
+ };
785
+ /**
786
+ * @hidden
787
+ */
788
+ DateInput.prototype.triggerBlur = function (args) {
789
+ if (args === void 0) { args = { event: {} }; }
790
+ return this.trigger(BLUR, extend({}, args));
791
+ };
792
+ /**
793
+ * @hidden
794
+ */
795
+ DateInput.prototype.triggerBlurEnd = function (args) {
796
+ if (args === void 0) { args = { event: {} }; }
797
+ return this.trigger(BLUR_END, extend({}, args));
798
+ };
799
+ /**
800
+ * @hidden
801
+ */
802
+ DateInput.prototype.triggerChange = function (args) {
803
+ if (args === void 0) { args = { event: {} }; }
804
+ return this.trigger(CHANGE, extend(args, {
805
+ value: this.value
806
+ }));
807
+ };
808
+ /**
809
+ * @hidden
810
+ */
811
+ DateInput.prototype.triggerKeyDown = function (args) {
812
+ if (args === void 0) { args = { event: {} }; }
813
+ return this.trigger(KEY_DOWN, extend({}, args));
814
+ };
815
+ /**
816
+ * @hidden
817
+ */
818
+ DateInput.prototype.triggerMouseWheel = function (args) {
819
+ if (args === void 0) { args = { event: {} }; }
820
+ return this.trigger(MOUSE_WHEEL, extend({}, args));
821
+ };
822
+ /**
823
+ * @hidden
824
+ */
825
+ DateInput.prototype.forceUpdate = function () {
826
+ this.setTextAndFormat();
827
+ this.refreshElementValue();
828
+ };
829
+ /**
830
+ * @hidden
831
+ */
832
+ DateInput.prototype.setTextAndFormat = function () {
833
+ var _a = this.dateObject.getTextAndFormat(), currentText = _a.text, currentFormat = _a.format;
834
+ this.currentFormat = currentFormat;
835
+ this.currentText = currentText;
836
+ };
837
+ /**
838
+ * @hidden
839
+ */
840
+ DateInput.prototype.setElementValue = function (value) {
841
+ this.previousElementValue = this.element.value;
842
+ this.element.value = value;
843
+ };
844
+ /**
845
+ * @hidden
846
+ */
847
+ DateInput.prototype.getStepFromSymbol = function (symbol) {
848
+ /* eslint-disable no-fallthrough */
849
+ switch (symbol) {
850
+ case "S":
851
+ return Number(this.options.steps.millisecond);
852
+ case "s":
853
+ return Number(this.options.steps.second);
854
+ case "m":
855
+ return Number(this.options.steps.minute);
856
+ // represents hour as value from 01 through 12
857
+ case "h":
858
+ // represents hour as value from 01 through 23
859
+ case "H":
860
+ return Number(this.options.steps.hour);
861
+ case "M":
862
+ return Number(this.options.steps.month);
863
+ // there is no 'D' format specifier for day
864
+ case "d":
865
+ // used for formats such as "EEEE, MMMM d, yyyy",
866
+ // where "EEEE" stands for full name of the day e.g. Monday
867
+ case "E":
868
+ return Number(this.options.steps.day);
869
+ // there is no 'Y' format specifier for year
870
+ case "y":
871
+ return Number(this.options.steps.year);
872
+ default:
873
+ return DEFAULT_SEGMENT_STEP;
874
+ }
875
+ /* eslint-enable no-fallthrough */
876
+ };
877
+ DateInput.prototype.setPreviousInputEventState = function (keyDownEvent) {
878
+ var _a = this.selection, selectionStart = _a.start, selectionEnd = _a.end;
879
+ var selectionOffset = -1;
880
+ if (keyDownEvent.keyCode === KeyCode.BACKSPACE) {
881
+ selectionOffset = 1;
882
+ }
883
+ else if (keyDownEvent.keyCode === KeyCode.DELETE) {
884
+ selectionOffset = 0;
885
+ }
886
+ else if (keyDownEvent.keyCode === KeyCode.SPACE) {
887
+ selectionOffset = -1;
888
+ }
889
+ else {
890
+ selectionOffset = -1;
891
+ }
892
+ this.setElementValue(this.previousElementValue || '');
893
+ this.setSelection({ start: selectionStart + selectionOffset, end: selectionEnd + selectionOffset });
894
+ };
895
+ DateInput.prototype.writeValue = function (value) {
896
+ this.verifyValue(value);
897
+ this.dateObject = this.getDateObject(value);
898
+ this.refreshElementValue();
899
+ };
900
+ DateInput.prototype.verifyValue = function (value) {
901
+ if (value && !isDate(value)) {
902
+ throw new Error("The 'value' should be a valid JavaScript Date instance.");
903
+ }
904
+ };
905
+ DateInput.prototype.refreshElementValue = function () {
906
+ var start = this.caret()[0];
907
+ var element = this.element;
908
+ var format = this.isActive ? this.inputFormat : this.displayFormat;
909
+ var _a = this.dateObject.getTextAndFormat(format), currentText = _a.text, currentFormat = _a.format;
910
+ this.currentFormat = currentFormat;
911
+ this.currentText = currentText;
912
+ var showPlaceholder = !this.isActive &&
913
+ isPresent(this.options.placeholder) &&
914
+ !this.dateObject.hasValue();
915
+ if (isPresent(this.options.placeholder)) {
916
+ element.placeholder = this.options.placeholder;
917
+ }
918
+ var newElementValue = !showPlaceholder ? currentText : "";
919
+ this.setElementValue(newElementValue);
920
+ if (this.isActive && !this.options.allowCaretMode && this.options.selectNearestSegmentOnFocus) {
921
+ this.selectNearestSegment(start);
922
+ }
923
+ };
924
+ /**
925
+ * @hidden
926
+ */
927
+ DateInput.prototype.caret = function (start, end) {
928
+ if (end === void 0) { end = start; }
929
+ var isPosition = start !== undefined;
930
+ var returnValue = [start, start];
931
+ var element = this.element;
932
+ if (isPosition && (this.options.disabled || this.options.readonly)) {
933
+ return undefined;
934
+ }
935
+ try {
936
+ if (element.selectionStart !== undefined) {
937
+ if (isPosition) {
938
+ if (isDocumentAvailable() && document.activeElement !== element) {
939
+ element.focus();
940
+ }
941
+ element.setSelectionRange(start, end);
942
+ }
943
+ returnValue = [element.selectionStart, element.selectionEnd];
944
+ }
945
+ }
946
+ catch (e) {
947
+ returnValue = [];
948
+ }
949
+ return returnValue;
950
+ };
951
+ DateInput.prototype.selectNearestSegment = function (index) {
952
+ // Finds the nearest (in both directions) known part.
953
+ for (var i = index, j = index - 1; i < this.currentFormat.length || j >= 0; i++, j--) {
954
+ if (i < this.currentFormat.length && this.currentFormat[i] !== "_") {
955
+ this.selectDateSegment(this.currentFormat[i]);
956
+ return;
957
+ }
958
+ if (j >= 0 && this.currentFormat[j] !== "_") {
959
+ this.selectDateSegment(this.currentFormat[j]);
960
+ return;
961
+ }
962
+ }
963
+ };
964
+ DateInput.prototype.selectDateSegment = function (symbol) {
965
+ var begin = -1;
966
+ var end = 0;
967
+ for (var i = 0; i < this.currentFormat.length; i++) {
968
+ if (this.currentFormat[i] === symbol) {
969
+ end = i + 1;
970
+ if (begin === -1) {
971
+ begin = i;
972
+ }
973
+ }
974
+ }
975
+ if (begin < 0) {
976
+ begin = 0;
977
+ }
978
+ this.caret(0, 0);
979
+ this.caret(begin, end);
980
+ };
981
+ /**
982
+ * @hidden
983
+ */
984
+ DateInput.prototype.getDateObject = function (value) {
985
+ var leadingZero = ((this.dateObject || {}) || null).leadingZero;
986
+ var dateObject = this.createDateObject({
987
+ value: value
988
+ });
989
+ dateObject.setLeadingZero(this.isActive ? leadingZero : null);
990
+ return dateObject;
991
+ };
992
+ /* tslint:disable:align */
993
+ /**
994
+ * @hidden
995
+ */
996
+ DateInput.prototype.createDateObject = function (options) {
997
+ if (options === void 0) { options = {}; }
998
+ var dateObject = new DateObject(extend({
999
+ intlService: this.intl,
1000
+ formatPlaceholder: this.formatPlaceholder,
1001
+ format: this.inputFormat,
1002
+ localeId: this.localeId,
1003
+ cycleTime: this.options.cycleTime,
1004
+ twoDigitYearMax: this.options.twoDigitYearMax,
1005
+ autoCorrectParts: this.options.autoCorrectParts
1006
+ }, options));
1007
+ return dateObject;
1008
+ };
1009
+ return DateInput;
1010
+ }(Observable));
1011
+ export { DateInput };