Blame view

src/components/date-picker/base/date-table.vue 5.73 KB
17e1fcf1   梁灏   init DatePicker
1
  <template>
95eae081   Sergio Crisostomo   refactor Datepicker
2
      <div :class="classes">
50637863   梁灏   update DatePicker
3
          <div :class="[prefixCls + '-header']">
af6a7c48   Danny Spangenberg   weekStartDay did ...
4
5
6
              <span v-for="day in headerDays" :key="day">
                  {{day}}
              </span>
50637863   梁灏   update DatePicker
7
          </div>
95eae081   Sergio Crisostomo   refactor Datepicker
8
9
10
11
12
13
14
15
          <span
                  :class="getCellCls(cell)"
                  v-for="cell in readCells"
                  @click="handleClick(cell)"
                  @mouseenter="handleMouseMove(cell)"
          >
              <em>{{ cell.text }}</em>
          </span>
50637863   梁灏   update DatePicker
16
      </div>
17e1fcf1   梁灏   init DatePicker
17
18
  </template>
  <script>
95eae081   Sergio Crisostomo   refactor Datepicker
19
      import { getFirstDayOfMonth, getDayCountOfMonth, clearHours, isInRange } from '../util';
50637863   梁灏   update DatePicker
20
      import { deepCopy } from '../../../utils/assist';
4ab11811   梁灏   some component su...
21
      import Locale from '../../../mixins/locale';
50637863   梁灏   update DatePicker
22
  
95eae081   Sergio Crisostomo   refactor Datepicker
23
24
      import mixin from './mixin';
      import prefixCls from './prefixCls';
50637863   梁灏   update DatePicker
25
  
0f677893   梁灏   update DatePicker
26
  
17e1fcf1   梁灏   init DatePicker
27
      export default {
95eae081   Sergio Crisostomo   refactor Datepicker
28
29
          mixins: [ Locale, mixin ],
  
0f677893   梁灏   update DatePicker
30
          props: {
95eae081   Sergio Crisostomo   refactor Datepicker
31
              /* more props in mixin */
0f677893   梁灏   update DatePicker
32
          },
17e1fcf1   梁灏   init DatePicker
33
          data () {
0f677893   梁灏   update DatePicker
34
              return {
fa3a666d   梁灏   update DatePicker
35
                  prefixCls: prefixCls,
b0893113   jingsam   :art: add eslint
36
              };
0f677893   梁灏   update DatePicker
37
          },
0f677893   梁灏   update DatePicker
38
39
40
          computed: {
              classes () {
                  return [
50637863   梁灏   update DatePicker
41
                      `${prefixCls}`
b0893113   jingsam   :art: add eslint
42
                  ];
50637863   梁灏   update DatePicker
43
              },
af6a7c48   Danny Spangenberg   weekStartDay did ...
44
45
46
47
              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...
48
                  });
af6a7c48   Danny Spangenberg   weekStartDay did ...
49
50
51
                  const weekDays = translatedDays.splice(weekStartDay, 7 - weekStartDay).concat(translatedDays.splice(0, weekStartDay));
                  return weekDays;
              },
95eae081   Sergio Crisostomo   refactor Datepicker
52
53
54
55
              readCells () {
                  const tableYear = this.tableDate.getFullYear();
                  const tableMonth = this.tableDate.getMonth();
                  const date = new Date(tableYear, tableMonth, 1);
09166420   Sergio Crisostomo   add weekStartDay ...
56
57
                  const weekStartDay = Number(this.t('i.datepicker.weekStartDay'));
                  const day = (getFirstDayOfMonth(date) || 7) - weekStartDay; // day of first day
50637863   梁灏   update DatePicker
58
                  const today = clearHours(new Date());    // timestamp of today
95eae081   Sergio Crisostomo   refactor Datepicker
59
60
61
62
                  const selectedDays = this.dates.filter(Boolean).map(clearHours);    // timestamp of selected days
                  const [minDay, maxDay] = this.dates.map(clearHours);
                  const rangeStart = this.rangeState.from && clearHours(this.rangeState.from);
                  const rangeEnd = this.rangeState.to && clearHours(this.rangeState.to);
50637863   梁灏   update DatePicker
63
64
65
66
67
68
69
70
71
72
  
                  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...
73
                      date: null,
50637863   梁灏   update DatePicker
74
                      selected: false,
3cf7cfd1   梁灏   update DatePicker
75
76
77
78
                      disabled: false,
                      range: false,
                      start: false,
                      end: false
50637863   梁灏   update DatePicker
79
80
81
82
83
84
                  };
                  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;
95eae081   Sergio Crisostomo   refactor Datepicker
85
                          cell.date = new Date(tableYear, tableMonth - 1, cell.text);
3747aecd   Sergio Crisostomo   Refactor date-pic...
86
                          const time = clearHours(cell.date);
50637863   梁灏   update DatePicker
87
88
89
90
91
92
93
                          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
94
                      cell.text = i;
95eae081   Sergio Crisostomo   refactor Datepicker
95
                      cell.date = new Date(tableYear, tableMonth, cell.text);
3747aecd   Sergio Crisostomo   Refactor date-pic...
96
97
                      const time = clearHours(cell.date);
                      cell.type = time === today ? 'today' : 'normal';
95eae081   Sergio Crisostomo   refactor Datepicker
98
                      cell.selected = selectedDays.includes(time);
50637863   梁灏   update DatePicker
99
                      cell.disabled = typeof disabledDate === 'function' && disabledDate(new Date(time));
95eae081   Sergio Crisostomo   refactor Datepicker
100
101
102
103
104
                      if (this.selectionMode === 'range'){
                          cell.range = isInRange(time, rangeStart, rangeEnd);
                          cell.start = time === minDay;
                          cell.end = time === maxDay;
                      }
50637863   梁灏   update DatePicker
105
106
107
108
109
110
111
112
                      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;
95eae081   Sergio Crisostomo   refactor Datepicker
113
                      cell.date = new Date(tableYear, tableMonth + 1, cell.text);
3747aecd   Sergio Crisostomo   Refactor date-pic...
114
                      const time = clearHours(cell.date);
50637863   梁灏   update DatePicker
115
116
117
118
119
                      cell.disabled = typeof disabledDate === 'function' && disabledDate(new Date(time));
                      cells.push(cell);
                  }
  
                  return cells;
0f677893   梁灏   update DatePicker
120
              }
17e1fcf1   梁灏   init DatePicker
121
          },
0f677893   梁灏   update DatePicker
122
          methods: {
0f677893   梁灏   update DatePicker
123
124
125
126
              getCellCls (cell) {
                  return [
                      `${prefixCls}-cell`,
                      {
3cf7cfd1   梁灏   update DatePicker
127
                          [`${prefixCls}-cell-selected`]: cell.selected || cell.start || cell.end,
50637863   梁灏   update DatePicker
128
129
130
                          [`${prefixCls}-cell-disabled`]: cell.disabled,
                          [`${prefixCls}-cell-today`]: cell.type === 'today',
                          [`${prefixCls}-cell-prev-month`]: cell.type === 'prev-month',
3cf7cfd1   梁灏   update DatePicker
131
132
                          [`${prefixCls}-cell-next-month`]: cell.type === 'next-month',
                          [`${prefixCls}-cell-range`]: cell.range && !cell.start && !cell.end
0f677893   梁灏   update DatePicker
133
                      }
b0893113   jingsam   :art: add eslint
134
                  ];
50637863   梁灏   update DatePicker
135
136
              },
  
0f677893   梁灏   update DatePicker
137
          }
b0893113   jingsam   :art: add eslint
138
139
      };
  </script>