Blame view

src/components/date-picker/base/time-spinner.vue 8.75 KB
9d844d53   梁灏   fixed Layout bug
1
2
  <template>
      <div :class="classes">
531cd165   梁灏   support DatePicke...
3
          <div :class="[prefixCls+ '-list']" ref="hours">
9b376832   Sergio Crisostomo   Add feature: allo...
4
5
              <ul :class="[prefixCls + '-ul']">
                  <li :class="getCellCls(item)" v-for="item in hoursList" v-show="!item.hide" @click="handleClick('hours', item)">{{ formatTime(item.text) }}</li>
9d844d53   梁灏   fixed Layout bug
6
7
              </ul>
          </div>
531cd165   梁灏   support DatePicke...
8
          <div :class="[prefixCls+ '-list']" ref="minutes">
9b376832   Sergio Crisostomo   Add feature: allo...
9
10
              <ul :class="[prefixCls + '-ul']">
                  <li :class="getCellCls(item)" v-for="item in minutesList" v-show="!item.hide" @click="handleClick('minutes', item)">{{ formatTime(item.text) }}</li>
36628aca   梁灏   update TimePicker
11
              </ul>
9d844d53   梁灏   fixed Layout bug
12
          </div>
531cd165   梁灏   support DatePicke...
13
          <div :class="[prefixCls+ '-list']" v-show="showSeconds" ref="seconds">
9b376832   Sergio Crisostomo   Add feature: allo...
14
15
              <ul :class="[prefixCls + '-ul']">
                  <li :class="getCellCls(item)" v-for="item in secondsList" v-show="!item.hide" @click="handleClick('seconds', item)">{{ formatTime(item.text) }}</li>
36628aca   梁灏   update TimePicker
16
              </ul>
9d844d53   梁灏   fixed Layout bug
17
18
19
20
          </div>
      </div>
  </template>
  <script>
36628aca   梁灏   update TimePicker
21
      import Options from '../time-mixins';
d70cad45   梁灏   update TimePicker
22
      import { deepCopy, scrollTop, firstUpperCase } from '../../../utils/assist';
36628aca   梁灏   update TimePicker
23
  
9d844d53   梁灏   fixed Layout bug
24
      const prefixCls = 'ivu-time-picker-cells';
75cb2998   Sergio Crisostomo   Add keyboard navi...
25
      const timeParts = ['hours', 'minutes', 'seconds'];
