Blame view

src/components/date-picker/picker.vue 15.9 KB
17e1fcf1   梁灏   init DatePicker
1
  <template>
ecaf8d51   梁灏   Date add transfer...
2
      <div :class="[prefixCls]" v-clickoutside="handleClose">
531cd165   梁灏   support DatePicke...
3
          <div ref="reference" :class="[prefixCls + '-rel']">
e9dd4dab   梁灏   publish 0.9.11-rc-1
4
5
              <slot>
                  <i-input
acb79ba3   梁灏   fixed #433
6
                      :element-id="elementId"
e9dd4dab   梁灏   publish 0.9.11-rc-1
7
8
9
10
11
12
                      :class="[prefixCls + '-editor']"
                      :readonly="!editable || readonly"
                      :disabled="disabled"
                      :size="size"
                      :placeholder="placeholder"
                      :value="visualValue"
0460a1e8   梁灏   fixed #812
13
                      :name="name"
531cd165   梁灏   support DatePicke...
14
                      @on-input-change="handleInputChange"
e9dd4dab   梁灏   publish 0.9.11-rc-1
15
                      @on-focus="handleFocus"
030a522d   Sergio Crisostomo   make picker close...
16
                      @on-blur="handleBlur"
e9dd4dab   梁灏   publish 0.9.11-rc-1
17
                      @on-click="handleIconClick"
531cd165   梁灏   support DatePicke...
18
19
                      @mouseenter.native="handleInputMouseenter"
                      @mouseleave.native="handleInputMouseleave"
e9dd4dab   梁灏   publish 0.9.11-rc-1
20
21
22
                      :icon="iconType"></i-input>
              </slot>
          </div>
531cd165   梁灏   support DatePicke...
23
          <transition :name="transition">
ecaf8d51   梁灏   Date add transfer...
24
25
26
27
28
29
30
31
              <Drop
                  @click.native="handleTransferClick"
                  v-show="opened"
                  :class="{ [prefixCls + '-transfer']: transfer }"
                  :placement="placement"
                  ref="drop"
                  :data-transfer="transfer"
                  v-transfer-dom>
95eae081   Sergio Crisostomo   refactor Datepicker
32
33
34
35
36
37
38
39
40
41
                  <div>
                      <component
                          :is="panel"
                          :visible="visible"
                          :showTime="type === 'datetime' || type === 'datetimerange'"
                          :confirm="isConfirm"
                          :selectionMode="selectionMode"
                          :steps="steps"
                          :format="format"
                          :value="internalValue"
63bd0f7d   Sergio Crisostomo   Add start-date pr...
42
                          :start-date="startDate"
95eae081   Sergio Crisostomo   refactor Datepicker
43
44
45
46
47
48
49
50
51
52
  
                          v-bind="ownPickerProps"
  
                          @on-pick="onPick"
                          @on-pick-clear="handleClear"
                          @on-pick-success="onPickSuccess"
                          @on-pick-click="disableClickOutSide = true"
                          @on-selection-mode-change="onSelectionModeChange"
                      ></component>
                  </div>
531cd165   梁灏   support DatePicke...
53
54
              </Drop>
          </transition>
0f677893   梁灏   update DatePicker
55
      </div>
17e1fcf1   梁灏   init DatePicker
56
57
  </template>
  <script>
95eae081   Sergio Crisostomo   refactor Datepicker
58
59
  
  
0f677893   梁灏   update DatePicker
60
61
62
      import iInput from '../../components/input/input.vue';
      import Drop from '../../components/select/dropdown.vue';
      import clickoutside from '../../directives/clickoutside';
ecaf8d51   梁灏   Date add transfer...
63
      import TransferDom from '../../directives/transfer-dom';
0f677893   梁灏   update DatePicker
64
      import { oneOf } from '../../utils/assist';
af713093   梁灏   update TimePicker
65
      import { formatDate, parseDate } from './util';
cd78c9c4   梁灏   some comps suppor...
66
      import Emitter from '../../mixins/emitter';
