iov-design 2.15.59 → 2.15.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/README.md +148 -148
  2. package/lib/alert.js +24 -105
  3. package/lib/aside.js +24 -34
  4. package/lib/autocomplete.js +36 -240
  5. package/lib/avatar.js +21 -20
  6. package/lib/backtop.js +27 -52
  7. package/lib/badge.js +30 -58
  8. package/lib/breadcrumb-item.js +24 -53
  9. package/lib/breadcrumb.js +24 -37
  10. package/lib/button-group.js +24 -29
  11. package/lib/button.js +38 -60
  12. package/lib/calendar.js +29 -146
  13. package/lib/card.js +24 -51
  14. package/lib/carousel-item.js +30 -68
  15. package/lib/carousel.js +27 -159
  16. package/lib/cascader-panel.js +41 -64
  17. package/lib/cascader.js +37 -470
  18. package/lib/checkbox-button.js +28 -164
  19. package/lib/checkbox-group.js +24 -37
  20. package/lib/checkbox.js +35 -189
  21. package/lib/col.js +1 -0
  22. package/lib/collapse-item.js +27 -125
  23. package/lib/collapse.js +24 -37
  24. package/lib/color-picker.js +104 -424
  25. package/lib/container.js +24 -34
  26. package/lib/date-picker.js +126 -1981
  27. package/lib/descriptions-item.js +1 -0
  28. package/lib/descriptions.js +1 -0
  29. package/lib/dialog.js +24 -137
  30. package/lib/divider.js +29 -64
  31. package/lib/drawer.js +24 -139
  32. package/lib/dropdown-item.js +27 -45
  33. package/lib/dropdown-menu.js +24 -51
  34. package/lib/dropdown.js +21 -20
  35. package/lib/empty.js +24 -61
  36. package/lib/footer.js +24 -34
  37. package/lib/form-item.js +42 -128
  38. package/lib/form.js +27 -40
  39. package/lib/header.js +24 -34
  40. package/lib/icon.js +24 -29
  41. package/lib/image.js +36 -220
  42. package/lib/index.js +1 -1
  43. package/lib/infinite-scroll.js +1 -0
  44. package/lib/input-number.js +30 -163
  45. package/lib/input.js +42 -274
  46. package/lib/iov-design.common.js +1638 -11132
  47. package/lib/link.js +33 -55
  48. package/lib/loading.js +24 -77
  49. package/lib/locale/format.js +10 -10
  50. package/lib/main.js +24 -29
  51. package/lib/menu-item-group.js +24 -40
  52. package/lib/menu-item.js +27 -83
  53. package/lib/menu.js +21 -20
  54. package/lib/message-box.js +24 -321
  55. package/lib/message.js +30 -82
  56. package/lib/mixins/migrating.js +20 -20
  57. package/lib/notification.js +24 -109
  58. package/lib/option-group.js +24 -50
  59. package/lib/option.js +27 -76
  60. package/lib/page-header.js +24 -56
  61. package/lib/pagination.js +24 -97
  62. package/lib/popconfirm.js +24 -97
  63. package/lib/popover.js +24 -89
  64. package/lib/progress.js +30 -133
  65. package/lib/radio-button.js +29 -111
  66. package/lib/radio-group.js +24 -39
  67. package/lib/radio.js +36 -152
  68. package/lib/rate.js +24 -94
  69. package/lib/result.js +72 -187
  70. package/lib/row.js +1 -0
  71. package/lib/scrollbar.js +1 -0
  72. package/lib/select.js +48 -637
  73. package/lib/skeleton-item.js +36 -68
  74. package/lib/skeleton.js +28 -70
  75. package/lib/slider.js +36 -267
  76. package/lib/spinner.js +24 -51
  77. package/lib/statistic.js +24 -76
  78. package/lib/step.js +29 -118
  79. package/lib/steps.js +27 -40
  80. package/lib/submenu.js +21 -20
  81. package/lib/switch.js +24 -130
  82. package/lib/tab-pane.js +24 -52
  83. package/lib/table-column.js +1 -0
  84. package/lib/table.js +93 -633
  85. package/lib/tabs.js +34 -49
  86. package/lib/tag.js +22 -21
  87. package/lib/theme-chalk/base.css +1 -1
  88. package/lib/theme-chalk/button.css +1 -1
  89. package/lib/theme-chalk/calendar.css +1 -1
  90. package/lib/theme-chalk/cascader-panel.css +1 -1
  91. package/lib/theme-chalk/cascader.css +1 -1
  92. package/lib/theme-chalk/dropdown.css +1 -1
  93. package/lib/theme-chalk/index.css +1 -1
  94. package/lib/theme-chalk/iovfont.css +1 -1
  95. package/lib/theme-chalk/link.css +1 -1
  96. package/lib/theme-chalk/message-box.css +1 -1
  97. package/lib/theme-chalk/pagination.css +1 -1
  98. package/lib/theme-chalk/radio.css +1 -1
  99. package/lib/theme-chalk/select.css +1 -1
  100. package/lib/theme-chalk/table-column.css +1 -1
  101. package/lib/theme-chalk/table.css +1 -1
  102. package/lib/theme-chalk/tabs.css +1 -1
  103. package/lib/theme-chalk/tag.css +1 -1
  104. package/lib/theme-chalk/transfer.css +1 -1
  105. package/lib/time-picker.js +58 -820
  106. package/lib/time-select.js +42 -303
  107. package/lib/timeline-item.js +29 -75
  108. package/lib/timeline.js +21 -20
  109. package/lib/tooltip.js +1 -0
  110. package/lib/transfer.js +36 -299
  111. package/lib/tree.js +54 -243
  112. package/lib/upload.js +86 -654
  113. package/lib/utils/clickoutside.js +7 -7
  114. package/lib/utils/vue-popper.js +7 -7
  115. package/package.json +154 -154
  116. package/packages/autocomplete/src/autocomplete-suggestions.vue +76 -76
  117. package/packages/autocomplete/src/autocomplete.vue +285 -285
  118. package/packages/button/src/button.vue +90 -90
  119. package/packages/calendar/src/date-table.vue +200 -200
  120. package/packages/calendar/src/main.vue +280 -280
  121. package/packages/carousel/src/item.vue +138 -138
  122. package/packages/carousel/src/main.vue +315 -315
  123. package/packages/cascader/src/cascader.vue +776 -776
  124. package/packages/cascader-panel/src/cascader-menu.vue +138 -138
  125. package/packages/cascader-panel/src/cascader-node.vue +246 -246
  126. package/packages/cascader-panel/src/cascader-panel.vue +391 -391
  127. package/packages/cascader-panel/src/node.js +166 -166
  128. package/packages/cascader-panel/src/store.js +58 -58
  129. package/packages/checkbox/src/checkbox-button.vue +199 -199
  130. package/packages/checkbox/src/checkbox-group.vue +49 -49
  131. package/packages/checkbox/src/checkbox.vue +225 -225
  132. package/packages/collapse/src/collapse-item.vue +114 -114
  133. package/packages/color-picker/src/components/picker-dropdown.vue +121 -121
  134. package/packages/color-picker/src/main.vue +188 -188
  135. package/packages/date-picker/src/basic/date-table.vue +441 -441
  136. package/packages/date-picker/src/basic/month-table.vue +269 -269
  137. package/packages/date-picker/src/basic/time-spinner.vue +304 -304
  138. package/packages/date-picker/src/basic/year-table.vue +111 -111
  139. package/packages/date-picker/src/panel/date-range.vue +680 -680
  140. package/packages/date-picker/src/panel/date.vue +609 -609
  141. package/packages/date-picker/src/panel/month-range.vue +289 -289
  142. package/packages/date-picker/src/panel/time-range.vue +248 -248
  143. package/packages/date-picker/src/panel/time-select.vue +178 -178
  144. package/packages/date-picker/src/panel/time.vue +186 -186
  145. package/packages/date-picker/src/picker.vue +967 -967
  146. package/packages/descriptions/src/index.js +180 -180
  147. package/packages/dialog/src/component.vue +262 -262
  148. package/packages/drawer/src/main.vue +205 -205
  149. package/packages/dropdown/src/dropdown-item.vue +37 -37
  150. package/packages/dropdown/src/dropdown-menu.vue +63 -63
  151. package/packages/dropdown/src/dropdown.vue +293 -293
  152. package/packages/empty/src/index.vue +70 -70
  153. package/packages/form/src/form-item.vue +324 -324
  154. package/packages/form/src/form.vue +182 -182
  155. package/packages/image/src/image-viewer.vue +330 -330
  156. package/packages/image/src/main.vue +249 -249
  157. package/packages/infinite-scroll/src/main.js +150 -150
  158. package/packages/input/src/input.vue +477 -477
  159. package/packages/input-number/src/input-number.vue +283 -283
  160. package/packages/link/src/main.vue +2 -0
  161. package/packages/loading/src/directive.js +133 -133
  162. package/packages/loading/src/index.js +106 -106
  163. package/packages/menu/src/menu-item.vue +112 -112
  164. package/packages/menu/src/menu.vue +325 -325
  165. package/packages/menu/src/submenu.vue +349 -349
  166. package/packages/message/src/main.js +91 -91
  167. package/packages/message-box/src/main.js +216 -216
  168. package/packages/message-box/src/main.vue +333 -333
  169. package/packages/notification/src/main.js +94 -94
  170. package/packages/page-header/src/main.vue +30 -30
  171. package/packages/pagination/src/pagination.js +390 -390
  172. package/packages/popconfirm/src/main.vue +104 -104
  173. package/packages/popover/src/main.vue +239 -239
  174. package/packages/radio/src/radio-button.vue +115 -115
  175. package/packages/radio/src/radio-group.vue +115 -115
  176. package/packages/radio/src/radio.vue +148 -148
  177. package/packages/rate/src/main.vue +348 -348
  178. package/packages/scrollbar/src/bar.js +92 -92
  179. package/packages/scrollbar/src/main.js +130 -130
  180. package/packages/select/src/option-group.vue +60 -60
  181. package/packages/select/src/option.vue +171 -171
  182. package/packages/select/src/select-dropdown.vue +74 -74
  183. package/packages/select/src/select.vue +979 -979
  184. package/packages/slider/src/button.vue +238 -238
  185. package/packages/slider/src/main.vue +427 -427
  186. package/packages/statistic/src/main.vue +204 -204
  187. package/packages/steps/src/steps.vue +68 -68
  188. package/packages/switch/src/component.vue +182 -182
  189. package/packages/table/src/config.js +153 -153
  190. package/packages/table/src/filter-panel.vue +194 -194
  191. package/packages/table/src/store/current.js +76 -76
  192. package/packages/table/src/store/helper.js +41 -41
  193. package/packages/table/src/store/index.js +147 -147
  194. package/packages/table/src/store/watcher.js +502 -502
  195. package/packages/table/src/table-body.js +469 -469
  196. package/packages/table/src/table-column.js +328 -328
  197. package/packages/table/src/table-header.js +571 -571
  198. package/packages/table/src/table-layout.js +249 -249
  199. package/packages/table/src/table-row.js +101 -101
  200. package/packages/table/src/table.vue +740 -740
  201. package/packages/table/src/util.js +273 -273
  202. package/packages/tabs/src/tab-bar.vue +57 -57
  203. package/packages/tabs/src/tab-nav.vue +294 -294
  204. package/packages/tabs/src/tabs.vue +201 -201
  205. package/packages/tag/src/tag.vue +1 -1
  206. package/packages/theme-chalk/src/button.scss +416 -416
  207. package/packages/theme-chalk/src/cascader.scss +252 -252
  208. package/packages/theme-chalk/src/checkbox.scss +419 -419
  209. package/packages/theme-chalk/src/common/var.scss +795 -816
  210. package/packages/theme-chalk/src/form.scss +201 -201
  211. package/packages/theme-chalk/src/input.scss +591 -591
  212. package/packages/theme-chalk/src/iovfont.scss +803 -784
  213. package/packages/theme-chalk/src/link.scss +36 -19
  214. package/packages/theme-chalk/src/message-box.scss +207 -207
  215. package/packages/theme-chalk/src/mixins/_button.scss +136 -136
  216. package/packages/theme-chalk/src/mixins/mixins.scss +190 -190
  217. package/packages/theme-chalk/src/radio-button.scss +115 -115
  218. package/packages/theme-chalk/src/radio.scss +1 -0
  219. package/packages/theme-chalk/src/select.scss +270 -270
  220. package/packages/theme-chalk/src/table.scss +1 -2
  221. package/packages/theme-chalk/src/tabs.scss +755 -750
  222. package/packages/theme-chalk/src/tag.scss +4 -4
  223. package/packages/theme-chalk/src/upload.scss +568 -568
  224. package/packages/tooltip/src/main.js +242 -242
  225. package/packages/transfer/src/main.vue +231 -231
  226. package/packages/transfer/src/transfer-panel.vue +251 -251
  227. package/packages/tree/src/model/node.js +484 -484
  228. package/packages/tree/src/tree-node.vue +279 -279
  229. package/packages/tree/src/tree.vue +496 -496
  230. package/packages/upload/src/index.vue +420 -420
  231. package/packages/upload/src/upload-file.vue +98 -98
  232. package/packages/upload/src/upload-list.vue +115 -115
  233. package/packages/upload/src/upload-picture.vue +98 -98
  234. package/packages/upload/src/upload.vue +231 -231
  235. package/packages/upload/src/utils.js +31 -31
  236. package/src/directives/repeat-click.js +26 -26
  237. package/src/index.js +259 -259
  238. package/src/locale/format.js +46 -46
  239. package/src/locale/index.js +48 -48
  240. package/src/locale/lang/lo-LA.js +126 -126
  241. package/src/mixins/locale.js +9 -9
  242. package/src/mixins/migrating.js +54 -54
  243. package/src/transitions/collapse-transition.js +77 -77
  244. package/src/utils/clickoutside.js +76 -76
  245. package/src/utils/date-util.js +282 -282
  246. package/src/utils/popup/index.js +218 -218
  247. package/src/utils/popup/popup-manager.js +194 -194
  248. package/src/utils/util.js +245 -245
  249. package/src/utils/vdom.js +5 -5
  250. package/src/utils/vue-popper.js +198 -198