9d844d53   梁灏   fixed Layout bug
26
27
  
      export default {
75cb2998   Sergio Crisostomo   Add keyboard navi...
28
          name: 'TimeSpinner',
36628aca   梁灏   update TimePicker
29
          mixins: [Options],
9d844d53   梁灏   fixed Layout bug
30
31
          props: {
              hours: {
2dbbd7de   梁灏   update TimePicker
32
                  type: [Number, String],
29a91fbb   Sergio Crisostomo   Correct passing o...
33
                  default: NaN
9d844d53   梁灏   fixed Layout bug
34
35
              },
              minutes: {
2dbbd7de   梁灏   update TimePicker
36
                  type: [Number, String],
29a91fbb   Sergio Crisostomo   Correct passing o...
37
                  default: NaN
9d844d53   梁灏   fixed Layout bug
38
39
              },
              seconds: {
2dbbd7de   梁灏   update TimePicker
40
                  type: [Number, String],
29a91fbb   Sergio Crisostomo   Correct passing o...
41
                  default: NaN
9d844d53   梁灏   fixed Layout bug
42
43
44
45
              },
              showSeconds: {
                  type: Boolean,
                  default: true
9b376832   Sergio Crisostomo   Add feature: allo...
46
47
48
49
              },
              steps: {
                  type: Array,
                  default: () => []
9d844d53   梁灏   fixed Layout bug
50
51
52
53
              }
          },
          data () {
              return {
9b376832   Sergio Crisostomo   Add feature: allo...
54
                  spinerSteps: [1, 1, 1].map((one, i) => Math.abs(this.steps[i]) || one),
af713093   梁灏   update TimePicker
55
                  prefixCls: prefixCls,
75cb2998   Sergio Crisostomo   Add keyboard navi...
56
57
58
                  compiled: false,
                  focusedColumn: -1, // which column inside the picker
                  focusedTime: [0, 0, 0] // the values array into [hh, mm, ss]
9d844d53   梁灏   fixed Layout bug
59
60
61
62
63
64
65
66
67
68
69
70
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-with-seconds`]: this.showSeconds
                      }
                  ];
              },
              hoursList () {
36628aca   梁灏   update TimePicker
71
                  let hours = [];
9b376832   Sergio Crisostomo   Add feature: allo...
72
                  const step = this.spinerSteps[0];
75cb2998   Sergio Crisostomo   Add keyboard navi...
73
                  const focusedHour = this.focusedColumn === 0 && this.focusedTime[0];
36628aca   梁灏   update TimePicker
74
                  const hour_tmpl = {
2d948738   梁灏   update TimePicker...
75
                      text: 0,
36628aca   梁灏   update TimePicker
76
77
78
79
80
                      selected: false,
                      disabled: false,
                      hide: false
                  };
  
9b376832   Sergio Crisostomo   Add feature: allo...
81
                  for (let i = 0; i < 24; i += step) {
36628aca   梁灏   update TimePicker
82
83
                      const hour = deepCopy(hour_tmpl);
                      hour.text = i;
75cb2998   Sergio Crisostomo   Add keyboard navi...
84
                      hour.focused = i === focusedHour;
36628aca   梁灏   update TimePicker
85
  
d70cad45   梁灏   update TimePicker
86
                      if (this.disabledHours.length && this.disabledHours.indexOf(i) > -1) {
36628aca   梁灏   update TimePicker
87
88
89
                          hour.disabled = true;
                          if (this.hideDisabledOptions) hour.hide = true;
                      }
2d948738   梁灏   update TimePicker...
90
                      if (this.hours === i) hour.selected = true;
36628aca   梁灏   update TimePicker
91
92
93
94
                      hours.push(hour);
                  }
  
                  return hours;
9d844d53   梁灏   fixed Layout bug
95
96
              },
              minutesList () {
36628aca   梁灏   update TimePicker
97
                  let minutes = [];
9b376832   Sergio Crisostomo   Add feature: allo...
98
                  const step = this.spinerSteps[1];
75cb2998   Sergio Crisostomo   Add keyboard navi...
99
                  const focusedMinute = this.focusedColumn === 1 && this.focusedTime[1];
36628aca   梁灏   update TimePicker
100
                  const minute_tmpl = {
2d948738   梁灏   update TimePicker...
101
                      text: 0,
36628aca   梁灏   update TimePicker
102
103
104
105
106
                      selected: false,
                      disabled: false,
                      hide: false
                  };
  
9b376832   Sergio Crisostomo   Add feature: allo...
107
                  for (let i = 0; i < 60; i += step) {
36628aca   梁灏   update TimePicker
108
109
                      const minute = deepCopy(minute_tmpl);
                      minute.text = i;
75cb2998   Sergio Crisostomo   Add keyboard navi...
110
                      minute.focused = i === focusedMinute;
36628aca   梁灏   update TimePicker
111
  
d70cad45   梁灏   update TimePicker
112
                      if (this.disabledMinutes.length && this.disabledMinutes.indexOf(i) > -1) {
36628aca   梁灏   update TimePicker
113
114
115
                          minute.disabled = true;
                          if (this.hideDisabledOptions) minute.hide = true;
                      }
2d948738   梁灏   update TimePicker...
116
                      if (this.minutes === i) minute.selected = true;
36628aca   梁灏   update TimePicker
117
118
                      minutes.push(minute);
                  }
36628aca   梁灏   update TimePicker
119
                  return minutes;
9d844d53   梁灏   fixed Layout bug
120
121
              },
              secondsList () {
36628aca   梁灏   update TimePicker
122
                  let seconds = [];
9b376832   Sergio Crisostomo   Add feature: allo...
123
                  const step = this.spinerSteps[2];
75cb2998   Sergio Crisostomo   Add keyboard navi...
124
                  const focusedMinute = this.focusedColumn === 2 && this.focusedTime[2];
36628aca   梁灏   update TimePicker
125
                  const second_tmpl = {
2d948738   梁灏   update TimePicker...
126
                      text: 0,
36628aca   梁灏   update TimePicker
127
128
129
130
131
                      selected: false,
                      disabled: false,
                      hide: false
                  };
  
9b376832   Sergio Crisostomo   Add feature: allo...
132
                  for (let i = 0; i < 60; i += step) {
36628aca   梁灏   update TimePicker
133
134
                      const second = deepCopy(second_tmpl);
                      second.text = i;
75cb2998   Sergio Crisostomo   Add keyboard navi...
135
                      second.focused = i === focusedMinute;
36628aca   梁灏   update TimePicker
136
  
d70cad45   梁灏   update TimePicker
137
                      if (this.disabledSeconds.length && this.disabledSeconds.indexOf(i) > -1) {
36628aca   梁灏   update TimePicker
138
139
140
                          second.disabled = true;
                          if (this.hideDisabledOptions) second.hide = true;
                      }
2d948738   梁灏   update TimePicker...
141
                      if (this.seconds === i) second.selected = true;
36628aca   梁灏   update TimePicker
142
143
144
145
                      seconds.push(second);
                  }
  
                  return seconds;
9d844d53   梁灏   fixed Layout bug
146
147
148
              }
          },
          methods: {
36628aca   梁灏   update TimePicker
149
150
151
152
153
              getCellCls (cell) {
                  return [
                      `${prefixCls}-cell`,
                      {
                          [`${prefixCls}-cell-selected`]: cell.selected,
75cb2998   Sergio Crisostomo   Add keyboard navi...
154
                          [`${prefixCls}-cell-focused`]: cell.focused,
36628aca   梁灏   update TimePicker
155
                          [`${prefixCls}-cell-disabled`]: cell.disabled
75cb2998   Sergio Crisostomo   Add keyboard navi...
156
  
36628aca   梁灏   update TimePicker
157
158
                      }
                  ];
2d948738   梁灏   update TimePicker...
159
              },
75cb2998   Sergio Crisostomo   Add keyboard navi...
160
161
162
163
164
165
166
167
168
169
170
171
172
              chooseValue(values){
                  const changes = timeParts.reduce((obj, part, i) => {
                      const value = values[i];
                      if (this[part] ===  value) return obj;
                      return {
                          ...obj,
                          [part]: value
                      };
                  }, {});
                  if (Object.keys(changes).length > 0) {
                      this.emitChange(changes);
                  }
              },
9b376832   Sergio Crisostomo   Add feature: allo...
173
174
              handleClick (type, cell) {
                  if (cell.disabled) return;
75cb2998   Sergio Crisostomo   Add keyboard navi...
175
176
177
178
179
                  const data = {[type]: cell.text};
                  this.emitChange(data);
              },
              emitChange(changes){
                  this.$emit('on-change', changes);
4db5926d   梁灏   fixed #153
180
                  this.$emit('on-pick-click');
91505561   梁灏   TimePicker
181
              },
af713093   梁灏   update TimePicker
182
              scroll (type, index) {
531cd165   梁灏   support DatePicke...
183
                  const from = this.$refs[type].scrollTop;
af713093   梁灏   update TimePicker
184
                  const to = 24 * this.getScrollIndex(type, index);
531cd165   梁灏   support DatePicke...
185
                  scrollTop(this.$refs[type], from, to, 500);
af713093   梁灏   update TimePicker
186
              },
91505561   梁灏   TimePicker
187
188
189
190
191
192
193
194
195
196
197
              getScrollIndex (type, index) {
                  const Type = firstUpperCase(type);
                  const disabled = this[`disabled${Type}`];
                  if (disabled.length && this.hideDisabledOptions) {
                      let _count = 0;
                      disabled.forEach(item => item <= index ? _count++ : '');
                      index -= _count;
                  }
                  return index;
              },
              updateScroll () {
91505561   梁灏   TimePicker
198
                  this.$nextTick(() => {
75cb2998   Sergio Crisostomo   Add keyboard navi...
199
                      timeParts.forEach(type => {
d5e44814   Sergio Crisostomo   remove Array.find...
200
                          this.$refs[type].scrollTop = 24 * this[`${type}List`].findIndex(obj => obj.text == this[type]);
91505561   梁灏   TimePicker
201
                      });
91505561   梁灏   TimePicker
202
                  });
b27687d3   梁灏   update TimePicker
203
204
205
              },
              formatTime (text) {
                  return text < 10 ? '0' + text : text;
75cb2998   Sergio Crisostomo   Add keyboard navi...
206
207
208
209
210
              },
              updateFocusedTime(col, time) {
                  this.focusedColumn = col;
                  this.focusedTime = time.slice();
  
36628aca   梁灏   update TimePicker
211
              }
91505561   梁灏   TimePicker
212
          },
af713093   梁灏   update TimePicker
213
214
215
          watch: {
              hours (val) {
                  if (!this.compiled) return;
7309b434   梁灏   InputNumber add `...
216
                  this.scroll('hours', this.hoursList.findIndex(obj => obj.text == val));
af713093   梁灏   update TimePicker
217
218
219
              },
              minutes (val) {
                  if (!this.compiled) return;
7309b434   梁灏   InputNumber add `...
220
                  this.scroll('minutes', this.minutesList.findIndex(obj => obj.text == val));
af713093   梁灏   update TimePicker
221
222
223
              },
              seconds (val) {
                  if (!this.compiled) return;
7309b434   梁灏   InputNumber add `...
224
                  this.scroll('seconds', this.secondsList.findIndex(obj => obj.text == val));
75cb2998   Sergio Crisostomo   Add keyboard navi...
225
226
227
228
229
230
231
              },
              focusedTime(updated, old){
                  timeParts.forEach((part, i) => {
                      if (updated[i] === old[i] || typeof updated[i] === 'undefined') return;
                      const valueIndex = this[`${part}List`].findIndex(obj => obj.text === updated[i]);
                      this.scroll(part, valueIndex);
                  });
af713093   梁灏   update TimePicker
232
233
              }
          },
531cd165   梁灏   support DatePicke...
234
          mounted () {
af713093   梁灏   update TimePicker
235
              this.$nextTick(() => this.compiled = true);
9d844d53   梁灏   fixed Layout bug
236
237
          }
      };
9b376832   Sergio Crisostomo   Add feature: allo...
238
  </script>