forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime-mixin.js
More file actions
127 lines (118 loc) · 3.23 KB
/
datetime-mixin.js
File metadata and controls
127 lines (118 loc) · 3.23 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
import { between } from '../../utils/format'
import { inline as props } from './datetime-props'
import {
convertDateToFormat,
getDateBetween,
inferDateFormat,
startOfDate,
isSameDate,
isValid
} from '../../utils/date'
export default {
props,
computed: {
model: {
get () {
let date = isValid(this.value)
? new Date(this.value)
: (this.defaultValue ? new Date(this.defaultValue) : startOfDate(new Date(), 'day'))
return getDateBetween(
date,
this.pmin,
this.pmax
)
},
set (val) {
const date = getDateBetween(val, this.pmin, this.pmax)
const value = convertDateToFormat(date, this.formatModel === 'auto' ? inferDateFormat(this.value) : this.formatModel)
this.$emit('input', value)
this.$nextTick(() => {
if (!isSameDate(value, this.value)) {
this.$emit('change', value)
}
})
}
},
pmin () {
return this.min ? new Date(this.min) : null
},
pmax () {
return this.max ? new Date(this.max) : null
},
typeHasDate () {
return this.type === 'date' || this.type === 'datetime'
},
typeHasTime () {
return this.type === 'time' || this.type === 'datetime'
},
year () {
return this.model.getFullYear()
},
month () {
return this.model.getMonth() + 1
},
day () {
return this.model.getDate()
},
minute () {
return this.model.getMinutes()
},
yearInterval () {
let
min = this.pmin !== null ? this.pmin.getFullYear() : 1950,
max = this.pmax !== null ? this.pmax.getFullYear() : 2050
return Math.max(1, max - min + 1)
},
yearMin () {
return this.pmin !== null ? this.pmin.getFullYear() - 1 : 1949
},
monthInterval () {
let
min = this.pmin !== null && this.pmin.getFullYear() === this.model.getFullYear() ? this.pmin.getMonth() : 0,
max = this.pmax !== null && this.pmax.getFullYear() === this.model.getFullYear() ? this.pmax.getMonth() : 11
return Math.max(1, max - min + 1)
},
monthMin () {
return this.pmin !== null && this.pmin.getFullYear() === this.model.getFullYear()
? this.pmin.getMonth()
: 0
},
daysInMonth () {
return (new Date(this.model.getFullYear(), this.model.getMonth() + 1, 0)).getDate()
},
editable () {
return !this.disable && !this.readonly
}
},
methods: {
toggleAmPm () {
if (!this.editable) {
return
}
let
hour = this.model.getHours(),
offset = this.am ? 12 : -12
this.model = new Date(this.model.setHours(hour + offset))
},
__parseTypeValue (type, value) {
if (type === 'month') {
return between(value, 1, 12)
}
if (type === 'date') {
return between(value, 1, this.daysInMonth)
}
if (type === 'year') {
let
min = this.pmin ? this.pmin.getFullYear() : 1950,
max = this.pmax ? this.pmax.getFullYear() : 2050
return between(value, min, max)
}
if (type === 'hour') {
return between(value, 0, 23)
}
if (type === 'minute') {
return between(value, 0, 59)
}
}
}
}