index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import { VantComponent } from '../common/component';
  2. import { isDef } from '../common/utils';
  3. import { pickerProps } from '../picker/shared';
  4. const currentYear = new Date().getFullYear();
  5. function isValidDate(date) {
  6. return isDef(date) && !isNaN(new Date(date).getTime());
  7. }
  8. function range(num, min, max) {
  9. return Math.min(Math.max(num, min), max);
  10. }
  11. function padZero(val) {
  12. return `00${val}`.slice(-2);
  13. }
  14. function times(n, iteratee) {
  15. let index = -1;
  16. const result = Array(n < 0 ? 0 : n);
  17. while (++index < n) {
  18. result[index] = iteratee(index);
  19. }
  20. return result;
  21. }
  22. function getTrueValue(formattedValue) {
  23. if (!formattedValue)
  24. return;
  25. while (isNaN(parseInt(formattedValue, 10))) {
  26. formattedValue = formattedValue.slice(1);
  27. }
  28. return parseInt(formattedValue, 10);
  29. }
  30. function getMonthEndDay(year, month) {
  31. return 32 - new Date(year, month - 1, 32).getDate();
  32. }
  33. const defaultFormatter = (_, value) => value;
  34. VantComponent({
  35. classes: ['active-class', 'toolbar-class', 'column-class'],
  36. props: Object.assign(Object.assign({}, pickerProps), { value: null, filter: null, type: {
  37. type: String,
  38. value: 'datetime'
  39. }, showToolbar: {
  40. type: Boolean,
  41. value: true
  42. }, formatter: {
  43. type: null,
  44. value: defaultFormatter
  45. }, minDate: {
  46. type: Number,
  47. value: new Date(currentYear - 10, 0, 1).getTime()
  48. }, maxDate: {
  49. type: Number,
  50. value: new Date(currentYear + 10, 11, 31).getTime()
  51. }, minHour: {
  52. type: Number,
  53. value: 0
  54. }, maxHour: {
  55. type: Number,
  56. value: 23
  57. }, minMinute: {
  58. type: Number,
  59. value: 0
  60. }, maxMinute: {
  61. type: Number,
  62. value: 59
  63. } }),
  64. data: {
  65. innerValue: Date.now(),
  66. columns: []
  67. },
  68. watch: {
  69. value: 'updateValue',
  70. type: 'updateValue',
  71. minDate: 'updateValue',
  72. maxDate: 'updateValue',
  73. minHour: 'updateValue',
  74. maxHour: 'updateValue',
  75. minMinute: 'updateValue',
  76. maxMinute: 'updateValue'
  77. },
  78. methods: {
  79. updateValue() {
  80. const { data } = this;
  81. const val = this.correctValue(this.data.value);
  82. const isEqual = val === data.innerValue;
  83. if (!isEqual) {
  84. this.updateColumnValue(val).then(() => {
  85. this.$emit('input', val);
  86. });
  87. }
  88. else {
  89. this.updateColumns();
  90. }
  91. },
  92. getPicker() {
  93. if (this.picker == null) {
  94. this.picker = this.selectComponent('.van-datetime-picker');
  95. const { picker } = this;
  96. const { setColumnValues } = picker;
  97. picker.setColumnValues = (...args) => setColumnValues.apply(picker, [...args, false]);
  98. }
  99. return this.picker;
  100. },
  101. updateColumns() {
  102. const { formatter = defaultFormatter } = this.data;
  103. const results = this.getOriginColumns().map(column => ({
  104. values: column.values.map(value => formatter(column.type, value))
  105. }));
  106. return this.set({ columns: results });
  107. },
  108. getOriginColumns() {
  109. const { filter } = this.data;
  110. const results = this.getRanges().map(({ type, range }) => {
  111. let values = times(range[1] - range[0] + 1, index => {
  112. let value = range[0] + index;
  113. value = type === 'year' ? `${value}` : padZero(value);
  114. return value;
  115. });
  116. if (filter) {
  117. values = filter(type, values);
  118. }
  119. return { type, values };
  120. });
  121. return results;
  122. },
  123. getRanges() {
  124. const { data } = this;
  125. if (data.type === 'time') {
  126. return [
  127. {
  128. type: 'hour',
  129. range: [data.minHour, data.maxHour]
  130. },
  131. {
  132. type: 'minute',
  133. range: [data.minMinute, data.maxMinute]
  134. }
  135. ];
  136. }
  137. const { maxYear, maxDate, maxMonth, maxHour, maxMinute } = this.getBoundary('max', data.innerValue);
  138. const { minYear, minDate, minMonth, minHour, minMinute } = this.getBoundary('min', data.innerValue);
  139. const result = [
  140. {
  141. type: 'year',
  142. range: [minYear, maxYear]
  143. },
  144. {
  145. type: 'month',
  146. range: [minMonth, maxMonth]
  147. },
  148. {
  149. type: 'day',
  150. range: [minDate, maxDate]
  151. },
  152. {
  153. type: 'hour',
  154. range: [minHour, maxHour]
  155. },
  156. {
  157. type: 'minute',
  158. range: [minMinute, maxMinute]
  159. }
  160. ];
  161. if (data.type === 'date')
  162. result.splice(3, 2);
  163. if (data.type === 'year-month')
  164. result.splice(2, 3);
  165. return result;
  166. },
  167. correctValue(value) {
  168. const { data } = this;
  169. // validate value
  170. const isDateType = data.type !== 'time';
  171. if (isDateType && !isValidDate(value)) {
  172. value = data.minDate;
  173. }
  174. else if (!isDateType && !value) {
  175. const { minHour } = data;
  176. value = `${padZero(minHour)}:00`;
  177. }
  178. // time type
  179. if (!isDateType) {
  180. let [hour, minute] = value.split(':');
  181. hour = padZero(range(hour, data.minHour, data.maxHour));
  182. minute = padZero(range(minute, data.minMinute, data.maxMinute));
  183. return `${hour}:${minute}`;
  184. }
  185. // date type
  186. value = Math.max(value, data.minDate);
  187. value = Math.min(value, data.maxDate);
  188. return value;
  189. },
  190. getBoundary(type, innerValue) {
  191. const value = new Date(innerValue);
  192. const boundary = new Date(this.data[`${type}Date`]);
  193. const year = boundary.getFullYear();
  194. let month = 1;
  195. let date = 1;
  196. let hour = 0;
  197. let minute = 0;
  198. if (type === 'max') {
  199. month = 12;
  200. date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
  201. hour = 23;
  202. minute = 59;
  203. }
  204. if (value.getFullYear() === year) {
  205. month = boundary.getMonth() + 1;
  206. if (value.getMonth() + 1 === month) {
  207. date = boundary.getDate();
  208. if (value.getDate() === date) {
  209. hour = boundary.getHours();
  210. if (value.getHours() === hour) {
  211. minute = boundary.getMinutes();
  212. }
  213. }
  214. }
  215. }
  216. return {
  217. [`${type}Year`]: year,
  218. [`${type}Month`]: month,
  219. [`${type}Date`]: date,
  220. [`${type}Hour`]: hour,
  221. [`${type}Minute`]: minute
  222. };
  223. },
  224. onCancel() {
  225. this.$emit('cancel');
  226. },
  227. onConfirm() {
  228. this.$emit('confirm', this.data.innerValue);
  229. },
  230. onChange() {
  231. const { data } = this;
  232. let value;
  233. const picker = this.getPicker();
  234. if (data.type === 'time') {
  235. const indexes = picker.getIndexes();
  236. value = `${+data.columns[0].values[indexes[0]]}:${+data.columns[1].values[indexes[1]]}`;
  237. }
  238. else {
  239. const values = picker.getValues();
  240. const year = getTrueValue(values[0]);
  241. const month = getTrueValue(values[1]);
  242. const maxDate = getMonthEndDay(year, month);
  243. let date = getTrueValue(values[2]);
  244. if (data.type === 'year-month') {
  245. date = 1;
  246. }
  247. date = date > maxDate ? maxDate : date;
  248. let hour = 0;
  249. let minute = 0;
  250. if (data.type === 'datetime') {
  251. hour = getTrueValue(values[3]);
  252. minute = getTrueValue(values[4]);
  253. }
  254. value = new Date(year, month - 1, date, hour, minute);
  255. }
  256. value = this.correctValue(value);
  257. this.updateColumnValue(value).then(() => {
  258. this.$emit('input', value);
  259. this.$emit('change', picker);
  260. });
  261. },
  262. updateColumnValue(value) {
  263. let values = [];
  264. const { type, formatter = defaultFormatter } = this.data;
  265. const picker = this.getPicker();
  266. if (type === 'time') {
  267. const pair = value.split(':');
  268. values = [
  269. formatter('hour', pair[0]),
  270. formatter('minute', pair[1])
  271. ];
  272. }
  273. else {
  274. const date = new Date(value);
  275. values = [
  276. formatter('year', `${date.getFullYear()}`),
  277. formatter('month', padZero(date.getMonth() + 1))
  278. ];
  279. if (type === 'date') {
  280. values.push(formatter('day', padZero(date.getDate())));
  281. }
  282. if (type === 'datetime') {
  283. values.push(formatter('day', padZero(date.getDate())), formatter('hour', padZero(date.getHours())), formatter('minute', padZero(date.getMinutes())));
  284. }
  285. }
  286. return this.set({ innerValue: value })
  287. .then(() => this.updateColumns())
  288. .then(() => picker.setValues(values));
  289. }
  290. },
  291. created() {
  292. const innerValue = this.correctValue(this.data.value);
  293. this.updateColumnValue(innerValue).then(() => {
  294. this.$emit('input', innerValue);
  295. });
  296. }
  297. });