Blame view

src/components/calendar/panel/Date/date.vue 7.01 KB
e80b805f   chenhaodong   初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  <template>
      <div :class="classes" @mousedown.prevent>
          <div :class="[prefixCls + '-body']">
              <div :class="[datePrefixCls + '-header']" v-show="currentView !== 'time'">
                  <span :class="iconBtnCls('prev', '-double')" @click="changeYear(-1)">
                      <Icon type="ios-arrow-back"></Icon>
                  </span>
                  <span v-if="pickerTable === 'date-table'" :class="iconBtnCls('prev')" @click="changeMonth(-1)" v-show="currentView === 'date'">
                      <Icon type="ios-arrow-back"></Icon>
                  </span>
                  <date-panel-label :date-panel-label="datePanelLabel" :current-view="pickerTable.split('-').shift()" :date-prefix-cls="datePrefixCls">
                  </date-panel-label>
                  <span :class="iconBtnCls('next', '-double')" @click="changeYear(+1)">
                      <Icon type="ios-arrow-forward"></Icon>
                  </span>
                  <span v-if="pickerTable === 'date-table'" :class="iconBtnCls('next')" @click="changeMonth(+1)" v-show="currentView === 'date'">
                      <Icon type="ios-arrow-forward"></Icon>
                  </span>
              </div>
              <div :class="[prefixCls + '-content']">
                  <component :is="pickerTable" :information="information" :calendarWidth="calendarWidth" :date-panel-label="datePanelLabel" ref="pickerTable" v-if="currentView !== 'time'" :table-date="panelDate" :show-week-numbers="showWeekNumbers" :value="dates" :selection-mode="selectionMode" :disabled-date="disabledDate" :focused-date="focusedDate" @on-pick="panelPickerHandlers" @on-pick-click="handlePickClick" />
  
              </div>
  
          </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 datePanelLabel from './date-panel-label.vue';
  
  import Mixin from '../panel-mixin';
  import DateMixin from './date-panel-mixin';
  import Locale from '../../../../mixins/locale';
  
  import { siblingMonth, formatDateLabels } from '../../util';
  
  const prefixCls = 'ivu-picker-panel';
  const datePrefixCls = 'ivu-calendar';
  
  export default {
      name: 'DatePickerPanel',
      mixins: [Mixin, Locale, DateMixin],
      components: { Icon, DateTable, YearTable, MonthTable, datePanelLabel },
      props: {
          multiple: {
              type: Boolean,
              default: false
          },
          information: {
              type: Array,
              default() {
                  return [];
              }
          },
          calendarWidth: {
              type: String
          },
          onPanelDateChange: {
              type: Function,
              default() {
                  return {};
              }
          }
      },
      data() {
          const { selectionMode, value } = this;
  
          const dates = value.slice().sort();
          return {
              prefixCls: prefixCls,
              datePrefixCls: datePrefixCls,
              currentView: selectionMode || 'date',
              pickerTable: this.getTableType(selectionMode),
              dates: dates,
              panelDate: this.startDate || dates[0] || new Date()
          };
      },
      computed: {
          classes() {
              return [`${prefixCls}-body-wrapper`];
          },
          panelPickerHandlers() {
              return this.pickerTable === `${this.currentView}-table`
          ? this.handlePick
          : this.handlePreSelection;
          },
          datePanelLabel() {
              const locale = this.t('i.locale');
              const datePanelLabel = this.t('i.datepicker.datePanelLabel');
              const date = this.panelDate;
  
              const { labels, separator } = formatDateLabels(
          locale,
          datePanelLabel,
          date
        );
  
              const handler = type => {
                  return () => (this.pickerTable = this.getTableType(type));
              };
  
              return {
                  separator: separator,
                  labels: labels.map(obj => ((obj.handler = handler(obj.type)), obj))
              };
          },
  
          timeDisabled() {
              return !this.dates[0];
          }
      },
      watch: {
          panelDate() {
              let date = new Date(this.panelDate);
              date.setHours(0, 0, 0, 0);
              let start = new Date(date);
              start.setDate(1);
              let end = new Date(start);
              end.setMonth(end.getMonth() + 1);
              this.onPanelDateChange(start.getTime(), end.getTime());
          },
          value(newVal) {
              this.dates = newVal;
              const panelDate = this.multiple
          ? this.dates[this.dates.length - 1]
          : this.startDate || this.dates[0];
              this.panelDate = panelDate || new Date();
          },
          currentView(currentView) {
              this.$emit('on-selection-mode-change', currentView);
  
              if (this.currentView === 'time') {
                  this.$nextTick(() => {
                      const spinner = this.$refs.timePicker.$refs.timeSpinner;
                      spinner.updateScroll();
                  });
              }
          },
          selectionMode(type) {
              this.currentView = type;
              this.pickerTable = this.getTableType(type);
          },
          focusedDate(date) {
              const isDifferentYear =
          date.getFullYear() !== this.panelDate.getFullYear();
              const isDifferentMonth =
          isDifferentYear || date.getMonth() !== this.panelDate.getMonth();
              if (isDifferentYear || isDifferentMonth) {
                  if (!this.multiple) this.panelDate = date;
              }
          }
      },
  
      methods: {
          reset() {
              this.currentView = this.selectionMode;
              this.pickerTable = this.getTableType(this.currentView);
          },
          changeYear(dir) {
              if (this.selectionMode === 'year' || this.pickerTable === 'year-table') {
                  this.panelDate = new Date(
            this.panelDate.getFullYear() + dir * 10,
            0,
            1
          );
              } else {
                  this.panelDate = siblingMonth(this.panelDate, dir * 12);
              }
          },
          getTableType(currentView) {
              return currentView.match(/^time/)
          ? 'time-picker'
          : `${currentView}-table`;
          },
          changeMonth(dir) {
              this.panelDate = siblingMonth(this.panelDate, dir);
          },
          handlePreSelection(value) {
              this.panelDate = value;
              if (this.pickerTable === 'year-table') this.pickerTable = 'month-table';
              else this.pickerTable = this.getTableType(this.currentView);
          },
          handlePick(value, type) {
              const { selectionMode, panelDate } = this;
              if (selectionMode === 'year') value = new Date(value.getFullYear(), 0, 1);
              else if (selectionMode === 'month')
                  value = new Date(panelDate.getFullYear(), value.getMonth(), 1);
              else value = new Date(value);
              this.dates = [value];
              this.$emit('on-pick', value, false, type || selectionMode);
          }
      }
  };
  </script>