Blame view

src/components/date-picker/panel/date-range.vue 14.1 KB
17e1fcf1   梁灏   init DatePicker
1
  <template>
e0cd7f90   梁灏   fixed #134
2
      <div :class="classes">
3cf7cfd1   梁灏   update DatePicker
3
4
5
6
7
8
9
10
          <div :class="[prefixCls + '-sidebar']" v-if="shortcuts.length">
              <div
                  :class="[prefixCls + '-shortcut']"
                  v-for="shortcut in shortcuts"
                  @click="handleShortcutClick(shortcut)">{{ shortcut.text }}</div>
          </div>
          <div :class="[prefixCls + '-body']">
              <div :class="[prefixCls + '-content', prefixCls + '-content-left']">
0a5c5f41   梁灏   update DatePicker
11
                  <div :class="[datePrefixCls + '-header']" v-show="leftCurrentView !== 'time'">
3cf7cfd1   梁灏   update DatePicker
12
13
                      <span
                          :class="iconBtnCls('prev', '-double')"
0a5c5f41   梁灏   update DatePicker
14
                          @click="prevYear('left')"><Icon type="ios-arrow-left"></Icon></span>
3cf7cfd1   梁灏   update DatePicker
15
16
17
                      <span
                          :class="iconBtnCls('prev')"
                          @click="prevMonth"
0a5c5f41   梁灏   update DatePicker
18
                          v-show="leftCurrentView === 'date'"><Icon type="ios-arrow-left"></Icon></span>
3cf7cfd1   梁灏   update DatePicker
19
20
                      <span
                          :class="[datePrefixCls + '-header-label']"
0a5c5f41   梁灏   update DatePicker
21
                          @click="showYearPicker('left')">{{ leftYearLabel }}</span>
3cf7cfd1   梁灏   update DatePicker
22
23
                      <span
                          :class="[datePrefixCls + '-header-label']"
0a5c5f41   梁灏   update DatePicker
24
25
26
27
28
29
                          @click="showMonthPicker('left')"
                          v-show="leftCurrentView === 'date'">{{ leftMonth + 1 }} 月</span>
                      <span
                          :class="iconBtnCls('next', '-double')"
                          @click="nextYear('left')"
                          v-show="leftCurrentView === 'year' || leftCurrentView === 'month'"><Icon type="ios-arrow-right"></Icon></span>
3cf7cfd1   梁灏   update DatePicker
30
31
                  </div>
                  <date-table
0a5c5f41   梁灏   update DatePicker
32
                      v-show="leftCurrentView === 'date'"
3cf7cfd1   梁灏   update DatePicker
33
34
35
36
37
38
                      :year="leftYear"
                      :month="leftMonth"
                      :date="date"
                      :min-date="minDate"
                      :max-date="maxDate"
                      :range-state="rangeState"
472b4ff1   梁灏   update DatePicker
39
                      selection-mode="range"
3cf7cfd1   梁灏   update DatePicker
40
                      :disabled-date="disabledDate"
472b4ff1   梁灏   update DatePicker
41
42
                      @on-changerange="handleChangeRange"
                      @on-pick="handleRangePick"></date-table>
0a5c5f41   梁灏   update DatePicker
43
44
45
                  <year-table
                      v-ref:left-year-table
                      v-show="leftCurrentView === 'year'"
3732707e   梁灏   update DatePicker
46
47
                      :year="leftTableYear"
                      :date="leftTableDate"
0a5c5f41   梁灏   update DatePicker
48
49
50
51
52
53
54
                      selection-mode="range"
                      :disabled-date="disabledDate"
                      @on-pick="handleLeftYearPick"></year-table>
                  <month-table
                      v-ref:left-month-table
                      v-show="leftCurrentView === 'month'"
                      :month="leftMonth"
3732707e   梁灏   update DatePicker
55
                      :date="leftTableDate"
0a5c5f41   梁灏   update DatePicker
56
57
58
                      selection-mode="range"
                      :disabled-date="disabledDate"
                      @on-pick="handleLeftMonthPick"></month-table>
