@94ai/nf-timezone-picker 5.0.0

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 (41) hide show
  1. package/.eslintrc.cjs +49 -0
  2. package/README.md +17 -0
  3. package/components.d.ts +8 -0
  4. package/lib/basic/date-table.vue.d.ts +86 -0
  5. package/lib/basic/month-table.vue.d.ts +51 -0
  6. package/lib/basic/time-spinner.vue.d.ts +42 -0
  7. package/lib/basic/year-table.vue.d.ts +26 -0
  8. package/lib/index.d.ts +333 -0
  9. package/lib/nf-timezone-picker.cjs.js +5984 -0
  10. package/lib/nf-timezone-picker.esm-bundler.js +5984 -0
  11. package/lib/nf-timezone-picker.vue.d.ts +157 -0
  12. package/lib/panel/date-range.vue.d.ts +55 -0
  13. package/lib/panel/date.vue.d.ts +35 -0
  14. package/lib/panel/month-range.vue.d.ts +30 -0
  15. package/lib/panel/time.vue.d.ts +27 -0
  16. package/lib/picker.vue.d.ts +142 -0
  17. package/lib/style/css.d.ts +1 -0
  18. package/lib/style/css.js +1 -0
  19. package/lib/style/index.d.ts +1 -0
  20. package/lib/style/index.js +1 -0
  21. package/package/basic/date-table.vue +448 -0
  22. package/package/basic/month-table.vue +253 -0
  23. package/package/basic/time-spinner.vue +316 -0
  24. package/package/basic/year-table.vue +101 -0
  25. package/package/index.ts +7 -0
  26. package/package/nf-timezone-picker.vue +50 -0
  27. package/package/panel/date-range.vue +700 -0
  28. package/package/panel/date.vue +607 -0
  29. package/package/panel/month-range.vue +293 -0
  30. package/package/panel/time-range.vue +254 -0
  31. package/package/panel/time-select.vue +178 -0
  32. package/package/panel/time.vue +189 -0
  33. package/package/picker/date-picker.js +43 -0
  34. package/package/picker/time-picker.js +39 -0
  35. package/package/picker/time-select.js +21 -0
  36. package/package/picker.vue +943 -0
  37. package/package/popper.js +1276 -0
  38. package/package/style/css.ts +1 -0
  39. package/package/style/index.ts +1 -0
  40. package/package.json +34 -0
  41. package/postcss.config.js +8 -0
