@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.
- package/.eslintrc.cjs +49 -0
- package/README.md +17 -0
- package/components.d.ts +8 -0
- package/lib/basic/date-table.vue.d.ts +86 -0
- package/lib/basic/month-table.vue.d.ts +51 -0
- package/lib/basic/time-spinner.vue.d.ts +42 -0
- package/lib/basic/year-table.vue.d.ts +26 -0
- package/lib/index.d.ts +333 -0
- package/lib/nf-timezone-picker.cjs.js +5984 -0
- package/lib/nf-timezone-picker.esm-bundler.js +5984 -0
- package/lib/nf-timezone-picker.vue.d.ts +157 -0
- package/lib/panel/date-range.vue.d.ts +55 -0
- package/lib/panel/date.vue.d.ts +35 -0
- package/lib/panel/month-range.vue.d.ts +30 -0
- package/lib/panel/time.vue.d.ts +27 -0
- package/lib/picker.vue.d.ts +142 -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/package/basic/date-table.vue +448 -0
- package/package/basic/month-table.vue +253 -0
- package/package/basic/time-spinner.vue +316 -0
- package/package/basic/year-table.vue +101 -0
- package/package/index.ts +7 -0
- package/package/nf-timezone-picker.vue +50 -0
- package/package/panel/date-range.vue +700 -0
- package/package/panel/date.vue +607 -0
- package/package/panel/month-range.vue +293 -0
- package/package/panel/time-range.vue +254 -0
- package/package/panel/time-select.vue +178 -0
- package/package/panel/time.vue +189 -0
- package/package/picker/date-picker.js +43 -0
- package/package/picker/time-picker.js +39 -0
- package/package/picker/time-select.js +21 -0
- package/package/picker.vue +943 -0
- package/package/popper.js +1276 -0
- package/package/style/css.ts +1 -0
- package/package/style/index.ts +1 -0
- package/package.json +34 -0
- package/postcss.config.js +8 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<table
|
|
3
|
+
cellspacing="0"
|
|
4
|
+
cellpadding="0"
|
|
5
|
+
class="el-date-table"
|
|
6
|
+
:class="{ 'is-week-mode': selectionMode === 'week' }"
|
|
7
|
+
@click="handleClick"
|
|
8
|
+
@mousemove="handleMouseMove"
|
|
9
|
+
>
|
|
10
|
+
<tbody>
|
|
11
|
+
<tr>
|
|
12
|
+
<th v-if="showWeekNumber">{{ t('el.datepicker.week') }}</th>
|
|
13
|
+
<th v-for="(week, key) in WEEKS" :key="key">{{ t('el.datepicker.weeks.' + week) }}</th>
|
|
14
|
+
</tr>
|
|
15
|
+
<tr
|
|
16
|
+
v-for="(row, key) in rows"
|
|
17
|
+
:key="key"
|
|
18
|
+
class="el-date-table__row"
|
|
19
|
+
:class="{ current: isWeekActive(row[1]) }"
|
|
20
|
+
>
|
|
21
|
+
<td
|
|
22
|
+
v-for="(cell, key) in row"
|
|
23
|
+
:key="key"
|
|
24
|
+
:class="getCellClasses(cell)"
|
|
25
|
+
>
|
|
26
|
+
<div>
|
|
27
|
+
<span>
|
|
28
|
+
{{ cell.text }}
|
|
29
|
+
</span>
|
|
30
|
+
</div>
|
|
31
|
+
</td>
|
|
32
|
+
</tr>
|
|
33
|
+
</tbody>
|
|
34
|
+
</table>
|
|
35
|
+
</template>
|
|
36
|
+
|
|
37
|
+
<script>
|
|
38
|
+
import { getFirstDayOfMonth, getDayCountOfMonth, getWeekNumber, getStartDateOfMonth, prevDate, nextDate, isDate, clearTime as _clearTime } from 'element-ui/src/utils/date-util'
|
|
39
|
+
import Locale from 'element-ui/src/mixins/locale'
|
|
40
|
+
import { arrayFindIndex, arrayFind, coerceTruthyValueToArray } from 'element-ui/src/utils/util'
|
|
41
|
+
import { getLocalTime } from '@94ai/common-utils/lib/util'
|
|
42
|
+
const WEEKS = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
|
|
43
|
+
const getDateTimestamp = function(time) {
|
|
44
|
+
if (typeof time === 'number' || typeof time === 'string') {
|
|
45
|
+
return _clearTime(new Date(time)).getTime()
|
|
46
|
+
} else if (time instanceof Date) {
|
|
47
|
+
return _clearTime(time).getTime()
|
|
48
|
+
} else {
|
|
49
|
+
return NaN
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// remove the first element that satisfies `pred` from arr
|
|
54
|
+
// return a new array if modification occurs
|
|
55
|
+
// return the original array otherwise
|
|
56
|
+
const removeFromArray = function(arr, pred) {
|
|
57
|
+
const idx = typeof pred === 'function' ? arrayFindIndex(arr, pred) : arr.indexOf(pred)
|
|
58
|
+
return idx >= 0 ? [...arr.slice(0, idx), ...arr.slice(idx + 1)] : arr
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default {
|
|
62
|
+
mixins: [Locale],
|
|
63
|
+
|
|
64
|
+
props: {
|
|
65
|
+
firstDayOfWeek: {
|
|
66
|
+
default: 7,
|
|
67
|
+
type: Number,
|
|
68
|
+
validator: val => val >= 1 && val <= 7
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
value: {},
|
|
72
|
+
|
|
73
|
+
defaultValue: {
|
|
74
|
+
validator(val) {
|
|
75
|
+
// either: null, valid Date object, Array of valid Date objects
|
|
76
|
+
return val === null || isDate(val) || (Array.isArray(val) && val.every(isDate))
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
date: {},
|
|
81
|
+
|
|
82
|
+
selectionMode: {
|
|
83
|
+
default: 'day'
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
showWeekNumber: {
|
|
87
|
+
type: Boolean,
|
|
88
|
+
default: false
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
disabledDate: {},
|
|
92
|
+
|
|
93
|
+
cellClassName: {},
|
|
94
|
+
|
|
95
|
+
minDate: {},
|
|
96
|
+
|
|
97
|
+
maxDate: {},
|
|
98
|
+
|
|
99
|
+
rangeState: {
|
|
100
|
+
default() {
|
|
101
|
+
return {
|
|
102
|
+
endDate: null,
|
|
103
|
+
selecting: false
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
timezone: {
|
|
108
|
+
type: String || Number,
|
|
109
|
+
default: '' // 没有传则使用本地的时区
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
data() {
|
|
114
|
+
return {
|
|
115
|
+
tableRows: [[], [], [], [], [], []],
|
|
116
|
+
lastRow: null,
|
|
117
|
+
lastColumn: null
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
computed: {
|
|
122
|
+
offsetDay() {
|
|
123
|
+
const week = this.firstDayOfWeek
|
|
124
|
+
// 周日为界限,左右偏移的天数,3217654 例如周一就是 -1,目的是调整前两行日期的位置
|
|
125
|
+
return week > 3 ? 7 - week : -week
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
WEEKS() {
|
|
129
|
+
const week = this.firstDayOfWeek
|
|
130
|
+
return WEEKS.concat(WEEKS).slice(week, week + 7)
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
year() {
|
|
134
|
+
return this.date.getFullYear()
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
month() {
|
|
138
|
+
return this.date.getMonth()
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
startDate() {
|
|
142
|
+
return getStartDateOfMonth(this.year, this.month)
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
rows() {
|
|
146
|
+
// TODO: refactory rows / getCellClasses
|
|
147
|
+
const date = new Date(this.year, this.month, 1)
|
|
148
|
+
let day = getFirstDayOfMonth(date) // day of first day
|
|
149
|
+
const dateCountOfMonth = getDayCountOfMonth(date.getFullYear(), date.getMonth())
|
|
150
|
+
const dateCountOfLastMonth = getDayCountOfMonth(date.getFullYear(), (date.getMonth() === 0 ? 11 : date.getMonth() - 1))
|
|
151
|
+
|
|
152
|
+
day = (day === 0 ? 7 : day)
|
|
153
|
+
|
|
154
|
+
const offset = this.offsetDay
|
|
155
|
+
const rows = this.tableRows
|
|
156
|
+
let count = 1
|
|
157
|
+
|
|
158
|
+
const startDate = this.startDate
|
|
159
|
+
const disabledDate = this.disabledDate
|
|
160
|
+
const cellClassName = this.cellClassName
|
|
161
|
+
const selectedDate = this.selectionMode === 'dates' ? coerceTruthyValueToArray(this.value) : []
|
|
162
|
+
const now = getDateTimestamp(getLocalTime(this.timezone))
|
|
163
|
+
|
|
164
|
+
for (let i = 0; i < 6; i++) {
|
|
165
|
+
const row = rows[i]
|
|
166
|
+
|
|
167
|
+
if (this.showWeekNumber) {
|
|
168
|
+
if (!row[0]) {
|
|
169
|
+
row[0] = { type: 'week', text: getWeekNumber(nextDate(startDate, i * 7 + 1)) }
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
for (let j = 0; j < 7; j++) {
|
|
174
|
+
let cell = row[this.showWeekNumber ? j + 1 : j]
|
|
175
|
+
if (!cell) {
|
|
176
|
+
cell = { row: i, column: j, type: 'normal', inRange: false, start: false, end: false }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
cell.type = 'normal'
|
|
180
|
+
|
|
181
|
+
const index = i * 7 + j
|
|
182
|
+
const time = nextDate(startDate, index - offset).getTime()
|
|
183
|
+
cell.inRange = time >= getDateTimestamp(this.minDate) && time <= getDateTimestamp(this.maxDate)
|
|
184
|
+
cell.start = this.minDate && time === getDateTimestamp(this.minDate)
|
|
185
|
+
cell.end = this.maxDate && time === getDateTimestamp(this.maxDate)
|
|
186
|
+
const isToday = time === now
|
|
187
|
+
|
|
188
|
+
if (isToday) {
|
|
189
|
+
cell.type = 'today'
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (i >= 0 && i <= 1) {
|
|
193
|
+
const numberOfDaysFromPreviousMonth = day + offset < 0 ? 7 + day + offset : day + offset
|
|
194
|
+
|
|
195
|
+
if (j + i * 7 >= numberOfDaysFromPreviousMonth) {
|
|
196
|
+
cell.text = count++
|
|
197
|
+
} else {
|
|
198
|
+
cell.text = dateCountOfLastMonth - (numberOfDaysFromPreviousMonth - j % 7) + 1 + i * 7
|
|
199
|
+
cell.type = 'prev-month'
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
if (count <= dateCountOfMonth) {
|
|
203
|
+
cell.text = count++
|
|
204
|
+
} else {
|
|
205
|
+
cell.text = count++ - dateCountOfMonth
|
|
206
|
+
cell.type = 'next-month'
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const cellDate = new Date(time)
|
|
211
|
+
cell.disabled = typeof disabledDate === 'function' && disabledDate(cellDate)
|
|
212
|
+
cell.selected = arrayFind(selectedDate, date => date.getTime() === cellDate.getTime())
|
|
213
|
+
cell.customClass = typeof cellClassName === 'function' && cellClassName(cellDate)
|
|
214
|
+
this.$set(row, this.showWeekNumber ? j + 1 : j, cell)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (this.selectionMode === 'week') {
|
|
218
|
+
const start = this.showWeekNumber ? 1 : 0
|
|
219
|
+
const end = this.showWeekNumber ? 7 : 6
|
|
220
|
+
const isWeekActive = this.isWeekActive(row[start + 1])
|
|
221
|
+
|
|
222
|
+
row[start].inRange = isWeekActive
|
|
223
|
+
row[start].start = isWeekActive
|
|
224
|
+
row[end].inRange = isWeekActive
|
|
225
|
+
row[end].end = isWeekActive
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return rows
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
watch: {
|
|
234
|
+
'rangeState.endDate'(newVal) {
|
|
235
|
+
this.markRange(this.minDate, newVal)
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
minDate(newVal, oldVal) {
|
|
239
|
+
if (getDateTimestamp(newVal) !== getDateTimestamp(oldVal)) {
|
|
240
|
+
this.markRange(this.minDate, this.maxDate)
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
|
|
244
|
+
maxDate(newVal, oldVal) {
|
|
245
|
+
if (getDateTimestamp(newVal) !== getDateTimestamp(oldVal)) {
|
|
246
|
+
this.markRange(this.minDate, this.maxDate)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
methods: {
|
|
252
|
+
cellMatchesDate(cell, date) {
|
|
253
|
+
const value = new Date(date)
|
|
254
|
+
return this.year === value.getFullYear() &&
|
|
255
|
+
this.month === value.getMonth() &&
|
|
256
|
+
Number(cell.text) === value.getDate()
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
getCellClasses(cell) {
|
|
260
|
+
const selectionMode = this.selectionMode
|
|
261
|
+
const defaultValue = this.defaultValue ? Array.isArray(this.defaultValue) ? this.defaultValue : [this.defaultValue] : []
|
|
262
|
+
|
|
263
|
+
const classes = []
|
|
264
|
+
if ((cell.type === 'normal' || cell.type === 'today') && !cell.disabled) {
|
|
265
|
+
classes.push('available')
|
|
266
|
+
if (cell.type === 'today') {
|
|
267
|
+
classes.push('today')
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
classes.push(cell.type)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (cell.type === 'normal' && defaultValue.some(date => this.cellMatchesDate(cell, date))) {
|
|
274
|
+
classes.push('default')
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (selectionMode === 'day' && (cell.type === 'normal' || cell.type === 'today') && this.cellMatchesDate(cell, this.value)) {
|
|
278
|
+
classes.push('current')
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (cell.inRange && ((cell.type === 'normal' || cell.type === 'today') || this.selectionMode === 'week')) {
|
|
282
|
+
classes.push('in-range')
|
|
283
|
+
|
|
284
|
+
if (cell.start) {
|
|
285
|
+
classes.push('start-date')
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (cell.end) {
|
|
289
|
+
classes.push('end-date')
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (cell.disabled) {
|
|
294
|
+
classes.push('disabled')
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (cell.selected) {
|
|
298
|
+
classes.push('selected')
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (cell.customClass) {
|
|
302
|
+
classes.push(cell.customClass)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return classes.join(' ')
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
getDateOfCell(row, column) {
|
|
309
|
+
const offsetFromStart = row * 7 + (column - (this.showWeekNumber ? 1 : 0)) - this.offsetDay
|
|
310
|
+
return nextDate(this.startDate, offsetFromStart)
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
isWeekActive(cell) {
|
|
314
|
+
if (this.selectionMode !== 'week') return false
|
|
315
|
+
const newDate = new Date(this.year, this.month, 1)
|
|
316
|
+
const year = newDate.getFullYear()
|
|
317
|
+
const month = newDate.getMonth()
|
|
318
|
+
|
|
319
|
+
if (cell.type === 'prev-month') {
|
|
320
|
+
newDate.setMonth(month === 0 ? 11 : month - 1)
|
|
321
|
+
newDate.setFullYear(month === 0 ? year - 1 : year)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (cell.type === 'next-month') {
|
|
325
|
+
newDate.setMonth(month === 11 ? 0 : month + 1)
|
|
326
|
+
newDate.setFullYear(month === 11 ? year + 1 : year)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
newDate.setDate(parseInt(cell.text, 10))
|
|
330
|
+
|
|
331
|
+
if (isDate(this.value)) {
|
|
332
|
+
const dayOffset = (this.value.getDay() - this.firstDayOfWeek + 7) % 7 - 1
|
|
333
|
+
const weekDate = prevDate(this.value, dayOffset)
|
|
334
|
+
return weekDate.getTime() === newDate.getTime()
|
|
335
|
+
}
|
|
336
|
+
return false
|
|
337
|
+
},
|
|
338
|
+
|
|
339
|
+
markRange(minDate, maxDate) {
|
|
340
|
+
minDate = getDateTimestamp(minDate)
|
|
341
|
+
maxDate = getDateTimestamp(maxDate) || minDate;
|
|
342
|
+
[minDate, maxDate] = [Math.min(minDate, maxDate), Math.max(minDate, maxDate)]
|
|
343
|
+
|
|
344
|
+
const startDate = this.startDate
|
|
345
|
+
const rows = this.rows
|
|
346
|
+
for (let i = 0, k = rows.length; i < k; i++) {
|
|
347
|
+
const row = rows[i]
|
|
348
|
+
for (let j = 0, l = row.length; j < l; j++) {
|
|
349
|
+
if (this.showWeekNumber && j === 0) continue
|
|
350
|
+
|
|
351
|
+
const cell = row[j]
|
|
352
|
+
const index = i * 7 + j + (this.showWeekNumber ? -1 : 0)
|
|
353
|
+
const time = nextDate(startDate, index - this.offsetDay).getTime()
|
|
354
|
+
|
|
355
|
+
cell.inRange = minDate && time >= minDate && time <= maxDate
|
|
356
|
+
cell.start = minDate && time === minDate
|
|
357
|
+
cell.end = maxDate && time === maxDate
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
|
|
362
|
+
handleMouseMove(event) {
|
|
363
|
+
if (!this.rangeState.selecting) return
|
|
364
|
+
|
|
365
|
+
let target = event.target
|
|
366
|
+
if (target.tagName === 'SPAN') {
|
|
367
|
+
target = target.parentNode.parentNode
|
|
368
|
+
}
|
|
369
|
+
if (target.tagName === 'DIV') {
|
|
370
|
+
target = target.parentNode
|
|
371
|
+
}
|
|
372
|
+
if (target.tagName !== 'TD') return
|
|
373
|
+
|
|
374
|
+
const row = target.parentNode.rowIndex - 1
|
|
375
|
+
const column = target.cellIndex
|
|
376
|
+
|
|
377
|
+
// can not select disabled date
|
|
378
|
+
if (this.rows[row][column].disabled) return
|
|
379
|
+
|
|
380
|
+
// only update rangeState when mouse moves to a new cell
|
|
381
|
+
// this avoids frequent Date object creation and improves performance
|
|
382
|
+
if (row !== this.lastRow || column !== this.lastColumn) {
|
|
383
|
+
this.lastRow = row
|
|
384
|
+
this.lastColumn = column
|
|
385
|
+
this.$emit('changerange', {
|
|
386
|
+
minDate: this.minDate,
|
|
387
|
+
maxDate: this.maxDate,
|
|
388
|
+
rangeState: {
|
|
389
|
+
selecting: true,
|
|
390
|
+
endDate: this.getDateOfCell(row, column)
|
|
391
|
+
}
|
|
392
|
+
})
|
|
393
|
+
}
|
|
394
|
+
},
|
|
395
|
+
|
|
396
|
+
handleClick(event) {
|
|
397
|
+
let target = event.target
|
|
398
|
+
if (target.tagName === 'SPAN') {
|
|
399
|
+
target = target.parentNode.parentNode
|
|
400
|
+
}
|
|
401
|
+
if (target.tagName === 'DIV') {
|
|
402
|
+
target = target.parentNode
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (target.tagName !== 'TD') return
|
|
406
|
+
|
|
407
|
+
const row = target.parentNode.rowIndex - 1
|
|
408
|
+
const column = this.selectionMode === 'week' ? 1 : target.cellIndex
|
|
409
|
+
const cell = this.rows[row][column]
|
|
410
|
+
|
|
411
|
+
if (cell.disabled || cell.type === 'week') return
|
|
412
|
+
|
|
413
|
+
const newDate = this.getDateOfCell(row, column)
|
|
414
|
+
|
|
415
|
+
if (this.selectionMode === 'range') {
|
|
416
|
+
if (!this.rangeState.selecting) {
|
|
417
|
+
this.$emit('pick', { minDate: newDate, maxDate: null })
|
|
418
|
+
this.rangeState.selecting = true
|
|
419
|
+
} else {
|
|
420
|
+
if (newDate >= this.minDate) {
|
|
421
|
+
this.$emit('pick', { minDate: this.minDate, maxDate: newDate })
|
|
422
|
+
} else {
|
|
423
|
+
this.$emit('pick', { minDate: newDate, maxDate: this.minDate })
|
|
424
|
+
}
|
|
425
|
+
this.rangeState.selecting = false
|
|
426
|
+
}
|
|
427
|
+
} else if (this.selectionMode === 'day') {
|
|
428
|
+
this.$emit('pick', newDate)
|
|
429
|
+
} else if (this.selectionMode === 'week') {
|
|
430
|
+
const weekNumber = getWeekNumber(newDate)
|
|
431
|
+
const value = newDate.getFullYear() + 'w' + weekNumber
|
|
432
|
+
this.$emit('pick', {
|
|
433
|
+
year: newDate.getFullYear(),
|
|
434
|
+
week: weekNumber,
|
|
435
|
+
value: value,
|
|
436
|
+
date: newDate
|
|
437
|
+
})
|
|
438
|
+
} else if (this.selectionMode === 'dates') {
|
|
439
|
+
const value = this.value || []
|
|
440
|
+
const newValue = cell.selected
|
|
441
|
+
? removeFromArray(value, date => date.getTime() === newDate.getTime())
|
|
442
|
+
: [...value, newDate]
|
|
443
|
+
this.$emit('pick', newValue)
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
</script>
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<table class="el-month-table" @click="handleMonthTableClick" @mousemove="handleMouseMove">
|
|
3
|
+
<tbody>
|
|
4
|
+
<tr v-for="(row, key) in rows" :key="key">
|
|
5
|
+
<td v-for="(cell, key) in row" :key="key" :class="getCellStyle(cell)">
|
|
6
|
+
<div>
|
|
7
|
+
<a class="cell">{{ t('el.datepicker.months.' + months[cell.text]) }}</a>
|
|
8
|
+
</div>
|
|
9
|
+
</td>
|
|
10
|
+
</tr>
|
|
11
|
+
</tbody>
|
|
12
|
+
</table>
|
|
13
|
+
</template>
|
|
14
|
+
|
|
15
|
+
<script type="text/babel">
|
|
16
|
+
import Locale from 'element-ui/src/mixins/locale'
|
|
17
|
+
import { isDate, range, getDayCountOfMonth, nextDate } from 'element-ui/src/utils/date-util'
|
|
18
|
+
import { hasClass } from 'element-ui/src/utils/dom'
|
|
19
|
+
import { arrayFindIndex, coerceTruthyValueToArray, arrayFind } from 'element-ui/src/utils/util'
|
|
20
|
+
|
|
21
|
+
const datesInMonth = (year, month) => {
|
|
22
|
+
const numOfDays = getDayCountOfMonth(year, month)
|
|
23
|
+
const firstDay = new Date(year, month, 1)
|
|
24
|
+
return range(numOfDays).map(n => nextDate(firstDay, n))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const clearDate = (date) => {
|
|
28
|
+
return new Date(date.getFullYear(), date.getMonth())
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const getMonthTimestamp = function(time) {
|
|
32
|
+
if (typeof time === 'number' || typeof time === 'string') {
|
|
33
|
+
return clearDate(new Date(time)).getTime()
|
|
34
|
+
} else if (time instanceof Date) {
|
|
35
|
+
return clearDate(time).getTime()
|
|
36
|
+
} else {
|
|
37
|
+
return NaN
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export default {
|
|
41
|
+
|
|
42
|
+
mixins: [Locale],
|
|
43
|
+
props: {
|
|
44
|
+
disabledDate: {},
|
|
45
|
+
value: {},
|
|
46
|
+
selectionMode: {
|
|
47
|
+
default: 'month'
|
|
48
|
+
},
|
|
49
|
+
minDate: {},
|
|
50
|
+
|
|
51
|
+
maxDate: {},
|
|
52
|
+
defaultValue: {
|
|
53
|
+
validator(val) {
|
|
54
|
+
// null or valid Date Object
|
|
55
|
+
return val === null || isDate(val) || (Array.isArray(val) && val.every(isDate))
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
date: {},
|
|
59
|
+
rangeState: {
|
|
60
|
+
default() {
|
|
61
|
+
return {
|
|
62
|
+
endDate: null,
|
|
63
|
+
selecting: false
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
data() {
|
|
70
|
+
return {
|
|
71
|
+
months: ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
|
|
72
|
+
tableRows: [[], [], []],
|
|
73
|
+
lastRow: null,
|
|
74
|
+
lastColumn: null
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
computed: {
|
|
79
|
+
rows() {
|
|
80
|
+
// TODO: refactory rows / getCellClasses
|
|
81
|
+
const rows = this.tableRows
|
|
82
|
+
const disabledDate = this.disabledDate
|
|
83
|
+
const selectedDate = []
|
|
84
|
+
const now = getMonthTimestamp(new Date())
|
|
85
|
+
|
|
86
|
+
for (let i = 0; i < 3; i++) {
|
|
87
|
+
const row = rows[i]
|
|
88
|
+
for (let j = 0; j < 4; j++) {
|
|
89
|
+
let cell = row[j]
|
|
90
|
+
if (!cell) {
|
|
91
|
+
cell = { row: i, column: j, type: 'normal', inRange: false, start: false, end: false }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
cell.type = 'normal'
|
|
95
|
+
|
|
96
|
+
const index = i * 4 + j
|
|
97
|
+
const time = new Date(this.date.getFullYear(), index).getTime()
|
|
98
|
+
cell.inRange = time >= getMonthTimestamp(this.minDate) && time <= getMonthTimestamp(this.maxDate)
|
|
99
|
+
cell.start = this.minDate && time === getMonthTimestamp(this.minDate)
|
|
100
|
+
cell.end = this.maxDate && time === getMonthTimestamp(this.maxDate)
|
|
101
|
+
const isToday = time === now
|
|
102
|
+
|
|
103
|
+
if (isToday) {
|
|
104
|
+
cell.type = 'today'
|
|
105
|
+
}
|
|
106
|
+
cell.text = index
|
|
107
|
+
const cellDate = new Date(time)
|
|
108
|
+
cell.disabled = typeof disabledDate === 'function' && disabledDate(cellDate)
|
|
109
|
+
cell.selected = arrayFind(selectedDate, date => date.getTime() === cellDate.getTime())
|
|
110
|
+
|
|
111
|
+
this.$set(row, j, cell)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return rows
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
watch: {
|
|
119
|
+
'rangeState.endDate'(newVal) {
|
|
120
|
+
this.markRange(this.minDate, newVal)
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
minDate(newVal, oldVal) {
|
|
124
|
+
if (getMonthTimestamp(newVal) !== getMonthTimestamp(oldVal)) {
|
|
125
|
+
this.markRange(this.minDate, this.maxDate)
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
maxDate(newVal, oldVal) {
|
|
130
|
+
if (getMonthTimestamp(newVal) !== getMonthTimestamp(oldVal)) {
|
|
131
|
+
this.markRange(this.minDate, this.maxDate)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
methods: {
|
|
137
|
+
cellMatchesDate(cell, date) {
|
|
138
|
+
const value = new Date(date)
|
|
139
|
+
return this.date.getFullYear() === value.getFullYear() && Number(cell.text) === value.getMonth()
|
|
140
|
+
},
|
|
141
|
+
getCellStyle(cell) {
|
|
142
|
+
const style = {}
|
|
143
|
+
const year = this.date.getFullYear()
|
|
144
|
+
const today = new Date()
|
|
145
|
+
const month = cell.text
|
|
146
|
+
const defaultValue = this.defaultValue ? Array.isArray(this.defaultValue) ? this.defaultValue : [this.defaultValue] : []
|
|
147
|
+
style.disabled = typeof this.disabledDate === 'function'
|
|
148
|
+
? datesInMonth(year, month).every(this.disabledDate)
|
|
149
|
+
: false
|
|
150
|
+
style.current = arrayFindIndex(coerceTruthyValueToArray(this.value), date => date.getFullYear() === year && date.getMonth() === month) >= 0
|
|
151
|
+
style.today = today.getFullYear() === year && today.getMonth() === month
|
|
152
|
+
style.default = defaultValue.some(date => this.cellMatchesDate(cell, date))
|
|
153
|
+
|
|
154
|
+
if (cell.inRange) {
|
|
155
|
+
style['in-range'] = true
|
|
156
|
+
|
|
157
|
+
if (cell.start) {
|
|
158
|
+
style['start-date'] = true
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (cell.end) {
|
|
162
|
+
style['end-date'] = true
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return style
|
|
166
|
+
},
|
|
167
|
+
getMonthOfCell(month) {
|
|
168
|
+
const year = this.date.getFullYear()
|
|
169
|
+
return new Date(year, month, 1)
|
|
170
|
+
},
|
|
171
|
+
markRange(minDate, maxDate) {
|
|
172
|
+
minDate = getMonthTimestamp(minDate)
|
|
173
|
+
maxDate = getMonthTimestamp(maxDate) || minDate;
|
|
174
|
+
[minDate, maxDate] = [Math.min(minDate, maxDate), Math.max(minDate, maxDate)]
|
|
175
|
+
const rows = this.rows
|
|
176
|
+
for (let i = 0, k = rows.length; i < k; i++) {
|
|
177
|
+
const row = rows[i]
|
|
178
|
+
for (let j = 0, l = row.length; j < l; j++) {
|
|
179
|
+
const cell = row[j]
|
|
180
|
+
const index = i * 4 + j
|
|
181
|
+
const time = new Date(this.date.getFullYear(), index).getTime()
|
|
182
|
+
|
|
183
|
+
cell.inRange = minDate && time >= minDate && time <= maxDate
|
|
184
|
+
cell.start = minDate && time === minDate
|
|
185
|
+
cell.end = maxDate && time === maxDate
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
handleMouseMove(event) {
|
|
190
|
+
if (!this.rangeState.selecting) return
|
|
191
|
+
|
|
192
|
+
let target = event.target
|
|
193
|
+
if (target.tagName === 'A') {
|
|
194
|
+
target = target.parentNode.parentNode
|
|
195
|
+
}
|
|
196
|
+
if (target.tagName === 'DIV') {
|
|
197
|
+
target = target.parentNode
|
|
198
|
+
}
|
|
199
|
+
if (target.tagName !== 'TD') return
|
|
200
|
+
|
|
201
|
+
const row = target.parentNode.rowIndex
|
|
202
|
+
const column = target.cellIndex
|
|
203
|
+
// can not select disabled date
|
|
204
|
+
if (this.rows[row][column].disabled) return
|
|
205
|
+
|
|
206
|
+
// only update rangeState when mouse moves to a new cell
|
|
207
|
+
// this avoids frequent Date object creation and improves performance
|
|
208
|
+
if (row !== this.lastRow || column !== this.lastColumn) {
|
|
209
|
+
this.lastRow = row
|
|
210
|
+
this.lastColumn = column
|
|
211
|
+
this.$emit('changerange', {
|
|
212
|
+
minDate: this.minDate,
|
|
213
|
+
maxDate: this.maxDate,
|
|
214
|
+
rangeState: {
|
|
215
|
+
selecting: true,
|
|
216
|
+
endDate: this.getMonthOfCell(row * 4 + column)
|
|
217
|
+
}
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
handleMonthTableClick(event) {
|
|
222
|
+
let target = event.target
|
|
223
|
+
if (target.tagName === 'A') {
|
|
224
|
+
target = target.parentNode.parentNode
|
|
225
|
+
}
|
|
226
|
+
if (target.tagName === 'DIV') {
|
|
227
|
+
target = target.parentNode
|
|
228
|
+
}
|
|
229
|
+
if (target.tagName !== 'TD') return
|
|
230
|
+
if (hasClass(target, 'disabled')) return
|
|
231
|
+
const column = target.cellIndex
|
|
232
|
+
const row = target.parentNode.rowIndex
|
|
233
|
+
const month = row * 4 + column
|
|
234
|
+
const newDate = this.getMonthOfCell(month)
|
|
235
|
+
if (this.selectionMode === 'range') {
|
|
236
|
+
if (!this.rangeState.selecting) {
|
|
237
|
+
this.$emit('pick', { minDate: newDate, maxDate: null })
|
|
238
|
+
this.rangeState.selecting = true
|
|
239
|
+
} else {
|
|
240
|
+
if (newDate >= this.minDate) {
|
|
241
|
+
this.$emit('pick', { minDate: this.minDate, maxDate: newDate })
|
|
242
|
+
} else {
|
|
243
|
+
this.$emit('pick', { minDate: newDate, maxDate: this.minDate })
|
|
244
|
+
}
|
|
245
|
+
this.rangeState.selecting = false
|
|
246
|
+
}
|
|
247
|
+
} else {
|
|
248
|
+
this.$emit('pick', month)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
</script>
|