-
Notifications
You must be signed in to change notification settings - Fork 725
/
Copy pathPickerDay.vue
379 lines (369 loc) · 12 KB
/
PickerDay.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<template>
<div :class="[calendarClass, 'vdp-datepicker__calendar']" v-show="showDayView" :style="calendarStyle" @mousedown.prevent>
<slot name="beforeCalendarHeader"></slot>
<header>
<span
@click="isRtl ? nextMonth() : previousMonth()"
class="prev"
:class="{'disabled': isLeftNavDisabled}"><</span>
<span class="day__month_btn" @click="showMonthCalendar" :class="allowedToShowView('month') ? 'up' : ''">{{ isYmd ? currYearName : currMonthName }} {{ isYmd ? currMonthName : currYearName }}</span>
<span
@click="isRtl ? previousMonth() : nextMonth()"
class="next"
:class="{'disabled': isRightNavDisabled}">></span>
</header>
<div :class="isRtl ? 'flex-rtl' : ''">
<span class="cell day-header" v-for="d in daysOfWeek" :key="d.timestamp">{{ d }}</span>
<template v-if="blankDays > 0">
<span class="cell day blank" v-for="d in blankDays" :key="d.timestamp"></span>
</template><!--
--><span class="cell day"
v-for="day in days"
:key="day.timestamp"
:class="dayClasses(day)"
v-html="dayCellContent(day)"
@click="selectDate(day)"></span>
</div>
</div>
</template>
<script>
import { makeDateUtils } from '../utils/DateUtils'
export default {
props: {
showDayView: Boolean,
selectedDate: Date,
pageDate: Date,
pageTimestamp: Number,
fullMonthName: Boolean,
allowedToShowView: Function,
dayCellContent: {
type: Function,
default: day => {
if (day && Object.keys(day).length) {
return day.date
}
}
},
disabledDates: Object,
highlighted: Object,
calendarClass: [String, Object, Array],
calendarStyle: Object,
translation: Object,
isRtl: Boolean,
mondayFirst: Boolean,
useUtc: Boolean
},
data () {
const constructedDateUtils = makeDateUtils(this.useUtc)
return {
utils: constructedDateUtils
}
},
computed: {
/**
* Returns an array of day names
* @return {String[]}
*/
daysOfWeek () {
if (this.mondayFirst) {
const tempDays = this.translation.days.slice()
tempDays.push(tempDays.shift())
return tempDays
}
return this.translation.days
},
/**
* Returns the day number of the week less one for the first of the current month
* Used to show amount of empty cells before the first in the day calendar layout
* @return {Number}
*/
blankDays () {
const d = this.pageDate
let dObj = this.useUtc
? new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), 1))
: new Date(d.getFullYear(), d.getMonth(), 1, d.getHours(), d.getMinutes())
if (this.mondayFirst) {
return this.utils.getDay(dObj) > 0 ? this.utils.getDay(dObj) - 1 : 6
}
return this.utils.getDay(dObj)
},
/**
* @return {Object[]}
*/
days () {
const d = this.pageDate
let days = []
// set up a new date object to the beginning of the current 'page'
let dObj = this.useUtc
? new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), 1))
: new Date(d.getFullYear(), d.getMonth(), 1, d.getHours(), d.getMinutes())
let daysInMonth = this.utils.daysInMonth(this.utils.getFullYear(dObj), this.utils.getMonth(dObj))
for (let i = 0; i < daysInMonth; i++) {
days.push({
date: this.utils.getDate(dObj),
timestamp: dObj.getTime(),
isSelected: this.isSelectedDate(dObj),
isDisabled: this.isDisabledDate(dObj),
isHighlighted: this.isHighlightedDate(dObj),
isHighlightStart: this.isHighlightStart(dObj),
isHighlightEnd: this.isHighlightEnd(dObj),
isToday: this.utils.compareDates(dObj, new Date()),
isWeekend: this.utils.getDay(dObj) === 0 || this.utils.getDay(dObj) === 6,
isSaturday: this.utils.getDay(dObj) === 6,
isSunday: this.utils.getDay(dObj) === 0
})
this.utils.setDate(dObj, this.utils.getDate(dObj) + 1)
}
return days
},
/**
* Gets the name of the month the current page is on
* @return {String}
*/
currMonthName () {
const monthName = this.fullMonthName ? this.translation.months : this.translation.monthsAbbr
return this.utils.getMonthNameAbbr(this.utils.getMonth(this.pageDate), monthName)
},
/**
* Gets the name of the year that current page is on
* @return {Number}
*/
currYearName () {
const yearSuffix = this.translation.yearSuffix
return `${this.utils.getFullYear(this.pageDate)}${yearSuffix}`
},
/**
* Is this translation using year/month/day format?
* @return {Boolean}
*/
isYmd () {
return this.translation.ymd && this.translation.ymd === true
},
/**
* Is the left hand navigation button disabled?
* @return {Boolean}
*/
isLeftNavDisabled () {
return this.isRtl
? this.isNextMonthDisabled(this.pageTimestamp)
: this.isPreviousMonthDisabled(this.pageTimestamp)
},
/**
* Is the right hand navigation button disabled?
* @return {Boolean}
*/
isRightNavDisabled () {
return this.isRtl
? this.isPreviousMonthDisabled(this.pageTimestamp)
: this.isNextMonthDisabled(this.pageTimestamp)
}
},
methods: {
selectDate (date) {
if (date.isDisabled) {
this.$emit('selectedDisabled', date)
return false
}
this.$emit('selectDate', date)
},
/**
* @return {Number}
*/
getPageMonth () {
return this.utils.getMonth(this.pageDate)
},
/**
* Emit an event to show the month picker
*/
showMonthCalendar () {
this.$emit('showMonthCalendar')
},
/**
* Change the page month
* @param {Number} incrementBy
*/
changeMonth (incrementBy) {
let date = this.pageDate
this.utils.setMonth(date, this.utils.getMonth(date) + incrementBy)
this.$emit('changedMonth', date)
},
/**
* Decrement the page month
*/
previousMonth () {
if (!this.isPreviousMonthDisabled()) {
this.changeMonth(-1)
}
},
/**
* Is the previous month disabled?
* @return {Boolean}
*/
isPreviousMonthDisabled () {
if (!this.disabledDates || !this.disabledDates.to) {
return false
}
let d = this.pageDate
return this.utils.getMonth(this.disabledDates.to) >= this.utils.getMonth(d) &&
this.utils.getFullYear(this.disabledDates.to) >= this.utils.getFullYear(d)
},
/**
* Increment the current page month
*/
nextMonth () {
if (!this.isNextMonthDisabled()) {
this.changeMonth(+1)
}
},
/**
* Is the next month disabled?
* @return {Boolean}
*/
isNextMonthDisabled () {
if (!this.disabledDates || !this.disabledDates.from) {
return false
}
let d = this.pageDate
return this.utils.getMonth(this.disabledDates.from) <= this.utils.getMonth(d) &&
this.utils.getFullYear(this.disabledDates.from) <= this.utils.getFullYear(d)
},
/**
* Whether a day is selected
* @param {Date}
* @return {Boolean}
*/
isSelectedDate (dObj) {
return this.selectedDate && this.utils.compareDates(this.selectedDate, dObj)
},
/**
* Whether a day is disabled
* @param {Date}
* @return {Boolean}
*/
isDisabledDate (date) {
let disabledDates = false
if (typeof this.disabledDates === 'undefined') {
return false
}
if (typeof this.disabledDates.dates !== 'undefined') {
this.disabledDates.dates.forEach((d) => {
if (this.utils.compareDates(date, d)) {
disabledDates = true
return true
}
})
}
if (typeof this.disabledDates.to !== 'undefined' && this.disabledDates.to && date < this.disabledDates.to) {
disabledDates = true
}
if (typeof this.disabledDates.from !== 'undefined' && this.disabledDates.from && date > this.disabledDates.from) {
disabledDates = true
}
if (typeof this.disabledDates.ranges !== 'undefined') {
this.disabledDates.ranges.forEach((range) => {
if (typeof range.from !== 'undefined' && range.from && typeof range.to !== 'undefined' && range.to) {
if (date < range.to && date > range.from) {
disabledDates = true
return true
}
}
})
}
if (typeof this.disabledDates.days !== 'undefined' && this.disabledDates.days.indexOf(this.utils.getDay(date)) !== -1) {
disabledDates = true
}
if (typeof this.disabledDates.daysOfMonth !== 'undefined' && this.disabledDates.daysOfMonth.indexOf(this.utils.getDate(date)) !== -1) {
disabledDates = true
}
if (typeof this.disabledDates.customPredictor === 'function' && this.disabledDates.customPredictor(date)) {
disabledDates = true
}
return disabledDates
},
/**
* Whether a day is highlighted (only if it is not disabled already except when highlighted.includeDisabled is true)
* @param {Date}
* @return {Boolean}
*/
isHighlightedDate (date) {
if (!(this.highlighted && this.highlighted.includeDisabled) && this.isDisabledDate(date)) {
return false
}
let highlighted = false
if (typeof this.highlighted === 'undefined') {
return false
}
if (typeof this.highlighted.dates !== 'undefined') {
this.highlighted.dates.forEach((d) => {
if (this.utils.compareDates(date, d)) {
highlighted = true
return true
}
})
}
if (this.isDefined(this.highlighted.from) && this.isDefined(this.highlighted.to)) {
highlighted = date >= this.highlighted.from && date <= this.highlighted.to
}
if (typeof this.highlighted.days !== 'undefined' && this.highlighted.days.indexOf(this.utils.getDay(date)) !== -1) {
highlighted = true
}
if (typeof this.highlighted.daysOfMonth !== 'undefined' && this.highlighted.daysOfMonth.indexOf(this.utils.getDate(date)) !== -1) {
highlighted = true
}
if (typeof this.highlighted.customPredictor === 'function' && this.highlighted.customPredictor(date)) {
highlighted = true
}
return highlighted
},
dayClasses (day) {
return {
'selected': day.isSelected,
'disabled': day.isDisabled,
'highlighted': day.isHighlighted,
'today': day.isToday,
'weekend': day.isWeekend,
'sat': day.isSaturday,
'sun': day.isSunday,
'highlight-start': day.isHighlightStart,
'highlight-end': day.isHighlightEnd
}
},
/**
* Whether a day is highlighted and it is the first date
* in the highlighted range of dates
* @param {Date}
* @return {Boolean}
*/
isHighlightStart (date) {
return this.isHighlightedDate(date) &&
(this.highlighted.from instanceof Date) &&
(this.utils.getFullYear(this.highlighted.from) === this.utils.getFullYear(date)) &&
(this.utils.getMonth(this.highlighted.from) === this.utils.getMonth(date)) &&
(this.utils.getDate(this.highlighted.from) === this.utils.getDate(date))
},
/**
* Whether a day is highlighted and it is the first date
* in the highlighted range of dates
* @param {Date}
* @return {Boolean}
*/
isHighlightEnd (date) {
return this.isHighlightedDate(date) &&
(this.highlighted.to instanceof Date) &&
(this.utils.getFullYear(this.highlighted.to) === this.utils.getFullYear(date)) &&
(this.utils.getMonth(this.highlighted.to) === this.utils.getMonth(date)) &&
(this.utils.getDate(this.highlighted.to) === this.utils.getDate(date))
},
/**
* Helper
* @param {mixed} prop
* @return {Boolean}
*/
isDefined (prop) {
return typeof prop !== 'undefined' && prop
}
}
}
// eslint-disable-next-line
;
</script>