index.js 12 KB

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