Blame view

src/components/date-picker/panel/date.vue 10.8 KB
17e1fcf1   梁灏   init DatePicker
1
  <template>
2533a192   梁灏   update DatePicker
2
      <div :class="classes">
3cf7cfd1   梁灏   update DatePicker
3
          <div :class="[prefixCls + '-sidebar']" v-if="shortcuts.length">
0f677893   梁灏   update DatePicker
4
5
6
7
8
9
10
11
12
              <div
                  :class="[prefixCls + '-shortcut']"
                  v-for="shortcut in shortcuts"
                  @click="handleShortcutClick(shortcut)">{{ shortcut.text }}</div>
          </div>
          <div :class="[prefixCls + '-body']">
              <div :class="[datePrefixCls + '-header']" v-show="currentView !== 'time'">
                  <span
                      :class="iconBtnCls('prev', '-double')"
c46f385a   梁灏   update DatePicker
13
                      @click="prevYear"><Icon type="ios-arrow-left"></Icon></span>
0f677893   梁灏   update DatePicker
14
15
16
                  <span
                      :class="iconBtnCls('prev')"
                      @click="prevMonth"
c46f385a   梁灏   update DatePicker
17
                      v-show="currentView === 'date'"><Icon type="ios-arrow-left"></Icon></span>
0f677893   梁灏   update DatePicker
18
19
                  <span
                      :class="[datePrefixCls + '-header-label']"
c46f385a   梁灏   update DatePicker
20
                      @click="showYearPicker">{{ yearLabel }}</span>
0f677893   梁灏   update DatePicker
21
22
23
                  <span
                      :class="[datePrefixCls + '-header-label']"
                      @click="showMonthPicker"
4ab11811   梁灏   some component su...
24
                      v-show="currentView === 'date'">{{ monthLabel }}</span>
c46f385a   梁灏   update DatePicker
25
26
27
                  <span
                      :class="iconBtnCls('next', '-double')"
                      @click="nextYear"><Icon type="ios-arrow-right"></Icon></span>
0f677893   梁灏   update DatePicker
28
29
30
                  <span
                      :class="iconBtnCls('next')"
                      @click="nextMonth"
c46f385a   梁灏   update DatePicker
31
                      v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span>
0f677893   梁灏   update DatePicker
32
33
34
              </div>
              <div :class="[prefixCls + '-content']">
                  <date-table
50637863   梁灏   update DatePicker
35
36
37
38
39
                      v-show="currentView === 'date'"
                      :year="year"
                      :month="month"
                      :date="date"
                      :value="value"
50637863   梁灏   update DatePicker
40
                      :selection-mode="selectionMode"
c46f385a   梁灏   update DatePicker
41
                      :disabled-date="disabledDate"
68e9b100   梁灏   update DatePicker
42
43
                      @on-pick="handleDatePick"
                      @on-pick-click="handlePickClick"></date-table>
0f677893   梁灏   update DatePicker
44
                  <year-table
c46f385a   梁灏   update DatePicker
45
46
47
48
                      v-ref:year-table
                      v-show="currentView === 'year'"
                      :year="year"
                      :date="date"
13be4434   梁灏   update DatePicker
49
                      :selection-mode="selectionMode"
c46f385a   梁灏   update DatePicker
50
                      :disabled-date="disabledDate"
68e9b100   梁灏   update DatePicker
51
52
                      @on-pick="handleYearPick"
                      @on-pick-click="handlePickClick"></year-table>
0f677893   梁灏   update DatePicker
53
                  <month-table
c46f385a   梁灏   update DatePicker
54
55
56
57
                      v-ref:month-table
                      v-show="currentView === 'month'"
                      :month="month"
                      :date="date"
13be4434   梁灏   update DatePicker
58
                      :selection-mode="selectionMode"
c46f385a   梁灏   update DatePicker
59
                      :disabled-date="disabledDate"
68e9b100   梁灏   update DatePicker
60
61
                      @on-pick="handleMonthPick"
                      @on-pick-click="handlePickClick"></month-table>