3cf7cfd1   梁灏   update DatePicker
59
60
              </div>
              <div :class="[prefixCls + '-content', prefixCls + '-content-right']">
0a5c5f41   梁灏   update DatePicker
61
62
63
64
65
                  <div :class="[datePrefixCls + '-header']" v-show="rightCurrentView !== 'time'">
                       <span
                           :class="iconBtnCls('prev', '-double')"
                           @click="prevYear('right')"
                           v-show="rightCurrentView === 'year' || rightCurrentView === 'month'"><Icon type="ios-arrow-left"></Icon></span>
3cf7cfd1   梁灏   update DatePicker
66
67
                      <span
                          :class="[datePrefixCls + '-header-label']"
0a5c5f41   梁灏   update DatePicker
68
                          @click="showYearPicker('right')">{{ rightYearLabel }}</span>
3cf7cfd1   梁灏   update DatePicker
69
70
                      <span
                          :class="[datePrefixCls + '-header-label']"
0a5c5f41   梁灏   update DatePicker
71
72
                          @click="showMonthPicker('right')"
                          v-show="rightCurrentView === 'date'">{{ rightMonth + 1 }} 月</span>
3cf7cfd1   梁灏   update DatePicker
73
74
                      <span
                          :class="iconBtnCls('next', '-double')"
0a5c5f41   梁灏   update DatePicker
75
                          @click="nextYear('right')"><Icon type="ios-arrow-right"></Icon></span>
3cf7cfd1   梁灏   update DatePicker
76
77
78
                      <span
                          :class="iconBtnCls('next')"
                          @click="nextMonth"
0a5c5f41   梁灏   update DatePicker
79
                          v-show="rightCurrentView === 'date'"><Icon type="ios-arrow-right"></Icon></span>
3cf7cfd1   梁灏   update DatePicker
80
81
                  </div>
                  <date-table
0a5c5f41   梁灏   update DatePicker
82
                      v-show="rightCurrentView === 'date'"
3cf7cfd1   梁灏   update DatePicker
83
84
85
86
87
88
                      :year="rightYear"
                      :month="rightMonth"
                      :date="rightDate"
                      :min-date="minDate"
                      :max-date="maxDate"
                      :range-state="rangeState"
472b4ff1   梁灏   update DatePicker
89
                      selection-mode="range"
3cf7cfd1   梁灏   update DatePicker
90
                      :disabled-date="disabledDate"
472b4ff1   梁灏   update DatePicker
91
92
                      @on-changerange="handleChangeRange"
                      @on-pick="handleRangePick"></date-table>
0a5c5f41   梁灏   update DatePicker
93
94
95
                  <year-table
                      v-ref:right-year-table
                      v-show="rightCurrentView === 'year'"
3732707e   梁灏   update DatePicker
96
97
                      :year="rightTableYear"
                      :date="rightTableDate"
0a5c5f41   梁灏   update DatePicker
98
99
100
101
102
103
104
                      selection-mode="range"
                      :disabled-date="disabledDate"
                      @on-pick="handleRightYearPick"></year-table>
                  <month-table
                      v-ref:right-month-table
                      v-show="rightCurrentView === 'month'"
                      :month="rightMonth"
3732707e   梁灏   update DatePicker
105
                      :date="rightTableDate"
0a5c5f41   梁灏   update DatePicker
106
107
108
                      selection-mode="range"
                      :disabled-date="disabledDate"
                      @on-pick="handleRightMonthPick"></month-table>
3cf7cfd1   梁灏   update DatePicker
109
              </div>
b9041a0d   梁灏   DatePicker add co...
110
111
112
113
              <Confirm
                  v-if="confirm"
                  @on-pick-clear="handlePickClear"
                  @on-pick-success="handlePickSuccess"></Confirm>
3cf7cfd1   梁灏   update DatePicker
114
          </div>
e0cd7f90   梁灏   fixed #134
115
      </div>
17e1fcf1   梁灏   init DatePicker
116
117
  </template>
  <script>
3cf7cfd1   梁灏   update DatePicker
118
119
      import Icon from '../../icon/icon.vue';
      import DateTable from '../base/date-table.vue';
