Blame view

src/components/date-picker/base/time-spinner.vue 7.15 KB
9d844d53   梁灏   fixed Layout bug
1
2
  <template>
      <div :class="classes">
531cd165   梁灏   support DatePicke...
3
          <div :class="[prefixCls+ '-list']" ref="hours">
4db5926d   梁灏   fixed #153
4
              <ul :class="[prefixCls + '-ul']" @click="handleClickHours">
531cd165   梁灏   support DatePicke...
5
                  <li :class="getCellCls(item)" v-for="(item, index) in hoursList" v-show="!item.hide" :index="index">{{ formatTime(item.text) }}</li>
9d844d53   梁灏   fixed Layout bug
6
7
              </ul>
          </div>
531cd165   梁灏   support DatePicke...
8
          <div :class="[prefixCls+ '-list']" ref="minutes">
4db5926d   梁灏   fixed #153
9
              <ul :class="[prefixCls + '-ul']" @click="handleClickMinutes">
531cd165   梁灏   support DatePicke...
10
                  <li :class="getCellCls(item)" v-for="(item, index) in minutesList" v-show="!item.hide" :index="index">{{ 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">
4db5926d   梁灏   fixed #153
14
              <ul :class="[prefixCls + '-ul']" @click="handleClickSeconds">
531cd165   梁灏   support DatePicke...
15
                  <li :class="getCellCls(item)" v-for="(item, index) in secondsList" v-show="!item.hide" :index="index">{{ 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
25
26
      const prefixCls = 'ivu-time-picker-cells';
  
      export default {
36628aca   梁灏   update TimePicker
27
          mixins: [Options],
9d844d53   梁灏   fixed Layout bug
28
29
          props: {
              hours: {
2dbbd7de   梁灏   update TimePicker
30
                  type: [Number, String],
9d844d53   梁灏   fixed Layout bug
31
32
33
                  default: 0
              },
              minutes: {
2dbbd7de   梁灏   update TimePicker
34
                  type: [Number, String],
9d844d53   梁灏   fixed Layout bug
35
36
37
                  default: 0
              },
              seconds: {
2dbbd7de   梁灏   update TimePicker
38
                  type: [Number, String],
9d844d53   梁灏   fixed Layout bug
39
40
41
42
43
44
45
46
47
                  default: 0
              },
              showSeconds: {
                  type: Boolean,
                  default: true
              }
          },
          data () {
              return {
af713093   梁灏   update TimePicker
48
49
                  prefixCls: prefixCls,
                  compiled: false
9d844d53   梁灏   fixed Layout bug
50
51
52
53
54
55
56
57
58
59
60
61
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-with-seconds`]: this.showSeconds
                      }
                  ];
              },
              hoursList () {
36628aca   梁灏   update TimePicker
62
63
                  let hours = [];
                  const hour_tmpl = {
2d948738   梁灏   update TimePicker...
64
                      text: 0,
36628aca   梁灏   update TimePicker
65
66
67
68
69
70
71
72
73
                      selected: false,
                      disabled: false,
                      hide: false
                  };
  
                  for (let i = 0; i < 24; i++) {
                      const hour = deepCopy(hour_tmpl);
                      hour.text = i;
  
d70cad45   梁灏   update TimePicker
74
                      if (this.disabledHours.length && this.disabledHours.indexOf(i) > -1) {
36628aca   梁灏   update TimePicker
75
76
77
                          hour.disabled = true;
                          if (this.hideDisabledOptions) hour.hide = true;
                      }
2d948738   梁灏   update TimePicker...
78
                      if (this.hours === i) hour.selected = true;
36628aca   梁灏   update TimePicker
79
80
81
82
                      hours.push(hour);
                  }
  
                  return hours;
9d844d53   梁灏   fixed Layout bug
83
84
              },
              minutesList () {
36628aca   梁灏   update TimePicker
85
86
                  let minutes = [];
                  const minute_tmpl = {
2d948738   梁灏   update TimePicker...
87
                      text: 0,
36628aca   梁灏   update TimePicker
88
89
90
91
92
93
94
95
96
                      selected: false,
                      disabled: false,
                      hide: false
                  };
  
                  for (let i = 0; i < 60; i++) {
                      const minute = deepCopy(minute_tmpl);
                      minute.text = i;
  
d70cad45   梁灏   update TimePicker
97
                      if (this.disabledMinutes.length && this.disabledMinutes.indexOf(i) > -1) {
36628aca   梁灏   update TimePicker
98
99
100
                          minute.disabled = true;
                          if (this.hideDisabledOptions) minute.hide = true;
                      }
2d948738   梁灏   update TimePicker...
101
                      if (this.minutes === i) minute.selected = true;
36628aca   梁灏   update TimePicker
102
103
104
105
                      minutes.push(minute);
                  }
  
                  return minutes;
9d844d53   梁灏   fixed Layout bug
106
107
              },
              secondsList () {
36628aca   梁灏   update TimePicker
108
109
                  let seconds = [];
                  const second_tmpl = {
2d948738   梁灏   update TimePicker...
110
                      text: 0,
36628aca   梁灏   update TimePicker
111
112
113
114
115
116
117
118
119
                      selected: false,
                      disabled: false,
                      hide: false
                  };
  
                  for (let i = 0; i < 60; i++) {
                      const second = deepCopy(second_tmpl);
                      second.text = i;
  
d70cad45   梁灏   update TimePicker
120
                      if (this.disabledSeconds.length && this.disabledSeconds.indexOf(i) > -1) {
36628aca   梁灏   update TimePicker
121
122
123
                          second.disabled = true;
                          if (this.hideDisabledOptions) second.hide = true;
                      }
2d948738   梁灏   update TimePicker...
124
                      if (this.seconds === i) second.selected = true;
36628aca   梁灏   update TimePicker
125
126
127
128
                      seconds.push(second);
                  }
  
                  return seconds;
9d844d53   梁灏   fixed Layout bug
129
130
131
              }
          },
          methods: {
36628aca   梁灏   update TimePicker
132
133
134
135
136
137
138
139
              getCellCls (cell) {
                  return [
                      `${prefixCls}-cell`,
                      {
                          [`${prefixCls}-cell-selected`]: cell.selected,
                          [`${prefixCls}-cell-disabled`]: cell.disabled
                      }
                  ];
2d948738   梁灏   update TimePicker...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
              },
              handleClickHours (event) {
                  this.handleClick('hours', event);
              },
              handleClickMinutes (event) {
                  this.handleClick('minutes', event);
              },
              handleClickSeconds (event) {
                  this.handleClick('seconds', event);
              },
              handleClick (type, event) {
                  const target = event.target;
                  if (target.tagName === 'LI') {
                      const cell = this[`${type}List`][parseInt(event.target.getAttribute('index'))];
                      if (cell.disabled) return;
                      const data = {};
                      data[type] = cell.text;
                      this.$emit('on-change', data);
2d948738   梁灏   update TimePicker...
158
                  }
4db5926d   梁灏   fixed #153
159
                  this.$emit('on-pick-click');
91505561   梁灏   TimePicker
160
              },
af713093   梁灏   update TimePicker
161
              scroll (type, index) {
531cd165   梁灏   support DatePicke...
162
                  const from = this.$refs[type].scrollTop;
af713093   梁灏   update TimePicker
163
                  const to = 24 * this.getScrollIndex(type, index);
531cd165   梁灏   support DatePicke...
164
                  scrollTop(this.$refs[type], from, to, 500);
af713093   梁灏   update TimePicker
165
              },
91505561   梁灏   TimePicker
166
167
168
169
170
171
172
173
174
175
176
177
              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 () {
                  const times = ['hours', 'minutes', 'seconds'];
91505561   梁灏   TimePicker
178
179
                  this.$nextTick(() => {
                      times.forEach(type => {
531cd165   梁灏   support DatePicke...
180
                          this.$refs[type].scrollTop = 24 * this.getScrollIndex(type, this[type]);
91505561   梁灏   TimePicker
181
                      });
91505561   梁灏   TimePicker
182
                  });
b27687d3   梁灏   update TimePicker
183
184
185
              },
              formatTime (text) {
                  return text < 10 ? '0' + text : text;
36628aca   梁灏   update TimePicker
186
              }
91505561   梁灏   TimePicker
187
          },
af713093   梁灏   update TimePicker
188
189
190
191
192
193
194
195
196
197
198
199
200
201
          watch: {
              hours (val) {
                  if (!this.compiled) return;
                  this.scroll('hours', val);
              },
              minutes (val) {
                  if (!this.compiled) return;
                  this.scroll('minutes', val);
              },
              seconds (val) {
                  if (!this.compiled) return;
                  this.scroll('seconds', val);
              }
          },
531cd165   梁灏   support DatePicke...
202
          mounted () {
91505561   梁灏   TimePicker
203
              this.updateScroll();
af713093   梁灏   update TimePicker
204
              this.$nextTick(() => this.compiled = true);
9d844d53   梁灏   fixed Layout bug
205
206
207
          }
      };
  </script>