Blame view

src/components/date-picker/base/date-table.vue 9.85 KB
17e1fcf1   梁灏   init DatePicker
1
  <template>
50637863   梁灏   update DatePicker
2
      <div
0f677893   梁灏   update DatePicker
3
          :class="classes"
0f677893   梁灏   update DatePicker
4
          @mousemove="handleMouseMove">
50637863   梁灏   update DatePicker
5
          <div :class="[prefixCls + '-header']">
af6a7c48   Danny Spangenberg   weekStartDay did ...
6
7
8
              <span v-for="day in headerDays" :key="day">
                  {{day}}
              </span>
50637863   梁灏   update DatePicker
9
          </div>
3747aecd   Sergio Crisostomo   Refactor date-pic...
10
          <span :class="getCellCls(cell)" v-for="(cell, index) in readCells"><em :index="index" @click="handleClick(cell)">{{ cell.text }}</em></span>
50637863   梁灏   update DatePicker
11
      </div>
17e1fcf1   梁灏   init DatePicker
12
13
  </template>
  <script>
b0893113   jingsam   :art: add eslint
14
      import { getFirstDayOfMonth, getDayCountOfMonth } from '../util';
50637863   梁灏   update DatePicker
15
      import { deepCopy } from '../../../utils/assist';
4ab11811   梁灏   some component su...
16
      import Locale from '../../../mixins/locale';
50637863   梁灏   update DatePicker
17
18
19
20
21
22
23
24
  
      const prefixCls = 'ivu-date-picker-cells';
  
      const clearHours = function (time) {
          const cloneDate = new Date(time);
          cloneDate.setHours(0, 0, 0, 0);
          return cloneDate.getTime();
      };
0f677893   梁灏   update DatePicker
25
  
