Blame view

src/components/date-picker/base/date-table.vue 4.48 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
          <span
                  :class="getCellCls(cell)"
e55ba7a2   Sergio Crisostomo   Add week numbers
10
11
                  v-for="(cell, i) in readCells"
                  :key="String(cell.date) + i"
95eae081   Sergio Crisostomo   refactor Datepicker
12
13
14
                  @click="handleClick(cell)"
                  @mouseenter="handleMouseMove(cell)"
          >
e55ba7a2   Sergio Crisostomo   Add week numbers
15
              <em>{{ cell.desc }}</em>
95eae081   Sergio Crisostomo   refactor Datepicker
16
          </span>
50637863   梁灏   update DatePicker
17
      </div>
17e1fcf1   梁灏   init DatePicker
18
19
  </template>
  <script>
e55ba7a2   Sergio Crisostomo   Add week numbers
20
      import { clearHours, isInRange } from '../util';
4ab11811   梁灏   some component su...
21
      import Locale from '../../../mixins/locale';
e55ba7a2   Sergio Crisostomo   Add week numbers
22
      import jsCalendar from 'js-calendar';
50637863   梁灏   update DatePicker
23
  
95eae081   Sergio Crisostomo   refactor Datepicker
24
25
      import mixin from './mixin';
      import prefixCls from './prefixCls';
50637863   梁灏   update DatePicker
26
  
0f677893   梁灏   update DatePicker
27
  
17e1fcf1   梁灏   init DatePicker
28
      export default {
95eae081   Sergio Crisostomo   refactor Datepicker
29
30
          mixins: [ Locale, mixin ],
  
0f677893   梁灏   update DatePicker
31
          props: {
95eae081   Sergio Crisostomo   refactor Datepicker
32
              /* more props in mixin */
e55ba7a2   Sergio Crisostomo   Add week numbers
33
34
35
36
              showWeekNumbers: {
                  type: Boolean,
                  default: false
              },
0f677893   梁灏   update DatePicker
37
          },
17e1fcf1   梁灏   init DatePicker
38
          data () {
e55ba7a2   Sergio Crisostomo   Add week numbers
39
              const weekStartDay = Number(this.t('i.datepicker.weekStartDay'));
0f677893   梁灏   update DatePicker
40
              return {
fa3a666d   梁灏   update DatePicker
41
                  prefixCls: prefixCls,
e55ba7a2   Sergio Crisostomo   Add week numbers
42
                  calendar: new jsCalendar.Generator({onlyDays: !this.showWeekNumbers, weekStart: weekStartDay})
b0893113   jingsam   :art: add eslint
43
              };
0f677893   梁灏   update DatePicker
44
          },
0f677893   梁灏   update DatePicker
45
46
47
          computed: {
              classes () {
                  return [
e55ba7a2   Sergio Crisostomo   Add week numbers
48
49
50
51
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-show-week-numbers`]: this.showWeekNumbers
                      }
b0893113   jingsam   :art: add eslint
52
                  ];
50637863   梁灏   update DatePicker
53
              },
af6a7c48   Danny Spangenberg   weekStartDay did ...
54
55
56
57
              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...
58
                  });
af6a7c48   Danny Spangenberg   weekStartDay did ...
59
                  const weekDays = translatedDays.splice(weekStartDay, 7 - weekStartDay).concat(translatedDays.splice(0, weekStartDay));
e55ba7a2   Sergio Crisostomo   Add week numbers
60
                  return this.showWeekNumbers ? [''].concat(weekDays) : weekDays;
af6a7c48   Danny Spangenberg   weekStartDay did ...
61
              },
95eae081   Sergio Crisostomo   refactor Datepicker
62
63
64
              readCells () {
                  const tableYear = this.tableDate.getFullYear();
                  const tableMonth = this.tableDate.getMonth();
50637863   梁灏   update DatePicker
65
                  const today = clearHours(new Date());    // timestamp of today
95eae081   Sergio Crisostomo   refactor Datepicker
66
67
68
69
                  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
70
  
e55ba7a2   Sergio Crisostomo   Add week numbers
71
72
                  const isRange = this.selectionMode === 'range';
                  const disabledTestFn = typeof this.disabledDate === 'function' && this.disabledDate;
50637863   梁灏   update DatePicker
73
  
e55ba7a2   Sergio Crisostomo   Add week numbers
74
75
                  return this.calendar(tableYear, tableMonth, (cell) => {
                      const time = cell.date && clearHours(cell.date);
a781ad1a   Sergio Crisostomo   hide range and se...
76
                      const dateIsInCurrentMonth = cell.date && tableMonth === cell.date.getMonth();
e55ba7a2   Sergio Crisostomo   Add week numbers
77
78
79
                      return {
                          ...cell,
                          type: time === today ? 'today' : cell.type,
a781ad1a   Sergio Crisostomo   hide range and se...
80
                          selected: dateIsInCurrentMonth && selectedDays.includes(time),
e55ba7a2   Sergio Crisostomo   Add week numbers
81
                          disabled: (cell.date && disabledTestFn) && disabledTestFn(new Date(time)),
a781ad1a   Sergio Crisostomo   hide range and se...
82
83
84
                          range: dateIsInCurrentMonth && isRange && isInRange(time, rangeStart, rangeEnd),
                          start: dateIsInCurrentMonth && isRange && time === minDay,
                          end: dateIsInCurrentMonth && isRange && time === maxDay
e55ba7a2   Sergio Crisostomo   Add week numbers
85
                      };
a1c88eba   Sergio Crisostomo   Correct showWeekN...
86
                  }).cells.slice(this.showWeekNumbers ? 8 : 0);
0f677893   梁灏   update DatePicker
87
              }
17e1fcf1   梁灏   init DatePicker
88
          },
0f677893   梁灏   update DatePicker
89
          methods: {
0f677893   梁灏   update DatePicker
90
91
92
93
              getCellCls (cell) {
                  return [
                      `${prefixCls}-cell`,
                      {
3cf7cfd1   梁灏   update DatePicker
94
                          [`${prefixCls}-cell-selected`]: cell.selected || cell.start || cell.end,
50637863   梁灏   update DatePicker
95
96
                          [`${prefixCls}-cell-disabled`]: cell.disabled,
                          [`${prefixCls}-cell-today`]: cell.type === 'today',
e55ba7a2   Sergio Crisostomo   Add week numbers
97
98
99
                          [`${prefixCls}-cell-prev-month`]: cell.type === 'prevMonth',
                          [`${prefixCls}-cell-next-month`]: cell.type === 'nextMonth',
                          [`${prefixCls}-cell-week-label`]: cell.type === 'weekLabel',
3cf7cfd1   梁灏   update DatePicker
100
                          [`${prefixCls}-cell-range`]: cell.range && !cell.start && !cell.end
0f677893   梁灏   update DatePicker
101
                      }
b0893113   jingsam   :art: add eslint
102
                  ];
50637863   梁灏   update DatePicker
103
104
              },
  
0f677893   梁灏   update DatePicker
105
          }
b0893113   jingsam   :art: add eslint
106
107
      };
  </script>