-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathindex.vue
More file actions
340 lines (329 loc) · 10.4 KB
/
index.vue
File metadata and controls
340 lines (329 loc) · 10.4 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
<template>
<div
:id="id"
ref="parent"
v-click-outside="() => { toggleDatePicker(false) }"
class="date-time-picker"
>
<!-- Input -->
<CustomInput
v-if="hasInput"
:id="id"
ref="custom-input"
v-model="dateFormatted"
:disabled="disabled"
:dark="dark"
:hint="hint"
:error-hint="error"
:is-focus="hasPickerOpen"
:color="color"
:label="label"
:no-label="noLabel"
:input-size="inputSize"
:no-clear-button="noClearButton"
@focus="toggleDatePicker(true)"
@clear="$emit('input', null)"
/>
<slot v-else />
<div
v-if="hasPickerOpen && overlay"
class="time-picker-overlay"
@click.stop="toggleDatePicker(false)"
/>
<!-- Date picker container -->
<PickersContainer
v-if="!disabled"
:id="id"
ref="agenda"
v-model="dateTime"
:visible="hasPickerOpen"
:position="pickerPosition"
:inline="inline"
:color="color"
:button-color="buttonColor"
:dark="dark"
:no-header="noHeader"
:only-time="onlyTime"
:only-date="hasOnlyDate"
:minute-interval="minuteInterval"
:locale="locale"
:min-date="minDate"
:max-date="maxDate"
:format="format"
:no-weekends-days="noWeekendsDays"
:disabled-weekly="disabledWeekly"
:has-button-validate="hasButtonValidate"
:has-no-button="hasNoButton"
:range="range"
:disabled-dates="disabledDates"
:disabled-hours="disabledHours"
:enabled-dates="enabledDates"
:no-shortcuts="noShortcuts"
:button-now-translation="buttonNowTranslation"
:no-button-now="noButtonNow"
:first-day-of-week="firstDayOfWeek"
:custom-shortcuts="customShortcuts"
:no-keyboard="noKeyboard"
:right="right"
:no-scroll-event="noScrollEvent"
@validate="validate"
@close="toggleDatePicker(false)"
/>
</div>
</template>
<script>
import moment from 'moment'
import vClickOutside from 'v-click-outside'
import CustomInput from './_subs/CustomInput'
import PickersContainer from './_subs/PickersContainer'
import props from './props'
const updateMomentLocale = (locale, firstDayOfWeek) => {
moment.locale(locale)
if (firstDayOfWeek) {
const firstDayNumber = Number.isInteger(firstDayOfWeek) && firstDayOfWeek === 0
? 7
: firstDayOfWeek || moment.localeData(locale).firstDayOfWeek()
moment.updateLocale(locale, {
week: {
dow: firstDayNumber
}
})
}
}
const nearestMinutes = (interval, date, format) => {
const roundedMinutes = Math.ceil(date.minute() / interval) * interval
return moment(date.clone().minute(roundedMinutes).second(0), format)
}
export default {
name: 'VueCtkDateTimePicker',
components: {
CustomInput,
PickersContainer
},
directives: {
clickOutside: vClickOutside.directive
},
props,
data () {
return {
pickerOpen: false,
pickerPosition: this.position
}
},
computed: {
hasPickerOpen () {
return this.persistent || this.pickerOpen
},
hasNoButton () {
return this.noButton
},
hasButtonValidate () {
return !this.inline && !this.autoClose
},
hasOnlyDate () {
return this.onlyDate || this.range
},
dateFormatted () {
const dateFormatted = this.range
? this.getRangeDatesFormatted(this.locale)
: this.getDateFormatted(this.locale)
this.$emit('formatted-value', dateFormatted)
return dateFormatted
},
hasCustomElem () {
return this.$slots.default
},
hasInput () {
return !this.inline && !this.$slots.default
},
dateTime: {
get () {
const dateTime = this.range
? { start: this.value && this.value.start ? moment(this.value.start, this.formatOutput).format('YYYY-MM-DD') : null,
end: this.value && this.value.end ? moment(this.value.end, this.formatOutput).format('YYYY-MM-DD') : null }
: this.getDateTime()
return dateTime
},
set (value) {
if (this.autoClose && this.range && (value.end && value.start)) {
this.toggleDatePicker(false)
} else if (this.autoClose && !this.range) {
this.toggleDatePicker(false)
}
const newValue = this.range ? this.getRangeDateToSend(value) : this.getDateTimeToSend(value)
this.$emit('input', newValue)
if (this.hasCustomElem && !this.noValueToCustomElem) {
this.$nextTick(() => {
this.setValueToCustomElem()
})
}
}
},
formatOutput () {
return this.outputFormat || this.format
}
},
watch: {
open (val) {
if (this.disabled) return
this.pickerOpen = val
},
locale (value) {
updateMomentLocale(value, this.firstDayOfWeek)
}
},
mounted () {
updateMomentLocale(this.locale, this.firstDayOfWeek)
this.pickerPosition = this.getPosition()
this.pickerOpen = this.open
if (this.hasCustomElem) {
this.addEventToTriggerElement()
if (!this.noValueToCustomElem) {
this.setValueToCustomElem()
}
}
if (this.format === 'YYYY-MM-DD hh:mm a' && this.onlyTime) {
window.console.warn(`A (time) format must be indicated/ (Ex : format="HH:mm")`)
}
},
beforeDestroy () {
this.$emit('destroy')
if (this.hasCustomElem) {
this.addEventToTriggerElement()
}
},
methods: {
setValueToCustomElem () {
const target = this.$slots.default[0]
if (target) {
if (target.tag === 'input') {
target.elm.value = this.dateFormatted
} else {
target.elm.innerHTML = this.dateFormatted ? this.dateFormatted : this.label
}
} else {
window.console.warn(`Impossible to find custom element`)
}
},
addEventToTriggerElement () {
const target = this.$slots.default[0].elm
if (target) {
target.addEventListener('click', () => {
this.toggleDatePicker()
})
} else {
window.console.warn(`Impossible to find custom element`)
}
},
getRangeDatesFormatted () {
const hasStartValues = this.value && this.value.start
const hasEndValues = this.value && this.value.end
if (hasStartValues || hasEndValues) {
const datesFormatted = hasStartValues ? `${moment(this.value.start, this.formatOutput).set({ hour: 0, minute: 0, second: 0 }).format(this.formatted)}` : '...'
return hasEndValues ? `${datesFormatted} - ${moment(this.value.end, this.formatOutput).set({ hour: 23, minute: 59, second: 59 }).format(this.formatted)}` : `${datesFormatted} - ...`
} else {
return null
}
},
getDateFormatted () {
const date = this.value
? moment(this.value, this.formatOutput).format(this.formatted)
: null
return date
},
getRangeDateToSend (payload) {
const { start, end } = typeof payload !== 'undefined' ? payload : this.value
return start || end
? { start: start ? moment(start, 'YYYY-MM-DD').set({ hour: 0, minute: 0, second: 0 }).format(this.formatOutput) : null,
end: end ? moment(end, 'YYYY-MM-DD').set({ hour: 23, minute: 59, second: 59 }).format(this.formatOutput) : null,
shortcut: payload.value }
: { start: moment().format(this.formatOutput),
end: moment().format(this.formatOutput),
shortcut: payload.value }
},
getDateTimeToSend (value) {
const dateTime = typeof value !== 'undefined' ? value : this.value
const dateToSend = dateTime
? moment(dateTime, 'YYYY-MM-DD HH:mm')
: null
const dateTimeToSend = dateToSend ? nearestMinutes(this.minuteInterval, moment(dateToSend), 'YYYY-MM-DD HH:mm').format(this.formatOutput) : null
return dateTimeToSend
},
getDateTime () {
const date = this.value
? moment(this.value, this.formatOutput)
: null
return date ? nearestMinutes(this.minuteInterval, date, this.formatOutput).format('YYYY-MM-DD HH:mm') : null
},
toggleDatePicker (val) {
if (this.disabled) return
const isOpen = (val === false || val === true) ? val : !this.pickerOpen
this.setBodyOverflow(isOpen)
this.pickerOpen = isOpen
this.$emit(isOpen ? 'is-shown' : 'is-hidden')
if (this.pickerOpen && !this.position) {
this.pickerPosition = this.getPosition()
}
},
setBodyOverflow (value) {
if (window.innerWidth < 412) {
const body = document.getElementsByTagName('body')[0]
body.style.overflow = value ? 'hidden' : null
}
},
getPosition () {
if (this.position) {
return this.position
} else {
const parentRect = this.$refs.parent.getBoundingClientRect()
const windowHeight = window.innerHeight
let datePickerHeight = 445
datePickerHeight = this.noButton ? datePickerHeight - 41 : datePickerHeight
datePickerHeight = this.noHeader ? datePickerHeight - 58 : datePickerHeight
if (parentRect.top < datePickerHeight) {
// No place on top --> bottom
return 'bottom'
} else if (windowHeight - (parentRect.height + datePickerHeight + parentRect.top) >= 0) {
// Have place on bottom --> bottom
return 'bottom'
} else {
// No place on bottom --> top
return 'top'
}
}
},
validate () {
this.$emit('validate')
this.toggleDatePicker(false)
}
}
}
</script>
<style lang="scss">
@import "./assets/main.scss";
.date-time-picker {
width: 100%;
margin: 0 auto;
text-align: left;
font-size: 14px;
border-radius: 4px;
position: relative;
.time-picker-overlay {
z-index: 2;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.4);
}
}
@media screen and (max-width: 415px) {
.time-picker-overlay {
display: none;
}
.date-time-picker:not(.inline) {
position: inherit !important;
}
}
</style>