Blame view

src/components/date-picker/panel/Time/time.vue 5.38 KB
9d844d53   梁灏   fixed Layout bug
1
  <template>
030a522d   Sergio Crisostomo   make picker close...
2
      <div :class="[prefixCls + '-body-wrapper']" @mousedown.prevent>
9d844d53   梁灏   fixed Layout bug
3
          <div :class="[prefixCls + '-body']">
a8571a5f   梁灏   update DateTimePi...
4
              <div :class="[timePrefixCls + '-header']" v-if="showDate">{{ visibleDate }}</div>
9d844d53   梁灏   fixed Layout bug
5
6
              <div :class="[prefixCls + '-content']">
                  <time-spinner
531cd165   梁灏   support DatePicke...
7
                      ref="timeSpinner"
9d844d53   梁灏   fixed Layout bug
8
                      :show-seconds="showSeconds"
9b376832   Sergio Crisostomo   Add feature: allo...
9
                      :steps="steps"
3cd62242   Sergio Crisostomo   Allow disableDate...
10
11
12
13
14
15
                      :hours="timeSlots[0]"
                      :minutes="timeSlots[1]"
                      :seconds="timeSlots[2]"
                      :disabled-hours="disabledHMS.disabledHours"
                      :disabled-minutes="disabledHMS.disabledMinutes"
                      :disabled-seconds="disabledHMS.disabledSeconds"
36628aca   梁灏   update TimePicker
16
                      :hide-disabled-options="hideDisabledOptions"
9d844d53   梁灏   fixed Layout bug
17
18
19
20
                      @on-change="handleChange"
                      @on-pick-click="handlePickClick"></time-spinner>
              </div>
              <Confirm
d596b9e4   梁灏   update TimePicker
21
                  v-if="confirm"
9d844d53   梁灏   fixed Layout bug
22
23
24
25
26
27
                  @on-pick-clear="handlePickClear"
                  @on-pick-success="handlePickSuccess"></Confirm>
          </div>
      </div>
  </template>
  <script>
ca8e830c   Sergio Crisostomo   move files to sub...
28
29
30
      import TimeSpinner from '../../base/time-spinner.vue';
      import Confirm from '../../base/confirm.vue';
      import Options from '../../time-mixins';
9d844d53   梁灏   fixed Layout bug
31
  
9d844d53   梁灏   fixed Layout bug
32
  
ca8e830c   Sergio Crisostomo   move files to sub...
33
34
35
36
      import Mixin from '../panel-mixin';
      import Locale from '../../../../mixins/locale';
  
      import { initTimeDate } from '../../util';
2dbbd7de   梁灏   update TimePicker
37
  
9d844d53   梁灏   fixed Layout bug
38
39
40
      const prefixCls = 'ivu-picker-panel';
      const timePrefixCls = 'ivu-time-picker';
  
ca8e830c   Sergio Crisostomo   move files to sub...
41
      const capitalize = (str) => str[0].toUpperCase() + str.slice(1);
3cd62242   Sergio Crisostomo   Allow disableDate...
42
43
44
45
46
47
48
49
50
      const mergeDateHMS = (date, hours, minutes, seconds) => {
          const newDate = new Date(date.getTime());
          newDate.setHours(hours);
          newDate.setMinutes(minutes);
          newDate.setSeconds(seconds);
          return newDate;
      };
      const unique = (el, i, arr) => arr.indexOf(el) === i;
      const returnFalse = () => false;
ca8e830c   Sergio Crisostomo   move files to sub...
51
  
