Blame view

src/components/date-picker/panel/Date/date-range.vue 15.4 KB
c4e3fe33   Sergio Crisostomo   move date-range.v...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <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>
          <div :class="[prefixCls + '-body']">
              <div :class="[prefixCls + '-content', prefixCls + '-content-left']" v-show="!isTime">
                  <div :class="[datePrefixCls + '-header']" v-show="currentView !== 'time'">
                      <span
                          :class="iconBtnCls('prev', '-double')"
                          @click="prevYear('left')"><Icon type="ios-arrow-left"></Icon></span>
                      <span
abb0cc87   Sergio Crisostomo   Correct arrows sh...
16
                          v-if="leftPickerTable === 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
17
18
19
20
21
                          :class="iconBtnCls('prev')"
                          @click="prevMonth('left')"
                          v-show="currentView === 'date'"><Icon type="ios-arrow-left"></Icon></span>
                      <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
27
28
                          :class="iconBtnCls('next', '-double')"
                          @click="nextYear('left')"><Icon type="ios-arrow-right"></Icon></span>
                      <span
abb0cc87   Sergio Crisostomo   Correct arrows sh...
29
                          v-if="splitPanels && leftPickerTable === 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
30
31
32
33
34
                          :class="iconBtnCls('next')"
                          @click="nextMonth('left')"
                          v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span>
                  </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"
c4e3fe33   Sergio Crisostomo   move date-range.v...
44
                      @on-change-range="handleChangeRange"
b52e02e4   Sergio Crisostomo   Fix month|year pr...
45
                      @on-pick="panelPickerHandlers.left"
c4e3fe33   Sergio Crisostomo   move date-range.v...
46
47
48
49
50
51
                      @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...
52
                          v-if="splitPanels || rightPickerTable !== 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
53
54
55
                          :class="iconBtnCls('prev', '-double')"
                          @click="prevYear('right')"><Icon type="ios-arrow-left"></Icon></span>
                      <span
abb0cc87   Sergio Crisostomo   Correct arrows sh...
56
                          v-if="splitPanels && rightPickerTable === 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
57
58
59
60
61
                          :class="iconBtnCls('prev')"
                          @click="prevMonth('right')"
                          v-show="currentView === 'date'"><Icon type="ios-arrow-left"></Icon></span>
                      <date-panel-label
                          :date-panel-label="rightDatePanelLabel"
73a34dfa   Sergio Crisostomo   Fix year/month pr...
62
                          :current-view="rightDatePanelView"
c4e3fe33   Sergio Crisostomo   move date-range.v...
63
64
65
66
67
                          :date-prefix-cls="datePrefixCls"></date-panel-label>
                      <span
                          :class="iconBtnCls('next', '-double')"
                          @click="nextYear('right')"><Icon type="ios-arrow-right"></Icon></span>
                      <span
abb0cc87   Sergio Crisostomo   Correct arrows sh...
68
                          v-if="rightPickerTable === 'date-table'"
c4e3fe33   Sergio Crisostomo   move date-range.v...
69
70
71
72
73
                          :class="iconBtnCls('next')"
                          @click="nextMonth('right')"
                          v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span>
                  </div>
                  <component
b52e02e4   Sergio Crisostomo   Fix month|year pr...
74
                      :is="rightPickerTable"
c4e3fe33   Sergio Crisostomo   move date-range.v...
75
76
77
78
79
80
                      ref="rightYearTable"
                      v-if="currentView !== 'time'"
                      :table-date="rightPanelDate"
                      selection-mode="range"
                      :range-state="rangeState"
                      :disabled-date="disabledDate"
e55ba7a2   Sergio Crisostomo   Add week numbers
81
                      :show-week-numbers="showWeekNumbers"
ebf1f86b   Sergio Crisostomo   show only 1 date ...
82
                      :value="preSelecting.right ? [dates[dates.length - 1]] : dates"
c4e3fe33   Sergio Crisostomo   move date-range.v...
83
                      @on-change-range="handleChangeRange"
b52e02e4   Sergio Crisostomo   Fix month|year pr...
84
                      @on-pick="panelPickerHandlers.right"
c4e3fe33   Sergio Crisostomo   move date-range.v...
85
86
87
88
89
90
91
92
93
                      @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 ...
94
                      v-bind="timePickerOptions"
c4e3fe33   Sergio Crisostomo   move date-range.v...
95
96
97
98
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
                      @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 ...
131
132
133
134
      const dateSorter = (a, b) => {
          if (!a || !b) return 0;
          return a.getTime() - b.getTime();
      };
