Blame view

src/components/date-picker/panel/Date/date-range.vue 16.5 KB
c4e3fe33   Sergio Crisostomo   move date-range.v...
1
2
3
4
5
6
7
8
  <template>
      <div :class="classes" @mousedown.prevent>
          <div :class="[prefixCls + '-sidebar']" v-if="shortcuts.length">
              <div
                  :class="[prefixCls + '-shortcut']"
                  v-for="shortcut in shortcuts"
                  @click="handleShortcutClick(shortcut)">{{ shortcut.text }}</div>
          </div>
e56c394d   Sergio Crisostomo   Correct time layo...
9
          <div :class="panelBodyClasses">
c4e3fe33   Sergio Crisostomo   move date-range.v...
10
11
12
13
              <div :class="[prefixCls + '-content', prefixCls + '-content-left']" v-show="!isTime">
                  <div :class="[datePrefixCls + '-header']" v-show="currentView !== 'time'">
                      <span
                          :class="iconBtnCls('prev', '-double')"
0f512d6a   梁灏   update Date Icons
14
                          @click="prevYear('left')"><Icon type="ios-arrow-back"></Icon></span>
c4e3fe33   Sergio Crisostomo   move date-range.v...
15
                      <span
abb0cc87   Sergio Crisostomo   Correct arrows sh...
16
                          v-if="leftPickerTable === 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
17
18
                          :class="iconBtnCls('prev')"
                          @click="prevMonth('left')"
0f512d6a   梁灏   update Date Icons
19
                          v-show="currentView === 'date'"><Icon type="ios-arrow-back"></Icon></span>
c4e3fe33   Sergio Crisostomo   move date-range.v...
20
21
                      <date-panel-label
                          :date-panel-label="leftDatePanelLabel"
73a34dfa   Sergio Crisostomo   Fix year/month pr...
22
                          :current-view="leftDatePanelView"
c4e3fe33   Sergio Crisostomo   move date-range.v...
23
24
                          :date-prefix-cls="datePrefixCls"></date-panel-label>
                      <span
73a34dfa   Sergio Crisostomo   Fix year/month pr...
25
                          v-if="splitPanels || leftPickerTable !== 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
26
                          :class="iconBtnCls('next', '-double')"
0f512d6a   梁灏   update Date Icons
27
                          @click="nextYear('left')"><Icon type="ios-arrow-forward"></Icon></span>
c4e3fe33   Sergio Crisostomo   move date-range.v...
28
                      <span
abb0cc87   Sergio Crisostomo   Correct arrows sh...
29
                          v-if="splitPanels && leftPickerTable === 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
30
31
                          :class="iconBtnCls('next')"
                          @click="nextMonth('left')"
0f512d6a   梁灏   update Date Icons
32
                          v-show="currentView === 'date'"><Icon type="ios-arrow-forward"></Icon></span>
c4e3fe33   Sergio Crisostomo   move date-range.v...
33
34
                  </div>
                  <component
b52e02e4   Sergio Crisostomo   Fix month|year pr...
35
                      :is="leftPickerTable"
c4e3fe33   Sergio Crisostomo   move date-range.v...
36
37
38
39
40
41
                      ref="leftYearTable"
                      v-if="currentView !== 'time'"
                      :table-date="leftPanelDate"
                      selection-mode="range"
                      :disabled-date="disabledDate"
                      :range-state="rangeState"
e55ba7a2   Sergio Crisostomo   Add week numbers
42
                      :show-week-numbers="showWeekNumbers"
ebf1f86b   Sergio Crisostomo   show only 1 date ...
43
                      :value="preSelecting.left ? [dates[0]] : dates"
75cb2998   Sergio Crisostomo   Add keyboard navi...
44
45
                      :focused-date="focusedDate"
  
c4e3fe33   Sergio Crisostomo   move date-range.v...
46
                      @on-change-range="handleChangeRange"
b52e02e4   Sergio Crisostomo   Fix month|year pr...
47
                      @on-pick="panelPickerHandlers.left"