17e1fcf1   梁灏   init DatePicker
26
      export default {
4ab11811   梁灏   some component su...
27
          mixins: [ Locale ],
0f677893   梁灏   update DatePicker
28
29
30
31
          props: {
              date: {},
              year: {},
              month: {},
0f677893   梁灏   update DatePicker
32
33
34
              selectionMode: {
                  default: 'day'
              },
0f677893   梁灏   update DatePicker
35
36
37
38
39
40
41
              disabledDate: {},
              minDate: {},
              maxDate: {},
              rangeState: {
                  default () {
                      return {
                          endDate: null,
3cf7cfd1   梁灏   update DatePicker
42
                          selecting: false
0f677893   梁灏   update DatePicker
43
44
45
                      };
                  }
              },
50637863   梁灏   update DatePicker
46
              value: ''
0f677893   梁灏   update DatePicker
47
          },
17e1fcf1   梁灏   init DatePicker
48
          data () {
0f677893   梁灏   update DatePicker
49
              return {
fa3a666d   梁灏   update DatePicker
50
51
                  prefixCls: prefixCls,
                  readCells: []
b0893113   jingsam   :art: add eslint
52
              };
0f677893   梁灏   update DatePicker
53
          },
472b4ff1   梁灏   update DatePicker
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
          watch: {
              'rangeState.endDate' (newVal) {
                  this.markRange(newVal);
              },
              minDate(newVal, oldVal) {
                  if (newVal && !oldVal) {
                      this.rangeState.selecting = true;
                      this.markRange(newVal);
                  } else if (!newVal) {
                      this.rangeState.selecting = false;
                      this.markRange(newVal);
                  } else {
                      this.markRange();
                  }
              },
              maxDate(newVal, oldVal) {
                  if (newVal && !oldVal) {
                      this.rangeState.selecting = false;
                      this.markRange(newVal);
3602b78d   梁灏   update DatePicker
73
74
75
76
  //                    this.$emit('on-pick', {
  //                        minDate: this.minDate,
  //                        maxDate: this.maxDate
  //                    });
472b4ff1   梁灏   update DatePicker
77
                  }
fa3a666d   梁灏   update DatePicker
78
              },
7c5ccdab   梁灏   update DatePicker
79
80
81
82
83
              cells: {
                  handler (cells) {
                      this.readCells = cells;
                  },
                  immediate: true
472b4ff1   梁灏   update DatePicker
84
85
              }
          },
0f677893   梁灏   update DatePicker
86
87
88
          computed: {
              classes () {
                  return [
50637863   梁灏   update DatePicker
89
                      `${prefixCls}`
b0893113   jingsam   :art: add eslint
90
                  ];
50637863   梁灏   update DatePicker
91
              },
af6a7c48   Danny Spangenberg   weekStartDay did ...
92
93
94
95
              headerDays () {
                  const weekStartDay = Number(this.t('i.datepicker.weekStartDay'));
                  const translatedDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'].map(item => {
                      return this.t('i.datepicker.weeks.' + item);
6850c1da   梁灏   Scroll add `heigh...
96
                  });
af6a7c48   Danny Spangenberg   weekStartDay did ...
97
98
99
                  const weekDays = translatedDays.splice(weekStartDay, 7 - weekStartDay).concat(translatedDays.splice(0, weekStartDay));
                  return weekDays;
              },
50637863   梁灏   update DatePicker
100
101
              cells () {
                  const date = new Date(this.year, this.month, 1);
09166420   Sergio Crisostomo   add weekStartDay ...
102
103
                  const weekStartDay = Number(this.t('i.datepicker.weekStartDay'));
                  const day = (getFirstDayOfMonth(date) || 7) - weekStartDay; // day of first day
50637863   梁灏   update DatePicker
104
105
                  const today = clearHours(new Date());    // timestamp of today
                  const selectDay = clearHours(new Date(this.value));    // timestamp of selected day
3cf7cfd1   梁灏   update DatePicker
106
107
                  const minDay = clearHours(new Date(this.minDate));
                  const maxDay = clearHours(new Date(this.maxDate));
50637863   梁灏   update DatePicker
108
109
110
111
112
113
114
115
116
117
  
                  const dateCountOfMonth = getDayCountOfMonth(date.getFullYear(), date.getMonth());
                  const dateCountOfLastMonth = getDayCountOfMonth(date.getFullYear(), (date.getMonth() === 0 ? 11 : date.getMonth() - 1));
  
                  const disabledDate = this.disabledDate;
  
                  let cells = [];
                  const cell_tmpl = {
                      text: '',
                      type: '',
3747aecd   Sergio Crisostomo   Refactor date-pic...
118
                      date: null,
50637863   梁灏   update DatePicker
119
                      selected: false,
3cf7cfd1   梁灏   update DatePicker
120
121
122
123
                      disabled: false,
                      range: false,
                      start: false,
                      end: false
50637863   梁灏   update DatePicker
124
125
126
127
128
129
                  };
                  if (day !== 7) {
                      for (let i = 0; i < day; i++) {
                          const cell = deepCopy(cell_tmpl);
                          cell.type = 'prev-month';
                          cell.text = dateCountOfLastMonth - (day - 1) + i;
3747aecd   Sergio Crisostomo   Refactor date-pic...
130
131
                          cell.date = new Date(this.year, this.month - 1, cell.text);
                          const time = clearHours(cell.date);
50637863   梁灏   update DatePicker
132
133
134
135
136
137
138
                          cell.disabled = typeof disabledDate === 'function' && disabledDate(new Date(time));
                          cells.push(cell);
                      }
                  }
  
                  for (let i = 1; i <= dateCountOfMonth; i++) {
                      const cell = deepCopy(cell_tmpl);
50637863   梁灏   update DatePicker
139
                      cell.text = i;
3747aecd   Sergio Crisostomo   Refactor date-pic...
140
141
142
                      cell.date = new Date(this.year, this.month, cell.text);
                      const time = clearHours(cell.date);
                      cell.type = time === today ? 'today' : 'normal';
50637863   梁灏   update DatePicker
143
144
                      cell.selected = time === selectDay;
                      cell.disabled = typeof disabledDate === 'function' && disabledDate(new Date(time));
3cf7cfd1   梁灏   update DatePicker
145
146
147
148
                      cell.range = time >= minDay && time <= maxDay;
                      cell.start = this.minDate && time === minDay;
                      cell.end = this.maxDate && time === maxDay;
  
50637863   梁灏   update DatePicker
149
150
151
152
153
154
155
156
                      cells.push(cell);
                  }
  
                  const nextMonthCount = 42 - cells.length;
                  for (let i = 1; i <= nextMonthCount; i++) {
                      const cell = deepCopy(cell_tmpl);
                      cell.type = 'next-month';
                      cell.text = i;
3747aecd   Sergio Crisostomo   Refactor date-pic...
157
158
                      cell.date = new Date(this.year, this.month + 1, cell.text);
                      const time = clearHours(cell.date);
50637863   梁灏   update DatePicker
159
160
161
162
163
                      cell.disabled = typeof disabledDate === 'function' && disabledDate(new Date(time));
                      cells.push(cell);
                  }
  
                  return cells;
0f677893   梁灏   update DatePicker
164
              }
17e1fcf1   梁灏   init DatePicker
165
          },
0f677893   梁灏   update DatePicker
166
          methods: {
3747aecd   Sergio Crisostomo   Refactor date-pic...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
              handleClick (cell) {
  
                  if (cell.disabled) return;
                  const newDate = cell.date;
  
                  if (this.selectionMode === 'range') {
                      if (this.minDate && this.maxDate) {
                          const minDate = new Date(newDate.getTime());
                          const maxDate = null;
                          this.rangeState.selecting = true;
                          this.markRange(this.minDate);
  
                          this.$emit('on-pick', {minDate, maxDate}, false);
                      } else if (this.minDate && !this.maxDate) {
                          if (newDate >= this.minDate) {
                              const maxDate = new Date(newDate.getTime());
                              this.rangeState.selecting = false;
  
                              this.$emit('on-pick', {minDate: this.minDate, maxDate});
                          } else {
3cf7cfd1   梁灏   update DatePicker
187
                              const minDate = new Date(newDate.getTime());
472b4ff1   梁灏   update DatePicker
188
189
  
                              this.$emit('on-pick', {minDate, maxDate: this.maxDate}, false);
3cf7cfd1   梁灏   update DatePicker
190
                          }
3747aecd   Sergio Crisostomo   Refactor date-pic...
191
192
193
194
195
196
                      } else if (!this.minDate) {
                          const minDate = new Date(newDate.getTime());
                          this.rangeState.selecting = true;
                          this.markRange(this.minDate);
  
                          this.$emit('on-pick', {minDate, maxDate: this.maxDate}, false);
c46f385a   梁灏   update DatePicker
197
                      }
3747aecd   Sergio Crisostomo   Refactor date-pic...
198
199
                  } else {
                      this.$emit('on-pick', newDate);
c46f385a   梁灏   update DatePicker
200
                  }
68e9b100   梁灏   update DatePicker
201
                  this.$emit('on-pick-click');
0f677893   梁灏   update DatePicker
202
              },
472b4ff1   梁灏   update DatePicker
203
204
              handleMouseMove (event) {
                  if (!this.rangeState.selecting) return;
0f677893   梁灏   update DatePicker
205
  
472b4ff1   梁灏   update DatePicker
206
207
208
209
210
211
212
213
214
215
                  this.$emit('on-changerange', {
                      minDate: this.minDate,
                      maxDate: this.maxDate,
                      rangeState: this.rangeState
                  });
  
                  const target = event.target;
                  if (target.tagName === 'EM') {
                      const cell = this.cells[parseInt(event.target.getAttribute('index'))];
  //                    if (cell.disabled) return;    // todo 待确定
3747aecd   Sergio Crisostomo   Refactor date-pic...
216
                      this.rangeState.endDate = cell.date;
472b4ff1   梁灏   update DatePicker
217
                  }
0f677893   梁灏   update DatePicker
218
              },
3cf7cfd1   梁灏   update DatePicker
219
              markRange (maxDate) {
3cf7cfd1   梁灏   update DatePicker
220
                  const minDate = this.minDate;
472b4ff1   梁灏   update DatePicker
221
222
223
224
225
226
227
228
229
230
231
                  if (!maxDate) maxDate = this.maxDate;
  
                  const minDay = clearHours(new Date(minDate));
                  const maxDay = clearHours(new Date(maxDate));
  
                  this.cells.forEach(cell => {
                      if (cell.type === 'today' || cell.type === 'normal') {
                          const time = clearHours(new Date(this.year, this.month, cell.text));
                          cell.range = time >= minDay && time <= maxDay;
                          cell.start = minDate && time === minDay;
                          cell.end = maxDate && time === maxDay;
3cf7cfd1   梁灏   update DatePicker
232
                      }
472b4ff1   梁灏   update DatePicker
233
                  });
3cf7cfd1   梁灏   update DatePicker
234
              },
0f677893   梁灏   update DatePicker
235
236
237
238
              getCellCls (cell) {
                  return [
                      `${prefixCls}-cell`,
                      {
3cf7cfd1   梁灏   update DatePicker
239
                          [`${prefixCls}-cell-selected`]: cell.selected || cell.start || cell.end,
50637863   梁灏   update DatePicker
240
241
242
                          [`${prefixCls}-cell-disabled`]: cell.disabled,
                          [`${prefixCls}-cell-today`]: cell.type === 'today',
                          [`${prefixCls}-cell-prev-month`]: cell.type === 'prev-month',
3cf7cfd1   梁灏   update DatePicker
243
244
                          [`${prefixCls}-cell-next-month`]: cell.type === 'next-month',
                          [`${prefixCls}-cell-range`]: cell.range && !cell.start && !cell.end
0f677893   梁灏   update DatePicker
245
                      }
b0893113   jingsam   :art: add eslint
246
                  ];
50637863   梁灏   update DatePicker
247
248
              },
  
0f677893   梁灏   update DatePicker
249
          }
b0893113   jingsam   :art: add eslint
250
251
      };
  </script>