@@ -1,282 +1,282 @@
1
- import fecha from 'iov-design/src/utils/date';
2
- import { t } from 'iov-design/src/locale';
3
-
4
- const weeks = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
5
- const months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
6
-
7
- const newArray = function(start, end) {
8
- let result = [];
9
- for (let i = start; i <= end; i++) {
10
- result.push(i);
11
- }
12
- return result;
13
- };
14
-
15
- export const getI18nSettings = () => {
16
- return {
17
- dayNamesShort: weeks.map(week => t(`el.datepicker.weeks.${ week }`)),
18
- dayNames: weeks.map(week => t(`el.datepicker.weeks.${ week }`)),
19
- monthNamesShort: months.map(month => t(`el.datepicker.months.${ month }`)),
20
- monthNames: months.map((month, index) => t(`el.datepicker.month${ index + 1 }`)),
21
- amPm: ['am', 'pm']
22
- };
23
- };
24
-
25
- export const toDate = function(date) {
26
- return isDate(date) ? new Date(date) : null;
27
- };
28
-
29
- export const isDate = function(date) {
30
- if (date === null || date === undefined) return false;
31
- if (isNaN(new Date(date).getTime())) return false;
32
- if (Array.isArray(date)) return false; // deal with `new Date([ new Date() ]) -> new Date()`
33
- return true;
34
- };
35
-
36
- export const isDateObject = function(val) {
37
- return val instanceof Date;
38
- };
39
-
40
- export const formatDate = function(date, format) {
41
- date = toDate(date);
42
- if (!date) return '';
43
- return fecha.format(date, format || 'yyyy-MM-dd', getI18nSettings());
44
- };
45
-
46
- export const parseDate = function(string, format) {
47
- return fecha.parse(string, format || 'yyyy-MM-dd', getI18nSettings());
48
- };
49
-
50
- export const getDayCountOfMonth = function(year, month) {
51
- if (isNaN(+month)) return 31;
52
-
53
- return new Date(year, +month + 1, 0).getDate();
54
- };
55
-
56
- export const getDayCountOfYear = function(year) {
57
- const isLeapYear = year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
58
- return isLeapYear ? 366 : 365;
59
- };
60
-
61
- export const getFirstDayOfMonth = function(date) {
62
- const temp = new Date(date.getTime());
63
- temp.setDate(1);
64
- return temp.getDay();
65
- };
66
-
67
- // see: https://stackoverflow.com/questions/3674539/incrementing-a-date-in-javascript
68
- // {prev, next} Date should work for Daylight Saving Time
69
- // Adding 24 * 60 * 60 * 1000 does not work in the above scenario
70
- export const prevDate = function(date, amount = 1) {
71
- return new Date(date.getFullYear(), date.getMonth(), date.getDate() - amount);
72
- };
73
-
74
- export const nextDate = function(date, amount = 1) {
75
- return new Date(date.getFullYear(), date.getMonth(), date.getDate() + amount);
76
- };
77
-
78
- export const getStartDateOfMonth = function(year, month) {
79
- const result = new Date(year, month, 1);
80
- const day = result.getDay();
81
-
82
- if (day === 0) {
83
- return prevDate(result, 7);
84
- } else {
85
- return prevDate(result, day);
86
- }
87
- };
88
-
89
- export const getWeekNumber = function(src) {
90
- if (!isDate(src)) return null;
91
- const date = new Date(src.getTime());
92
- date.setHours(0, 0, 0, 0);
93
- // Thursday in current week decides the year.
94
- date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
95
- // January 4 is always in week 1.
96
- const week1 = new Date(date.getFullYear(), 0, 4);
97
- // Adjust to Thursday in week 1 and count number of weeks from date to week 1.
98
- // Rounding should be fine for Daylight Saving Time. Its shift should never be more than 12 hours.
99
- return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
100
- };
101
-
102
- export const getRangeHours = function(ranges) {
103
- const hours = [];
104
- let disabledHours = [];
105
-
106
- (ranges || []).forEach(range => {
107
- const value = range.map(date => date.getHours());
108
-
109
- disabledHours = disabledHours.concat(newArray(value[0], value[1]));
110
- });
111
-
112
- if (disabledHours.length) {
113
- for (let i = 0; i < 24; i++) {
114
- hours[i] = disabledHours.indexOf(i) === -1;
115
- }
116
- } else {
117
- for (let i = 0; i < 24; i++) {
118
- hours[i] = false;
119
- }
120
- }
121
-
122
- return hours;
123
- };
124
-
125
- export const getPrevMonthLastDays = (date, amount) => {
126
- if (amount <= 0) return [];
127
- const temp = new Date(date.getTime());
128
- temp.setDate(0);
129
- const lastDay = temp.getDate();
130
- return range(amount).map((_, index) => lastDay - (amount - index - 1));
131
- };
132
-
133
- export const getMonthDays = (date) => {
134
- const temp = new Date(date.getFullYear(), date.getMonth() + 1, 0);
135
- const days = temp.getDate();
136
- return range(days).map((_, index) => index + 1);
137
- };
138
-
139
- function setRangeData(arr, start, end, value) {
140
- for (let i = start; i < end; i++) {
141
- arr[i] = value;
142
- }
143
- }
144
-
145
- export const getRangeMinutes = function(ranges, hour) {
146
- const minutes = new Array(60);
147
-
148
- if (ranges.length > 0) {
149
- ranges.forEach(range => {
150
- const start = range[0];
151
- const end = range[1];
152
- const startHour = start.getHours();
153
- const startMinute = start.getMinutes();
154
- const endHour = end.getHours();
155
- const endMinute = end.getMinutes();
156
- if (startHour === hour && endHour !== hour) {
157
- setRangeData(minutes, startMinute, 60, true);
158
- } else if (startHour === hour && endHour === hour) {
159
- setRangeData(minutes, startMinute, endMinute + 1, true);
160
- } else if (startHour !== hour && endHour === hour) {
161
- setRangeData(minutes, 0, endMinute + 1, true);
162
- } else if (startHour < hour && endHour > hour) {
163
- setRangeData(minutes, 0, 60, true);
164
- }
165
- });
166
- } else {
167
- setRangeData(minutes, 0, 60, true);
168
- }
169
- return minutes;
170
- };
171
-
172
- export const range = function(n) {
173
- // see https://stackoverflow.com/questions/3746725/create-a-javascript-array-containing-1-n
174
- return Array.apply(null, {length: n}).map((_, n) => n);
175
- };
176
-
177
- export const modifyDate = function(date, y, m, d) {
178
- return new Date(y, m, d, date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
179
- };
180
-
181
- export const modifyTime = function(date, h, m, s) {
182
- return new Date(date.getFullYear(), date.getMonth(), date.getDate(), h, m, s, date.getMilliseconds());
183
- };
184
-
185
- export const modifyWithTimeString = (date, time) => {
186
- if (date == null || !time) {
187
- return date;
188
- }
189
- time = parseDate(time, 'HH:mm:ss');
190
- return modifyTime(date, time.getHours(), time.getMinutes(), time.getSeconds());
191
- };
192
-
193
- export const clearTime = function(date) {
194
- return new Date(date.getFullYear(), date.getMonth(), date.getDate());
195
- };
196
-
197
- export const clearMilliseconds = function(date) {
198
- return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), 0);
199
- };
200
-
201
- export const limitTimeRange = function(date, ranges, format = 'HH:mm:ss') {
202
- // TODO: refactory a more elegant solution
203
- if (ranges.length === 0) return date;
204
- const normalizeDate = date => fecha.parse(fecha.format(date, format), format);
205
- const ndate = normalizeDate(date);
206
- const nranges = ranges.map(range => range.map(normalizeDate));
207
- if (nranges.some(nrange => ndate >= nrange[0] && ndate <= nrange[1])) return date;
208
-
209
- let minDate = nranges[0][0];
210
- let maxDate = nranges[0][0];
211
-
212
- nranges.forEach(nrange => {
213
- minDate = new Date(Math.min(nrange[0], minDate));
214
- maxDate = new Date(Math.max(nrange[1], minDate));
215
- });
216
-
217
- const ret = ndate < minDate ? minDate : maxDate;
218
- // preserve Year/Month/Date
219
- return modifyDate(
220
- ret,
221
- date.getFullYear(),
222
- date.getMonth(),
223
- date.getDate()
224
- );
225
- };
226
-
227
- export const timeWithinRange = function(date, selectableRange, format) {
228
- const limitedDate = limitTimeRange(date, selectableRange, format);
229
- return limitedDate.getTime() === date.getTime();
230
- };
231
-
232
- export const changeYearMonthAndClampDate = function(date, year, month) {
233
- // clamp date to the number of days in `year`, `month`
234
- // eg: (2010-1-31, 2010, 2) => 2010-2-28
235
- const monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month));
236
- return modifyDate(date, year, month, monthDate);
237
- };
238
-
239
- export const prevMonth = function(date) {
240
- const year = date.getFullYear();
241
- const month = date.getMonth();
242
- return month === 0
243
- ? changeYearMonthAndClampDate(date, year - 1, 11)
244
- : changeYearMonthAndClampDate(date, year, month - 1);
245
- };
246
-
247
- export const nextMonth = function(date) {
248
- const year = date.getFullYear();
249
- const month = date.getMonth();
250
- return month === 11
251
- ? changeYearMonthAndClampDate(date, year + 1, 0)
252
- : changeYearMonthAndClampDate(date, year, month + 1);
253
- };
254
-
255
- export const prevYear = function(date, amount = 1) {
256
- const year = date.getFullYear();
257
- const month = date.getMonth();
258
- return changeYearMonthAndClampDate(date, year - amount, month);
259
- };
260
-
261
- export const nextYear = function(date, amount = 1) {
262
- const year = date.getFullYear();
263
- const month = date.getMonth();
264
- return changeYearMonthAndClampDate(date, year + amount, month);
265
- };
266
-
267
- export const extractDateFormat = function(format) {
268
- return format
269
- .replace(/\W?m{1,2}|\W?ZZ/g, '')
270
- .replace(/\W?h{1,2}|\W?s{1,3}|\W?a/gi, '')
271
- .trim();
272
- };
273
-
274
- export const extractTimeFormat = function(format) {
275
- return format
276
- .replace(/\W?D{1,2}|\W?Do|\W?d{1,4}|\W?M{1,4}|\W?y{2,4}/g, '')
277
- .trim();
278
- };
279
-
280
- export const validateRangeInOneMonth = function(start, end) {
281
- return (start.getMonth() === end.getMonth()) && (start.getFullYear() === end.getFullYear());
282
- };
1
+ import fecha from 'iov-design/src/utils/date';
2
+ import { t } from 'iov-design/src/locale';
3
+
4
+ const weeks = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
5
+ const months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
6
+
7
+ const newArray = function(start, end) {
8
+ let result = [];
9
+ for (let i = start; i <= end; i++) {
10
+ result.push(i);
11
+ }
12
+ return result;
13
+ };
14
+
15
+ export const getI18nSettings = () => {
16
+ return {
17
+ dayNamesShort: weeks.map(week => t(`el.datepicker.weeks.${ week }`)),
18
+ dayNames: weeks.map(week => t(`el.datepicker.weeks.${ week }`)),
19
+ monthNamesShort: months.map(month => t(`el.datepicker.months.${ month }`)),
20
+ monthNames: months.map((month, index) => t(`el.datepicker.month${ index + 1 }`)),
21
+ amPm: ['am', 'pm']
22
+ };
23
+ };
24
+
25
+ export const toDate = function(date) {
26
+ return isDate(date) ? new Date(date) : null;
27
+ };
28
+
29
+ export const isDate = function(date) {
30
+ if (date === null || date === undefined) return false;
31
+ if (isNaN(new Date(date).getTime())) return false;
32
+ if (Array.isArray(date)) return false; // deal with `new Date([ new Date() ]) -> new Date()`
33
+ return true;
34
+ };
35
+
36
+ export const isDateObject = function(val) {
37
+ return val instanceof Date;
38
+ };
39
+
40
+ export const formatDate = function(date, format) {
41
+ date = toDate(date);
42
+ if (!date) return '';
43
+ return fecha.format(date, format || 'yyyy-MM-dd', getI18nSettings());
44
+ };
45
+
46
+ export const parseDate = function(string, format) {
47
+ return fecha.parse(string, format || 'yyyy-MM-dd', getI18nSettings());
48
+ };
49
+
50
+ export const getDayCountOfMonth = function(year, month) {
51
+ if (isNaN(+month)) return 31;
52
+
53
+ return new Date(year, +month + 1, 0).getDate();
54
+ };
55
+
56
+ export const getDayCountOfYear = function(year) {
57
+ const isLeapYear = year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
58
+ return isLeapYear ? 366 : 365;
59
+ };
60
+
61
+ export const getFirstDayOfMonth = function(date) {
62
+ const temp = new Date(date.getTime());
63
+ temp.setDate(1);
64
+ return temp.getDay();
65
+ };
66
+
67
+ // see: https://stackoverflow.com/questions/3674539/incrementing-a-date-in-javascript
68
+ // {prev, next} Date should work for Daylight Saving Time
69
+ // Adding 24 * 60 * 60 * 1000 does not work in the above scenario
70
+ export const prevDate = function(date, amount = 1) {
71
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate() - amount);
72
+ };
73
+
74
+ export const nextDate = function(date, amount = 1) {
75
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate() + amount);
76
+ };
77
+
78
+ export const getStartDateOfMonth = function(year, month) {
79
+ const result = new Date(year, month, 1);
80
+ const day = result.getDay();
81
+
82
+ if (day === 0) {
83
+ return prevDate(result, 7);
84
+ } else {
85
+ return prevDate(result, day);
86
+ }
87
+ };
88
+
89
+ export const getWeekNumber = function(src) {
90
+ if (!isDate(src)) return null;
91
+ const date = new Date(src.getTime());
92
+ date.setHours(0, 0, 0, 0);
93
+ // Thursday in current week decides the year.
94
+ date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
95
+ // January 4 is always in week 1.
96
+ const week1 = new Date(date.getFullYear(), 0, 4);
97
+ // Adjust to Thursday in week 1 and count number of weeks from date to week 1.
98
+ // Rounding should be fine for Daylight Saving Time. Its shift should never be more than 12 hours.
99
+ return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
100
+ };
101
+
102
+ export const getRangeHours = function(ranges) {
103
+ const hours = [];
104
+ let disabledHours = [];
105
+
106
+ (ranges || []).forEach(range => {
107
+ const value = range.map(date => date.getHours());
108
+
109
+ disabledHours = disabledHours.concat(newArray(value[0], value[1]));
110
+ });
111
+
112
+ if (disabledHours.length) {
113
+ for (let i = 0; i < 24; i++) {
114
+ hours[i] = disabledHours.indexOf(i) === -1;
115
+ }
116
+ } else {
117
+ for (let i = 0; i < 24; i++) {
118
+ hours[i] = false;
119
+ }
120
+ }
121
+
122
+ return hours;
123
+ };
124
+
125
+ export const getPrevMonthLastDays = (date, amount) => {
126
+ if (amount <= 0) return [];
127
+ const temp = new Date(date.getTime());
128
+ temp.setDate(0);
129
+ const lastDay = temp.getDate();
130
+ return range(amount).map((_, index) => lastDay - (amount - index - 1));
131
+ };
132
+
133
+ export const getMonthDays = (date) => {
134
+ const temp = new Date(date.getFullYear(), date.getMonth() + 1, 0);
135
+ const days = temp.getDate();
136
+ return range(days).map((_, index) => index + 1);
137
+ };
138
+
139
+ function setRangeData(arr, start, end, value) {
140
+ for (let i = start; i < end; i++) {
141
+ arr[i] = value;
142
+ }
143
+ }
144
+
145
+ export const getRangeMinutes = function(ranges, hour) {
146
+ const minutes = new Array(60);
147
+
148
+ if (ranges.length > 0) {
149
+ ranges.forEach(range => {
150
+ const start = range[0];
151
+ const end = range[1];
152
+ const startHour = start.getHours();
153
+ const startMinute = start.getMinutes();
154
+ const endHour = end.getHours();
155
+ const endMinute = end.getMinutes();
156
+ if (startHour === hour && endHour !== hour) {
157
+ setRangeData(minutes, startMinute, 60, true);
158
+ } else if (startHour === hour && endHour === hour) {
159
+ setRangeData(minutes, startMinute, endMinute + 1, true);
160
+ } else if (startHour !== hour && endHour === hour) {
161
+ setRangeData(minutes, 0, endMinute + 1, true);
162
+ } else if (startHour < hour && endHour > hour) {
163
+ setRangeData(minutes, 0, 60, true);
164
+ }
165
+ });
166
+ } else {
167
+ setRangeData(minutes, 0, 60, true);
168
+ }
169
+ return minutes;
170
+ };
171
+
172
+ export const range = function(n) {
173
+ // see https://stackoverflow.com/questions/3746725/create-a-javascript-array-containing-1-n
174
+ return Array.apply(null, {length: n}).map((_, n) => n);
175
+ };
176
+
177
+ export const modifyDate = function(date, y, m, d) {
178
+ return new Date(y, m, d, date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
179
+ };
180
+
181
+ export const modifyTime = function(date, h, m, s) {
182
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate(), h, m, s, date.getMilliseconds());
183
+ };
184
+
185
+ export const modifyWithTimeString = (date, time) => {
186
+ if (date == null || !time) {
187
+ return date;
188
+ }
189
+ time = parseDate(time, 'HH:mm:ss');
190
+ return modifyTime(date, time.getHours(), time.getMinutes(), time.getSeconds());
191
+ };
192
+
193
+ export const clearTime = function(date) {
194
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate());
195
+ };
196
+
197
+ export const clearMilliseconds = function(date) {
198
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), 0);
199
+ };
200
+
201
+ export const limitTimeRange = function(date, ranges, format = 'HH:mm:ss') {
202
+ // TODO: refactory a more elegant solution
203
+ if (ranges.length === 0) return date;
204
+ const normalizeDate = date => fecha.parse(fecha.format(date, format), format);
205
+ const ndate = normalizeDate(date);
206
+ const nranges = ranges.map(range => range.map(normalizeDate));
207
+ if (nranges.some(nrange => ndate >= nrange[0] && ndate <= nrange[1])) return date;
208
+
209
+ let minDate = nranges[0][0];
210
+ let maxDate = nranges[0][0];
211
+
212
+ nranges.forEach(nrange => {
213
+ minDate = new Date(Math.min(nrange[0], minDate));
214
+ maxDate = new Date(Math.max(nrange[1], minDate));
215
+ });
216
+
217
+ const ret = ndate < minDate ? minDate : maxDate;
218
+ // preserve Year/Month/Date
219
+ return modifyDate(
220
+ ret,
221
+ date.getFullYear(),
222
+ date.getMonth(),
223
+ date.getDate()
224
+ );
225
+ };
226
+
227
+ export const timeWithinRange = function(date, selectableRange, format) {
228
+ const limitedDate = limitTimeRange(date, selectableRange, format);
229
+ return limitedDate.getTime() === date.getTime();
230
+ };
231
+
232
+ export const changeYearMonthAndClampDate = function(date, year, month) {
233
+ // clamp date to the number of days in `year`, `month`
234
+ // eg: (2010-1-31, 2010, 2) => 2010-2-28
235
+ const monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month));
236
+ return modifyDate(date, year, month, monthDate);
237
+ };
238
+
239
+ export const prevMonth = function(date) {
240
+ const year = date.getFullYear();
241
+ const month = date.getMonth();
242
+ return month === 0
243
+ ? changeYearMonthAndClampDate(date, year - 1, 11)
244
+ : changeYearMonthAndClampDate(date, year, month - 1);
245
+ };
246
+
247
+ export const nextMonth = function(date) {
248
+ const year = date.getFullYear();
249
+ const month = date.getMonth();
250
+ return month === 11
251
+ ? changeYearMonthAndClampDate(date, year + 1, 0)
252
+ : changeYearMonthAndClampDate(date, year, month + 1);
253
+ };
254
+
255
+ export const prevYear = function(date, amount = 1) {
256
+ const year = date.getFullYear();
257
+ const month = date.getMonth();
258
+ return changeYearMonthAndClampDate(date, year - amount, month);
259
+ };
260
+
261
+ export const nextYear = function(date, amount = 1) {
262
+ const year = date.getFullYear();
263
+ const month = date.getMonth();
264
+ return changeYearMonthAndClampDate(date, year + amount, month);
265
+ };
266
+
267
+ export const extractDateFormat = function(format) {
268
+ return format
269
+ .replace(/\W?m{1,2}|\W?ZZ/g, '')
270
+ .replace(/\W?h{1,2}|\W?s{1,3}|\W?a/gi, '')
271
+ .trim();
272
+ };
273
+
274
+ export const extractTimeFormat = function(format) {
275
+ return format
276
+ .replace(/\W?D{1,2}|\W?Do|\W?d{1,4}|\W?M{1,4}|\W?y{2,4}/g, '')
277
+ .trim();
278
+ };
279
+
280
+ export const validateRangeInOneMonth = function(start, end) {
281
+ return (start.getMonth() === end.getMonth()) && (start.getFullYear() === end.getFullYear());
282
+ };