c4e3fe33   Sergio Crisostomo   move date-range.v...
48
49
50
51
52
53
                      @on-pick-click="handlePickClick"
                  ></component>
              </div>
              <div :class="[prefixCls + '-content', prefixCls + '-content-right']" v-show="!isTime">
                  <div :class="[datePrefixCls + '-header']" v-show="currentView !== 'time'">
                      <span
73a34dfa   Sergio Crisostomo   Fix year/month pr...
54
                          v-if="splitPanels || rightPickerTable !== 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
55
                          :class="iconBtnCls('prev', '-double')"
0f512d6a   梁灏   update Date Icons
56
                          @click="prevYear('right')"><Icon type="ios-arrow-back"></Icon></span>
c4e3fe33   Sergio Crisostomo   move date-range.v...
57
                      <span
abb0cc87   Sergio Crisostomo   Correct arrows sh...
58
                          v-if="splitPanels && rightPickerTable === 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
59
60
                          :class="iconBtnCls('prev')"
                          @click="prevMonth('right')"
0f512d6a   梁灏   update Date Icons
61
                          v-show="currentView === 'date'"><Icon type="ios-arrow-back"></Icon></span>
c4e3fe33   Sergio Crisostomo   move date-range.v...
62
63
                      <date-panel-label
                          :date-panel-label="rightDatePanelLabel"
73a34dfa   Sergio Crisostomo   Fix year/month pr...
64
                          :current-view="rightDatePanelView"
c4e3fe33   Sergio Crisostomo   move date-range.v...
65
66
67
                          :date-prefix-cls="datePrefixCls"></date-panel-label>
                      <span
                          :class="iconBtnCls('next', '-double')"
0f512d6a   梁灏   update Date Icons
68
                          @click="nextYear('right')"><Icon type="ios-arrow-forward"></Icon></span>
c4e3fe33   Sergio Crisostomo   move date-range.v...
69
                      <span
abb0cc87   Sergio Crisostomo   Correct arrows sh...
70
                          v-if="rightPickerTable === 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
71
72
                          :class="iconBtnCls('next')"
                          @click="nextMonth('right')"
0f512d6a   梁灏   update Date Icons
73
                          v-show="currentView === 'date'"><Icon type="ios-arrow-forward"></Icon></span>
c4e3fe33   Sergio Crisostomo   move date-range.v...
74
75
                  </div>
                  <component
b52e02e4   Sergio Crisostomo   Fix month|year pr...
76
                      :is="rightPickerTable"
c4e3fe33   Sergio Crisostomo   move date-range.v...
77
78
79
80
81
82
                      ref="rightYearTable"
                      v-if="currentView !== 'time'"
                      :table-date="rightPanelDate"
                      selection-mode="range"
                      :range-state="rangeState"
                      :disabled-date="disabledDate"
e55ba7a2   Sergio Crisostomo   Add week numbers
83
                      :show-week-numbers="showWeekNumbers"
ebf1f86b   Sergio Crisostomo   show only 1 date ...
84
                      :value="preSelecting.right ? [dates[dates.length - 1]] : dates"
75cb2998   Sergio Crisostomo   Add keyboard navi...
85
86
                      :focused-date="focusedDate"
  
c4e3fe33   Sergio Crisostomo   move date-range.v...
87
                      @on-change-range="handleChangeRange"
b52e02e4   Sergio Crisostomo   Fix month|year pr...
88
                      @on-pick="panelPickerHandlers.right"
c4e3fe33   Sergio Crisostomo   move date-range.v...
89
90
91
92
93
94
95
96
97
                      @on-pick-click="handlePickClick"></component>
              </div>
              <div :class="[prefixCls + '-content']" v-show="isTime">
                  <time-picker
                      ref="timePicker"
                      v-if="currentView === 'time'"
                      :value="dates"
                      :format="format"
                      :time-disabled="timeDisabled"
79ac2457   Sergio Crisostomo   Allow DatePicker ...
98
                      v-bind="timePickerOptions"