0a5c5f41   梁灏   update DatePicker
120
121
      import YearTable from '../base/year-table.vue';
      import MonthTable from '../base/month-table.vue';
b9041a0d   梁灏   DatePicker add co...
122
      import Confirm from '../base/confirm.vue';
0a5c5f41   梁灏   update DatePicker
123
      import { toDate, prevMonth, nextMonth } from '../util';
3cf7cfd1   梁灏   update DatePicker
124
125
126
  
      import Mixin from './mixin';
  
e0cd7f90   梁灏   fixed #134
127
128
129
      const prefixCls = 'ivu-picker-panel';
      const datePrefixCls = 'ivu-date-picker';
  
17e1fcf1   梁灏   init DatePicker
130
      export default {
3cf7cfd1   梁灏   update DatePicker
131
          mixins: [Mixin],
b9041a0d   梁灏   DatePicker add co...
132
          components: { Icon, DateTable, YearTable, MonthTable, Confirm },
17e1fcf1   梁灏   init DatePicker
133
          data () {
3cf7cfd1   梁灏   update DatePicker
134
135
136
137
138
139
140
141
              return {
                  prefixCls: prefixCls,
                  datePrefixCls: datePrefixCls,
                  shortcuts: [],
                  date: new Date(),
                  value: '',
                  minDate: '',
                  maxDate: '',
b9041a0d   梁灏   DatePicker add co...
142
                  confirm: false,
3cf7cfd1   梁灏   update DatePicker
143
144
145
146
147
148
                  rangeState: {
                      endDate: null,
                      selecting: false
                  },
                  showTime: false,
                  disabledDate: '',
0a5c5f41   梁灏   update DatePicker
149
150
                  leftCurrentView: 'date',
                  rightCurrentView: 'date',
3732707e   梁灏   update DatePicker
151
152
153
                  selectionMode: 'range',
                  leftTableYear: null,
                  rightTableYear: null
3cf7cfd1   梁灏   update DatePicker
154
              }
17e1fcf1   梁灏   init DatePicker
155
          },
e0cd7f90   梁灏   fixed #134
156
157
158
159
          computed: {
              classes () {
                  return [
                      `${prefixCls}-body-wrapper`,
3cf7cfd1   梁灏   update DatePicker
160
                      `${datePrefixCls}-with-range`,
e0cd7f90   梁灏   fixed #134
161
162
163
164
                      {
                          [`${prefixCls}-with-sidebar`]: this.shortcuts.length
                      }
                  ]
3cf7cfd1   梁灏   update DatePicker
165
              },
3732707e   梁灏   update DatePicker
166
              leftYear () {
3cf7cfd1   梁灏   update DatePicker
167
168
                  return this.date.getFullYear();
              },
3732707e   梁灏   update DatePicker
169
170
171
172
173
174
175
              leftTableDate () {
                  if (this.leftCurrentView === 'year' || this.leftCurrentView === 'month') {
                      return new Date(this.leftTableYear);
                  } else {
                      return this.date;
                  }
              },
0a5c5f41   梁灏   update DatePicker
176
              leftYearLabel () {
0a5c5f41   梁灏   update DatePicker
177
                  if (this.leftCurrentView === 'year') {
3732707e   梁灏   update DatePicker
178
179
                      const year = this.leftTableYear;
                      if (!year) return '';
0a5c5f41   梁灏   update DatePicker
180
181
                      const startYear = Math.floor(year / 10) * 10;
                      return `${startYear}年 - ${startYear + 9}年`;
3732707e   梁灏   update DatePicker
182
183
184
185
                  } else {
                      const year = this.leftCurrentView === 'month' ? this.leftTableYear : this.leftYear;
                      if (!year) return '';
                      return `${year}年`;
0a5c5f41   梁灏   update DatePicker
186
                  }
0a5c5f41   梁灏   update DatePicker
187
              },
3732707e   梁灏   update DatePicker
188
              leftMonth () {
3cf7cfd1   梁灏   update DatePicker
189
190
                  return this.date.getMonth();
              },
3732707e   梁灏   update DatePicker
191
              rightYear () {
3cf7cfd1   梁灏   update DatePicker
192
193
                  return this.rightDate.getFullYear();
              },
3732707e   梁灏   update DatePicker
194
195
196
197
198
199
200
              rightTableDate () {
                  if (this.rightCurrentView === 'year' || this.rightCurrentView === 'month') {
                      return new Date(this.rightTableYear);
                  } else {
                      return this.date;
                  }
              },
0a5c5f41   梁灏   update DatePicker
201
              rightYearLabel () {
0a5c5f41   梁灏   update DatePicker
202
                  if (this.rightCurrentView === 'year') {
3732707e   梁灏   update DatePicker
203
204
                      const year = this.rightTableYear;
                      if (!year) return '';
0a5c5f41   梁灏   update DatePicker
205
206
                      const startYear = Math.floor(year / 10) * 10;
                      return `${startYear}年 - ${startYear + 9}年`;
3732707e   梁灏   update DatePicker
207
208
209
210
                  } else {
                      const year = this.rightCurrentView === 'month' ? this.rightTableYear : this.rightYear;
                      if (!year) return '';
                      return `${year}年`;
0a5c5f41   梁灏   update DatePicker
211
                  }
0a5c5f41   梁灏   update DatePicker
212
              },
3732707e   梁灏   update DatePicker
213
              rightMonth () {
3cf7cfd1   梁灏   update DatePicker
214
215
                  return this.rightDate.getMonth();
              },
3732707e   梁灏   update DatePicker
216
              rightDate () {
3cf7cfd1   梁灏   update DatePicker
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
                  const newDate = new Date(this.date);
                  const month = newDate.getMonth();
                  newDate.setDate(1);
  
                  if (month === 11) {
                      newDate.setFullYear(newDate.getFullYear() + 1);
                      newDate.setMonth(0);
                  } else {
                      newDate.setMonth(month + 1);
                  }
                  return newDate;
              }
          },
          watch: {
              value(newVal) {
                  if (!newVal) {
                      this.minDate = null;
                      this.maxDate = null;
                  } else if (Array.isArray(newVal)) {
                      this.minDate = newVal[0] ? toDate(newVal[0]) : null;
                      this.maxDate = newVal[1] ? toDate(newVal[1]) : null;
                      if (this.minDate) this.date = new Date(this.minDate);
3cf7cfd1   梁灏   update DatePicker
239
                  }
e0cd7f90   梁灏   fixed #134
240
241
              }
          },
3cf7cfd1   梁灏   update DatePicker
242
          methods: {
f92ef70f   梁灏   update DatePicker
243
244
245
246
247
              resetDate () {
                  this.date = new Date(this.date);
                  this.leftTableYear = this.date.getFullYear();
                  this.rightTableYear = this.rightDate.getFullYear();
              },
472b4ff1   梁灏   update DatePicker
248
249
250
              handleClear() {
                  this.minDate = null;
                  this.maxDate = null;
f92ef70f   梁灏   update DatePicker
251
                  this.date = new Date();
472b4ff1   梁灏   update DatePicker
252
253
                  this.handleConfirm();
              },
f92ef70f   梁灏   update DatePicker
254
255
256
257
258
259
260
              resetView() {
                  this.leftCurrentView = 'date';
                  this.rightCurrentView = 'date';
  
                  this.leftTableYear = this.leftYear;
                  this.rightTableYear = this.rightYear;
              },
0a5c5f41   梁灏   update DatePicker
261
262
263
              prevYear (direction) {
                  if (this[`${direction}CurrentView`] === 'year') {
                      this.$refs[`${direction}YearTable`].prevTenYear();
f92ef70f   梁灏   update DatePicker
264
265
                  } else if (this[`${direction}CurrentView`] === 'month') {
                      this[`${direction}TableYear`]--;
0a5c5f41   梁灏   update DatePicker
266
267
268
269
270
                  } else {
                      const date = this.date;
                      date.setFullYear(date.getFullYear() - 1);
                      this.resetDate();
                  }
3cf7cfd1   梁灏   update DatePicker
271
              },
0a5c5f41   梁灏   update DatePicker
272
273
274
              nextYear (direction) {
                  if (this[`${direction}CurrentView`] === 'year') {
                      this.$refs[`${direction}YearTable`].nextTenYear();
f92ef70f   梁灏   update DatePicker
275
276
                  } else if (this[`${direction}CurrentView`] === 'month') {
                      this[`${direction}TableYear`]--;
0a5c5f41   梁灏   update DatePicker
277
278
279
280
281
                  } else {
                      const date = this.date;
                      date.setFullYear(date.getFullYear() + 1);
                      this.resetDate();
                  }
3cf7cfd1   梁灏   update DatePicker
282
283
              },
              prevMonth () {
0a5c5f41   梁灏   update DatePicker
284
                  this.date = prevMonth(this.date);
3cf7cfd1   梁灏   update DatePicker
285
286
              },
              nextMonth () {
0a5c5f41   梁灏   update DatePicker
287
                  this.date = nextMonth(this.date);
3cf7cfd1   梁灏   update DatePicker
288
              },
0a5c5f41   梁灏   update DatePicker
289
290
291
292
293
              handleLeftYearPick (year, close = true) {
                  this.handleYearPick(year, close, 'left');
              },
              handleRightYearPick (year, close = true) {
                  this.handleYearPick(year, close, 'right');
3cf7cfd1   梁灏   update DatePicker
294
              },
0a5c5f41   梁灏   update DatePicker
295
              handleYearPick (year, close, direction) {
3732707e   梁灏   update DatePicker
296
                  this[`${direction}TableYear`] = year;
0a5c5f41   梁灏   update DatePicker
297
                  if (!close) return;
3cf7cfd1   梁灏   update DatePicker
298
  
0a5c5f41   梁灏   update DatePicker
299
300
301
302
303
304
305
306
307
                  this[`${direction}CurrentView`] = 'month';
              },
              handleLeftMonthPick (month) {
                  this.handleMonthPick(month, 'left');
              },
              handleRightMonthPick (month) {
                  this.handleMonthPick(month, 'right');
              },
              handleMonthPick (month, direction) {
3732707e   梁灏   update DatePicker
308
309
310
311
312
313
314
315
316
317
318
                  let year = this[`${direction}TableYear`];
                  if (direction === 'right') {
                      if (month === 0) {
                          month = 11;
                          year--;
                      } else {
                          month--;
                      }
                  }
  
                  this.date.setYear(year);
0a5c5f41   梁灏   update DatePicker
319
320
321
322
323
324
                  this.date.setMonth(month);
                  this[`${direction}CurrentView`] = 'date';
                  this.resetDate();
              },
              showYearPicker (direction) {
                  this[`${direction}CurrentView`] = 'year';
3732707e   梁灏   update DatePicker
325
                  this[`${direction}TableYear`] = this[`${direction}Year`];
0a5c5f41   梁灏   update DatePicker
326
327
328
              },
              showMonthPicker (direction) {
                  this[`${direction}CurrentView`] = 'month';
3cf7cfd1   梁灏   update DatePicker
329
              },
3cf7cfd1   梁灏   update DatePicker
330
331
              handleConfirm(visible) {
                  this.$emit('on-pick', [this.minDate, this.maxDate], visible);
472b4ff1   梁灏   update DatePicker
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
              },
              handleRangePick (val, close = true) {
                  if (this.maxDate === val.maxDate && this.minDate === val.minDate) return;
  
                  this.minDate = val.minDate;
                  this.maxDate = val.maxDate;
  
                  if (!close) return;
                  if (!this.showTime) {
                      this.handleConfirm(false);
                  }
              },
              handleChangeRange (val) {
                  this.minDate = val.minDate;
                  this.maxDate = val.maxDate;
                  this.rangeState = val.rangeState;
3cf7cfd1   梁灏   update DatePicker
348
349
              }
          }
17e1fcf1   梁灏   init DatePicker
350
351
      }
  </script>