index.js 12 KB

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