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