17e1fcf1
梁灏
init DatePicker
|
1
|
<template>
|
0f677893
梁灏
update DatePicker
|
2
3
|
<div
:class="[prefixCls]"
|
c46f385a
梁灏
update DatePicker
|
4
|
v-clickoutside="handleClose">
|
e9dd4dab
梁灏
publish 0.9.11-rc-1
|
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<div v-el:reference>
<slot>
<i-input
:class="[prefixCls + '-editor']"
:readonly="!editable || readonly"
:disabled="disabled"
:size="size"
:placeholder="placeholder"
:value="visualValue"
@on-change="handleInputChange"
@on-focus="handleFocus"
@on-click="handleIconClick"
@mouseenter="handleInputMouseenter"
@mouseleave="handleInputMouseleave"
:icon="iconType"></i-input>
</slot>
</div>
|
d20fe0ee
梁灏
update DatePicker
|
22
|
<Drop v-show="opened" :placement="placement" :transition="transition" v-ref:drop>
|
0f677893
梁灏
update DatePicker
|
23
24
25
|
<div v-el:picker></div>
</Drop>
</div>
|
17e1fcf1
梁灏
init DatePicker
|
26
27
|
</template>
<script>
|
0f677893
梁灏
update DatePicker
|
28
29
30
31
32
|
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
|
33
|
import { formatDate, parseDate } from './util';
|
0f677893
梁灏
update DatePicker
|
34
35
36
37
38
39
|
const prefixCls = 'ivu-date-picker';
const DEFAULT_FORMATS = {
date: 'yyyy-MM-dd',
month: 'yyyy-MM',
|
c46f385a
梁灏
update DatePicker
|
40
|
year: 'yyyy',
|
0f677893
梁灏
update DatePicker
|
41
42
43
44
45
46
47
|
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
|
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
|
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;
}
}
}
};
|
17e1fcf1
梁灏
init DatePicker
|
138
|
export default {
|
0f677893
梁灏
update DatePicker
|
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
components: { iInput, Drop },
directives: { clickoutside },
props: {
format: {
type: String
},
readonly: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
editable: {
type: Boolean,
default: true
},
|
fe44201b
梁灏
DatePicker add cl...
|
157
158
159
160
|
clearable: {
type: Boolean,
default: true
},
|
b9041a0d
梁灏
DatePicker add co...
|
161
162
163
164
|
confirm: {
type: Boolean,
default: false
},
|
e9dd4dab
梁灏
publish 0.9.11-rc-1
|
165
166
167
168
|
open: {
type: Boolean,
default: null
},
|
0f677893
梁灏
update DatePicker
|
169
170
171
172
173
174
175
176
177
|
size: {
validator (value) {
return oneOf(value, ['small', 'large']);
}
},
placeholder: {
type: String,
default: ''
},
|
68e9b100
梁灏
update DatePicker
|
178
|
placement: {
|
0f677893
梁灏
update DatePicker
|
179
|
validator (value) {
|
68e9b100
梁灏
update DatePicker
|
180
|
return oneOf(value, ['top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end']);
|
0f677893
梁灏
update DatePicker
|
181
|
},
|
68e9b100
梁灏
update DatePicker
|
182
|
default: 'bottom-start'
|
0f677893
梁灏
update DatePicker
|
183
184
185
186
187
|
},
options: {
type: Object
}
},
|
17e1fcf1
梁灏
init DatePicker
|
188
|
data () {
|
0f677893
梁灏
update DatePicker
|
189
190
191
|
return {
prefixCls: prefixCls,
showClose: false,
|
0f677893
梁灏
update DatePicker
|
192
|
visible: false,
|
50637863
梁灏
update DatePicker
|
193
|
picker: null,
|
68e9b100
梁灏
update DatePicker
|
194
195
|
internalValue: '',
disableClickOutSide: false // fixed when click a date,trigger clickoutside to close picker
|
0f677893
梁灏
update DatePicker
|
196
197
198
|
}
},
computed: {
|
e9dd4dab
梁灏
publish 0.9.11-rc-1
|
199
200
201
|
opened () {
return this.open === null ? this.visible : this.open;
},
|
0f677893
梁灏
update DatePicker
|
202
203
204
|
iconType () {
return this.showClose ? 'ios-close' : 'ios-calendar-outline';
},
|
d20fe0ee
梁灏
update DatePicker
|
205
206
207
208
209
210
211
|
transition () {
if (this.placement === 'bottom-start' || this.placement === 'bottom' || this.placement === 'bottom-end') {
return 'slide-up';
} else {
return 'slide-down';
}
},
|
c46f385a
梁灏
update DatePicker
|
212
|
selectionMode() {
|
472b4ff1
梁灏
update DatePicker
|
213
|
if (this.type === 'month') {
|
c46f385a
梁灏
update DatePicker
|
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
|
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]);
|
c46f385a
梁灏
update DatePicker
|
242
243
244
245
246
247
248
|
if (parsedValue) {
if (this.picker) this.picker.value = parsedValue;
}
return;
}
if (this.picker) this.picker.value = value;
}
|
0f677893
梁灏
update DatePicker
|
249
250
251
252
|
}
},
methods: {
handleClose () {
|
68e9b100
梁灏
update DatePicker
|
253
254
|
if (!this.disableClickOutSide) this.visible = false;
this.disableClickOutSide = false;
|
0f677893
梁灏
update DatePicker
|
255
256
|
},
handleFocus () {
|
e1874103
梁灏
update DatePicker
|
257
|
if (this.readonly) return;
|
0f677893
梁灏
update DatePicker
|
258
259
|
this.visible = true;
},
|
e1874103
梁灏
update DatePicker
|
260
261
262
263
264
|
handleInputChange (event) {
const oldValue = this.visualValue;
const value = event.target.value;
let correctValue = '';
|
7c5ccdab
梁灏
update DatePicker
|
265
266
267
|
let correctDate = '';
const type = this.type;
const format = this.format || DEFAULT_FORMATS[type];
|
e1874103
梁灏
update DatePicker
|
268
|
|
7c5ccdab
梁灏
update DatePicker
|
269
270
271
272
273
274
275
276
277
278
279
280
281
|
if (type === 'daterange' || type === 'timerange' || type === 'datetimerange') {
const parser = (
TYPE_VALUE_RESOLVER_MAP[type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).parser;
const formatter = (
TYPE_VALUE_RESOLVER_MAP[type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).formatter;
const parsedValue = parser(value, format);
|
3602b78d
梁灏
update DatePicker
|
282
283
284
285
|
if (parsedValue[0] instanceof Date && parsedValue[1] instanceof Date) {
if (parsedValue[0].getTime() > parsedValue[1].getTime()) {
correctValue = oldValue;
} else {
|
3602b78d
梁灏
update DatePicker
|
286
287
|
correctValue = formatter(parsedValue, format);
}
|
699a9dc8
梁灏
update DatePicker
|
288
|
// todo 判断disabledDate
|
e1874103
梁灏
update DatePicker
|
289
|
} else {
|
7c5ccdab
梁灏
update DatePicker
|
290
|
correctValue = oldValue;
|
e1874103
梁灏
update DatePicker
|
291
|
}
|
7c5ccdab
梁灏
update DatePicker
|
292
|
|
3602b78d
梁灏
update DatePicker
|
293
|
correctDate = parser(correctValue, format);
|
e1874103
梁灏
update DatePicker
|
294
|
} else {
|
7c5ccdab
梁灏
update DatePicker
|
295
296
297
298
299
300
301
302
303
304
305
306
|
const parsedDate = parseDate(value, format);
if (parsedDate instanceof Date) {
const options = this.options;
if (options.disabledDate && typeof options.disabledDate === 'function' && options.disabledDate(new Date(parsedDate))) {
correctValue = oldValue;
} else {
correctValue = formatDate(parsedDate, format);
}
} else {
correctValue = oldValue;
}
|
e1874103
梁灏
update DatePicker
|
307
|
|
7c5ccdab
梁灏
update DatePicker
|
308
309
|
correctDate = parseDate(correctValue, format);
}
|
e1874103
梁灏
update DatePicker
|
310
311
312
313
|
this.visualValue = correctValue;
event.target.value = correctValue;
this.internalValue = correctDate;
|
344131a7
梁灏
update DatePicker
|
314
315
|
if (correctValue !== oldValue) this.emitChange(correctDate);
|
c46f385a
梁灏
update DatePicker
|
316
317
|
},
handleInputMouseenter () {
|
0f677893
梁灏
update DatePicker
|
318
|
if (this.readonly || this.disabled) return;
|
fe44201b
梁灏
DatePicker add cl...
|
319
|
if (this.visualValue && this.clearable) {
|
0f677893
梁灏
update DatePicker
|
320
321
322
|
this.showClose = true;
}
},
|
c46f385a
梁灏
update DatePicker
|
323
|
handleInputMouseleave () {
|
0f677893
梁灏
update DatePicker
|
324
325
326
|
this.showClose = false;
},
handleIconClick () {
|
c46f385a
梁灏
update DatePicker
|
327
|
if (!this.showClose) return;
|
b9041a0d
梁灏
DatePicker add co...
|
328
329
330
|
this.handleClear();
},
handleClear () {
|
c46f385a
梁灏
update DatePicker
|
331
332
333
|
this.visible = false;
this.internalValue = '';
this.value = '';
|
d20fe0ee
梁灏
update DatePicker
|
334
|
this.$emit('on-clear');
|
0f677893
梁灏
update DatePicker
|
335
336
337
338
|
},
showPicker () {
if (!this.picker) {
this.picker = new Vue(this.panel).$mount(this.$els.picker);
|
50637863
梁灏
update DatePicker
|
339
|
this.picker.value = this.internalValue;
|
b9041a0d
梁灏
DatePicker add co...
|
340
|
this.picker.confirm = this.confirm;
|
c46f385a
梁灏
update DatePicker
|
341
|
this.picker.selectionMode = this.selectionMode;
|
50637863
梁灏
update DatePicker
|
342
343
344
345
346
347
|
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
|
348
349
|
this.picker.$on('on-pick', (date, visible = false) => {
|
b9041a0d
梁灏
DatePicker add co...
|
350
351
|
if (!this.confirm) this.visible = visible;
|
344131a7
梁灏
update DatePicker
|
352
|
this.emitChange(date);
|
c46f385a
梁灏
update DatePicker
|
353
|
this.value = date;
|
e1874103
梁灏
update DatePicker
|
354
|
this.picker.value = date;
|
c46f385a
梁灏
update DatePicker
|
355
356
357
|
this.picker.resetView && this.picker.resetView();
});
|
b9041a0d
梁灏
DatePicker add co...
|
358
359
360
361
|
this.picker.$on('on-pick-clear', () => {
this.handleClear();
});
this.picker.$on('on-pick-success', () => {
|
e9dd4dab
梁灏
publish 0.9.11-rc-1
|
362
|
// this.emitChange(this.value);
|
b9041a0d
梁灏
DatePicker add co...
|
363
|
this.visible = false;
|
d20fe0ee
梁灏
update DatePicker
|
364
|
this.$emit('on-ok');
|
b9041a0d
梁灏
DatePicker add co...
|
365
|
});
|
68e9b100
梁灏
update DatePicker
|
366
|
this.picker.$on('on-pick-click', () => this.disableClickOutSide = true);
|
b9041a0d
梁灏
DatePicker add co...
|
367
|
|
472b4ff1
梁灏
update DatePicker
|
368
|
// todo $on('on-time-range')
|
0f677893
梁灏
update DatePicker
|
369
|
}
|
c46f385a
梁灏
update DatePicker
|
370
371
372
373
374
375
|
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();
|
344131a7
梁灏
update DatePicker
|
376
377
|
},
emitChange (date) {
|
b9041a0d
梁灏
DatePicker add co...
|
378
|
const format = this.format || DEFAULT_FORMATS[this.type];
|
699a9dc8
梁灏
update DatePicker
|
379
380
381
382
383
384
|
const formatter = (
TYPE_VALUE_RESOLVER_MAP[this.type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).formatter;
this.$emit('on-change', formatter(date, format));
|
0f677893
梁灏
update DatePicker
|
385
386
387
388
389
390
391
|
}
},
watch: {
visible (val) {
if (val) {
this.showPicker();
this.$refs.drop.update();
|
d20fe0ee
梁灏
update DatePicker
|
392
|
if (this.open === null) this.$emit('on-open-change', true);
|
0f677893
梁灏
update DatePicker
|
393
|
} else {
|
d20fe0ee
梁灏
update DatePicker
|
394
|
if (this.picker) this.picker.resetView && this.picker.resetView();
|
0f677893
梁灏
update DatePicker
|
395
|
this.$refs.drop.destroy();
|
d20fe0ee
梁灏
update DatePicker
|
396
|
if (this.open === null) this.$emit('on-open-change', false);
|
c46f385a
梁灏
update DatePicker
|
397
398
399
400
401
|
}
},
internalValue(val) {
if (!val && this.picker && typeof this.picker.handleClear === 'function') {
this.picker.handleClear();
|
0f677893
梁灏
update DatePicker
|
402
|
}
|
50637863
梁灏
update DatePicker
|
403
404
405
406
|
},
value: {
immediate: true,
handler (val) {
|
c46f385a
梁灏
update DatePicker
|
407
|
this.internalValue = val;
|
50637863
梁灏
update DatePicker
|
408
|
}
|
d20fe0ee
梁灏
update DatePicker
|
409
410
411
412
413
414
415
416
|
},
open (val) {
if (val === true) {
this.visible = val;
this.$emit('on-open-change', true);
} else if (val === false) {
this.$emit('on-open-change', false);
}
|
0f677893
梁灏
update DatePicker
|
417
|
}
|
17e1fcf1
梁灏
init DatePicker
|
418
|
},
|
0f677893
梁灏
update DatePicker
|
419
420
421
422
|
beforeDestroy () {
if (this.picker) {
this.picker.$destroy();
}
|
e9dd4dab
梁灏
publish 0.9.11-rc-1
|
423
424
|
},
ready () {
|
d20fe0ee
梁灏
update DatePicker
|
425
|
if (this.open !== null) this.visible = this.open;
|
0f677893
梁灏
update DatePicker
|
426
|
}
|
17e1fcf1
梁灏
init DatePicker
|
427
428
|
}
</script>
|