index.vue 13 KB

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