-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeDatePicker.vue
More file actions
484 lines (467 loc) · 13.3 KB
/
RangeDatePicker.vue
File metadata and controls
484 lines (467 loc) · 13.3 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<template>
<div class="dp-range-date override field has-addons" :class="{'is-range': isRangePicker}"><!-- .dp-range-date.o-h override hook for more explicit css -->
<div v-show="isPickerActive" class="dp-click-overlay" @click="closePicker"></div>
<input ref="inp" class="input dp-input " @blur="parseDate"/>
<slot name="buttons">
<button class="dp-open-button button is-primary" @click="togglePicker">
<i class="far fa-calendar" />
</button>
</slot>
<transition>
<div v-show="isPickerActive" class="dp-picker">
<div class="dp-month-year field has-addons">
<div class="control">
<div class="select is-medium">
<select v-model="selectedMonth">
<option :value="0">Jan</option>
<option :value="1">Feb</option>
<option :value="2">Mar</option>
<option :value="3">Apr</option>
<option :value="4">May</option>
<option :value="5">Jun</option>
<option :value="6">Jul</option>
<option :value="7">Aug</option>
<option :value="8">Sept</option>
<option :value="9">Oct</option>
<option :value="10">Nov</option>
<option :value="11">Dec</option>
</select>
</div>
</div>
<div class="control">
<div class="select is-medium">
<select v-model="selectedYear">
<option v-for="(year, index) in yearRange" :key="index" :value="year">{{year}}</option>
</select>
</div>
</div>
</div>
<div class="dp-days">
<div class="dp-day-row dp-day-header">
<div class="dp-day dp-day-text" v-for="(dayText, index) in ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']" :key="index">
{{dayText}}
</div>
</div>
<div class="dp-day-row" v-for="(dayrow, index) in dayRows" :key="index">
<div class="dp-day" @click="dateClicked(day)" @mouseover="updateHoverDate(day)"
:class="{
'is-diff-month': day.date.getMonth() !== selectedMonth,
'is-hovered': hoverDate != null && innerFrom != null && day.date.getTime() > innerFrom && day.date.getTime() < hoverDate,
'is-active-one': innerFrom != null && day.date.getTime() === innerFrom.getTime(),
'is-active-two': innerTo != null && day.date.getTime() === innerTo.getTime(),
'is-active-between': innerFrom != null && innerTo != null && day.date.getTime() > innerFrom && day.date.getTime() < innerTo
}"
v-for="(day, index) in dayrow" :key="index">
{{day.date.getDate()}}
</div>
</div>
</div>
<div v-if="type === 'datetime'" class="dp-month-year field has-addons">
<div class="control">
<div class="select is-medium">
<select v-model="selectedHour" @change="setTimeOnFrom">
<option :value="1">1</option>
<option :value="2">2</option>
<option :value="3">3</option>
<option :value="4">4</option>
<option :value="5">5</option>
<option :value="6">6</option>
<option :value="7">7</option>
<option :value="8">8</option>
<option :value="9">9</option>
<option :value="10">10</option>
<option :value="11">11</option>
<option :value="12">12</option>
</select>
</div>
</div>
<div class="control">
<div class="select is-medium">
<select v-model="selectedMinute" @change="setTimeOnFrom">
<option :value="0">00</option>
<option :value="15">15</option>
<option :value="30">30</option>
<option :value="45">45</option>
</select>
</div>
</div>
<div class="control">
<div class="select is-medium">
<select v-model="selectedAMPM" @change="setTimeOnFrom">
<option :value="0">AM</option>
<option :value="12">PM</option>
</select>
</div>
</div>
<div class="control">
<button class="button is-success">Set</button>
</div>
</div>
</div>
</transition>
</div>
</template>
<script>
import parseTime from 'parsetime'
const month_names =["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
function rd_getDOY (now) {
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
return day
}
function rd_fmt(date) {
var day = date.getDate();
var month_index = date.getMonth();
var year = date.getFullYear();
return "" + day + " " + month_names[month_index] + " " + year;
}
function rd_fmttime(date) {
let hours = date.getHours()
let minutes = date.getMinutes()
let amPm = 'AM'
if (hours > 11) {
amPm = 'PM'
}
if (hours === 0) {
hours = 12
}
if (hours > 12) {
hours = hours - 12
}
if (minutes < 10) {
minutes = '0'+minutes
}
return hours + ':' + minutes + ' '+amPm
}
export default {
// COMPONENT
// ______________________________________
name: 'RangeDatePicker',
props: {
type: {
type: String,
required: true,
validator: function (value) {
// The value must match one of these strings
return ['date', 'datetime', 'rangedate'].indexOf(value) !== -1
}
},
cssPrefix: {
type: String,
default: ''
},
startDate: Date,
endDate: Date,
from: Date,
to: Date,
isRange: Boolean
},
components: {},
computed: {
isRangePicker () {
return this.type.indexOf('range') >= 0
},
fmttedRangeDate () {
let fmt = ''
if (this.innerFrom) {
fmt += rd_fmt(this.innerFrom)
}
if (this.innerTo) {
fmt += ' - ' + rd_fmt(this.innerTo)
}
return fmt
},
selectedDate () {
return new Date(this.selectedYear, this.selectedMonth, 1)
},
selectedDateDayOfYear () {
return rd_getDOY(this.selectedDate)
},
yearRange () {
let start = new Date().getFullYear()
let end = new Date(new Date().getFullYear()+10)
if (this.startDate) {
start = new Date(this.startDate)
}
if (this.endDate) {
end = new Date(this.endDate)
}
let years = []
while (start < end) {
years.push(start)
start++
}
return years
},
dayRows () {
// THIS always starts on sunday.. so if your month starts on a monday then sunday will be a 0 array item
let days = []
let i = 0
let selectedDay = this.selectedDate.getDay()
let monthEnd = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth()+1, 0).getDate()
//padd start of month
while (i < selectedDay && i < 32) {
days.push(0)
i++
}
i = 1 // one based cause dates don't start at 0
while (i <= monthEnd && i < 32) {
days.push(i)
i++
}
let dayRows = []
let dayRow = []
for (let i = 0; i < days.length; i++) {
if (i % 7 == 0 && i !== 0) {
dayRows.push(dayRow)
dayRow = []
}
dayRow.push({
dayNum: days[i],
date: new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth(), i - selectedDay + 1) //+1 for 0 based index with offset of padded days
})
}
//padd end of month
let padEnd = 7 - dayRow.length
i = 1
while (i <= padEnd && i < 32) {
dayRow.push({
dayNum: 0,
date: new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth()+1, i)
})
i++
}
dayRows.push(dayRow)
return dayRows
},
pfx () {
return this.cssPrefix
},
hasFrom () {
return (this.innerFrom !== null)
},
hasTo () {
return (this.innerTo !== null)
}
},
methods: {
parseDate (ev) {
let val = ev.target.value
let parsed = parseTime(val)
if (parsed.absolute) {
this.setFrom(new Date(parsed.absolute))
}
if (val.indexOf('-') > 0) {
let toVal = val.split('-')
if (toVal.length > 0) {
val = toVal[1]
}
let parsed = parseTime(val)
if (parsed.absolute) {
this.setTo(new Date(parsed.absolute))
}
}
this.closePicker()
},
dateClicked (day) {
this.hoverDate = null
if (this.isRangePicker) {
if (this.hasTo) {
this.setFrom(day.date)
this.setTo(null)
return
}
if (this.hasFrom && this.innerFrom.getTime() < day.date.getTime()) {
this.setTo(day.date)
this.isPickerActive = false
this.$emit('input', {from: this.innerFrom, to: this.innerTo})
return
}
}
this.setFrom(day.date)
if (this.isRangePicker) {
this.$emit('input', {from: this.innerFrom, to: this.innerTo})
} else {
this.$emit('input', this.innerFrom)
if (this.type !== 'datetime') {
this.isPickerActive = false
}
}
},
updateStartDate (day) {
this.startDate = day.date
},
updateHoverDate (day) {
if (!this.isRangePicker) {
return
}
if (this.innerFrom !== null && this.innerTo === null) {
this.hoverDate = day.date
}
},
closePicker () {
this.isPickerActive = false
},
togglePicker () {
this.isPickerActive = !this.isPickerActive
},
setFrom (val) {
this.innerFrom = val
if (this.type === 'datetime') {
this.setTimeOnFrom()
}
this.$emit('from-changed', this.innerFrom)
this.formatField()
},
setTimeOnFrom () {
let dt = this.innerFrom ? new Date(this.innerFrom) : new Date()
let hours = this.selectedHour
if (this.selectedAMPM === 0 && hours === 12) {
hours = 0
} else if (this.selectedAMPM === 12 && hours < 12) {
hours = this.selectedHour + this.selectedAMPM
}
dt.setHours(hours)
dt.setMinutes(this.selectedMinute)
this.innerFrom = dt
},
setTo (val) {
this.innerTo = val
this.$emit('to-changed', this.innerTo)
this.formatField()
},
formatField () {
let result = ''
if (this.innerFrom) {
result = rd_fmt(this.innerFrom)
if (this.type.indexOf('time') > -1) {
result += ' ' + rd_fmttime(this.innerFrom)
}
}
if (this.isRangePicker) {
result += ' - '
}
if (this.innerTo) {
result += rd_fmt(this.innerTo)
if (this.type.indexOf('time') > -1) {
result += ' ' + rd_fmttime(this.innerTo)
}
}
this.$refs.inp.value = result
}
},
watch: {},
data () {
return {
selectedMonth: new Date().getMonth(),
selectedYear: new Date().getFullYear(),
selectedHour: 1,
selectedMinute: 0,
selectedAMPM: 0,
start: null,
hoverDate: null,
isPickerActive: false,
innerFrom: null,
innerTo: null
}
},
// LIFECYCLE METHODS
// ______________________________________
beforeCreate () {
},
created () {
if (Object.hasOwnProperty(this.value, 'to')) {
this.innerFrom = this.value.from
this.innerTo = this.value.to
}
if (this.from || this.value) {
this.innerFrom = this.from || this.value
}
if (this.to) {
this.innerTo = this.to
}
},
beforeMount () {
},
mounted () {
},
beforeUpdate () {
},
updated () {
},
beforeDestroy () {
},
destroyed () {
}
}
</script>
<style lang="css">
.dp-range-date .dp-click-overlay {
position: fixed;
background: rgba(0, 0, 0, 0.2);
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index: 1;
}
.dp-range-date {
position: relative;
width: 100%;
}
.dp-range-date .dp-input {
position: relative;
width: 100%;
}
.dp-range-date .dp-picker{
background: white;
position: absolute;
margin-top: 0.5rem;
margin-left: 0.5rem;
padding: 0.5rem;
z-index: 2;
}
.dp-range-date .dp-month-year {
display: flex;
}
.dp-range-date .dp-month-year .dp-month-year-spacer{
display: block;
width: 0.5rem;
}
.dp-range-date .dp-days .dp-day-row {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.dp-range-date .dp-days .dp-day-row .dp-day {
cursor: pointer;
width: 14.25%;
text-align: center;
padding: 0.25rem 0.5rem;
margin-left: -1px;
}
.dp-range-date .dp-days .dp-day-row .dp-day.is-diff-month{
color: grey;
}
.dp-range-date .dp-days .dp-day-row .dp-day:hover,
.dp-range-date .dp-days .dp-day-row .dp-day.is-hovered {
background: red;
}
.dp-range-date .dp-days .dp-day-row .dp-day.is-active-one {
background: purple;
}
.dp-range-date .dp-days .dp-day-row .dp-day.is-active-two {
background: orange;
}
.dp-range-date .dp-days .dp-day-row .dp-day.is-active-between {
background: yellow;
}
.dp-range-date .dp-days .dp-day-row .dp-day.dp-day-text {
font-size: 0.7rem;
color: grey;
text-transform: uppercase;
background: white !important;
}
</style>