index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. 'use strict';
  2. var __assign =
  3. (this && this.__assign) ||
  4. function () {
  5. __assign =
  6. Object.assign ||
  7. function (t) {
  8. for (var s, i = 1, n = arguments.length; i < n; i++) {
  9. s = arguments[i];
  10. for (var p in s)
  11. if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  12. }
  13. return t;
  14. };
  15. return __assign.apply(this, arguments);
  16. };
  17. var __spreadArray =
  18. (this && this.__spreadArray) ||
  19. function (to, from) {
  20. for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
  21. to[j] = from[i];
  22. return to;
  23. };
  24. Object.defineProperty(exports, '__esModule', { value: true });
  25. var component_1 = require('../common/component');
  26. var validator_1 = require('../common/validator');
  27. var shared_1 = require('../picker/shared');
  28. var currentYear = new Date().getFullYear();
  29. function isValidDate(date) {
  30. return validator_1.isDef(date) && !isNaN(new Date(date).getTime());
  31. }
  32. function range(num, min, max) {
  33. return Math.min(Math.max(num, min), max);
  34. }
  35. function padZero(val) {
  36. return ('00' + val).slice(-2);
  37. }
  38. function times(n, iteratee) {
  39. var index = -1;
  40. var result = Array(n < 0 ? 0 : n);
  41. while (++index < n) {
  42. result[index] = iteratee(index);
  43. }
  44. return result;
  45. }
  46. function getTrueValue(formattedValue) {
  47. if (formattedValue === undefined) {
  48. formattedValue = '1';
  49. }
  50. while (isNaN(parseInt(formattedValue, 10))) {
  51. formattedValue = formattedValue.slice(1);
  52. }
  53. return parseInt(formattedValue, 10);
  54. }
  55. function getMonthEndDay(year, month) {
  56. return 32 - new Date(year, month - 1, 32).getDate();
  57. }
  58. var defaultFormatter = function (type, value) {
  59. return value;
  60. };
  61. component_1.VantComponent({
  62. classes: ['active-class', 'toolbar-class', 'column-class'],
  63. props: __assign(__assign({}, shared_1.pickerProps), {
  64. value: {
  65. type: null,
  66. observer: 'updateValue',
  67. },
  68. filter: null,
  69. type: {
  70. type: String,
  71. value: 'datetime',
  72. observer: 'updateValue',
  73. },
  74. showToolbar: {
  75. type: Boolean,
  76. value: true,
  77. },
  78. formatter: {
  79. type: null,
  80. value: defaultFormatter,
  81. },
  82. minDate: {
  83. type: Number,
  84. value: new Date(currentYear - 10, 0, 1).getTime(),
  85. observer: 'updateValue',
  86. },
  87. maxDate: {
  88. type: Number,
  89. value: new Date(currentYear + 10, 11, 31).getTime(),
  90. observer: 'updateValue',
  91. },
  92. minHour: {
  93. type: Number,
  94. value: 0,
  95. observer: 'updateValue',
  96. },
  97. maxHour: {
  98. type: Number,
  99. value: 23,
  100. observer: 'updateValue',
  101. },
  102. minMinute: {
  103. type: Number,
  104. value: 0,
  105. observer: 'updateValue',
  106. },
  107. maxMinute: {
  108. type: Number,
  109. value: 59,
  110. observer: 'updateValue',
  111. },
  112. }),
  113. data: {
  114. innerValue: Date.now(),
  115. columns: [],
  116. },
  117. methods: {
  118. updateValue: function () {
  119. var _this = this;
  120. var data = this.data;
  121. var val = this.correctValue(data.value);
  122. var isEqual = val === data.innerValue;
  123. this.updateColumnValue(val).then(function () {
  124. if (!isEqual) {
  125. _this.$emit('input', val);
  126. }
  127. });
  128. },
  129. getPicker: function () {
  130. if (this.picker == null) {
  131. this.picker = this.selectComponent('.van-datetime-picker');
  132. var picker_1 = this.picker;
  133. var setColumnValues_1 = picker_1.setColumnValues;
  134. picker_1.setColumnValues = function () {
  135. var args = [];
  136. for (var _i = 0; _i < arguments.length; _i++) {
  137. args[_i] = arguments[_i];
  138. }
  139. return setColumnValues_1.apply(
  140. picker_1,
  141. __spreadArray(__spreadArray([], args), [false])
  142. );
  143. };
  144. }
  145. return this.picker;
  146. },
  147. updateColumns: function () {
  148. var _a = this.data.formatter,
  149. formatter = _a === void 0 ? defaultFormatter : _a;
  150. var results = this.getOriginColumns().map(function (column) {
  151. return {
  152. values: column.values.map(function (value) {
  153. return formatter(column.type, value);
  154. }),
  155. };
  156. });
  157. return this.set({ columns: results });
  158. },
  159. getOriginColumns: function () {
  160. var filter = this.data.filter;
  161. var results = this.getRanges().map(function (_a) {
  162. var type = _a.type,
  163. range = _a.range;
  164. var values = times(range[1] - range[0] + 1, function (index) {
  165. var value = range[0] + index;
  166. return type === 'year' ? '' + value : padZero(value);
  167. });
  168. if (filter) {
  169. values = filter(type, values);
  170. }
  171. return { type: type, values: values };
  172. });
  173. return results;
  174. },
  175. getRanges: function () {
  176. var data = this.data;
  177. if (data.type === 'time') {
  178. return [
  179. {
  180. type: 'hour',
  181. range: [data.minHour, data.maxHour],
  182. },
  183. {
  184. type: 'minute',
  185. range: [data.minMinute, data.maxMinute],
  186. },
  187. ];
  188. }
  189. var _a = this.getBoundary('max', data.innerValue),
  190. maxYear = _a.maxYear,
  191. maxDate = _a.maxDate,
  192. maxMonth = _a.maxMonth,
  193. maxHour = _a.maxHour,
  194. maxMinute = _a.maxMinute;
  195. var _b = this.getBoundary('min', data.innerValue),
  196. minYear = _b.minYear,
  197. minDate = _b.minDate,
  198. minMonth = _b.minMonth,
  199. minHour = _b.minHour,
  200. minMinute = _b.minMinute;
  201. var result = [
  202. {
  203. type: 'year',
  204. range: [minYear, maxYear],
  205. },
  206. {
  207. type: 'month',
  208. range: [minMonth, maxMonth],
  209. },
  210. {
  211. type: 'day',
  212. range: [minDate, maxDate],
  213. },
  214. {
  215. type: 'hour',
  216. range: [minHour, maxHour],
  217. },
  218. {
  219. type: 'minute',
  220. range: [minMinute, maxMinute],
  221. },
  222. ];
  223. if (data.type === 'date') result.splice(3, 2);
  224. if (data.type === 'year-month') result.splice(2, 3);
  225. return result;
  226. },
  227. correctValue: function (value) {
  228. var data = this.data;
  229. // validate value
  230. var isDateType = data.type !== 'time';
  231. if (isDateType && !isValidDate(value)) {
  232. value = data.minDate;
  233. } else if (!isDateType && !value) {
  234. var minHour = data.minHour;
  235. value = padZero(minHour) + ':00';
  236. }
  237. // time type
  238. if (!isDateType) {
  239. var _a = value.split(':'),
  240. hour = _a[0],
  241. minute = _a[1];
  242. hour = padZero(range(hour, data.minHour, data.maxHour));
  243. minute = padZero(range(minute, data.minMinute, data.maxMinute));
  244. return hour + ':' + minute;
  245. }
  246. // date type
  247. value = Math.max(value, data.minDate);
  248. value = Math.min(value, data.maxDate);
  249. return value;
  250. },
  251. getBoundary: function (type, innerValue) {
  252. var _a;
  253. var value = new Date(innerValue);
  254. var boundary = new Date(this.data[type + 'Date']);
  255. var year = boundary.getFullYear();
  256. var month = 1;
  257. var date = 1;
  258. var hour = 0;
  259. var minute = 0;
  260. if (type === 'max') {
  261. month = 12;
  262. date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
  263. hour = 23;
  264. minute = 59;
  265. }
  266. if (value.getFullYear() === year) {
  267. month = boundary.getMonth() + 1;
  268. if (value.getMonth() + 1 === month) {
  269. date = boundary.getDate();
  270. if (value.getDate() === date) {
  271. hour = boundary.getHours();
  272. if (value.getHours() === hour) {
  273. minute = boundary.getMinutes();
  274. }
  275. }
  276. }
  277. }
  278. return (
  279. (_a = {}),
  280. (_a[type + 'Year'] = year),
  281. (_a[type + 'Month'] = month),
  282. (_a[type + 'Date'] = date),
  283. (_a[type + 'Hour'] = hour),
  284. (_a[type + 'Minute'] = minute),
  285. _a
  286. );
  287. },
  288. onCancel: function () {
  289. this.$emit('cancel');
  290. },
  291. onConfirm: function () {
  292. this.$emit('confirm', this.data.innerValue);
  293. },
  294. onChange: function () {
  295. var _this = this;
  296. var data = this.data;
  297. var value;
  298. var picker = this.getPicker();
  299. var originColumns = this.getOriginColumns();
  300. if (data.type === 'time') {
  301. var indexes = picker.getIndexes();
  302. value =
  303. +originColumns[0].values[indexes[0]] +
  304. ':' +
  305. +originColumns[1].values[indexes[1]];
  306. } else {
  307. var indexes = picker.getIndexes();
  308. var values = indexes.map(function (value, index) {
  309. return originColumns[index].values[value];
  310. });
  311. var year = getTrueValue(values[0]);
  312. var month = getTrueValue(values[1]);
  313. var maxDate = getMonthEndDay(year, month);
  314. var date = getTrueValue(values[2]);
  315. if (data.type === 'year-month') {
  316. date = 1;
  317. }
  318. date = date > maxDate ? maxDate : date;
  319. var hour = 0;
  320. var minute = 0;
  321. if (data.type === 'datetime') {
  322. hour = getTrueValue(values[3]);
  323. minute = getTrueValue(values[4]);
  324. }
  325. value = new Date(year, month - 1, date, hour, minute);
  326. }
  327. value = this.correctValue(value);
  328. this.updateColumnValue(value).then(function () {
  329. _this.$emit('input', value);
  330. _this.$emit('change', picker);
  331. });
  332. },
  333. updateColumnValue: function (value) {
  334. var _this = this;
  335. var values = [];
  336. var type = this.data.type;
  337. var formatter = this.data.formatter || defaultFormatter;
  338. var picker = this.getPicker();
  339. if (type === 'time') {
  340. var pair = value.split(':');
  341. values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
  342. } else {
  343. var date = new Date(value);
  344. values = [
  345. formatter('year', '' + date.getFullYear()),
  346. formatter('month', padZero(date.getMonth() + 1)),
  347. ];
  348. if (type === 'date') {
  349. values.push(formatter('day', padZero(date.getDate())));
  350. }
  351. if (type === 'datetime') {
  352. values.push(
  353. formatter('day', padZero(date.getDate())),
  354. formatter('hour', padZero(date.getHours())),
  355. formatter('minute', padZero(date.getMinutes()))
  356. );
  357. }
  358. }
  359. return this.set({ innerValue: value })
  360. .then(function () {
  361. return _this.updateColumns();
  362. })
  363. .then(function () {
  364. return picker.setValues(values);
  365. });
  366. },
  367. },
  368. created: function () {
  369. var _this = this;
  370. var innerValue = this.correctValue(this.data.value);
  371. this.updateColumnValue(innerValue).then(function () {
  372. _this.$emit('input', innerValue);
  373. });
  374. },
  375. });