5cc9b892   梁灏   update DateTimePi...
62
                  <time-picker
5cc9b892   梁灏   update DateTimePi...
63
                      v-ref:time-picker
a8571a5f   梁灏   update DateTimePi...
64
                      show-date
2dc27713   梁灏   update DateTimePi...
65
                      v-show="currentView === 'time'"
a2a78c38   梁灏   update DateTimePi...
66
67
                      @on-pick="handleTimePick"
                      @on-pick-click="handlePickClick"></time-picker>
0f677893   梁灏   update DatePicker
68
              </div>
b9041a0d   梁灏   DatePicker add co...
69
70
              <Confirm
                  v-if="confirm"
5cc9b892   梁灏   update DateTimePi...
71
72
73
                  :show-time="showTime"
                  :is-time="isTime"
                  @on-pick-toggle-time="handleToggleTime"
b9041a0d   梁灏   DatePicker add co...
74
75
                  @on-pick-clear="handlePickClear"
                  @on-pick-success="handlePickSuccess"></Confirm>
0f677893   梁灏   update DatePicker
76
77
          </div>
      </div>
17e1fcf1   梁灏   init DatePicker
78
79
  </template>
  <script>
c46f385a   梁灏   update DatePicker
80
      import Icon from '../../icon/icon.vue';
0f677893   梁灏   update DatePicker
81
82
83
      import DateTable from '../base/date-table.vue';
      import YearTable from '../base/year-table.vue';
      import MonthTable from '../base/month-table.vue';
5cc9b892   梁灏   update DateTimePi...
84
      import TimePicker from './time.vue';
b9041a0d   梁灏   DatePicker add co...
85
      import Confirm from '../base/confirm.vue';
0f677893   梁灏   update DatePicker
86
  
3cf7cfd1   梁灏   update DatePicker
87
      import Mixin from './mixin';
4ab11811   梁灏   some component su...
88
      import Locale from '../../../mixins/locale';
3cf7cfd1   梁灏   update DatePicker
89
  
5cc9b892   梁灏   update DateTimePi...
90
91
      import { initTimeDate } from '../util';
  
0f677893   梁灏   update DatePicker
92
93
94
      const prefixCls = 'ivu-picker-panel';
      const datePrefixCls = 'ivu-date-picker';
  