0f677893   梁灏   update DatePicker
67
68
69
70
71
72
  
      const prefixCls = 'ivu-date-picker';
  
      const DEFAULT_FORMATS = {
          date: 'yyyy-MM-dd',
          month: 'yyyy-MM',
c46f385a   梁灏   update DatePicker
73
          year: 'yyyy',
0f677893   梁灏   update DatePicker
74
75
76
77
78
79
80
          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
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
      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
171
      export default {
21dad188   梁灏   prevent dispatch ...
172
          name: 'CalendarPicker',
cd78c9c4   梁灏   some comps suppor...
173
          mixins: [ Emitter ],
0f677893   梁灏   update DatePicker
174
          components: { iInput, Drop },
ecaf8d51   梁灏   Date add transfer...
175
          directives: { clickoutside, TransferDom },
0f677893   梁灏   update DatePicker
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
          props: {
              format: {
                  type: String
              },
              readonly: {
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              editable: {
                  type: Boolean,
                  default: true
              },
fe44201b   梁灏   DatePicker add cl...
192
193
194
195
              clearable: {
                  type: Boolean,
                  default: true
              },
b9041a0d   梁灏   DatePicker add co...
196
197
198
199
              confirm: {
                  type: Boolean,
                  default: false
              },
e9dd4dab   梁灏   publish 0.9.11-rc-1
200
201
202
203
              open: {
                  type: Boolean,
                  default: null
              },
95eae081   Sergio Crisostomo   refactor Datepicker
204
205
206
207
              multiple: {
                  type: Boolean,
                  default: false
              },
63bd0f7d   Sergio Crisostomo   Add start-date pr...
208
209
210
              startDate: {
                  type: Date
              },
0f677893   梁灏   update DatePicker
211
212
              size: {
                  validator (value) {
f00a037c   梁灏   some Components's...
213
                      return oneOf(value, ['small', 'large', 'default']);
0f677893   梁灏   update DatePicker
214
215
216
217
218
219
                  }
              },
              placeholder: {
                  type: String,
                  default: ''
              },
68e9b100   梁灏   update DatePicker
220
              placement: {
0f677893   梁灏   update DatePicker
221
                  validator (value) {
68e9b100   梁灏   update DatePicker
222
                      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
223
                  },
68e9b100   梁灏   update DatePicker
224
                  default: 'bottom-start'
0f677893   梁灏   update DatePicker
225
226
227
              },
              options: {
                  type: Object
ecaf8d51   梁灏   Date add transfer...
228
229
230
231
              },
              transfer: {
                  type: Boolean,
                  default: false
0460a1e8   梁灏   fixed #812
232
233
234
              },
              name: {
                  type: String
acb79ba3   梁灏   fixed #433
235
236
237
              },
              elementId: {
                  type: String
95eae081   Sergio Crisostomo   refactor Datepicker
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
              },
              steps: {
                  type: Array,
                  default: () => []
              },
              value: {
                  type: [Date, String, Array],
                  validator(val){
                      if (Array.isArray(val)){
                          const [start, end] = val.map(v => new Date(v));
                          return !isNaN(start.getTime()) && !isNaN(end.getTime());
                      } else {
                          if (typeof val === 'string') val = val.trim();
                          const date = new Date(val);
                          return val === '' || val === null || !isNaN(date.getTime());
                      }
                  }
0f677893   梁灏   update DatePicker
255
256
              }
          },
95eae081   Sergio Crisostomo   refactor Datepicker
257
258
259
          data(){
              const initialValue = this.formatDate(this.value);
  
0f677893   梁灏   update DatePicker
260
261
262
              return {
                  prefixCls: prefixCls,
                  showClose: false,
0f677893   梁灏   update DatePicker
263
                  visible: false,
95eae081   Sergio Crisostomo   refactor Datepicker
264
                  internalValue: initialValue,
531cd165   梁灏   support DatePicke...
265
                  disableClickOutSide: false,    // fixed when click a date,trigger clickoutside to close picker
95eae081   Sergio Crisostomo   refactor Datepicker
266
267
                  disableCloseUnderTransfer: false,  // transfer 模式下,点击Drop也会触发关闭,
                  selectionMode: this.onSelectionModeChange(this.type)
b0893113   jingsam   :art: add eslint
268
              };
0f677893   梁灏   update DatePicker
269
270
          },
          computed: {
95eae081   Sergio Crisostomo   refactor Datepicker
271
              publicValue(){
d07b4f33   Sergio Crisostomo   fix logic for mul...
272
273
274
275
276
277
                  if (this.multiple){
                      return this.internalValue.map(date => this.formatDate(date));
                  } else {
                      const isRange = this.type.includes('range');
                      return isRange ? this.formatDate(this.internalValue) : this.formatDate(this.internalValue[0]);
                  }
95eae081   Sergio Crisostomo   refactor Datepicker
278
279
              },
  
e9dd4dab   梁灏   publish 0.9.11-rc-1
280
281
282
              opened () {
                  return this.open === null ? this.visible : this.open;
              },
0f677893   梁灏   update DatePicker
283
              iconType () {
9d844d53   梁灏   fixed Layout bug
284
                  let icon = 'ios-calendar-outline';
456877a1   梁灏   update TimePicker
285
                  if (this.type === 'time' || this.type === 'timerange') icon = 'ios-clock-outline';
9d844d53   梁灏   fixed Layout bug
286
287
                  if (this.showClose) icon = 'ios-close';
                  return icon;
0f677893   梁灏   update DatePicker
288
              },
d20fe0ee   梁灏   update DatePicker
289
              transition () {
95eae081   Sergio Crisostomo   refactor Datepicker
290
291
                  const bottomPlaced = this.placement.match(/^bottom/);
                  return bottomPlaced ? 'slide-up' : 'slide-down';
d20fe0ee   梁灏   update DatePicker
292
              },
95eae081   Sergio Crisostomo   refactor Datepicker
293
294
              visualValue() {
                  const value = this.internalValue;
95eae081   Sergio Crisostomo   refactor Datepicker
295
                  if (!value) return;
d07b4f33   Sergio Crisostomo   fix logic for mul...
296
297
298
  
                  if (this.multiple) return value.map(date => this.formatDate(date)).join(', ');
  
95eae081   Sergio Crisostomo   refactor Datepicker
299
300
301
302
303
304
305
306
                  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);
              },
              isConfirm(){
d07b4f33   Sergio Crisostomo   fix logic for mul...
307
                  return this.confirm || this.type === 'datetime' || this.type === 'datetimerange' || this.multiple;
0f677893   梁灏   update DatePicker
308
309
310
              }
          },
          methods: {
95eae081   Sergio Crisostomo   refactor Datepicker
311
312
313
314
315
316
              onSelectionModeChange(type){
  
                  if (type.match(/^date/)) type = 'date';
                  this.selectionMode = type;
                  return type;
              },
ecaf8d51   梁灏   Date add transfer...
317
318
319
320
              // 开启 transfer 时,点击 Drop 即会关闭,这里不让其关闭
              handleTransferClick () {
                  if (this.transfer) this.disableCloseUnderTransfer = true;
              },
0f677893   梁灏   update DatePicker
321
              handleClose () {
ecaf8d51   梁灏   Date add transfer...
322
323
324
325
                  if (this.disableCloseUnderTransfer) {
                      this.disableCloseUnderTransfer = false;
                      return false;
                  }
1b7aefea   梁灏   update Picker
326
                  if (this.open !== null) return;
95eae081   Sergio Crisostomo   refactor Datepicker
327
  
762c8ddf   梁灏   update DatePicker
328
                  this.visible = false;
68e9b100   梁灏   update DatePicker
329
                  this.disableClickOutSide = false;
0f677893   梁灏   update DatePicker
330
331
              },
              handleFocus () {
e1874103   梁灏   update DatePicker
332
                  if (this.readonly) return;
0f677893   梁灏   update DatePicker
333
334
                  this.visible = true;
              },
030a522d   Sergio Crisostomo   make picker close...
335
336
337
              handleBlur () {
                  this.visible = false;
              },
e1874103   梁灏   update DatePicker
338
339
              handleInputChange (event) {
                  const oldValue = this.visualValue;
95eae081   Sergio Crisostomo   refactor Datepicker
340
                  const newValue = event.target.value;
e1874103   梁灏   update DatePicker
341
  
95eae081   Sergio Crisostomo   refactor Datepicker
342
343
344
                  if (newValue !== oldValue) {
                      this.emitChange();
                      this.internalValue = this.formatDate(newValue);
7c5ccdab   梁灏   update DatePicker
345
                  }
c46f385a   梁灏   update DatePicker
346
347
              },
              handleInputMouseenter () {
0f677893   梁灏   update DatePicker
348
                  if (this.readonly || this.disabled) return;
fe44201b   梁灏   DatePicker add cl...
349
                  if (this.visualValue && this.clearable) {
0f677893   梁灏   update DatePicker
350
351
352
                      this.showClose = true;
                  }
              },
c46f385a   梁灏   update DatePicker
353
              handleInputMouseleave () {
0f677893   梁灏   update DatePicker
354
355
356
                  this.showClose = false;
              },
              handleIconClick () {
7b7178f1   梁灏   fixed #528
357
358
                  if (this.showClose) {
                      this.handleClear();
f1f0206c   Aresn   fixed Date bug
359
                  } else if (!this.disabled) {
7b7178f1   梁灏   fixed #528
360
361
                      this.handleFocus();
                  }
b9041a0d   梁灏   DatePicker add co...
362
363
              },
              handleClear () {
c46f385a   梁灏   update DatePicker
364
                  this.visible = false;
95eae081   Sergio Crisostomo   refactor Datepicker
365
                  this.internalValue = this.internalValue.map(() => null);
d20fe0ee   梁灏   update DatePicker
366
                  this.$emit('on-clear');
cd78c9c4   梁灏   some comps suppor...
367
                  this.dispatch('FormItem', 'on-form-change', '');
95eae081   Sergio Crisostomo   refactor Datepicker
368
                  this.emitChange();
9d844d53   梁灏   fixed Layout bug
369
  
95eae081   Sergio Crisostomo   refactor Datepicker
370
371
372
373
                  setTimeout(
                      () => this.onSelectionModeChange(this.type),
                      500 // delay to improve dropdown close visual effect
                  );
344131a7   梁灏   update DatePicker
374
              },
95eae081   Sergio Crisostomo   refactor Datepicker
375
376
              emitChange () {
                  this.$emit('on-change', this.publicValue);
fc0c4c78   梁灏   fixed #494
377
                  this.$nextTick(() => {
95eae081   Sergio Crisostomo   refactor Datepicker
378
                      this.dispatch('FormItem', 'on-form-change', this.publicValue);
fc0c4c78   梁灏   fixed #494
379
                  });
21dad188   梁灏   prevent dispatch ...
380
              },
95eae081   Sergio Crisostomo   refactor Datepicker
381
382
383
              formatDate (val) {
                  const isRange = this.type.includes('range');
  
456877a1   梁灏   update TimePicker
384
                  const type = this.type;
95eae081   Sergio Crisostomo   refactor Datepicker
385
                  const parser = (
456877a1   梁灏   update TimePicker
386
                      TYPE_VALUE_RESOLVER_MAP[type] ||
699a9dc8   梁灏   update DatePicker
387
                      TYPE_VALUE_RESOLVER_MAP['default']
95eae081   Sergio Crisostomo   refactor Datepicker
388
                  ).parser;
699a9dc8   梁灏   update DatePicker
389
  
95eae081   Sergio Crisostomo   refactor Datepicker
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
                  if (val && type === 'time' && !(val instanceof Date)) {
                      val = parser(val, this.format || DEFAULT_FORMATS[type]);
                  } else if (type.match(/range$/)) {
                      if (!val){
                          val = [null, null];
                      } else {
                          val = val.map(date => new Date(date)); // try to parse
                          val = val.map(date => isNaN(date.getTime()) ? null : date); // check if parse passed
                      }
                  } else if (typeof val === 'string' && type.indexOf('time') !== 0 ){
                      val = parser(val, this.format || DEFAULT_FORMATS[type]) || val;
                  }
                  return isRange ? val : [val];
              },
              onPick(dates, visible = false) {
  
d07b4f33   Sergio Crisostomo   fix logic for mul...
406
407
408
409
                  if (this.multiple){
                      const allDates = [...this.internalValue, dates].filter(Boolean);
                      const timeStamps = allDates.map(date => date.getTime()).filter((ts, i, arr) => arr.indexOf(ts) === i); // filter away duplicates
                      this.internalValue = timeStamps.map(ts => new Date(ts));
95eae081   Sergio Crisostomo   refactor Datepicker
410
411
                  } else {
                      this.internalValue = Array.isArray(dates) ? dates : [dates];
0fd13696   梁灏   fixed DatePicker ...
412
                  }
95eae081   Sergio Crisostomo   refactor Datepicker
413
414
415
416
417
418
419
420
  
                  if (!this.isConfirm) this.onSelectionModeChange(this.type); // reset the selectionMode
                  if (!this.isConfirm) this.visible = visible;
                  this.emitChange();
              },
              onPickSuccess(){
                  this.visible = false;
                  this.$emit('on-ok');
0f677893   梁灏   update DatePicker
421
              }
95eae081   Sergio Crisostomo   refactor Datepicker
422
  
0f677893   梁灏   update DatePicker
423
424
          },
          watch: {
95eae081   Sergio Crisostomo   refactor Datepicker
425
426
              visible (state) {
                  if (state === false){
0f677893   梁灏   update DatePicker
427
                      this.$refs.drop.destroy();
030a522d   Sergio Crisostomo   make picker close...
428
429
                      const input = this.$el.querySelector('input');
                      if (input) input.blur();
c46f385a   梁灏   update DatePicker
430
                  }
95eae081   Sergio Crisostomo   refactor Datepicker
431
                  this.$emit('on-open-change', state);
c46f385a   梁灏   update DatePicker
432
              },
95eae081   Sergio Crisostomo   refactor Datepicker
433
434
435
436
437
438
439
440
441
442
443
444
445
446
              value(val) {
                  const type = this.type;
                  const parser = (
                      TYPE_VALUE_RESOLVER_MAP[type] ||
                      TYPE_VALUE_RESOLVER_MAP['default']
                  ).parser;
  
                  if (val && type === 'time' && !(val instanceof Date)) {
                      val = parser(val, this.format || DEFAULT_FORMATS[type]);
                  } else if (val && type.match(/range$/) && Array.isArray(val) && val.filter(Boolean).length === 2 && !(val[0] instanceof Date) && !(val[1] instanceof Date)) {
                      val = val.join(RANGE_SEPARATOR);
                      val = parser(val, this.format || DEFAULT_FORMATS[type]);
                  } else if (typeof val === 'string' && type.indexOf('time') !== 0 ){
                      val = parser(val, this.format || DEFAULT_FORMATS[type]) || val;
0f677893   梁灏   update DatePicker
447
                  }
0dd7b94a   梁灏   update TimePicker
448
  
95eae081   Sergio Crisostomo   refactor Datepicker
449
450
                  this.internalValue = val;
                  this.$emit('input', val);
d20fe0ee   梁灏   update DatePicker
451
452
              },
              open (val) {
95eae081   Sergio Crisostomo   refactor Datepicker
453
454
455
456
                  this.visible = val === true;
              },
              type(type){
                  this.onSelectionModeChange(type);
0f677893   梁灏   update DatePicker
457
              }
e9dd4dab   梁灏   publish 0.9.11-rc-1
458
          },
531cd165   梁灏   support DatePicke...
459
          mounted () {
d20fe0ee   梁灏   update DatePicker
460
              if (this.open !== null) this.visible = this.open;
0f677893   梁灏   update DatePicker
461
          }
b0893113   jingsam   :art: add eslint
462
463
      };
  </script>