index.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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({}, pickerProps, { formatter: {
  37. type: Function,
  38. value: defaultFormatter
  39. }, value: null, type: {
  40. type: String,
  41. value: 'datetime'
  42. }, showToolbar: {
  43. type: Boolean,
  44. value: true
  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.getRanges().map(({ type, range }) => {
  104. const values = times(range[1] - range[0] + 1, index => {
  105. let value = range[0] + index;
  106. value = type === 'year' ? `${value}` : padZero(value);
  107. return formatter(type, value);
  108. });
  109. return { values };
  110. });
  111. return this.set({ columns: results });
  112. },
  113. getRanges() {
  114. const { data } = this;
  115. if (data.type === 'time') {
  116. return [
  117. {
  118. type: 'hour',
  119. range: [data.minHour, data.maxHour]
  120. },
  121. {
  122. type: 'minute',
  123. range: [data.minMinute, data.maxMinute]
  124. }
  125. ];
  126. }
  127. const { maxYear, maxDate, maxMonth, maxHour, maxMinute } = this.getBoundary('max', data.innerValue);
  128. const { minYear, minDate, minMonth, minHour, minMinute } = this.getBoundary('min', data.innerValue);
  129. const result = [
  130. {
  131. type: 'year',
  132. range: [minYear, maxYear]
  133. },
  134. {
  135. type: 'month',
  136. range: [minMonth, maxMonth]
  137. },
  138. {
  139. type: 'day',
  140. range: [minDate, maxDate]
  141. },
  142. {
  143. type: 'hour',
  144. range: [minHour, maxHour]
  145. },
  146. {
  147. type: 'minute',
  148. range: [minMinute, maxMinute]
  149. }
  150. ];
  151. if (data.type === 'date')
  152. result.splice(3, 2);
  153. if (data.type === 'year-month')
  154. result.splice(2, 3);
  155. return result;
  156. },
  157. correctValue(value) {
  158. const { data } = this;
  159. // validate value
  160. const isDateType = data.type !== 'time';
  161. if (isDateType && !isValidDate(value)) {
  162. value = data.minDate;
  163. }
  164. else if (!isDateType && !value) {
  165. const { minHour } = data;
  166. value = `${padZero(minHour)}:00`;
  167. }
  168. // time type
  169. if (!isDateType) {
  170. let [hour, minute] = value.split(':');
  171. hour = padZero(range(hour, data.minHour, data.maxHour));
  172. minute = padZero(range(minute, data.minMinute, data.maxMinute));
  173. return `${hour}:${minute}`;
  174. }
  175. // date type
  176. value = Math.max(value, data.minDate);
  177. value = Math.min(value, data.maxDate);
  178. return value;
  179. },
  180. getBoundary(type, innerValue) {
  181. const value = new Date(innerValue);
  182. const boundary = new Date(this.data[`${type}Date`]);
  183. const year = boundary.getFullYear();
  184. let month = 1;
  185. let date = 1;
  186. let hour = 0;
  187. let minute = 0;
  188. if (type === 'max') {
  189. month = 12;
  190. date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
  191. hour = 23;
  192. minute = 59;
  193. }
  194. if (value.getFullYear() === year) {
  195. month = boundary.getMonth() + 1;
  196. if (value.getMonth() + 1 === month) {
  197. date = boundary.getDate();
  198. if (value.getDate() === date) {
  199. hour = boundary.getHours();
  200. if (value.getHours() === hour) {
  201. minute = boundary.getMinutes();
  202. }
  203. }
  204. }
  205. }
  206. return {
  207. [`${type}Year`]: year,
  208. [`${type}Month`]: month,
  209. [`${type}Date`]: date,
  210. [`${type}Hour`]: hour,
  211. [`${type}Minute`]: minute
  212. };
  213. },
  214. onCancel() {
  215. this.$emit('cancel');
  216. },
  217. onConfirm() {
  218. this.$emit('confirm', this.data.innerValue);
  219. },
  220. onChange() {
  221. const { data } = this;
  222. let value;
  223. const picker = this.getPicker();
  224. if (data.type === 'time') {
  225. const indexes = picker.getIndexes();
  226. value = `${indexes[0] + data.minHour}:${indexes[1] + data.minMinute}`;
  227. }
  228. else {
  229. const values = picker.getValues();
  230. const year = getTrueValue(values[0]);
  231. const month = getTrueValue(values[1]);
  232. const maxDate = getMonthEndDay(year, month);
  233. let date = getTrueValue(values[2]);
  234. if (data.type === 'year-month') {
  235. date = 1;
  236. }
  237. date = date > maxDate ? maxDate : date;
  238. let hour = 0;
  239. let minute = 0;
  240. if (data.type === 'datetime') {
  241. hour = getTrueValue(values[3]);
  242. minute = getTrueValue(values[4]);
  243. }
  244. value = new Date(year, month - 1, date, hour, minute);
  245. }
  246. value = this.correctValue(value);
  247. this.updateColumnValue(value).then(() => {
  248. this.$emit('input', value);
  249. this.$emit('change', picker);
  250. });
  251. },
  252. updateColumnValue(value) {
  253. let values = [];
  254. const { type, formatter = defaultFormatter } = this.data;
  255. const picker = this.getPicker();
  256. if (type === 'time') {
  257. const pair = value.split(':');
  258. values = [
  259. formatter('hour', pair[0]),
  260. formatter('minute', pair[1])
  261. ];
  262. }
  263. else {
  264. const date = new Date(value);
  265. values = [
  266. formatter('year', `${date.getFullYear()}`),
  267. formatter('month', padZero(date.getMonth() + 1))
  268. ];
  269. if (type === 'date') {
  270. values.push(formatter('day', padZero(date.getDate())));
  271. }
  272. if (type === 'datetime') {
  273. values.push(formatter('day', padZero(date.getDate())), formatter('hour', padZero(date.getHours())), formatter('minute', padZero(date.getMinutes())));
  274. }
  275. }
  276. return this.set({ innerValue: value })
  277. .then(() => this.updateColumns())
  278. .then(() => picker.setValues(values));
  279. }
  280. },
  281. created() {
  282. const innerValue = this.correctValue(this.data.value);
  283. this.updateColumnValue(innerValue).then(() => {
  284. this.$emit('input', innerValue);
  285. });
  286. }
  287. });