Blame view

src/components/date-picker/util.js 7.35 KB
0f677893   梁灏   update DatePicker
1
2
  import dateUtil from '../../utils/date';
  
0f677893   梁灏   update DatePicker
3
  export const toDate = function(date) {
58ff14d7   Sergio Crisostomo   Make date more IE...
4
5
6
7
8
9
10
11
12
13
14
      let _date = new Date(date);
      // IE patch start (#1422)
      if (isNaN(_date.getTime()) && typeof date === 'string'){
          _date = date.split('-').map(Number);
          _date[1] += 1;
          _date = new Date(..._date);
      }
      // IE patch end
  
      if (isNaN(_date.getTime())) return null;
      return _date;
0f677893   梁灏   update DatePicker
15
16
  };
  
0b51803b   Sergio Crisostomo   Add functions to ...
17
18
19
20
21
22
23
24
25
26
27
28
  export const clearHours = function (time) {
      const cloneDate = new Date(time);
      cloneDate.setHours(0, 0, 0, 0);
      return cloneDate.getTime();
  };
  
  export const isInRange = (time, a, b) => {
      if (!a || !b) return false;
      const [start, end] = [a, b].sort();
      return time >= start && time <= end;
  };
  
0f677893   梁灏   update DatePicker
29
30
31
32
33
34
35
36
37
38
39
  export const formatDate = function(date, format) {
      date = toDate(date);
      if (!date) return '';
      return dateUtil.format(date, format || 'yyyy-MM-dd');
  };
  
  export const parseDate = function(string, format) {
      return dateUtil.parse(string, format || 'yyyy-MM-dd');
  };
  
  export const getDayCountOfMonth = function(year, month) {
c91c30cc   Sergio Crisostomo   Date utils improv...
40
      return new Date(year, month + 1, 0).getDate();
0f677893   梁灏   update DatePicker
41
42
43
44
45
46
47
48
  };
  
  export const getFirstDayOfMonth = function(date) {
      const temp = new Date(date.getTime());
      temp.setDate(1);
      return temp.getDay();
  };
  
c91c30cc   Sergio Crisostomo   Date utils improv...
49
50
51
52
53
54
  export const siblingMonth = function(src, diff) {
      const temp = new Date(src); // lets copy it so we don't change the original
      const newMonth = temp.getMonth() + diff;
      const newMonthDayCount = getDayCountOfMonth(temp.getFullYear(), newMonth);
      if (newMonthDayCount < temp.getDate()) {
          temp.setDate(newMonthDayCount);
0f677893   梁灏   update DatePicker
55
      }
c91c30cc   Sergio Crisostomo   Date utils improv...
56
      temp.setMonth(newMonth);
0f677893   梁灏   update DatePicker
57
  
c91c30cc   Sergio Crisostomo   Date utils improv...
58
59
      return temp;
  };
0f677893   梁灏   update DatePicker
60
  
c91c30cc   Sergio Crisostomo   Date utils improv...
61
62
  export const prevMonth = function(src) {
      return siblingMonth(src, -1);
0f677893   梁灏   update DatePicker
63
64
65
  };
  
  export const nextMonth = function(src) {
c91c30cc   Sergio Crisostomo   Date utils improv...
66
      return siblingMonth(src, 1);
2dbbd7de   梁灏   update TimePicker
67
68
  };
  
c91c30cc   Sergio Crisostomo   Date utils improv...
69
  export const initTimeDate = function() {
2dbbd7de   梁灏   update TimePicker
70
71
72
73
74
      const date = new Date();
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);
      return date;
c91c30cc   Sergio Crisostomo   Date utils improv...
75
  };
b27858dd   Sergio Crisostomo   add date panel la...
76
77
78
79
80
81
  
  export const formatDateLabels = (function() {
      /*
        Formats:
        yyyy - 4 digit year
        m - month, numeric, 1 - 12
3ed12b4e   Sergio Crisostomo   Correct month cal...
82
        mm - month, numeric, 01 - 12
b27858dd   Sergio Crisostomo   add date panel la...
83
84
85
86
87
88
89
90
        mmm - month, 3 letters, as in `toLocaleDateString`
        Mmm - month, 3 letters, capitalize the return from `toLocaleDateString`
        mmmm - month, full name, as in `toLocaleDateString`
        Mmmm - month, full name, capitalize the return from `toLocaleDateString`
      */
  
      const formats = {
          yyyy: date => date.getFullYear(),
3ed12b4e   Sergio Crisostomo   Correct month cal...
91
92
          m: date => date.getMonth() + 1,
          mm: date => ('0' + (date.getMonth() + 1)).slice(-2),
b27858dd   Sergio Crisostomo   add date panel la...
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
          mmm: (date, locale) => {
              const monthName = date.toLocaleDateString(locale, {
                  month: 'long'
              });
              return monthName.slice(0, 3);
          },
          Mmm: (date, locale) => {
              const monthName = date.toLocaleDateString(locale, {
                  month: 'long'
              });
              return (monthName[0].toUpperCase() + monthName.slice(1).toLowerCase()).slice(0, 3);
          },
          mmmm: (date, locale) =>
              date.toLocaleDateString(locale, {
                  month: 'long'
              }),
          Mmmm: (date, locale) => {
              const monthName = date.toLocaleDateString(locale, {
                  month: 'long'
              });
              return monthName[0].toUpperCase() + monthName.slice(1).toLowerCase();
          }
      };
      const formatRegex = new RegExp(['yyyy', 'Mmmm', 'mmmm', 'Mmm', 'mmm', 'mm', 'm'].join('|'), 'g');
  
      return function(locale, format, date) {
          const componetsRegex = /(\[[^\]]+\])([^\[\]]+)(\[[^\]]+\])/;
          const components = format.match(componetsRegex).slice(1);
          const separator = components[1];
          const labels = [components[0], components[2]].map(component => {
              const label = component.replace(/\[[^\]]+\]/, str => {
                  return str.slice(1, -1).replace(formatRegex, match => formats[match](date, locale));
              });
              return {
                  label: label,
236e0bfd   Sergio Crisostomo   DOn't use include...
128
                  type: component.indexOf('yy') != -1 ? 'year' : 'month'
b27858dd   Sergio Crisostomo   add date panel la...
129
130
131
132
133
134
135
136
              };
          });
          return {
              separator: separator,
              labels: labels
          };
      };
  })();
