Blame view

src/components/date-picker/panel/date.vue 9.9 KB
17e1fcf1   梁灏   init DatePicker
1
  <template>
030a522d   Sergio Crisostomo   make picker close...
2
      <div :class="classes" @mousedown.prevent>
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')"
e9c0d047   Sergio Crisostomo   refactor and redu...
13
                      @click="changeYear(-1)"><Icon type="ios-arrow-left"></Icon></span>
0f677893   梁灏   update DatePicker
14
15
                  <span
                      :class="iconBtnCls('prev')"
e9c0d047   Sergio Crisostomo   refactor and redu...
16
                      @click="changeMonth(-1)"
c46f385a   梁灏   update DatePicker
17
                      v-show="currentView === 'date'"><Icon type="ios-arrow-left"></Icon></span>
b27858dd   Sergio Crisostomo   add date panel la...
18
19
20
21
                  <date-panel-label
                      :date-panel-label="datePanelLabel"
                      :current-view="currentView"
                      :date-prefix-cls="datePrefixCls"/>
c46f385a   梁灏   update DatePicker
22
23
                  <span
                      :class="iconBtnCls('next', '-double')"
e9c0d047   Sergio Crisostomo   refactor and redu...
24
                      @click="changeYear(+1)"><Icon type="ios-arrow-right"></Icon></span>
0f677893   梁灏   update DatePicker
25
26
                  <span
                      :class="iconBtnCls('next')"
e9c0d047   Sergio Crisostomo   refactor and redu...
27
                      @click="changeMonth(+1)"
c46f385a   梁灏   update DatePicker
28
                      v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span>
0f677893   梁灏   update DatePicker
29
30
31
              </div>
              <div :class="[prefixCls + '-content']">
                  <date-table
50637863   梁灏   update DatePicker
32
33
34
35
36
                      v-show="currentView === 'date'"
                      :year="year"
                      :month="month"
                      :date="date"
                      :value="value"
50637863   梁灏   update DatePicker
37
                      :selection-mode="selectionMode"
c46f385a   梁灏   update DatePicker
38
                      :disabled-date="disabledDate"
68e9b100   梁灏   update DatePicker
39
40
                      @on-pick="handleDatePick"
                      @on-pick-click="handlePickClick"></date-table>
0f677893   梁灏   update DatePicker
41
                  <year-table
531cd165   梁灏   support DatePicke...
42
                      ref="yearTable"
c46f385a   梁灏   update DatePicker
43
44
45
                      v-show="currentView === 'year'"
                      :year="year"
                      :date="date"
13be4434   梁灏   update DatePicker
46
                      :selection-mode="selectionMode"
c46f385a   梁灏   update DatePicker
47
                      :disabled-date="disabledDate"
68e9b100   梁灏   update DatePicker
48
49
                      @on-pick="handleYearPick"
                      @on-pick-click="handlePickClick"></year-table>
0f677893   梁灏   update DatePicker
50
                  <month-table
531cd165   梁灏   support DatePicke...
51
                      ref="monthTable"
c46f385a   梁灏   update DatePicker
52
53
54
                      v-show="currentView === 'month'"
                      :month="month"
                      :date="date"
13be4434   梁灏   update DatePicker
55
                      :selection-mode="selectionMode"
c46f385a   梁灏   update DatePicker
56
                      :disabled-date="disabledDate"
68e9b100   梁灏   update DatePicker
57
58
                      @on-pick="handleMonthPick"
                      @on-pick-click="handlePickClick"></month-table>
5cc9b892   梁灏   update DateTimePi...
59
                  <time-picker
531cd165   梁灏   support DatePicke...
60
                      ref="timePicker"
a8571a5f   梁灏   update DateTimePi...
61
                      show-date
2dc27713   梁灏   update DateTimePi...
62
                      v-show="currentView === 'time'"
a2a78c38   梁灏   update DateTimePi...
63
64
                      @on-pick="handleTimePick"
                      @on-pick-click="handlePickClick"></time-picker>
0f677893   梁灏   update DatePicker
65
              </div>
b9041a0d   梁灏   DatePicker add co...
66
67
              <Confirm
                  v-if="confirm"
5cc9b892   梁灏   update DateTimePi...
68
69
70
                  :show-time="showTime"
                  :is-time="isTime"
                  @on-pick-toggle-time="handleToggleTime"
b9041a0d   梁灏   DatePicker add co...
71
72
                  @on-pick-clear="handlePickClear"
                  @on-pick-success="handlePickSuccess"></Confirm>
