index.js 11 KB

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