2fb29fae   Sergio Crisostomo   export utilities ...
137
138
139
140
141
142
143
144
145
146
147
148
149
  
  // Parsers and Formaters
  export const DEFAULT_FORMATS = {
      date: 'yyyy-MM-dd',
      month: 'yyyy-MM',
      year: 'yyyy',
      datetime: 'yyyy-MM-dd HH:mm:ss',
      time: 'HH:mm:ss',
      timerange: 'HH:mm:ss',
      daterange: 'yyyy-MM-dd',
      datetimerange: 'yyyy-MM-dd HH:mm:ss'
  };
  
4c534a77   梁灏   fixed #5146 , clo...
150
  // export const RANGE_SEPARATOR = ' - ';  // use picker.vue prop separator
2fb29fae   Sergio Crisostomo   export utilities ...
151
152
153
154
155
156
157
  
  const DATE_FORMATTER = function(value, format) {
      return formatDate(value, format);
  };
  const DATE_PARSER = function(text, format) {
      return parseDate(text, format);
  };
4c534a77   梁灏   fixed #5146 , clo...
158
  const RANGE_FORMATTER = function(value, format, RANGE_SEPARATOR) {
2fb29fae   Sergio Crisostomo   export utilities ...
159
160
161
162
163
164
165
      if (Array.isArray(value) && value.length === 2) {
          const start = value[0];
          const end = value[1];
  
          if (start && end) {
              return formatDate(start, format) + RANGE_SEPARATOR + formatDate(end, format);
          }
57f0582b   Sergio Crisostomo   Pass correct argu...
166
167
      } else if (!Array.isArray(value) && value instanceof Date){
          return formatDate(value, format);
2fb29fae   Sergio Crisostomo   export utilities ...
168
169
170
      }
      return '';
  };
4c534a77   梁灏   fixed #5146 , clo...
171
  const RANGE_PARSER = function(text, format, RANGE_SEPARATOR) {
4a1734b7   Sergio Crisostomo   fix time and time...
172
      const array = Array.isArray(text) ? text : text.split(RANGE_SEPARATOR);
2fb29fae   Sergio Crisostomo   export utilities ...
173
174
175
176
      if (array.length === 2) {
          const range1 = array[0];
          const range2 = array[1];
  
0017507b   SergioCrisostomo   don't use parseDa...
177
178
179
180
          return [
              range1 instanceof Date ? range1 : parseDate(range1, format),
              range2 instanceof Date ? range2 : parseDate(range2, format),
          ];
2fb29fae   Sergio Crisostomo   export utilities ...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
      }
      return [];
  };
  
  export const TYPE_VALUE_RESOLVER_MAP = {
      default: {
          formatter(value) {
              if (!value) return '';
              return '' + value;
          },
          parser(text) {
              if (text === undefined || text === '') return null;
              return text;
          }
      },
      date: {
          formatter: DATE_FORMATTER,
          parser: DATE_PARSER
      },
      datetime: {
          formatter: DATE_FORMATTER,
          parser: DATE_PARSER
      },
      daterange: {
          formatter: RANGE_FORMATTER,
          parser: RANGE_PARSER
      },
      datetimerange: {
          formatter: RANGE_FORMATTER,
          parser: RANGE_PARSER
      },
      timerange: {
          formatter: RANGE_FORMATTER,
          parser: RANGE_PARSER
      },
      time: {
          formatter: DATE_FORMATTER,
          parser: DATE_PARSER
      },
      month: {
          formatter: DATE_FORMATTER,
          parser: DATE_PARSER
      },
      year: {
          formatter: DATE_FORMATTER,
          parser: DATE_PARSER
      },
      multiple: {
          formatter: (value, format) => {
2fb29fae   Sergio Crisostomo   export utilities ...
230
231
              return value.filter(Boolean).map(date => formatDate(date, format)).join(',');
          },
88156b0b   Sergio Crisostomo   Handle data more ...
232
233
234
235
236
237
238
239
240
          parser: (value, format) => {
              const values = typeof value === 'string' ? value.split(',') : value;
              return values.map(value => {
                  if (value instanceof Date) return value;
                  if (typeof value === 'string') value = value.trim();
                  else if (typeof value !== 'number' && !value) value = '';
                  return parseDate(value, format);
              });
          }
2fb29fae   Sergio Crisostomo   export utilities ...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
      },
      number: {
          formatter(value) {
              if (!value) return '';
              return '' + value;
          },
          parser(text) {
              let result = Number(text);
  
              if (!isNaN(text)) {
                  return result;
              } else {
                  return null;
              }
          }
      }
  };