c4e3fe33   Sergio Crisostomo   move date-range.v...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
                      @on-pick="handleRangePick"
                      @on-pick-click="handlePickClick"
                      @on-pick-clear="handlePickClear"
                      @on-pick-success="handlePickSuccess"
                      @on-pick-toggle-time="handleToggleTime"
                  ></time-picker>
              </div>
              <Confirm
                  v-if="confirm"
                  :show-time="showTime"
                  :is-time="isTime"
                  :time-disabled="timeDisabled"
                  @on-pick-toggle-time="handleToggleTime"
                  @on-pick-clear="handlePickClear"
                  @on-pick-success="handlePickSuccess"></Confirm>
          </div>
      </div>
  </template>
  <script>
      import Icon from '../../../icon/icon.vue';
      import DateTable from '../../base/date-table.vue';
      import YearTable from '../../base/year-table.vue';
      import MonthTable from '../../base/month-table.vue';
      import TimePicker from '../Time/time-range.vue';
      import Confirm from '../../base/confirm.vue';
  
      import { toDate, initTimeDate, formatDateLabels } from '../../util';
      import datePanelLabel from './date-panel-label.vue';
  
      import Mixin from '../panel-mixin';
      import DateMixin from './date-panel-mixin';
      import Locale from '../../../../mixins/locale';
  
      const prefixCls = 'ivu-picker-panel';
      const datePrefixCls = 'ivu-date-picker';
  
5092777d   Sergio Crisostomo   Fix sort of date ...
135
136
137
138
      const dateSorter = (a, b) => {
          if (!a || !b) return 0;
          return a.getTime() - b.getTime();
      };
