@94ai/nf-double-year 3.1.36 → 3.1.38

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.
@@ -0,0 +1,945 @@
1
+ <template>
2
+ <el-input
3
+ class="el-date-editor nf-double-year nf-double-year-picker"
4
+ :class="'el-date-editor--' + type"
5
+ :readonly="!editable || readonly"
6
+ :disabled="pickerDisabled"
7
+ :size="pickerSize"
8
+ :name="name"
9
+ v-bind="firstInputId"
10
+ v-if="!ranged"
11
+ v-clickoutside="handleClose"
12
+ :placeholder="placeholder"
13
+ @focus="handleFocus"
14
+ @keydown.native="handleKeydown"
15
+ :value="displayValue"
16
+ @input="value => userInput = value"
17
+ @change="handleChange"
18
+ @mouseenter.native="handleMouseEnter"
19
+ @mouseleave.native="showClose = false"
20
+ :validateEvent="false"
21
+ ref="reference">
22
+ <i slot="prefix"
23
+ class="el-input__icon"
24
+ :class="triggerClass"
25
+ @click="handleFocus">
26
+ </i>
27
+ <i slot="suffix"
28
+ class="el-input__icon"
29
+ @click="handleClickIcon"
30
+ :class="[showClose ? '' + clearIcon : '']"
31
+ v-if="haveTrigger">
32
+ </i>
33
+ </el-input>
34
+ <div
35
+ class="el-date-editor el-range-editor el-input__inner nf-double-year nf-double-year-picker"
36
+ :class="[
37
+ 'el-date-editor--' + type,
38
+ pickerSize ? `el-range-editor--${ pickerSize }` : '',
39
+ pickerDisabled ? 'is-disabled' : '',
40
+ pickerVisible ? 'is-active' : ''
41
+ ]"
42
+ @click="handleRangeClick"
43
+ @mouseenter="handleMouseEnter"
44
+ @mouseleave="showClose = false"
45
+ @keydown="handleKeydown"
46
+ ref="reference"
47
+ v-clickoutside="handleClose"
48
+ v-else>
49
+ <i :class="['el-input__icon', 'el-range__icon', triggerClass]"></i>
50
+ <input
51
+ autocomplete="off"
52
+ :placeholder="startPlaceholder"
53
+ :value="displayValue && displayValue[0]"
54
+ :disabled="pickerDisabled"
55
+ v-bind="firstInputId"
56
+ :readonly="!editable || readonly"
57
+ :name="name && name[0]"
58
+ @input="handleStartInput"
59
+ @change="handleStartChange"
60
+ @focus="handleFocus"
61
+ class="el-range-input">
62
+ <slot name="range-separator">
63
+ <span class="el-range-separator">{{ rangeSeparator }}</span>
64
+ </slot>
65
+ <input
66
+ autocomplete="off"
67
+ :placeholder="endPlaceholder"
68
+ :value="displayValue && displayValue[1]"
69
+ :disabled="pickerDisabled"
70
+ v-bind="secondInputId"
71
+ :readonly="!editable || readonly"
72
+ :name="name && name[1]"
73
+ @input="handleEndInput"
74
+ @change="handleEndChange"
75
+ @focus="handleFocus"
76
+ class="el-range-input">
77
+ <i
78
+ @click="handleClickIcon"
79
+ v-if="haveTrigger"
80
+ :class="[showClose ? '' + clearIcon : '']"
81
+ class="el-input__icon el-range__close-icon">
82
+ </i>
83
+ </div>
84
+ </template>
85
+
86
+ <script>
87
+ import Vue from 'vue';
88
+ import Clickoutside from 'element-ui/src/utils/clickoutside';
89
+ import { formatDate, parseDate, isDateObject, getWeekNumber } from 'element-ui/src/utils/date-util';
90
+ import Popper from 'element-ui/src/utils/vue-popper';
91
+ import Emitter from 'element-ui/src/mixins/emitter';
92
+ import merge from 'element-ui/src/utils/merge';
93
+
94
+ const NewPopper = {
95
+ props: {
96
+ appendToBody: Popper.props.appendToBody,
97
+ offset: Popper.props.offset,
98
+ boundariesPadding: Popper.props.boundariesPadding,
99
+ arrowOffset: Popper.props.arrowOffset
100
+ },
101
+ methods: Popper.methods,
102
+ data() {
103
+ return merge({ visibleArrow: true }, Popper.data);
104
+ },
105
+ beforeDestroy: Popper.beforeDestroy
106
+ };
107
+
108
+ const DEFAULT_FORMATS = {
109
+ date: 'yyyy-MM-dd',
110
+ month: 'yyyy-MM',
111
+ datetime: 'yyyy-MM-dd HH:mm:ss',
112
+ time: 'HH:mm:ss',
113
+ week: 'yyyywWW',
114
+ timerange: 'HH:mm:ss',
115
+ daterange: 'yyyy-MM-dd',
116
+ monthrange: 'yyyy-MM',
117
+ datetimerange: 'yyyy-MM-dd HH:mm:ss',
118
+ year: 'yyyy',
119
+ yearrange: 'yyyy'
120
+ };
121
+ const HAVE_TRIGGER_TYPES = [
122
+ 'date',
123
+ 'datetime',
124
+ 'time',
125
+ 'time-select',
126
+ 'week',
127
+ 'month',
128
+ 'year',
129
+ 'yearrange',
130
+ 'daterange',
131
+ 'monthrange',
132
+ 'timerange',
133
+ 'datetimerange',
134
+ 'dates'
135
+ ];
136
+ const DATE_FORMATTER = function(value, format) {
137
+ if (format === 'timestamp') return value.getTime();
138
+ return formatDate(value, format);
139
+ };
140
+ const DATE_PARSER = function(text, format) {
141
+ if (format === 'timestamp') return new Date(Number(text));
142
+ return parseDate(text, format);
143
+ };
144
+ const RANGE_FORMATTER = function(value, format) {
145
+ if (Array.isArray(value) && value.length === 2) {
146
+ const start = value[0];
147
+ const end = value[1];
148
+
149
+ if (start && end) {
150
+ return [DATE_FORMATTER(start, format), DATE_FORMATTER(end, format)];
151
+ }
152
+ }
153
+ return '';
154
+ };
155
+ const RANGE_PARSER = function(array, format, separator) {
156
+ if (!Array.isArray(array)) {
157
+ array = array.split(separator);
158
+ }
159
+ if (array.length === 2) {
160
+ const range1 = array[0];
161
+ const range2 = array[1];
162
+
163
+ return [DATE_PARSER(range1, format), DATE_PARSER(range2, format)];
164
+ }
165
+ return [];
166
+ };
167
+ const TYPE_VALUE_RESOLVER_MAP = {
168
+ default: {
169
+ formatter(value) {
170
+ if (!value) return '';
171
+ return '' + value;
172
+ },
173
+ parser(text) {
174
+ if (text === undefined || text === '') return null;
175
+ return text;
176
+ }
177
+ },
178
+ week: {
179
+ formatter(value, format) {
180
+ let week = getWeekNumber(value);
181
+ let month = value.getMonth();
182
+ const trueDate = new Date(value);
183
+ if (week === 1 && month === 11) {
184
+ trueDate.setHours(0, 0, 0, 0);
185
+ trueDate.setDate(trueDate.getDate() + 3 - (trueDate.getDay() + 6) % 7);
186
+ }
187
+ let date = formatDate(trueDate, format);
188
+
189
+ date = /WW/.test(date)
190
+ ? date.replace(/WW/, week < 10 ? '0' + week : week)
191
+ : date.replace(/W/, week);
192
+ return date;
193
+ },
194
+ parser(text, format) {
195
+ // parse as if a normal date
196
+ return TYPE_VALUE_RESOLVER_MAP.date.parser(text, format);
197
+ }
198
+ },
199
+ date: {
200
+ formatter: DATE_FORMATTER,
201
+ parser: DATE_PARSER
202
+ },
203
+ datetime: {
204
+ formatter: DATE_FORMATTER,
205
+ parser: DATE_PARSER
206
+ },
207
+ daterange: {
208
+ formatter: RANGE_FORMATTER,
209
+ parser: RANGE_PARSER
210
+ },
211
+ monthrange: {
212
+ formatter: RANGE_FORMATTER,
213
+ parser: RANGE_PARSER
214
+ },
215
+ yearrange: {
216
+ formatter: RANGE_FORMATTER,
217
+ parser: RANGE_PARSER
218
+ },
219
+ datetimerange: {
220
+ formatter: RANGE_FORMATTER,
221
+ parser: RANGE_PARSER
222
+ },
223
+ timerange: {
224
+ formatter: RANGE_FORMATTER,
225
+ parser: RANGE_PARSER
226
+ },
227
+ time: {
228
+ formatter: DATE_FORMATTER,
229
+ parser: DATE_PARSER
230
+ },
231
+ month: {
232
+ formatter: DATE_FORMATTER,
233
+ parser: DATE_PARSER
234
+ },
235
+ year: {
236
+ formatter: DATE_FORMATTER,
237
+ parser: DATE_PARSER
238
+ },
239
+ number: {
240
+ formatter(value) {
241
+ if (!value) return '';
242
+ return '' + value;
243
+ },
244
+ parser(text) {
245
+ let result = Number(text);
246
+
247
+ if (!isNaN(text)) {
248
+ return result;
249
+ } else {
250
+ return null;
251
+ }
252
+ }
253
+ },
254
+ dates: {
255
+ formatter(value, format) {
256
+ return value.map(date => DATE_FORMATTER(date, format));
257
+ },
258
+ parser(value, format) {
259
+ return (typeof value === 'string' ? value.split(', ') : value)
260
+ .map(date => date instanceof Date ? date : DATE_PARSER(date, format));
261
+ }
262
+ }
263
+ };
264
+ const PLACEMENT_MAP = {
265
+ left: 'bottom-start',
266
+ center: 'bottom',
267
+ right: 'bottom-end'
268
+ };
269
+
270
+ const parseAsFormatAndType = (value, customFormat, type, rangeSeparator = '-') => {
271
+ if (!value) return null;
272
+ const parser = (
273
+ TYPE_VALUE_RESOLVER_MAP[type] ||
274
+ TYPE_VALUE_RESOLVER_MAP['default']
275
+ ).parser;
276
+ const format = customFormat || DEFAULT_FORMATS[type];
277
+ return parser(value, format, rangeSeparator);
278
+ };
279
+
280
+ const formatAsFormatAndType = (value, customFormat, type) => {
281
+ if (!value) return null;
282
+ const formatter = (
283
+ TYPE_VALUE_RESOLVER_MAP[type] ||
284
+ TYPE_VALUE_RESOLVER_MAP['default']
285
+ ).formatter;
286
+ const format = customFormat || DEFAULT_FORMATS[type];
287
+ return formatter(value, format);
288
+ };
289
+
290
+ /*
291
+ * Considers:
292
+ * 1. Date object
293
+ * 2. date string
294
+ * 3. array of 1 or 2
295
+ */
296
+ const valueEquals = function(a, b) {
297
+ // considers Date object and string
298
+ const dateEquals = function(a, b) {
299
+ const aIsDate = a instanceof Date;
300
+ const bIsDate = b instanceof Date;
301
+ if (aIsDate && bIsDate) {
302
+ return a.getTime() === b.getTime();
303
+ }
304
+ if (!aIsDate && !bIsDate) {
305
+ return a === b;
306
+ }
307
+ return false;
308
+ };
309
+
310
+ const aIsArray = a instanceof Array;
311
+ const bIsArray = b instanceof Array;
312
+ if (aIsArray && bIsArray) {
313
+ if (a.length !== b.length) {
314
+ return false;
315
+ }
316
+ return a.every((item, index) => dateEquals(item, b[index]));
317
+ }
318
+ if (!aIsArray && !bIsArray) {
319
+ return dateEquals(a, b);
320
+ }
321
+ return false;
322
+ };
323
+
324
+ const isString = function(val) {
325
+ return typeof val === 'string' || val instanceof String;
326
+ };
327
+
328
+ const validator = function(val) {
329
+ // either: String, Array of String, null / undefined
330
+ return (
331
+ val === null ||
332
+ val === undefined ||
333
+ isString(val) ||
334
+ (Array.isArray(val) && val.length === 2 && val.every(isString))
335
+ );
336
+ };
337
+
338
+ export default {
339
+ mixins: [Emitter, NewPopper],
340
+
341
+ inject: {
342
+ elForm: {
343
+ default: ''
344
+ },
345
+ elFormItem: {
346
+ default: ''
347
+ }
348
+ },
349
+
350
+ props: {
351
+ size: {
352
+ type: String,
353
+ default: 'small'
354
+ },
355
+ format: String,
356
+ valueFormat: String,
357
+ readonly: Boolean,
358
+ placeholder: String,
359
+ startPlaceholder: {
360
+ type: String,
361
+ default: '开始年份'
362
+ },
363
+ endPlaceholder: {
364
+ type: String,
365
+ default: '结束年份'
366
+ },
367
+ prefixIcon: String,
368
+ clearIcon: {
369
+ type: String,
370
+ default: 'el-icon-circle-close'
371
+ },
372
+ name: {
373
+ default: '',
374
+ validator
375
+ },
376
+ disabled: Boolean,
377
+ clearable: {
378
+ type: Boolean,
379
+ default: true
380
+ },
381
+ id: {
382
+ default: '',
383
+ validator
384
+ },
385
+ popperClass: String,
386
+ editable: {
387
+ type: Boolean,
388
+ default: true
389
+ },
390
+ align: {
391
+ type: String,
392
+ default: 'left'
393
+ },
394
+ value: {},
395
+ defaultValue: {},
396
+ defaultTime: {},
397
+ rangeSeparator: {
398
+ type: String,
399
+ default: '-'
400
+ },
401
+ pickerOptions: {},
402
+ unlinkPanels: Boolean,
403
+ validateEvent: {
404
+ type: Boolean,
405
+ default: true
406
+ }
407
+ },
408
+
409
+
410
+ directives: { Clickoutside },
411
+
412
+ data() {
413
+ return {
414
+ pickerVisible: false,
415
+ showClose: false,
416
+ userInput: null,
417
+ valueOnOpen: null, // value when picker opens, used to determine whether to emit change
418
+ unwatchPickerOptions: null
419
+ };
420
+ },
421
+
422
+ watch: {
423
+ pickerVisible(val) {
424
+ if (this.readonly || this.pickerDisabled) return;
425
+ if (val) {
426
+ this.showPicker();
427
+ this.valueOnOpen = Array.isArray(this.value) ? [...this.value] : this.value;
428
+ } else {
429
+ this.hidePicker();
430
+ this.emitChange(this.value);
431
+ this.userInput = null;
432
+ if (this.validateEvent) {
433
+ this.dispatch('ElFormItem', 'el.form.blur');
434
+ }
435
+ this.$emit('blur', this);
436
+ this.blur();
437
+ }
438
+ },
439
+ parsedValue: {
440
+ immediate: true,
441
+ handler(val) {
442
+ if (this.picker) {
443
+ this.picker.value = val;
444
+ }
445
+ }
446
+ },
447
+ defaultValue(val) {
448
+ // NOTE: should eventually move to jsx style picker + panel ?
449
+ if (this.picker) {
450
+ this.picker.defaultValue = val;
451
+ }
452
+ },
453
+ value(val, oldVal) {
454
+ if (!valueEquals(val, oldVal) && !this.pickerVisible && this.validateEvent) {
455
+ this.dispatch('ElFormItem', 'el.form.change', val);
456
+ }
457
+ }
458
+ },
459
+
460
+ computed: {
461
+ ranged() {
462
+ return this.type.indexOf('range') > -1;
463
+ },
464
+
465
+ reference() {
466
+ const reference = this.$refs.reference;
467
+ return reference.$el || reference;
468
+ },
469
+
470
+ refInput() {
471
+ if (this.reference) {
472
+ return [].slice.call(this.reference.querySelectorAll('input'));
473
+ }
474
+ return [];
475
+ },
476
+
477
+ valueIsEmpty() {
478
+ const val = this.value;
479
+ if (Array.isArray(val)) {
480
+ for (let i = 0, len = val.length; i < len; i++) {
481
+ if (val[i]) {
482
+ return false;
483
+ }
484
+ }
485
+ } else {
486
+ if (val) {
487
+ return false;
488
+ }
489
+ }
490
+ return true;
491
+ },
492
+
493
+ triggerClass() {
494
+ return this.prefixIcon || (this.type.indexOf('time') !== -1 ? 'el-icon-time' : 'el-icon-date');
495
+ },
496
+
497
+ selectionMode() {
498
+ if (this.type === 'week') {
499
+ return 'week';
500
+ } else if (this.type === 'month') {
501
+ return 'month';
502
+ } else if (this.type === 'year') {
503
+ return 'year';
504
+ } else if (this.type === 'dates') {
505
+ return 'dates';
506
+ }
507
+
508
+ return 'day';
509
+ },
510
+
511
+ haveTrigger() {
512
+ if (typeof this.showTrigger !== 'undefined') {
513
+ return this.showTrigger;
514
+ }
515
+ return HAVE_TRIGGER_TYPES.indexOf(this.type) !== -1;
516
+ },
517
+
518
+ displayValue() {
519
+ const formattedValue = formatAsFormatAndType(this.parsedValue, this.format, this.type, this.rangeSeparator);
520
+ if (Array.isArray(this.userInput)) {
521
+ return [
522
+ this.userInput[0] || (formattedValue && formattedValue[0]) || '',
523
+ this.userInput[1] || (formattedValue && formattedValue[1]) || ''
524
+ ];
525
+ } else if (this.userInput !== null) {
526
+ return this.userInput;
527
+ } else if (formattedValue) {
528
+ return this.type === 'dates'
529
+ ? formattedValue.join(', ')
530
+ : formattedValue;
531
+ } else {
532
+ return '';
533
+ }
534
+ },
535
+
536
+ parsedValue() {
537
+ if (!this.value) return this.value; // component value is not set
538
+ if (this.type === 'time-select') return this.value; // time-select does not require parsing, this might change in next major version
539
+
540
+ const valueIsDateObject = isDateObject(this.value) || (Array.isArray(this.value) && this.value.every(isDateObject));
541
+ if (valueIsDateObject) {
542
+ return this.value;
543
+ }
544
+
545
+ if (this.valueFormat) {
546
+ return parseAsFormatAndType(this.value, this.valueFormat, this.type, this.rangeSeparator) || this.value;
547
+ }
548
+
549
+ // NOTE: deal with common but incorrect usage, should remove in next major version
550
+ // user might provide string / timestamp without value-format, coerce them into date (or array of date)
551
+ return Array.isArray(this.value) ? this.value.map(val => new Date(val)) : new Date(this.value);
552
+ },
553
+
554
+ _elFormItemSize() {
555
+ return (this.elFormItem || {}).elFormItemSize;
556
+ },
557
+
558
+ pickerSize() {
559
+ return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
560
+ },
561
+
562
+ pickerDisabled() {
563
+ return this.disabled || (this.elForm || {}).disabled;
564
+ },
565
+
566
+ firstInputId() {
567
+ const obj = {};
568
+ let id;
569
+ if (this.ranged) {
570
+ id = this.id && this.id[0];
571
+ } else {
572
+ id = this.id;
573
+ }
574
+ if (id) obj.id = id;
575
+ return obj;
576
+ },
577
+
578
+ secondInputId() {
579
+ const obj = {};
580
+ let id;
581
+ if (this.ranged) {
582
+ id = this.id && this.id[1];
583
+ }
584
+ if (id) obj.id = id;
585
+ return obj;
586
+ }
587
+ },
588
+
589
+ created() {
590
+ // vue-popper
591
+ this.popperOptions = {
592
+ boundariesPadding: 0,
593
+ gpuAcceleration: false
594
+ };
595
+ this.placement = PLACEMENT_MAP[this.align] || PLACEMENT_MAP.left;
596
+
597
+ this.$on('fieldReset', this.handleFieldReset);
598
+ },
599
+
600
+ methods: {
601
+ focus() {
602
+ if (!this.ranged) {
603
+ this.$refs.reference.focus();
604
+ } else {
605
+ this.handleFocus();
606
+ }
607
+ },
608
+
609
+ blur() {
610
+ this.refInput.forEach(input => input.blur());
611
+ },
612
+
613
+ // {parse, formatTo} Value deals maps component value with internal Date
614
+ parseValue(value) {
615
+ const isParsed = isDateObject(value) || (Array.isArray(value) && value.every(isDateObject));
616
+ if (this.valueFormat && !isParsed) {
617
+ return parseAsFormatAndType(value, this.valueFormat, this.type, this.rangeSeparator) || value;
618
+ } else {
619
+ return value;
620
+ }
621
+ },
622
+
623
+ formatToValue(date) {
624
+ const isFormattable = isDateObject(date) || (Array.isArray(date) && date.every(isDateObject));
625
+ if (this.valueFormat && isFormattable) {
626
+ return formatAsFormatAndType(date, this.valueFormat, this.type, this.rangeSeparator);
627
+ } else {
628
+ return date;
629
+ }
630
+ },
631
+
632
+ // {parse, formatTo} String deals with user input
633
+ parseString(value) {
634
+ const type = Array.isArray(value) ? this.type : this.type.replace('range', '');
635
+ return parseAsFormatAndType(value, this.format, type);
636
+ },
637
+
638
+ formatToString(value) {
639
+ const type = Array.isArray(value) ? this.type : this.type.replace('range', '');
640
+ return formatAsFormatAndType(value, this.format, type);
641
+ },
642
+
643
+ handleMouseEnter() {
644
+ if (this.readonly || this.pickerDisabled) return;
645
+ if (!this.valueIsEmpty && this.clearable) {
646
+ this.showClose = true;
647
+ }
648
+ },
649
+
650
+ handleChange() {
651
+ if (this.userInput) {
652
+ const value = this.parseString(this.displayValue);
653
+ if (value) {
654
+ this.picker.value = value;
655
+ if (this.isValidValue(value)) {
656
+ this.emitInput(value);
657
+ this.userInput = null;
658
+ }
659
+ }
660
+ }
661
+ if (this.userInput === '') {
662
+ this.emitInput(null);
663
+ this.emitChange(null);
664
+ this.userInput = null;
665
+ }
666
+ },
667
+
668
+ handleStartInput(event) {
669
+ if (this.userInput) {
670
+ this.userInput = [event.target.value, this.userInput[1]];
671
+ } else {
672
+ this.userInput = [event.target.value, null];
673
+ }
674
+ },
675
+
676
+ handleEndInput(event) {
677
+ if (this.userInput) {
678
+ this.userInput = [this.userInput[0], event.target.value];
679
+ } else {
680
+ this.userInput = [null, event.target.value];
681
+ }
682
+ },
683
+
684
+ handleStartChange(event) {
685
+ const value = this.parseString(this.userInput && this.userInput[0]);
686
+ if (value) {
687
+ this.userInput = [this.formatToString(value), this.displayValue[1]];
688
+ const newValue = [value, this.picker.value && this.picker.value[1]];
689
+ this.picker.value = newValue;
690
+ if (this.isValidValue(newValue)) {
691
+ this.emitInput(newValue);
692
+ this.userInput = null;
693
+ }
694
+ }
695
+ },
696
+
697
+ handleEndChange(event) {
698
+ const value = this.parseString(this.userInput && this.userInput[1]);
699
+ if (value) {
700
+ this.userInput = [this.displayValue[0], this.formatToString(value)];
701
+ const newValue = [this.picker.value && this.picker.value[0], value];
702
+ this.picker.value = newValue;
703
+ if (this.isValidValue(newValue)) {
704
+ this.emitInput(newValue);
705
+ this.userInput = null;
706
+ }
707
+ }
708
+ },
709
+
710
+ handleClickIcon(event) {
711
+ if (this.readonly || this.pickerDisabled) return;
712
+ if (this.showClose) {
713
+ this.valueOnOpen = this.value;
714
+ event.stopPropagation();
715
+ this.emitInput(null);
716
+ this.emitChange(null);
717
+ this.showClose = false;
718
+ if (this.picker && typeof this.picker.handleClear === 'function') {
719
+ this.picker.handleClear();
720
+ }
721
+ } else {
722
+ this.pickerVisible = !this.pickerVisible;
723
+ }
724
+ },
725
+
726
+ handleClose() {
727
+ if (!this.pickerVisible) return;
728
+ this.pickerVisible = false;
729
+
730
+ if (this.type === 'dates') {
731
+ // restore to former value
732
+ const oldValue = parseAsFormatAndType(this.valueOnOpen, this.valueFormat, this.type, this.rangeSeparator) || this.valueOnOpen;
733
+ this.emitInput(oldValue);
734
+ }
735
+ },
736
+
737
+ handleFieldReset(initialValue) {
738
+ this.userInput = initialValue === '' ? null : initialValue;
739
+ },
740
+
741
+ handleFocus() {
742
+ const type = this.type;
743
+
744
+ if (HAVE_TRIGGER_TYPES.indexOf(type) !== -1 && !this.pickerVisible) {
745
+ this.pickerVisible = true;
746
+ }
747
+ this.$emit('focus', this);
748
+ },
749
+
750
+ handleKeydown(event) {
751
+ const keyCode = event.keyCode;
752
+
753
+ // ESC
754
+ if (keyCode === 27) {
755
+ this.pickerVisible = false;
756
+ event.stopPropagation();
757
+ return;
758
+ }
759
+
760
+ // Tab
761
+ if (keyCode === 9) {
762
+ if (!this.ranged) {
763
+ this.handleChange();
764
+ this.pickerVisible = this.picker.visible = false;
765
+ this.blur();
766
+ event.stopPropagation();
767
+ } else {
768
+ // user may change focus between two input
769
+ setTimeout(() => {
770
+ if (this.refInput.indexOf(document.activeElement) === -1) {
771
+ this.pickerVisible = false;
772
+ this.blur();
773
+ event.stopPropagation();
774
+ }
775
+ }, 0);
776
+ }
777
+ return;
778
+ }
779
+
780
+ // Enter
781
+ if (keyCode === 13) {
782
+ if (this.userInput === '' || this.isValidValue(this.parseString(this.displayValue))) {
783
+ this.handleChange();
784
+ this.pickerVisible = this.picker.visible = false;
785
+ this.blur();
786
+ }
787
+ event.stopPropagation();
788
+ return;
789
+ }
790
+
791
+ // if user is typing, do not let picker handle key input
792
+ if (this.userInput) {
793
+ event.stopPropagation();
794
+ return;
795
+ }
796
+
797
+ // delegate other keys to panel
798
+ if (this.picker && this.picker.handleKeydown) {
799
+ this.picker.handleKeydown(event);
800
+ }
801
+ },
802
+
803
+ handleRangeClick() {
804
+ const type = this.type;
805
+
806
+ if (HAVE_TRIGGER_TYPES.indexOf(type) !== -1 && !this.pickerVisible) {
807
+ this.pickerVisible = true;
808
+ }
809
+ this.$emit('focus', this);
810
+ },
811
+
812
+ hidePicker() {
813
+ if (this.picker) {
814
+ this.picker.resetView && this.picker.resetView();
815
+ this.pickerVisible = this.picker.visible = false;
816
+ this.destroyPopper();
817
+ }
818
+ },
819
+
820
+ showPicker() {
821
+ if (this.$isServer) return;
822
+ if (!this.picker) {
823
+ this.mountPicker();
824
+ }
825
+ this.pickerVisible = this.picker.visible = true;
826
+
827
+ this.updatePopper();
828
+
829
+ this.picker.value = this.parsedValue;
830
+ this.picker.resetView && this.picker.resetView();
831
+
832
+ this.$nextTick(() => {
833
+ this.picker.adjustSpinners && this.picker.adjustSpinners();
834
+ });
835
+ },
836
+
837
+ mountPicker() {
838
+ this.picker = new Vue(this.panel).$mount();
839
+ this.picker.defaultValue = this.defaultValue;
840
+ this.picker.defaultTime = this.defaultTime;
841
+ this.picker.popperClass = this.popperClass;
842
+ this.popperElm = this.picker.$el;
843
+ this.picker.width = this.reference.getBoundingClientRect().width;
844
+ this.picker.showTime = this.type === 'datetime' || this.type === 'datetimerange';
845
+ this.picker.selectionMode = this.selectionMode;
846
+ this.picker.unlinkPanels = this.unlinkPanels;
847
+ this.picker.arrowControl = this.arrowControl || this.timeArrowControl || false;
848
+ this.$watch('format', (format) => {
849
+ this.picker.format = format;
850
+ });
851
+
852
+ const updateOptions = () => {
853
+ const options = this.pickerOptions;
854
+
855
+ if (options && options.selectableRange) {
856
+ let ranges = options.selectableRange;
857
+ const parser = TYPE_VALUE_RESOLVER_MAP.datetimerange.parser;
858
+ const format = DEFAULT_FORMATS.timerange;
859
+
860
+ ranges = Array.isArray(ranges) ? ranges : [ranges];
861
+ this.picker.selectableRange = ranges.map(range => parser(range, format, this.rangeSeparator));
862
+ }
863
+
864
+ for (const option in options) {
865
+ if (options.hasOwnProperty(option) &&
866
+ // 忽略 time-picker 的该配置项
867
+ option !== 'selectableRange') {
868
+ this.picker[option] = options[option];
869
+ }
870
+ }
871
+
872
+ // main format must prevail over undocumented pickerOptions.format
873
+ if (this.format) {
874
+ this.picker.format = this.format;
875
+ }
876
+ };
877
+ updateOptions();
878
+ this.unwatchPickerOptions = this.$watch('pickerOptions', () => updateOptions(), { deep: true });
879
+ this.$el.appendChild(this.picker.$el);
880
+ this.picker.resetView && this.picker.resetView();
881
+
882
+ this.picker.$on('dodestroy', this.doDestroy);
883
+ this.picker.$on('pick', (date = '', visible = false) => {
884
+ this.userInput = null;
885
+ this.pickerVisible = this.picker.visible = visible;
886
+ this.emitInput(date);
887
+ this.picker.resetView && this.picker.resetView();
888
+ });
889
+
890
+ this.picker.$on('select-range', (start, end, pos) => {
891
+ if (this.refInput.length === 0) return;
892
+ if (!pos || pos === 'min') {
893
+ this.refInput[0].setSelectionRange(start, end);
894
+ this.refInput[0].focus();
895
+ } else if (pos === 'max') {
896
+ this.refInput[1].setSelectionRange(start, end);
897
+ this.refInput[1].focus();
898
+ }
899
+ });
900
+ },
901
+
902
+ unmountPicker() {
903
+ if (this.picker) {
904
+ this.picker.$destroy();
905
+ this.picker.$off();
906
+ if (typeof this.unwatchPickerOptions === 'function') {
907
+ this.unwatchPickerOptions();
908
+ }
909
+ this.picker.$el.parentNode.removeChild(this.picker.$el);
910
+ }
911
+ },
912
+
913
+ emitChange(val) {
914
+ // determine user real change only
915
+ if (!valueEquals(val, this.valueOnOpen)) {
916
+ this.$emit('change', val);
917
+ this.$emit('update:value', val);
918
+ this.valueOnOpen = val;
919
+ if (this.validateEvent) {
920
+ this.dispatch('ElFormItem', 'el.form.change', val);
921
+ }
922
+ }
923
+ },
924
+
925
+ emitInput(val) {
926
+ const formatted = this.formatToValue(val);
927
+ if (!valueEquals(this.value, formatted)) {
928
+ this.$emit('input', formatted);
929
+ this.$emit('update:value', formatted);
930
+ }
931
+ },
932
+
933
+ isValidValue(value) {
934
+ if (!this.picker) {
935
+ this.mountPicker();
936
+ }
937
+ if (this.picker.isValidValue) {
938
+ return value && this.picker.isValidValue(value);
939
+ } else {
940
+ return true;
941
+ }
942
+ }
943
+ }
944
+ };
945
+ </script>