@@ -0,0 +1,943 @@
1
+ <template>
2
+ <el-input
3
+ v-if="!ranged"
4
+ ref="reference"
5
+ v-clickoutside="handleClose"
6
+ class="el-date-editor"
7
+ :class="'el-date-editor--' + type"
8
+ :readonly="!editable || readonly || type === 'dates' || type === 'week'"
9
+ :disabled="pickerDisabled"
10
+ :size="pickerSize"
11
+ :name="name"
12
+ v-bind="firstInputId"
13
+ :placeholder="placeholder"
14
+ :value="displayValue"
15
+ :validate-event="false"
16
+ @focus="handleFocus"
17
+ @keydown.native="handleKeydown"
18
+ @input="value => userInput = value"
19
+ @change="handleChange"
20
+ @mouseenter.native="handleMouseEnter"
21
+ @mouseleave.native="showClose = false"
22
+ >
23
+ <i
24
+ slot="prefix"
25
+ class="el-input__icon"
26
+ :class="triggerClass"
27
+ @click="handleFocus"
28
+ />
29
+ <i
30
+ v-if="haveTrigger"
31
+ slot="suffix"
32
+ class="el-input__icon"
33
+ :class="[showClose ? '' + clearIcon : '']"
34
+ @click="handleClickIcon"
35
+ />
36
+ </el-input>
37
+ <div
38
+ v-else
39
+ ref="reference"
40
+ v-clickoutside="handleClose"
41
+ class="el-date-editor el-range-editor el-input__inner"
42
+ :class="[
43
+ 'el-date-editor--' + type,
44
+ pickerSize ? `el-range-editor--${ pickerSize }` : '',
45
+ pickerDisabled ? 'is-disabled' : '',
46
+ pickerVisible ? 'is-active' : ''
47
+ ]"
48
+ @click="handleRangeClick"
49
+ @mouseenter="handleMouseEnter"
50
+ @mouseleave="showClose = false"
51
+ @keydown="handleKeydown"
52
+ >
53
+ <i :class="['el-input__icon', 'el-range__icon', triggerClass]" />
54
+ <input
55
+ autocomplete="off"
56
+ :placeholder="startPlaceholder"
57
+ :value="displayValue && displayValue[0]"
58
+ :disabled="pickerDisabled"
59
+ v-bind="firstInputId"
60
+ :readonly="!editable || readonly"
61
+ :name="name && name[0]"
62
+ class="el-range-input"
63
+ @input="handleStartInput"
64
+ @change="handleStartChange"
65
+ @focus="handleFocus"
66
+ >
67
+ <slot name="range-separator">
68
+ <span class="el-range-separator">{{ rangeSeparator }}</span>
69
+ </slot>
70
+ <input
71
+ autocomplete="off"
72
+ :placeholder="endPlaceholder"
73
+ :value="displayValue && displayValue[1]"
74
+ :disabled="pickerDisabled"
75
+ v-bind="secondInputId"
76
+ :readonly="!editable || readonly"
77
+ :name="name && name[1]"
78
+ class="el-range-input"
79
+ @input="handleEndInput"
80
+ @change="handleEndChange"
81
+ @focus="handleFocus"
82
+ >
83
+ <i
84
+ v-if="haveTrigger"
85
+ :class="[showClose ? '' + clearIcon : '']"
86
+ class="el-input__icon el-range__close-icon"
87
+ @click="handleClickIcon"
88
+ />
89
+ </div>
90
+ </template>
91
+
92
+ <script>
93
+ import Vue from 'vue'
94
+ import Clickoutside from 'element-ui/src/utils/clickoutside'
95
+ import { formatDate, parseDate, isDateObject, getWeekNumber } from 'element-ui/src/utils/date-util'
96
+ import Popper from 'element-ui/src/utils/vue-popper'
97
+ import Emitter from 'element-ui/src/mixins/emitter'
98
+ import merge from 'element-ui/src/utils/merge'
99
+
100
+ const NewPopper = {
101
+ props: {
102
+ appendToBody: Popper.props.appendToBody,
103
+ offset: Popper.props.offset,
104
+ boundariesPadding: Popper.props.boundariesPadding,
105
+ arrowOffset: Popper.props.arrowOffset
106
+ },
107
+ methods: Popper.methods,
108
+ data() {
109
+ return merge({ visibleArrow: true }, Popper.data)
110
+ },
111
+ beforeDestroy: Popper.beforeDestroy
112
+ }
113
+
114
+ const DEFAULT_FORMATS = {
115
+ date: 'yyyy-MM-dd',
116
+ month: 'yyyy-MM',
117
+ datetime: 'yyyy-MM-dd HH:mm:ss',
118
+ time: 'HH:mm:ss',
119
+ week: 'yyyywWW',
120
+ timerange: 'HH:mm:ss',
121
+ daterange: 'yyyy-MM-dd',
122
+ monthrange: 'yyyy-MM',
123
+ datetimerange: 'yyyy-MM-dd HH:mm:ss',
124
+ year: 'yyyy'
125
+ }
126
+ const HAVE_TRIGGER_TYPES = [
127
+ 'date',
128
+ 'datetime',
129
+ 'time',
130
+ 'time-select',
131
+ 'week',
132
+ 'month',
133
+ 'year',
134
+ 'daterange',
135
+ 'monthrange',
136
+ 'timerange',
137
+ 'datetimerange',
138
+ 'dates'
139
+ ]
140
+ const DATE_FORMATTER = function(value, format) {
141
+ if (format === 'timestamp') return value.getTime()
142
+ return formatDate(value, format)
143
+ }
144
+ const DATE_PARSER = function(text, format) {
145
+ if (format === 'timestamp') return new Date(Number(text))
146
+ return parseDate(text, format)
147
+ }
148
+ const RANGE_FORMATTER = function(value, format) {
149
+ if (Array.isArray(value) && value.length === 2) {
150
+ const start = value[0]
151
+ const end = value[1]
152
+
153
+ if (start && end) {
154
+ return [DATE_FORMATTER(start, format), DATE_FORMATTER(end, format)]
155
+ }
156
+ }
157
+ return ''
158
+ }
159
+ const RANGE_PARSER = function(array, format, separator) {
160
+ if (!Array.isArray(array)) {
161
+ array = array.split(separator)
162
+ }
163
+ if (array.length === 2) {
164
+ const range1 = array[0]
165
+ const range2 = array[1]
166
+
167
+ return [DATE_PARSER(range1, format), DATE_PARSER(range2, format)]
168
+ }
169
+ return []
170
+ }
171
+ const TYPE_VALUE_RESOLVER_MAP = {
172
+ default: {
173
+ formatter(value) {
174
+ if (!value) return ''
175
+ return '' + value
176
+ },
177
+ parser(text) {
178
+ if (text === undefined || text === '') return null
179
+ return text
180
+ }
181
+ },
182
+ week: {
183
+ formatter(value, format) {
184
+ const week = getWeekNumber(value)
185
+ const month = value.getMonth()
186
+ const trueDate = new Date(value)
187
+ if (week === 1 && month === 11) {
188
+ trueDate.setHours(0, 0, 0, 0)
189
+ trueDate.setDate(trueDate.getDate() + 3 - (trueDate.getDay() + 6) % 7)
190
+ }
191
+ let date = formatDate(trueDate, format)
192
+
193
+ date = /WW/.test(date)
194
+ ? date.replace(/WW/, week < 10 ? '0' + week : week)
195
+ : date.replace(/W/, week)
196
+ return date
197
+ },
198
+ parser(text, format) {
199
+ // parse as if a normal date
200
+ return TYPE_VALUE_RESOLVER_MAP.date.parser(text, format)
201
+ }
202
+ },
203
+ date: {
204
+ formatter: DATE_FORMATTER,
205
+ parser: DATE_PARSER
206
+ },
207
+ datetime: {
208
+ formatter: DATE_FORMATTER,
209
+ parser: DATE_PARSER
210
+ },
211
+ daterange: {
212
+ formatter: RANGE_FORMATTER,
213
+ parser: RANGE_PARSER
214
+ },
215
+ monthrange: {
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
+ const 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
+
340
+ directives: { Clickoutside },
341
+ mixins: [Emitter, NewPopper],
342
+
343
+ inject: {
344
+ elForm: {
345
+ default: ''
346
+ },
347
+ elFormItem: {
348
+ default: ''
349
+ }
350
+ },
351
+
352
+ props: {
353
+ size: String,
354
+ format: String,
355
+ valueFormat: String,
356
+ readonly: Boolean,
357
+ placeholder: String,
358
+ startPlaceholder: String,
359
+ endPlaceholder: String,
360
+ prefixIcon: String,
361
+ clearIcon: {
362
+ type: String,
363
+ default: 'el-icon-circle-close'
364
+ },
365
+ name: {
366
+ default: '',
367
+ validator
368
+ },
369
+ disabled: Boolean,
370
+ clearable: {
371
+ type: Boolean,
372
+ default: true
373
+ },
374
+ id: {
375
+ default: '',
376
+ validator
377
+ },
378
+ popperClass: String,
379
+ editable: {
380
+ type: Boolean,
381
+ default: true
382
+ },
383
+ align: {
384
+ type: String,
385
+ default: 'left'
386
+ },
387
+ value: {},
388
+ defaultValue: {},
389
+ defaultTime: {},
390
+ rangeSeparator: {
391
+ default: '-'
392
+ },
393
+ pickerOptions: {},
394
+ unlinkPanels: Boolean,
395
+ validateEvent: {
396
+ type: Boolean,
397
+ default: true
398
+ },
399
+ timezone: {
400
+ type: String || Number,
401
+ default: '' // 没有传则使用本地的时区
402
+ }
403
+ },
404
+
405
+ data() {
406
+ return {
407
+ pickerVisible: false,
408
+ showClose: false,
409
+ userInput: null,
410
+ valueOnOpen: null, // value when picker opens, used to determine whether to emit change
411
+ unwatchPickerOptions: null
412
+ }
413
+ },
414
+
415
+ computed: {
416
+ ranged() {
417
+ return this.type.indexOf('range') > -1
418
+ },
419
+
420
+ reference() {
421
+ const reference = this.$refs.reference
422
+ return reference.$el || reference
423
+ },
424
+
425
+ refInput() {
426
+ if (this.reference) {
427
+ return [].slice.call(this.reference.querySelectorAll('input'))
428
+ }
429
+ return []
430
+ },
431
+
432
+ valueIsEmpty() {
433
+ const val = this.value
434
+ if (Array.isArray(val)) {
435
+ for (let i = 0, len = val.length; i < len; i++) {
436
+ if (val[i]) {
437
+ return false
438
+ }
439
+ }
440
+ } else {
441
+ if (val) {
442
+ return false
443
+ }
444
+ }
445
+ return true
446
+ },
447
+
448
+ triggerClass() {
449
+ return this.prefixIcon || (this.type.indexOf('time') !== -1 ? 'el-icon-time' : 'el-icon-date')
450
+ },
451
+
452
+ selectionMode() {
453
+ if (this.type === 'week') {
454
+ return 'week'
455
+ } else if (this.type === 'month') {
456
+ return 'month'
457
+ } else if (this.type === 'year') {
458
+ return 'year'
459
+ } else if (this.type === 'dates') {
460
+ return 'dates'
461
+ }
462
+
463
+ return 'day'
464
+ },
465
+
466
+ haveTrigger() {
467
+ if (typeof this.showTrigger !== 'undefined') {
468
+ return this.showTrigger
469
+ }
470
+ return HAVE_TRIGGER_TYPES.indexOf(this.type) !== -1
471
+ },
472
+
473
+ displayValue() {
474
+ const formattedValue = formatAsFormatAndType(this.parsedValue, this.format, this.type, this.rangeSeparator)
475
+ if (Array.isArray(this.userInput)) {
476
+ return [
477
+ this.userInput[0] || (formattedValue && formattedValue[0]) || '',
478
+ this.userInput[1] || (formattedValue && formattedValue[1]) || ''
479
+ ]
480
+ } else if (this.userInput !== null) {
481
+ return this.userInput
482
+ } else if (formattedValue) {
483
+ return this.type === 'dates'
484
+ ? formattedValue.join(', ')
485
+ : formattedValue
486
+ } else {
487
+ return ''
488
+ }
489
+ },
490
+
491
+ parsedValue() {
492
+ if (!this.value) return this.value // component value is not set
493
+ if (this.type === 'time-select') return this.value // time-select does not require parsing, this might change in next major version
494
+
495
+ const valueIsDateObject = isDateObject(this.value) || (Array.isArray(this.value) && this.value.every(isDateObject))
496
+ if (valueIsDateObject) {
497
+ return this.value
498
+ }
499
+
500
+ if (this.valueFormat) {
501
+ return parseAsFormatAndType(this.value, this.valueFormat, this.type, this.rangeSeparator) || this.value
502
+ }
503
+
504
+ // NOTE: deal with common but incorrect usage, should remove in next major version
505
+ // user might provide string / timestamp without value-format, coerce them into date (or array of date)
506
+ return Array.isArray(this.value) ? this.value.map(val => new Date(val)) : new Date(this.value)
507
+ },
508
+
509
+ _elFormItemSize() {
510
+ return (this.elFormItem || {}).elFormItemSize
511
+ },
512
+
513
+ pickerSize() {
514
+ return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size
515
+ },
516
+
517
+ pickerDisabled() {
518
+ return this.disabled || (this.elForm || {}).disabled
519
+ },
520
+
521
+ firstInputId() {
522
+ const obj = {}
523
+ let id
524
+ if (this.ranged) {
525
+ id = this.id && this.id[0]
526
+ } else {
527
+ id = this.id
528
+ }
529
+ if (id) obj.id = id
530
+ return obj
531
+ },
532
+
533
+ secondInputId() {
534
+ const obj = {}
535
+ let id
536
+ if (this.ranged) {
537
+ id = this.id && this.id[1]
538
+ }
539
+ if (id) obj.id = id
540
+ return obj
541
+ }
542
+ },
543
+
544
+ watch: {
545
+ pickerVisible(val) {
546
+ if (this.readonly || this.pickerDisabled) return
547
+ if (val) {
548
+ this.showPicker()
549
+ this.valueOnOpen = Array.isArray(this.value) ? [...this.value] : this.value
550
+ } else {
551
+ this.hidePicker()
552
+ this.emitChange(this.value)
553
+ this.userInput = null
554
+ if (this.validateEvent) {
555
+ this.dispatch('ElFormItem', 'el.form.blur')
556
+ }
557
+ this.$emit('blur', this)
558
+ this.blur()
559
+ }
560
+ },
561
+ parsedValue: {
562
+ immediate: true,
563
+ handler(val) {
564
+ if (this.picker) {
565
+ this.picker.value = val
566
+ }
567
+ }
568
+ },
569
+ defaultValue(val) {
570
+ // NOTE: should eventually move to jsx style picker + panel ?
571
+ if (this.picker) {
572
+ this.picker.defaultValue = val
573
+ }
574
+ },
575
+ value(val, oldVal) {
576
+ if (!valueEquals(val, oldVal) && !this.pickerVisible && this.validateEvent) {
577
+ this.dispatch('ElFormItem', 'el.form.change', val)
578
+ }
579
+ }
580
+ },
581
+
582
+ created() {
583
+ // vue-popper
584
+ this.popperOptions = {
585
+ boundariesPadding: 0,
586
+ gpuAcceleration: false
587
+ }
588
+ this.placement = PLACEMENT_MAP[this.align] || PLACEMENT_MAP.left
589
+
590
+ this.$on('fieldReset', this.handleFieldReset)
591
+ },
592
+
593
+ methods: {
594
+ focus() {
595
+ if (!this.ranged) {
596
+ this.$refs.reference.focus()
597
+ } else {
598
+ this.handleFocus()
599
+ }
600
+ },
601
+
602
+ blur() {
603
+ this.refInput.forEach(input => input.blur())
604
+ },
605
+
606
+ // {parse, formatTo} Value deals maps component value with internal Date
607
+ parseValue(value) {
608
+ const isParsed = isDateObject(value) || (Array.isArray(value) && value.every(isDateObject))
609
+ if (this.valueFormat && !isParsed) {
610
+ return parseAsFormatAndType(value, this.valueFormat, this.type, this.rangeSeparator) || value
611
+ } else {
612
+ return value
613
+ }
614
+ },
615
+
616
+ formatToValue(date) {
617
+ const isFormattable = isDateObject(date) || (Array.isArray(date) && date.every(isDateObject))
618
+ if (this.valueFormat && isFormattable) {
619
+ return formatAsFormatAndType(date, this.valueFormat, this.type, this.rangeSeparator)
620
+ } else {
621
+ return date
622
+ }
623
+ },
624
+
625
+ // {parse, formatTo} String deals with user input
626
+ parseString(value) {
627
+ const type = Array.isArray(value) ? this.type : this.type.replace('range', '')
628
+ return parseAsFormatAndType(value, this.format, type)
629
+ },
630
+
631
+ formatToString(value) {
632
+ const type = Array.isArray(value) ? this.type : this.type.replace('range', '')
633
+ return formatAsFormatAndType(value, this.format, type)
634
+ },
635
+
636
+ handleMouseEnter() {
637
+ if (this.readonly || this.pickerDisabled) return
638
+ if (!this.valueIsEmpty && this.clearable) {
639
+ this.showClose = true
640
+ }
641
+ },
642
+
643
+ handleChange() {
644
+ if (this.userInput) {
645
+ const value = this.parseString(this.displayValue)
646
+ if (value) {
647
+ this.picker.value = value
648
+ if (this.isValidValue(value)) {
649
+ this.emitInput(value)
650
+ this.userInput = null
651
+ }
652
+ }
653
+ }
654
+ if (this.userInput === '') {
655
+ this.emitInput(null)
656
+ this.emitChange(null)
657
+ this.userInput = null
658
+ }
659
+ },
660
+
661
+ handleStartInput(event) {
662
+ if (this.userInput) {
663
+ this.userInput = [event.target.value, this.userInput[1]]
664
+ } else {
665
+ this.userInput = [event.target.value, null]
666
+ }
667
+ },
668
+
669
+ handleEndInput(event) {
670
+ if (this.userInput) {
671
+ this.userInput = [this.userInput[0], event.target.value]
672
+ } else {
673
+ this.userInput = [null, event.target.value]
674
+ }
675
+ },
676
+
677
+ handleStartChange(event) {
678
+ const value = this.parseString(this.userInput && this.userInput[0])
679
+ if (value) {
680
+ this.userInput = [this.formatToString(value), this.displayValue[1]]
681
+ const newValue = [value, this.picker.value && this.picker.value[1]]
682
+ this.picker.value = newValue
683
+ if (this.isValidValue(newValue)) {
684
+ this.emitInput(newValue)
685
+ this.userInput = null
686
+ }
687
+ }
688
+ },
689
+
690
+ handleEndChange(event) {
691
+ const value = this.parseString(this.userInput && this.userInput[1])
692
+ if (value) {
693
+ this.userInput = [this.displayValue[0], this.formatToString(value)]
694
+ const newValue = [this.picker.value && this.picker.value[0], value]
695
+ this.picker.value = newValue
696
+ if (this.isValidValue(newValue)) {
697
+ this.emitInput(newValue)
698
+ this.userInput = null
699
+ }
700
+ }
701
+ },
702
+
703
+ handleClickIcon(event) {
704
+ if (this.readonly || this.pickerDisabled) return
705
+ if (this.showClose) {
706
+ this.valueOnOpen = this.value
707
+ event.stopPropagation()
708
+ this.emitInput(null)
709
+ this.emitChange(null)
710
+ this.showClose = false
711
+ if (this.picker && typeof this.picker.handleClear === 'function') {
712
+ this.picker.handleClear()
713
+ }
714
+ } else {
715
+ this.pickerVisible = !this.pickerVisible
716
+ }
717
+ },
718
+
719
+ handleClose() {
720
+ if (!this.pickerVisible) return
721
+ this.pickerVisible = false
722
+
723
+ if (this.type === 'dates') {
724
+ // restore to former value
725
+ const oldValue = parseAsFormatAndType(this.valueOnOpen, this.valueFormat, this.type, this.rangeSeparator) || this.valueOnOpen
726
+ this.emitInput(oldValue)
727
+ }
728
+ },
729
+
730
+ handleFieldReset(initialValue) {
731
+ this.userInput = initialValue === '' ? null : initialValue
732
+ },
733
+
734
+ handleFocus() {
735
+ const type = this.type
736
+
737
+ if (HAVE_TRIGGER_TYPES.indexOf(type) !== -1 && !this.pickerVisible) {
738
+ this.pickerVisible = true
739
+ }
740
+ this.$emit('focus', this)
741
+ },
742
+
743
+ handleKeydown(event) {
744
+ const keyCode = event.keyCode
745
+
746
+ // ESC
747
+ if (keyCode === 27) {
748
+ this.pickerVisible = false
749
+ event.stopPropagation()
750
+ return
751
+ }
752
+
753
+ // Tab
754
+ if (keyCode === 9) {
755
+ if (!this.ranged) {
756
+ this.handleChange()
757
+ this.pickerVisible = this.picker.visible = false
758
+ this.blur()
759
+ event.stopPropagation()
760
+ } else {
761
+ // user may change focus between two input
762
+ setTimeout(() => {
763
+ if (this.refInput.indexOf(document.activeElement) === -1) {
764
+ this.pickerVisible = false
765
+ this.blur()
766
+ event.stopPropagation()
767
+ }
768
+ }, 0)
769
+ }
770
+ return
771
+ }
772
+
773
+ // Enter
774
+ if (keyCode === 13) {
775
+ if (this.userInput === '' || this.isValidValue(this.parseString(this.displayValue))) {
776
+ this.handleChange()
777
+ this.pickerVisible = this.picker.visible = false
778
+ this.blur()
779
+ }
780
+ event.stopPropagation()
781
+ return
782
+ }
783
+
784
+ // if user is typing, do not let picker handle key input
785
+ if (this.userInput) {
786
+ event.stopPropagation()
787
+ return
788
+ }
789
+
790
+ // delegate other keys to panel
791
+ if (this.picker && this.picker.handleKeydown) {
792
+ this.picker.handleKeydown(event)
793
+ }
794
+ },
795
+
796
+ handleRangeClick() {
797
+ const type = this.type
798
+
799
+ if (HAVE_TRIGGER_TYPES.indexOf(type) !== -1 && !this.pickerVisible) {
800
+ this.pickerVisible = true
801
+ }
802
+ this.$emit('focus', this)
803
+ },
804
+
805
+ hidePicker() {
806
+ if (this.picker) {
807
+ this.picker.resetView && this.picker.resetView()
808
+ this.pickerVisible = this.picker.visible = false
809
+ this.destroyPopper()
810
+ }
811
+ },
812
+
813
+ showPicker() {
814
+ if (this.$isServer) return
815
+ if (!this.picker) {
816
+ this.mountPicker()
817
+ }
818
+ this.pickerVisible = this.picker.visible = true
819
+
820
+ this.updatePopper()
821
+
822
+ this.picker.value = this.parsedValue
823
+ this.picker.resetView && this.picker.resetView()
824
+
825
+ this.$nextTick(() => {
826
+ this.picker.adjustSpinners && this.picker.adjustSpinners()
827
+ })
828
+ },
829
+
830
+ mountPicker() {
831
+ const tempFun = this.panel.data
832
+ this.panel.data = () => {
833
+ return {
834
+ timezone: this.timezone, // add timezone to panel component
835
+ ...tempFun()
836
+ }
837
+ }
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.valueOnOpen = val
918
+ if (this.validateEvent) {
919
+ this.dispatch('ElFormItem', 'el.form.change', val)
920
+ }
921
+ }
922
+ },
923
+
924
+ emitInput(val) {
925
+ const formatted = this.formatToValue(val)
926
+ if (!valueEquals(this.value, formatted)) {
927
+ this.$emit('input', formatted)
928
+ }
929
+ },
930
+
931
+ isValidValue(value) {
932
+ if (!this.picker) {
933
+ this.mountPicker()
934
+ }
935
+ if (this.picker.isValidValue) {
936
+ return value && this.picker.isValidValue(value)
937
+ } else {
938
+ return true
939
+ }
940
+ }
941
+ }
942
+ }
943
+ </script>