9d844d53   梁灏   fixed Layout bug
52
      export default {
ca8e830c   Sergio Crisostomo   move files to sub...
53
54
          name: 'TimePickerPanel',
          mixins: [ Mixin, Locale, Options ],
9d844d53   梁灏   fixed Layout bug
55
          components: { TimeSpinner, Confirm },
9b376832   Sergio Crisostomo   Add feature: allo...
56
          props: {
3cd62242   Sergio Crisostomo   Allow disableDate...
57
58
59
60
              disabledDate: {
                  type: Function,
                  default: returnFalse
              },
9b376832   Sergio Crisostomo   Add feature: allo...
61
62
63
              steps: {
                  type: Array,
                  default: () => []
ca8e830c   Sergio Crisostomo   move files to sub...
64
65
66
67
68
69
70
71
72
              },
              format: {
                  type: String,
                  default: 'HH:mm:ss'
              },
              value: {
                  type: Array,
                  required: true
              },
9b376832   Sergio Crisostomo   Add feature: allo...
73
          },
9d844d53   梁灏   fixed Layout bug
74
75
76
77
          data () {
              return {
                  prefixCls: prefixCls,
                  timePrefixCls: timePrefixCls,
ca8e830c   Sergio Crisostomo   move files to sub...
78
                  date: this.value[0] || initTimeDate(),
29a91fbb   Sergio Crisostomo   Correct passing o...
79
                  showDate: false
9d844d53   梁灏   fixed Layout bug
80
81
82
83
              };
          },
          computed: {
              showSeconds () {
22ff9a62   Sergio Crisostomo   fix showSeconds f...
84
                  return !(this.format || '').match(/mm$/);
a8571a5f   梁灏   update DateTimePi...
85
              },
ca8e830c   Sergio Crisostomo   move files to sub...
86
              visibleDate () { // TODO
a8571a5f   梁灏   update DateTimePi...
87
                  const date = this.date;
4ab11811   梁灏   some component su...
88
89
90
91
                  const month = date.getMonth() + 1;
                  const tYear = this.t('i.datepicker.year');
                  const tMonth = this.t(`i.datepicker.month${month}`);
                  return `${date.getFullYear()}${tYear} ${tMonth}`;
3cd62242   Sergio Crisostomo   Allow disableDate...
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
              },
              timeSlots(){
                  if (!this.value[0]) return [];
                  return ['getHours', 'getMinutes', 'getSeconds'].map(slot => this.date[slot]());
              },
              disabledHMS(){
                  const disabledTypes = ['disabledHours', 'disabledMinutes', 'disabledSeconds'];
                  if (this.disabledDate === returnFalse || !this.value[0]) {
                      const disabled = disabledTypes.reduce(
                          (obj, type) => (obj[type] = this[type], obj), {}
                      );
                      return disabled;
                  } else {
                      const slots = [24, 60, 60];
                      const disabled = ['Hours', 'Minutes', 'Seconds'].map(type => this[`disabled${type}`]);
                      const disabledHMS = disabled.map((preDisabled, j) => {
                          const slot = slots[j];
                          const toDisable = preDisabled;
                          for (let i = 0; i < slot; i+= (this.steps[j] || 1)){
                              const hms = this.timeSlots.map((slot, x) => x === j ? i : slot);
                              const testDateTime = mergeDateHMS(this.date, ...hms);
                              if (this.disabledDate(testDateTime, true)) toDisable.push(i);
                          }
                          return toDisable.filter(unique);
                      });
                      return disabledTypes.reduce(
                          (obj, type, i) => (obj[type] = disabledHMS[i], obj), {}
                      );
                  }
9d844d53   梁灏   fixed Layout bug
121
122
123
              }
          },
          watch: {
ca8e830c   Sergio Crisostomo   move files to sub...
124
125
              value (dates) {
                  let newVal = dates[0] || initTimeDate();
9d844d53   梁灏   fixed Layout bug
126
                  newVal = new Date(newVal);
ca8e830c   Sergio Crisostomo   move files to sub...
127
                  this.date = newVal;
9d844d53   梁灏   fixed Layout bug
128
129
130
              }
          },
          methods: {
603e437b   梁灏   update TimePicker
131
              handleChange (date, emit = true) {
ca8e830c   Sergio Crisostomo   move files to sub...
132
133
134
135
136
137
  
                  const newDate = new Date(this.date);
                  Object.keys(date).forEach(
                      type => newDate[`set${capitalize(type)}`](date[type])
                  );
  
90ebd5a7   Sergio Crisostomo   Expose changed da...
138
                  if (emit) this.$emit('on-pick', newDate, 'time');
5cc9b892   梁灏   update DateTimePi...
139
              },
a2a78c38   梁灏   update DateTimePi...
140
          },
531cd165   梁灏   support DatePicke...
141
          mounted () {
a2a78c38   梁灏   update DateTimePi...
142
              if (this.$parent && this.$parent.$options.name === 'DatePicker') this.showDate = true;
9d844d53   梁灏   fixed Layout bug
143
144
          }
      };
9b376832   Sergio Crisostomo   Add feature: allo...
145
  </script>