c4e3fe33   Sergio Crisostomo   move date-range.v...
139
140
141
142
143
144
  
      export default {
          name: 'RangeDatePickerPanel',
          mixins: [ Mixin, Locale, DateMixin ],
          components: { Icon, DateTable, YearTable, MonthTable, TimePicker, Confirm, datePanelLabel },
          props: {
435bf781   Sergio Crisostomo   add split panel p...
145
146
147
148
149
              // more props in the mixin
              splitPanels: {
                  type: Boolean,
                  default: false
              },
c4e3fe33   Sergio Crisostomo   move date-range.v...
150
151
152
          },
          data(){
              const [minDate, maxDate] = this.value.map(date => date || initTimeDate());
63bd0f7d   Sergio Crisostomo   Add start-date pr...
153
154
              const leftPanelDate = this.startDate ? this.startDate : minDate;
  
c4e3fe33   Sergio Crisostomo   move date-range.v...
155
156
157
158
159
160
              return {
                  prefixCls: prefixCls,
                  datePrefixCls: datePrefixCls,
                  dates: this.value,
                  rangeState: {from: this.value[0], to: this.value[1], selecting: minDate && !maxDate},
                  currentView: this.selectionMode || 'range',
b52e02e4   Sergio Crisostomo   Fix month|year pr...
161
162
                  leftPickerTable: `${this.selectionMode}-table`,
                  rightPickerTable: `${this.selectionMode}-table`,
63bd0f7d   Sergio Crisostomo   Add start-date pr...
163
                  leftPanelDate: leftPanelDate,
abf60d81   Sergio Crisostomo   Use first day of ...
164
                  rightPanelDate: new Date(leftPanelDate.getFullYear(), leftPanelDate.getMonth() + 1, 1)
c4e3fe33   Sergio Crisostomo   move date-range.v...
165
166
167
168
169
170
171
172
              };
          },
          computed: {
              classes(){
                  return [
                      `${prefixCls}-body-wrapper`,
                      `${datePrefixCls}-with-range`,
                      {
f8bba1ac   梁灏   fixed DatePicker ...
173
174
                          [`${prefixCls}-with-sidebar`]: this.shortcuts.length,
                          [`${datePrefixCls}-with-week-numbers`]: this.showWeekNumbers
c4e3fe33   Sergio Crisostomo   move date-range.v...
175
176
177
                      }
                  ];
              },
e56c394d   Sergio Crisostomo   Correct time layo...
178
179
180
181
182
183
184
              panelBodyClasses(){
                  return [
                      prefixCls + '-body',
                      {
                          [prefixCls + '-body-time']: this.showTime,
                          [prefixCls + '-body-date']: !this.showTime,
                      }
75cb2998   Sergio Crisostomo   Add keyboard navi...
185
                  ];
e56c394d   Sergio Crisostomo   Correct time layo...
186
              },
c4e3fe33   Sergio Crisostomo   move date-range.v...
187
188
189
190
191
192
              leftDatePanelLabel(){
                  return this.panelLabelConfig('left');
              },
              rightDatePanelLabel(){
                  return this.panelLabelConfig('right');
              },
73a34dfa   Sergio Crisostomo   Fix year/month pr...
193
194
195
196
197
198
              leftDatePanelView(){
                  return this.leftPickerTable.split('-').shift();
              },
              rightDatePanelView(){
                  return this.rightPickerTable.split('-').shift();
              },
c4e3fe33   Sergio Crisostomo   move date-range.v...
199
200
              timeDisabled(){
                  return !(this.dates[0] && this.dates[1]);
b52e02e4   Sergio Crisostomo   Fix month|year pr...
201
              },
ebf1f86b   Sergio Crisostomo   show only 1 date ...
202
              preSelecting(){
b52e02e4   Sergio Crisostomo   Fix month|year pr...
203
                  const tableType = `${this.currentView}-table`;
ebf1f86b   Sergio Crisostomo   show only 1 date ...
204
205
206
207
208
209
210
  
                  return {
                      left: this.leftPickerTable !== tableType,
                      right: this.rightPickerTable !== tableType,
                  };
              },
              panelPickerHandlers(){
b52e02e4   Sergio Crisostomo   Fix month|year pr...
211
                  return {
ebf1f86b   Sergio Crisostomo   show only 1 date ...
212
213
                      left: this.preSelecting.left ? this.handlePreSelection.bind(this, 'left') : this.handleRangePick,
                      right: this.preSelecting.right ? this.handlePreSelection.bind(this, 'right') : this.handleRangePick,
5092777d   Sergio Crisostomo   Fix sort of date ...
214
                  };
c4e3fe33   Sergio Crisostomo   move date-range.v...
215
216
217
218
219
220
              }
          },
          watch: {
              value(newVal) {
                  const minDate = newVal[0] ? toDate(newVal[0]) : null;
                  const maxDate = newVal[1] ? toDate(newVal[1]) : null;
5092777d   Sergio Crisostomo   Fix sort of date ...
221
                  this.dates = [minDate, maxDate].sort(dateSorter);
15457562   Sergio Crisostomo   Reset panel date ...
222
  
c4e3fe33   Sergio Crisostomo   move date-range.v...
223
                  this.rangeState = {
15457562   Sergio Crisostomo   Reset panel date ...
224
225
                      from: this.dates[0],
                      to: this.dates[1],
c4e3fe33   Sergio Crisostomo   move date-range.v...
226
227
                      selecting: false
                  };
c2b7fed0   Sergio Crisostomo   Reset panel selec...
228
229
230
  
  
                  // set panels positioning
75cb2998   Sergio Crisostomo   Add keyboard navi...
231
                  this.setPanelDates(this.startDate || this.dates[0] || new Date());
c4e3fe33   Sergio Crisostomo   move date-range.v...
232
233
234
235
236
237
              },
              currentView(currentView){
                  const leftMonth = this.leftPanelDate.getMonth();
                  const rightMonth = this.rightPanelDate.getMonth();
                  const isSameYear = this.leftPanelDate.getFullYear() === this.rightPanelDate.getFullYear();
  
c4e3fe33   Sergio Crisostomo   move date-range.v...
238
239
240
241
242
243
244
245
246
                  if (currentView === 'date' && isSameYear && leftMonth === rightMonth){
                      this.changePanelDate('right', 'Month', 1);
                  }
                  if (currentView === 'month' && isSameYear){
                      this.changePanelDate('right', 'FullYear', 1);
                  }
                  if (currentView === 'year' && isSameYear){
                      this.changePanelDate('right', 'FullYear', 10);
                  }
c2b7fed0   Sergio Crisostomo   Reset panel selec...
247
248
              },
              selectionMode(type){
c2b7fed0   Sergio Crisostomo   Reset panel selec...
249
                  this.currentView = type || 'range';
75cb2998   Sergio Crisostomo   Add keyboard navi...
250
251
252
              },
              focusedDate(date){
                  this.setPanelDates(date || new Date());
c4e3fe33   Sergio Crisostomo   move date-range.v...
253
254
255
              }
          },
          methods: {
46726afd   Sergio Crisostomo   Fix panels reset ...
256
257
258
259
260
              reset(){
                  this.currentView = this.selectionMode;
                  this.leftPickerTable = `${this.currentView}-table`;
                  this.rightPickerTable = `${this.currentView}-table`;
              },
75cb2998   Sergio Crisostomo   Add keyboard navi...
261
262
              setPanelDates(leftPanelDate){
                  this.leftPanelDate = leftPanelDate;
226b084c   SergioCrisostomo   Use first of mont...
263
                  const rightPanelDate = new Date(leftPanelDate.getFullYear(), leftPanelDate.getMonth() + 1, 1);
3a9e8865   梁灏   #4524
264
                  const splitRightPanelDate = this.dates[1]? this.dates[1].getTime() : this.dates[1];
12e19b82   梁灏   fixed #4524
265
                  this.rightPanelDate = this.splitPanels ? new Date(Math.max(splitRightPanelDate, rightPanelDate.getTime())) : rightPanelDate;
75cb2998   Sergio Crisostomo   Add keyboard navi...
266
              },
c4e3fe33   Sergio Crisostomo   move date-range.v...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
              panelLabelConfig (direction) {
                  const locale = this.t('i.locale');
                  const datePanelLabel = this.t('i.datepicker.datePanelLabel');
                  const handler = type => {
                      const fn = type == 'month' ? this.showMonthPicker : this.showYearPicker;
                      return () => fn(direction);
                  };
  
                  const date = this[`${direction}PanelDate`];
                  const { labels, separator } = formatDateLabels(locale, datePanelLabel, date);
  
                  return {
                      separator: separator,
                      labels: labels.map(obj => ((obj.handler = handler(obj.type)), obj))
                  };
              },
77e43f2b   Sergio Crisostomo   Correct year date...
283
              prevYear (panel) {
77e43f2b   Sergio Crisostomo   Correct year date...
284
285
                  const increment = this.currentView === 'year' ? -10 : -1;
                  this.changePanelDate(panel, 'FullYear', increment);
c4e3fe33   Sergio Crisostomo   move date-range.v...
286
              },
77e43f2b   Sergio Crisostomo   Correct year date...
287
288
289
              nextYear (panel) {
                  const increment = this.currentView === 'year' ? 10 : 1;
                  this.changePanelDate(panel, 'FullYear', increment);
c4e3fe33   Sergio Crisostomo   move date-range.v...
290
              },
77e43f2b   Sergio Crisostomo   Correct year date...
291
292
              prevMonth(panel){
                  this.changePanelDate(panel, 'Month', -1);
c4e3fe33   Sergio Crisostomo   move date-range.v...
293
              },
77e43f2b   Sergio Crisostomo   Correct year date...
294
295
              nextMonth(panel){
                  this.changePanelDate(panel, 'Month', 1);
c4e3fe33   Sergio Crisostomo   move date-range.v...
296
              },
6d8c5666   Sergio Crisostomo   Correct logic for...
297
              changePanelDate(panel, type, increment, updateOtherPanel = true){
c4e3fe33   Sergio Crisostomo   move date-range.v...
298
299
300
301
                  const current = new Date(this[`${panel}PanelDate`]);
                  current[`set${type}`](current[`get${type}`]() + increment);
                  this[`${panel}PanelDate`] = current;
  
6d8c5666   Sergio Crisostomo   Correct logic for...
302
                  if (!updateOtherPanel) return;
77e43f2b   Sergio Crisostomo   Correct year date...
303
304
305
306
307
308
309
310
311
312
313
314
315
  
                  if (this.splitPanels){
                      // change other panel if dates overlap
                      const otherPanel = panel === 'left' ? 'right' : 'left';
                      if (panel === 'left' && this.leftPanelDate >= this.rightPanelDate){
                          this.changePanelDate(otherPanel, type, 1);
                      }
                      if (panel === 'right' && this.rightPanelDate <= this.leftPanelDate){
                          this.changePanelDate(otherPanel, type, -1);
                      }
                  } else {
                      // keep the panels together
                      const otherPanel = panel === 'left' ? 'right' : 'left';
3aeb2bc6   SergioCrisostomo   Correct month las...
316
317
318
319
320
321
322
323
324
325
326
327
                      const currentDate = this[`${otherPanel}PanelDate`];
                      const temp = new Date(currentDate);
  
                      if (type === 'Month') {
                          const nextMonthLastDate = new Date(
                              temp.getFullYear(), temp.getMonth() + increment + 1, 0
                          ).getDate();
                          temp.setDate(Math.min(nextMonthLastDate, temp.getDate()));
                      }
  
                      temp[`set${type}`](temp[`get${type}`]() + increment);
                      this[`${otherPanel}PanelDate`] = temp;
c4e3fe33   Sergio Crisostomo   move date-range.v...
328
329
                  }
              },
b52e02e4   Sergio Crisostomo   Fix month|year pr...
330
331
332
333
334
              showYearPicker (panel) {
                  this[`${panel}PickerTable`] = 'year-table';
              },
              showMonthPicker (panel) {
                  this[`${panel}PickerTable`] = 'month-table';
c4e3fe33   Sergio Crisostomo   move date-range.v...
335
              },
b52e02e4   Sergio Crisostomo   Fix month|year pr...
336
337
              handlePreSelection(panel, value){
                  this[`${panel}PanelDate`] = value;
73a34dfa   Sergio Crisostomo   Fix year/month pr...
338
339
340
341
342
343
                  const currentViewType = this[`${panel}PickerTable`];
                  if (currentViewType === 'year-table') this[`${panel}PickerTable`] = 'month-table';
                  else this[`${panel}PickerTable`] = `${this.currentView}-table`;
  
                  if (!this.splitPanels){
                      const otherPanel = panel === 'left' ? 'right' : 'left';
73a34dfa   Sergio Crisostomo   Fix year/month pr...
344
                      this[`${otherPanel}PanelDate`] = value;
623b9dde   梁灏   fixed #3973
345
346
347
348
  
                      const increment = otherPanel === 'left' ? -1 : 1; // #3973
  
                      this.changePanelDate(otherPanel, 'Month', increment, false);
73a34dfa   Sergio Crisostomo   Fix year/month pr...
349
                  }
c4e3fe33   Sergio Crisostomo   move date-range.v...
350
              },
90ebd5a7   Sergio Crisostomo   Expose changed da...
351
              handleRangePick (val, type) {
c4e3fe33   Sergio Crisostomo   move date-range.v...
352
                  if (this.rangeState.selecting || this.currentView === 'time'){
c4e3fe33   Sergio Crisostomo   move date-range.v...
353
354
355
                      if (this.currentView === 'time'){
                          this.dates = val;
                      } else {
b990d2f4   Sergio Crisostomo   move logic to ins...
356
357
                          const [minDate, maxDate] = [this.rangeState.from, val].sort(dateSorter);
                          this.dates = [minDate, maxDate];
c4e3fe33   Sergio Crisostomo   move date-range.v...
358
359
360
361
362
363
                          this.rangeState = {
                              from: minDate,
                              to: maxDate,
                              selecting: false
                          };
                      }
90ebd5a7   Sergio Crisostomo   Expose changed da...
364
                      this.handleConfirm(false, type || 'date');
c4e3fe33   Sergio Crisostomo   move date-range.v...
365
366
367
368
369
370
371
372
373
374
375
376
377
378
                  } else {
                      this.rangeState = {
                          from: val,
                          to: null,
                          selecting: true
                      };
                  }
              },
              handleChangeRange (val) {
                  this.rangeState.to = val;
              },
          },
      };
  </script>