c4e3fe33   Sergio Crisostomo   move date-range.v...
135
136
137
138
139
140
  
      export default {
          name: 'RangeDatePickerPanel',
          mixins: [ Mixin, Locale, DateMixin ],
          components: { Icon, DateTable, YearTable, MonthTable, TimePicker, Confirm, datePanelLabel },
          props: {
435bf781   Sergio Crisostomo   add split panel p...
141
142
143
144
145
              // more props in the mixin
              splitPanels: {
                  type: Boolean,
                  default: false
              },
c4e3fe33   Sergio Crisostomo   move date-range.v...
146
147
148
          },
          data(){
              const [minDate, maxDate] = this.value.map(date => date || initTimeDate());
63bd0f7d   Sergio Crisostomo   Add start-date pr...
149
150
              const leftPanelDate = this.startDate ? this.startDate : minDate;
  
c4e3fe33   Sergio Crisostomo   move date-range.v...
151
152
153
154
155
156
              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...
157
158
                  leftPickerTable: `${this.selectionMode}-table`,
                  rightPickerTable: `${this.selectionMode}-table`,
63bd0f7d   Sergio Crisostomo   Add start-date pr...
159
160
                  leftPanelDate: leftPanelDate,
                  rightPanelDate: new Date(leftPanelDate.getFullYear(), leftPanelDate.getMonth() + 1, leftPanelDate.getDate())
c4e3fe33   Sergio Crisostomo   move date-range.v...
161
162
163
164
165
166
167
168
169
170
171
172
              };
          },
          computed: {
              classes(){
                  return [
                      `${prefixCls}-body-wrapper`,
                      `${datePrefixCls}-with-range`,
                      {
                          [`${prefixCls}-with-sidebar`]: this.shortcuts.length
                      }
                  ];
              },
c4e3fe33   Sergio Crisostomo   move date-range.v...
173
174
175
176
177
178
              leftDatePanelLabel(){
                  return this.panelLabelConfig('left');
              },
              rightDatePanelLabel(){
                  return this.panelLabelConfig('right');
              },
73a34dfa   Sergio Crisostomo   Fix year/month pr...
179
180
181
182
183
184
              leftDatePanelView(){
                  return this.leftPickerTable.split('-').shift();
              },
              rightDatePanelView(){
                  return this.rightPickerTable.split('-').shift();
              },
c4e3fe33   Sergio Crisostomo   move date-range.v...
185
186
              timeDisabled(){
                  return !(this.dates[0] && this.dates[1]);
b52e02e4   Sergio Crisostomo   Fix month|year pr...
187
              },
ebf1f86b   Sergio Crisostomo   show only 1 date ...
188
              preSelecting(){
b52e02e4   Sergio Crisostomo   Fix month|year pr...
189
                  const tableType = `${this.currentView}-table`;
ebf1f86b   Sergio Crisostomo   show only 1 date ...
190
191
192
193
194
195
196
  
                  return {
                      left: this.leftPickerTable !== tableType,
                      right: this.rightPickerTable !== tableType,
                  };
              },
              panelPickerHandlers(){
b52e02e4   Sergio Crisostomo   Fix month|year pr...
197
                  return {
ebf1f86b   Sergio Crisostomo   show only 1 date ...
198
199
                      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 ...
200
                  };
c4e3fe33   Sergio Crisostomo   move date-range.v...
201
202
203
204
205
206
              }
          },
          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 ...
207
                  this.dates = [minDate, maxDate].sort(dateSorter);
15457562   Sergio Crisostomo   Reset panel date ...
208
  
c4e3fe33   Sergio Crisostomo   move date-range.v...
209
                  this.rangeState = {
15457562   Sergio Crisostomo   Reset panel date ...
210
211
                      from: this.dates[0],
                      to: this.dates[1],
c4e3fe33   Sergio Crisostomo   move date-range.v...
212
213
                      selecting: false
                  };
c2b7fed0   Sergio Crisostomo   Reset panel selec...
214
215
216
217
218
219
220
  
  
                  // set panels positioning
                  const leftPanelDate = this.startDate || this.dates[0] || new Date();
                  this.leftPanelDate = leftPanelDate;
                  const rightPanelDate = new Date(leftPanelDate.getFullYear(), leftPanelDate.getMonth() + 1, leftPanelDate.getDate());
                  this.rightPanelDate = this.splitPanels ? new Date(Math.max(this.dates[1], rightPanelDate)) : rightPanelDate;