0f677893   梁灏   update DatePicker
73
74
          </div>
      </div>
17e1fcf1   梁灏   init DatePicker
75
76
  </template>
  <script>
c46f385a   梁灏   update DatePicker
77
      import Icon from '../../icon/icon.vue';
0f677893   梁灏   update DatePicker
78
79
80
      import DateTable from '../base/date-table.vue';
      import YearTable from '../base/year-table.vue';
      import MonthTable from '../base/month-table.vue';
5cc9b892   梁灏   update DateTimePi...
81
      import TimePicker from './time.vue';
b9041a0d   梁灏   DatePicker add co...
82
      import Confirm from '../base/confirm.vue';
b27858dd   Sergio Crisostomo   add date panel la...
83
      import datePanelLabel from './date-panel-label.vue';
0f677893   梁灏   update DatePicker
84
  
3cf7cfd1   梁灏   update DatePicker
85
      import Mixin from './mixin';
4ab11811   梁灏   some component su...
86
      import Locale from '../../../mixins/locale';
3cf7cfd1   梁灏   update DatePicker
87
  
b27858dd   Sergio Crisostomo   add date panel la...
88
      import { initTimeDate, siblingMonth, formatDateLabels } from '../util';
5cc9b892   梁灏   update DateTimePi...
89
  
0f677893   梁灏   update DatePicker
90
91
92
      const prefixCls = 'ivu-picker-panel';
      const datePrefixCls = 'ivu-date-picker';
  
17e1fcf1   梁灏   init DatePicker
93
      export default {
a2a78c38   梁灏   update DateTimePi...
94
          name: 'DatePicker',
4ab11811   梁灏   some component su...
95
          mixins: [ Mixin, Locale ],
b27858dd   Sergio Crisostomo   add date panel la...
96
          components: { Icon, DateTable, YearTable, MonthTable, TimePicker, Confirm, datePanelLabel },
17e1fcf1   梁灏   init DatePicker
97
          data () {
0f677893   梁灏   update DatePicker
98
99
100
101
              return {
                  prefixCls: prefixCls,
                  datePrefixCls: datePrefixCls,
                  shortcuts: [],
50637863   梁灏   update DatePicker
102
                  currentView: 'date',
5cc9b892   梁灏   update DateTimePi...
103
                  date: initTimeDate(),
50637863   梁灏   update DatePicker
104
105
106
                  value: '',
                  showTime: false,
                  selectionMode: 'day',
50637863   梁灏   update DatePicker
107
108
109
                  disabledDate: '',
                  year: null,
                  month: null,
5cc9b892   梁灏   update DateTimePi...
110
                  confirm: false,
acde7262   梁灏   update DateTimePi...
111
112
                  isTime: false,
                  format: 'yyyy-MM-dd'
b0893113   jingsam   :art: add eslint
113
              };
50637863   梁灏   update DatePicker
114
115
          },
          computed: {
2533a192   梁灏   update DatePicker
116
117
118
119
120
121
              classes () {
                  return [
                      `${prefixCls}-body-wrapper`,
                      {
                          [`${prefixCls}-with-sidebar`]: this.shortcuts.length
                      }
b0893113   jingsam   :art: add eslint
122
                  ];
2533a192   梁灏   update DatePicker
123
              },
b27858dd   Sergio Crisostomo   add date panel la...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
              datePanelLabel () {
                  if (!this.year) return null; // not ready yet
                  const locale = this.t('i.locale');
                  const datePanelLabel = this.t('i.datepicker.datePanelLabel');
                  const date = new Date(this.year, this.month);
                  const { labels, separator } = formatDateLabels(locale, datePanelLabel, date);
  
                  const handler = type => {
                      return () => (this.currentView = type);
                  };
  
                  return {
                      separator: separator,
                      labels: labels.map(obj => ((obj.handler = handler(obj.type)), obj))
                  };
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
                      this.date = newVal;
e9c0d047   Sergio Crisostomo   refactor and redu...
147
                      this.setMonthYear(newVal);
50637863   梁灏   update DatePicker
148
                  }
36931442   梁灏   update DateTimePi...
149
150
151
152
153
154
155
                  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...
156
157
158
              },
              currentView (val) {
                  if (val === 'time') this.$refs.timePicker.updateScroll();
0f677893   梁灏   update DatePicker
159
              }
17e1fcf1   梁灏   init DatePicker
160
          },