17e1fcf1   梁灏   init DatePicker
95
      export default {
a2a78c38   梁灏   update DateTimePi...
96
          name: 'DatePicker',
4ab11811   梁灏   some component su...
97
          mixins: [ Mixin, Locale ],
5cc9b892   梁灏   update DateTimePi...
98
          components: { Icon, DateTable, YearTable, MonthTable, TimePicker, Confirm },
17e1fcf1   梁灏   init DatePicker
99
          data () {
0f677893   梁灏   update DatePicker
100
101
102
103
              return {
                  prefixCls: prefixCls,
                  datePrefixCls: datePrefixCls,
                  shortcuts: [],
50637863   梁灏   update DatePicker
104
                  currentView: 'date',
5cc9b892   梁灏   update DateTimePi...
105
                  date: initTimeDate(),
50637863   梁灏   update DatePicker
106
107
108
                  value: '',
                  showTime: false,
                  selectionMode: 'day',
50637863   梁灏   update DatePicker
109
110
111
                  disabledDate: '',
                  year: null,
                  month: null,
5cc9b892   梁灏   update DateTimePi...
112
                  confirm: false,
acde7262   梁灏   update DateTimePi...
113
114
                  isTime: false,
                  format: 'yyyy-MM-dd'
b0893113   jingsam   :art: add eslint
115
              };
50637863   梁灏   update DatePicker
116
117
          },
          computed: {
2533a192   梁灏   update DatePicker
118
119
120
121
122
123
              classes () {
                  return [
                      `${prefixCls}-body-wrapper`,
                      {
                          [`${prefixCls}-with-sidebar`]: this.shortcuts.length
                      }
b0893113   jingsam   :art: add eslint
124
                  ];
2533a192   梁灏   update DatePicker
125
              },
c46f385a   梁灏   update DatePicker
126
              yearLabel () {
4ab11811   梁灏   some component su...
127
                  const tYear = this.t('i.datepicker.year');
c46f385a   梁灏   update DatePicker
128
129
130
131
                  const year = this.year;
                  if (!year) return '';
                  if (this.currentView === 'year') {
                      const startYear = Math.floor(year / 10) * 10;
4ab11811   梁灏   some component su...
132
                      return `${startYear}${tYear} - ${startYear + 9}${tYear}`;
c46f385a   梁灏   update DatePicker
133
                  }
4ab11811   梁灏   some component su...
134
135
136
137
138
                  return `${year}${tYear}`;
              },
              monthLabel () {
                  const month = this.month + 1;
                  return this.t(`i.datepicker.month${month}`);
c46f385a   梁灏   update DatePicker
139
              }
50637863   梁灏   update DatePicker
140
141
142
          },
          watch: {
              value (newVal) {
c46f385a   梁灏   update DatePicker
143
                  if (!newVal) return;
50637863   梁灏   update DatePicker
144
145
                  newVal = new Date(newVal);
                  if (!isNaN(newVal)) {
50637863   梁灏   update DatePicker
146
147
148
                      this.date = newVal;
                      this.year = newVal.getFullYear();
                      this.month = newVal.getMonth();
50637863   梁灏   update DatePicker
149
                  }
36931442   梁灏   update DateTimePi...
150
151
152
153
154
155
156
                  if (this.showTime) this.$refs.timePicker.value = newVal;
              },
              date (val) {
                  if (this.showTime) this.$refs.timePicker.date = val;
              },
              format (val) {
                  if (this.showTime) this.$refs.timePicker.format = val;
5cc9b892   梁灏   update DateTimePi...
157
158
159
              },
              currentView (val) {
                  if (val === 'time') this.$refs.timePicker.updateScroll();
0f677893   梁灏   update DatePicker
160
              }
17e1fcf1   梁灏   init DatePicker
161
          },
0f677893   梁灏   update DatePicker
162
          methods: {
f92ef70f   梁灏   update DatePicker
163
164
165
              resetDate () {
                  this.date = new Date(this.date);
              },
5cc9b892   梁灏   update DateTimePi...
166
              handleClear () {
c46f385a   梁灏   update DatePicker
167
168
                  this.date = new Date();
                  this.$emit('on-pick', '');
5cc9b892   梁灏   update DateTimePi...
169
                  if (this.showTime) this.$refs.timePicker.handleClear();
c46f385a   梁灏   update DatePicker
170
              },
5cc9b892   梁灏   update DateTimePi...
171
172
173
174
175
176
177
178
179
              resetView (reset = false) {
                  if (this.currentView !== 'time' || reset) {
                      if (this.selectionMode === 'month') {
                          this.currentView = 'month';
                      } else if (this.selectionMode === 'year') {
                          this.currentView = 'year';
                      } else {
                          this.currentView = 'date';
                      }
0a5c5f41   梁灏   update DatePicker
180
181
182
183
                  }
  
                  this.year = this.date.getFullYear();
                  this.month = this.date.getMonth();
c46f385a   梁灏   update DatePicker
184
              },
0f677893   梁灏   update DatePicker
185
              prevYear () {
c46f385a   梁灏   update DatePicker
186
187
188
189
190
191
192
                  if (this.currentView === 'year') {
                      this.$refs.yearTable.prevTenYear();
                  } else {
                      this.year--;
                      this.date.setFullYear(this.year);
                      this.resetDate();
                  }
0f677893   梁灏   update DatePicker
193
194
              },
              nextYear () {
c46f385a   梁灏   update DatePicker
195
196
197
198
199
200
201
                  if (this.currentView === 'year') {
                      this.$refs.yearTable.nextTenYear();
                  } else {
                      this.year++;
                      this.date.setFullYear(this.year);
                      this.resetDate();
                  }
0f677893   梁灏   update DatePicker
202
203
              },
              prevMonth () {
c46f385a   梁灏   update DatePicker
204
205
206
207
208
                  this.month--;
                  if (this.month < 0) {
                      this.month = 11;
                      this.year--;
                  }
0f677893   梁灏   update DatePicker
209
210
              },
              nextMonth () {
c46f385a   梁灏   update DatePicker
211
212
213
214
215
                  this.month++;
                  if (this.month > 11) {
                      this.month = 0;
                      this.year++;
                  }
0f677893   梁灏   update DatePicker
216
217
              },
              showYearPicker () {
c46f385a   梁灏   update DatePicker
218
                  this.currentView = 'year';
0f677893   梁灏   update DatePicker
219
220
              },
              showMonthPicker () {
c46f385a   梁灏   update DatePicker
221
222
                  this.currentView = 'month';
              },
5cc9b892   梁灏   update DateTimePi...
223
224
225
226
227
228
229
230
231
              handleToggleTime () {
                  if (this.currentView === 'date') {
                      this.currentView = 'time';
                      this.isTime = true;
                  } else if (this.currentView === 'time') {
                      this.currentView = 'date';
                      this.isTime = false;
                  }
              },
c46f385a   梁灏   update DatePicker
232
233
234
              handleYearPick(year, close = true) {
                  this.year = year;
                  if (!close) return;
0f677893   梁灏   update DatePicker
235
  
c46f385a   梁灏   update DatePicker
236
237
                  this.date.setFullYear(year);
                  if (this.selectionMode === 'year') {
344131a7   梁灏   update DatePicker
238
                      this.$emit('on-pick', new Date(year, 0, 1));
c46f385a   梁灏   update DatePicker
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
                  } else {
                      this.currentView = 'month';
                  }
  
                  this.resetDate();
              },
              handleMonthPick (month) {
                  this.month = month;
                  const selectionMode = this.selectionMode;
                  if (selectionMode !== 'month') {
                      this.date.setMonth(month);
                      this.currentView = 'date';
                      this.resetDate();
                  } else {
                      this.date.setMonth(month);
                      this.year && this.date.setFullYear(this.year);
                      this.resetDate();
                      const value = new Date(this.date.getFullYear(), month, 1);
                      this.$emit('on-pick', value);
                  }
              },
              handleDatePick (value) {
                  if (this.selectionMode === 'day') {
5cc9b892   梁灏   update DateTimePi...
262
                      this.$emit('on-pick', new Date(value.getTime()));
c46f385a   梁灏   update DatePicker
263
264
265
266
267
268
                      this.date.setFullYear(value.getFullYear());
                      this.date.setMonth(value.getMonth());
                      this.date.setDate(value.getDate());
                  }
  
                  this.resetDate();
5cc9b892   梁灏   update DateTimePi...
269
270
271
              },
              handleTimePick (date) {
                  this.handleDatePick(date);
0f677893   梁灏   update DatePicker
272
273
              }
          },
50637863   梁灏   update DatePicker
274
275
276
277
278
279
280
281
282
          compiled () {
              if (this.selectionMode === 'month') {
                  this.currentView = 'month';
              }
  
              if (this.date && !this.year) {
                  this.year = this.date.getFullYear();
                  this.month = this.date.getMonth();
              }
36931442   梁灏   update DateTimePi...
283
              if (this.showTime) {
2dc27713   梁灏   update DateTimePi...
284
                  // todo 这里可能有问题,并不能进入到这里,但不影响正常使用
36931442   梁灏   update DateTimePi...
285
286
287
288
289
                  this.$refs.timePicker.date = this.date;
                  this.$refs.timePicker.value = this.value;
                  this.$refs.timePicker.format = this.format;
                  this.$refs.timePicker.showDate = true;
              }
0f677893   梁灏   update DatePicker
290
          }
b0893113   jingsam   :art: add eslint
291
292
      };
  </script>