c4e3fe33   Sergio Crisostomo   move date-range.v...
221
222
223
224
225
226
              },
              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...
227
228
229
230
231
232
233
234
235
                  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...
236
237
              },
              selectionMode(type){
c2b7fed0   Sergio Crisostomo   Reset panel selec...
238
                  this.currentView = type || 'range';
c4e3fe33   Sergio Crisostomo   move date-range.v...
239
240
241
              }
          },
          methods: {
46726afd   Sergio Crisostomo   Fix panels reset ...
242
243
244
245
246
              reset(){
                  this.currentView = this.selectionMode;
                  this.leftPickerTable = `${this.currentView}-table`;
                  this.rightPickerTable = `${this.currentView}-table`;
              },
c4e3fe33   Sergio Crisostomo   move date-range.v...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
              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...
263
              prevYear (panel) {
77e43f2b   Sergio Crisostomo   Correct year date...
264
265
                  const increment = this.currentView === 'year' ? -10 : -1;
                  this.changePanelDate(panel, 'FullYear', increment);
c4e3fe33   Sergio Crisostomo   move date-range.v...
266
              },
77e43f2b   Sergio Crisostomo   Correct year date...
267
268
269
              nextYear (panel) {
                  const increment = this.currentView === 'year' ? 10 : 1;
                  this.changePanelDate(panel, 'FullYear', increment);
c4e3fe33   Sergio Crisostomo   move date-range.v...
270
              },
77e43f2b   Sergio Crisostomo   Correct year date...
271
272
              prevMonth(panel){
                  this.changePanelDate(panel, 'Month', -1);
c4e3fe33   Sergio Crisostomo   move date-range.v...
273
              },
77e43f2b   Sergio Crisostomo   Correct year date...
274
275
              nextMonth(panel){
                  this.changePanelDate(panel, 'Month', 1);
c4e3fe33   Sergio Crisostomo   move date-range.v...
276
277
278
279
280
281
              },
              changePanelDate(panel, type, increment){
                  const current = new Date(this[`${panel}PanelDate`]);
                  current[`set${type}`](current[`get${type}`]() + increment);
                  this[`${panel}PanelDate`] = current;
  
77e43f2b   Sergio Crisostomo   Correct year date...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  
                  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';
                      const otherCurrent = new Date(this[`${otherPanel}PanelDate`]);
                      otherCurrent[`set${type}`](otherCurrent[`get${type}`]() + increment);
                      if (current[`get${type}`]() !== otherCurrent[`get${type}`]()){
                          this[`${otherPanel}PanelDate`] = otherCurrent;
                      }
c4e3fe33   Sergio Crisostomo   move date-range.v...
300
301
                  }
              },
b52e02e4   Sergio Crisostomo   Fix month|year pr...
302
303
304
305
306
              showYearPicker (panel) {
                  this[`${panel}PickerTable`] = 'year-table';
              },
              showMonthPicker (panel) {
                  this[`${panel}PickerTable`] = 'month-table';
c4e3fe33   Sergio Crisostomo   move date-range.v...
307
              },
b52e02e4   Sergio Crisostomo   Fix month|year pr...
308
309
              handlePreSelection(panel, value){
                  this[`${panel}PanelDate`] = value;
73a34dfa   Sergio Crisostomo   Fix year/month pr...
310
311
312
313
314
315
316
317
318
319
                  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';
                      const type = currentViewType === 'year-table' ? 'FullYear' : 'Month';
                      this[`${otherPanel}PanelDate`] = value;
                      this.changePanelDate(otherPanel, type, 1);
                  }
c4e3fe33   Sergio Crisostomo   move date-range.v...
320
321
322
              },
              handleRangePick (val) {
                  if (this.rangeState.selecting || this.currentView === 'time'){
c4e3fe33   Sergio Crisostomo   move date-range.v...
323
324
325
                      if (this.currentView === 'time'){
                          this.dates = val;
                      } else {
b990d2f4   Sergio Crisostomo   move logic to ins...
326
327
                          const [minDate, maxDate] = [this.rangeState.from, val].sort(dateSorter);
                          this.dates = [minDate, maxDate];
c4e3fe33   Sergio Crisostomo   move date-range.v...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
                          this.rangeState = {
                              from: minDate,
                              to: maxDate,
                              selecting: false
                          };
                      }
                      this.handleConfirm(false);
                  } else {
                      this.rangeState = {
                          from: val,
                          to: null,
                          selecting: true
                      };
                  }
              },
              handleChangeRange (val) {
                  this.rangeState.to = val;
              },
          },
      };
  </script>