17e1fcf1
梁灏
init DatePicker
|
1
|
<template>
|
0f677893
梁灏
update DatePicker
|
2
3
|
<div
:class="[prefixCls]"
|
c46f385a
梁灏
update DatePicker
|
4
|
v-clickoutside="handleClose">
|
0f677893
梁灏
update DatePicker
|
5
6
7
8
9
10
11
|
<i-input
v-el:reference
:class="[prefixCls + '-editor']"
:readonly="!editable || readonly"
:disabled="disabled"
:size="size"
:placeholder="placeholder"
|
c46f385a
梁灏
update DatePicker
|
12
13
|
:value="visualValue"
@on-change="handleInputChange"
|
0f677893
梁灏
update DatePicker
|
14
15
16
|
@on-focus="handleFocus"
@on-blur="handleBlur"
@on-click="handleIconClick"
|
c46f385a
梁灏
update DatePicker
|
17
18
|
@mouseenter="handleInputMouseenter"
@mouseleave="handleInputMouseleave"
|
0f677893
梁灏
update DatePicker
|
19
20
21
22
23
|
:icon="iconType"></i-input>
<Drop v-show="visible" :placement="placement" transition="slide-up" v-ref:drop>
<div v-el:picker></div>
</Drop>
</div>
|
17e1fcf1
梁灏
init DatePicker
|
24
25
|
</template>
<script>
|
0f677893
梁灏
update DatePicker
|
26
27
28
29
30
|
import Vue from 'vue';
import iInput from '../../components/input/input.vue';
import Drop from '../../components/select/dropdown.vue';
import clickoutside from '../../directives/clickoutside';
import { oneOf } from '../../utils/assist';
|
c46f385a
梁灏
update DatePicker
|
31
|
import { formatDate, parseDate } from './util';
|
0f677893
梁灏
update DatePicker
|
32
33
34
35
36
37
|
const prefixCls = 'ivu-date-picker';
const DEFAULT_FORMATS = {
date: 'yyyy-MM-dd',
month: 'yyyy-MM',
|
c46f385a
梁灏
update DatePicker
|
38
|
year: 'yyyy',
|
0f677893
梁灏
update DatePicker
|
39
40
41
42
43
44
45
|
datetime: 'yyyy-MM-dd HH:mm:ss',
time: 'HH:mm:ss',
timerange: 'HH:mm:ss',
daterange: 'yyyy-MM-dd',
datetimerange: 'yyyy-MM-dd HH:mm:ss'
};
|
c46f385a
梁灏
update DatePicker
|
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
|
const RANGE_SEPARATOR = ' - ';
const DATE_FORMATTER = function(value, format) {
return formatDate(value, format);
};
const DATE_PARSER = function(text, format) {
return parseDate(text, format);
};
const RANGE_FORMATTER = function(value, format) {
if (Array.isArray(value) && value.length === 2) {
const start = value[0];
const end = value[1];
if (start && end) {
return formatDate(start, format) + RANGE_SEPARATOR + formatDate(end, format);
}
}
return '';
};
const RANGE_PARSER = function(text, format) {
const array = text.split(RANGE_SEPARATOR);
if (array.length === 2) {
const range1 = array[0];
const range2 = array[1];
return [parseDate(range1, format), parseDate(range2, format)];
}
return [];
};
const TYPE_VALUE_RESOLVER_MAP = {
default: {
formatter(value) {
if (!value) return '';
return '' + value;
},
parser(text) {
if (text === undefined || text === '') return null;
return text;
}
},
date: {
formatter: DATE_FORMATTER,
parser: DATE_PARSER
},
datetime: {
formatter: DATE_FORMATTER,
parser: DATE_PARSER
},
daterange: {
formatter: RANGE_FORMATTER,
parser: RANGE_PARSER
},
datetimerange: {
formatter: RANGE_FORMATTER,
parser: RANGE_PARSER
},
timerange: {
formatter: RANGE_FORMATTER,
parser: RANGE_PARSER
},
time: {
formatter: DATE_FORMATTER,
parser: DATE_PARSER
},
month: {
formatter: DATE_FORMATTER,
parser: DATE_PARSER
},
year: {
formatter: DATE_FORMATTER,
parser: DATE_PARSER
},
number: {
formatter(value) {
if (!value) return '';
return '' + value;
},
parser(text) {
let result = Number(text);
if (!isNaN(text)) {
return result;
} else {
return null;
}
}
}
};
|
0f677893
梁灏
update DatePicker
|
136
137
138
139
140
141
|
const PLACEMENT_MAP = {
left: 'bottom-start',
center: 'bottom-center',
right: 'bottom-end'
};
|
17e1fcf1
梁灏
init DatePicker
|
142
|
export default {
|
0f677893
梁灏
update DatePicker
|
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
|
components: { iInput, Drop },
directives: { clickoutside },
props: {
format: {
type: String
},
readonly: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
editable: {
type: Boolean,
default: true
},
size: {
validator (value) {
return oneOf(value, ['small', 'large']);
}
},
placeholder: {
type: String,
default: ''
},
align: {
validator (value) {
return oneOf(value, ['left', 'center', 'right']);
},
default: 'left'
},
options: {
type: Object
}
},
|
17e1fcf1
梁灏
init DatePicker
|
180
|
data () {
|
0f677893
梁灏
update DatePicker
|
181
182
183
184
185
|
return {
prefixCls: prefixCls,
showClose: false,
visualValue: '',
visible: false,
|
50637863
梁灏
update DatePicker
|
186
187
|
picker: null,
internalValue: ''
|
0f677893
梁灏
update DatePicker
|
188
189
190
191
192
193
194
195
|
}
},
computed: {
iconType () {
return this.showClose ? 'ios-close' : 'ios-calendar-outline';
},
placement () {
return PLACEMENT_MAP[this.align];
|
c46f385a
梁灏
update DatePicker
|
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
|
},
selectionMode() {
if (this.type === 'week') {
return 'week';
} else if (this.type === 'month') {
return 'month';
} else if (this.type === 'year') {
return 'year';
}
return 'day';
},
visualValue: {
get () {
const value = this.internalValue;
if (!value) return;
const formatter = (
TYPE_VALUE_RESOLVER_MAP[this.type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).formatter;
const format = DEFAULT_FORMATS[this.type];
return formatter(value, this.format || format);
},
set (value) {
if (value) {
const type = this.type;
const parser = (
TYPE_VALUE_RESOLVER_MAP[type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).parser;
const parsedValue = parser(value, this.format || DEFAULT_FORMATS[type]);
if (parsedValue) {
if (this.picker) this.picker.value = parsedValue;
}
return;
}
if (this.picker) this.picker.value = value;
}
|
0f677893
梁灏
update DatePicker
|
237
238
239
240
241
242
243
244
245
246
247
248
|
}
},
methods: {
handleClose () {
this.visible = false;
},
handleFocus () {
this.visible = true;
},
handleBlur () {
},
|
c46f385a
梁灏
update DatePicker
|
249
250
251
252
|
handleInputChange (val) {
this.visualValue = val;
},
handleInputMouseenter () {
|
0f677893
梁灏
update DatePicker
|
253
254
255
256
257
|
if (this.readonly || this.disabled) return;
if (this.visualValue) {
this.showClose = true;
}
},
|
c46f385a
梁灏
update DatePicker
|
258
|
handleInputMouseleave () {
|
0f677893
梁灏
update DatePicker
|
259
260
261
|
this.showClose = false;
},
handleIconClick () {
|
c46f385a
梁灏
update DatePicker
|
262
263
264
265
|
if (!this.showClose) return;
this.visible = false;
this.internalValue = '';
this.value = '';
|
0f677893
梁灏
update DatePicker
|
266
267
268
269
|
},
showPicker () {
if (!this.picker) {
this.picker = new Vue(this.panel).$mount(this.$els.picker);
|
50637863
梁灏
update DatePicker
|
270
|
this.picker.value = this.internalValue;
|
c46f385a
梁灏
update DatePicker
|
271
|
this.picker.selectionMode = this.selectionMode;
|
50637863
梁灏
update DatePicker
|
272
273
274
275
276
277
|
if (this.format) this.picker.format = this.format;
const options = this.options;
for (const option in options) {
this.picker[option] = options[option];
}
|
c46f385a
梁灏
update DatePicker
|
278
279
280
281
282
283
284
285
286
|
this.picker.$on('on-pick', (date, visible = false) => {
this.$emit('on-change', date);
this.value = date;
this.visible = visible;
this.picker.resetView && this.picker.resetView();
});
// todo $on('on-range')
|
0f677893
梁灏
update DatePicker
|
287
|
}
|
c46f385a
梁灏
update DatePicker
|
288
289
290
291
292
293
|
if (this.internalValue instanceof Date) {
this.picker.date = new Date(this.internalValue.getTime());
} else {
this.picker.value = this.internalValue;
}
this.picker.resetView && this.picker.resetView();
|
0f677893
梁灏
update DatePicker
|
294
295
296
297
298
299
300
|
}
},
watch: {
visible (val) {
if (val) {
this.showPicker();
this.$refs.drop.update();
|
c46f385a
梁灏
update DatePicker
|
301
|
this.$emit('on-open-change', true);
|
0f677893
梁灏
update DatePicker
|
302
|
} else {
|
c46f385a
梁灏
update DatePicker
|
303
304
305
|
if (this.picker) {
this.picker.resetView && this.picker.resetView();
}
|
0f677893
梁灏
update DatePicker
|
306
|
this.$refs.drop.destroy();
|
c46f385a
梁灏
update DatePicker
|
307
308
309
310
311
312
|
this.$emit('on-open-change', false);
}
},
internalValue(val) {
if (!val && this.picker && typeof this.picker.handleClear === 'function') {
this.picker.handleClear();
|
0f677893
梁灏
update DatePicker
|
313
|
}
|
50637863
梁灏
update DatePicker
|
314
315
316
317
|
},
value: {
immediate: true,
handler (val) {
|
c46f385a
梁灏
update DatePicker
|
318
|
this.internalValue = val;
|
50637863
梁灏
update DatePicker
|
319
|
}
|
0f677893
梁灏
update DatePicker
|
320
|
}
|
17e1fcf1
梁灏
init DatePicker
|
321
|
},
|
0f677893
梁灏
update DatePicker
|
322
323
324
325
326
|
beforeDestroy () {
if (this.picker) {
this.picker.$destroy();
}
}
|
17e1fcf1
梁灏
init DatePicker
|
327
328
|
}
</script>
|