0f677893   梁灏   update DatePicker
161
          methods: {
f92ef70f   梁灏   update DatePicker
162
163
164
              resetDate () {
                  this.date = new Date(this.date);
              },
e9c0d047   Sergio Crisostomo   refactor and redu...
165
166
167
168
              setMonthYear(date){
                  this.month = date.getMonth();
                  this.year = date.getFullYear();
              },
5cc9b892   梁灏   update DateTimePi...
169
              handleClear () {
c46f385a   梁灏   update DatePicker
170
171
                  this.date = new Date();
                  this.$emit('on-pick', '');
5cc9b892   梁灏   update DateTimePi...
172
                  if (this.showTime) this.$refs.timePicker.handleClear();
c46f385a   梁灏   update DatePicker
173
              },
5cc9b892   梁灏   update DateTimePi...
174
175
176
177
178
179
180
181
182
              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
183
                  }
e9c0d047   Sergio Crisostomo   refactor and redu...
184
                  this.setMonthYear(this.date);
356953d9   梁灏   fixed #201
185
                  if (reset) this.isTime = false;
c46f385a   梁灏   update DatePicker
186
              },
e9c0d047   Sergio Crisostomo   refactor and redu...
187
              changeYear(dir){
c46f385a   梁灏   update DatePicker
188
                  if (this.currentView === 'year') {
e9c0d047   Sergio Crisostomo   refactor and redu...
189
                      this.$refs.yearTable[dir == 1 ? 'nextTenYear' : 'prevTenYear']();
c46f385a   梁灏   update DatePicker
190
                  } else {
e9c0d047   Sergio Crisostomo   refactor and redu...
191
192
                      this.year+= dir;
                      this.date = siblingMonth(this.date, dir * 12);
c46f385a   梁灏   update DatePicker
193
                  }
0f677893   梁灏   update DatePicker
194
              },
e9c0d047   Sergio Crisostomo   refactor and redu...
195
196
197
              changeMonth(dir){
                  this.date = siblingMonth(this.date, dir);
                  this.setMonthYear(this.date);
0f677893   梁灏   update DatePicker
198
              },
5cc9b892   梁灏   update DateTimePi...
199
200
201
202
203
204
205
206
207
              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
208
209
210
              handleYearPick(year, close = true) {
                  this.year = year;
                  if (!close) return;
0f677893   梁灏   update DatePicker
211
  
c46f385a   梁灏   update DatePicker
212
213
                  this.date.setFullYear(year);
                  if (this.selectionMode === 'year') {
344131a7   梁灏   update DatePicker
214
                      this.$emit('on-pick', new Date(year, 0, 1));
c46f385a   梁灏   update DatePicker
215
216
217
218
219
220
221
222
                  } else {
                      this.currentView = 'month';
                  }
  
                  this.resetDate();
              },
              handleMonthPick (month) {
                  this.month = month;
e9c0d047   Sergio Crisostomo   refactor and redu...
223
224
                  this.date.setMonth(month);
                  if (this.selectionMode !== 'month') {
c46f385a   梁灏   update DatePicker
225
226
227
                      this.currentView = 'date';
                      this.resetDate();
                  } else {
c46f385a   梁灏   update DatePicker
228
229
230
231
232
233
234
235
                      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...
236
                      this.$emit('on-pick', new Date(value.getTime()));
e9c0d047   Sergio Crisostomo   refactor and redu...
237
                      this.date = new Date(value);
c46f385a   梁灏   update DatePicker
238
                  }
5cc9b892   梁灏   update DateTimePi...
239
240
241
              },
              handleTimePick (date) {
                  this.handleDatePick(date);
0f677893   梁灏   update DatePicker
242
243
              }
          },
531cd165   梁灏   support DatePicke...
244
          mounted () {
50637863   梁灏   update DatePicker
245
246
247
248
249
              if (this.selectionMode === 'month') {
                  this.currentView = 'month';
              }
  
              if (this.date && !this.year) {
e9c0d047   Sergio Crisostomo   refactor and redu...
250
                  this.setMonthYear(this.date);
50637863   梁灏   update DatePicker
251
              }
36931442   梁灏   update DateTimePi...
252
              if (this.showTime) {
2dc27713   梁灏   update DateTimePi...
253
                  // todo 这里可能有问题,并不能进入到这里,但不影响正常使用
36931442   梁灏   update DateTimePi...
254
255
256
257
258
                  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
259
          }
b0893113   jingsam   :art: add eslint
260
261
      };
  </script>