DatePicker.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  6. var _utils = require("../utils");
  7. var _date = require("../utils/validate/date");
  8. var _string = require("../utils/format/string");
  9. var _utils2 = require("./utils");
  10. var _shared = require("./shared");
  11. var currentYear = new Date().getFullYear();
  12. var _createNamespace = (0, _utils.createNamespace)('date-picker'),
  13. createComponent = _createNamespace[0];
  14. var _default2 = createComponent({
  15. mixins: [_shared.TimePickerMixin],
  16. props: (0, _extends2.default)({}, _shared.sharedProps, {
  17. type: {
  18. type: String,
  19. default: 'datetime'
  20. },
  21. minDate: {
  22. type: Date,
  23. default: function _default() {
  24. return new Date(currentYear - 10, 0, 1);
  25. },
  26. validator: _date.isDate
  27. },
  28. maxDate: {
  29. type: Date,
  30. default: function _default() {
  31. return new Date(currentYear + 10, 11, 31);
  32. },
  33. validator: _date.isDate
  34. }
  35. }),
  36. watch: {
  37. filter: 'updateInnerValue',
  38. minDate: 'updateInnerValue',
  39. maxDate: 'updateInnerValue',
  40. value: function value(val) {
  41. val = this.formatValue(val);
  42. if (val && val.valueOf() !== this.innerValue.valueOf()) {
  43. this.innerValue = val;
  44. }
  45. }
  46. },
  47. computed: {
  48. ranges: function ranges() {
  49. var _this$getBoundary = this.getBoundary('max', this.innerValue ? this.innerValue : this.minDate),
  50. maxYear = _this$getBoundary.maxYear,
  51. maxDate = _this$getBoundary.maxDate,
  52. maxMonth = _this$getBoundary.maxMonth,
  53. maxHour = _this$getBoundary.maxHour,
  54. maxMinute = _this$getBoundary.maxMinute;
  55. var _this$getBoundary2 = this.getBoundary('min', this.innerValue ? this.innerValue : this.minDate),
  56. minYear = _this$getBoundary2.minYear,
  57. minDate = _this$getBoundary2.minDate,
  58. minMonth = _this$getBoundary2.minMonth,
  59. minHour = _this$getBoundary2.minHour,
  60. minMinute = _this$getBoundary2.minMinute;
  61. var result = [{
  62. type: 'year',
  63. range: [minYear, maxYear]
  64. }, {
  65. type: 'month',
  66. range: [minMonth, maxMonth]
  67. }, {
  68. type: 'day',
  69. range: [minDate, maxDate]
  70. }, {
  71. type: 'hour',
  72. range: [minHour, maxHour]
  73. }, {
  74. type: 'minute',
  75. range: [minMinute, maxMinute]
  76. }];
  77. switch (this.type) {
  78. case 'date':
  79. result = result.slice(0, 3);
  80. break;
  81. case 'year-month':
  82. result = result.slice(0, 2);
  83. break;
  84. case 'month-day':
  85. result = result.slice(1, 3);
  86. break;
  87. case 'datehour':
  88. result = result.slice(0, 4);
  89. break;
  90. }
  91. if (this.columnsOrder) {
  92. var columnsOrder = this.columnsOrder.concat(result.map(function (column) {
  93. return column.type;
  94. }));
  95. result.sort(function (a, b) {
  96. return columnsOrder.indexOf(a.type) - columnsOrder.indexOf(b.type);
  97. });
  98. }
  99. return result;
  100. }
  101. },
  102. methods: {
  103. formatValue: function formatValue(value) {
  104. if (!(0, _date.isDate)(value)) {
  105. return null;
  106. }
  107. value = Math.max(value, this.minDate.getTime());
  108. value = Math.min(value, this.maxDate.getTime());
  109. return new Date(value);
  110. },
  111. getBoundary: function getBoundary(type, value) {
  112. var _ref;
  113. var boundary = this[type + "Date"];
  114. var year = boundary.getFullYear();
  115. var month = 1;
  116. var date = 1;
  117. var hour = 0;
  118. var minute = 0;
  119. if (type === 'max') {
  120. month = 12;
  121. date = (0, _utils2.getMonthEndDay)(value.getFullYear(), value.getMonth() + 1);
  122. hour = 23;
  123. minute = 59;
  124. }
  125. if (value.getFullYear() === year) {
  126. month = boundary.getMonth() + 1;
  127. if (value.getMonth() + 1 === month) {
  128. date = boundary.getDate();
  129. if (value.getDate() === date) {
  130. hour = boundary.getHours();
  131. if (value.getHours() === hour) {
  132. minute = boundary.getMinutes();
  133. }
  134. }
  135. }
  136. }
  137. return _ref = {}, _ref[type + "Year"] = year, _ref[type + "Month"] = month, _ref[type + "Date"] = date, _ref[type + "Hour"] = hour, _ref[type + "Minute"] = minute, _ref;
  138. },
  139. updateInnerValue: function updateInnerValue() {
  140. var _this = this;
  141. var type = this.type;
  142. var indexes = this.getPicker().getIndexes();
  143. var getValue = function getValue(type) {
  144. var index = 0;
  145. _this.originColumns.forEach(function (column, columnIndex) {
  146. if (type === column.type) {
  147. index = columnIndex;
  148. }
  149. });
  150. var values = _this.originColumns[index].values;
  151. return (0, _utils2.getTrueValue)(values[indexes[index]]);
  152. };
  153. var year;
  154. var month;
  155. var day;
  156. if (type === 'month-day') {
  157. year = (this.innerValue ? this.innerValue : this.minDate).getFullYear();
  158. month = getValue('month');
  159. day = getValue('day');
  160. } else {
  161. year = getValue('year');
  162. month = getValue('month');
  163. day = type === 'year-month' ? 1 : getValue('day');
  164. }
  165. var maxDay = (0, _utils2.getMonthEndDay)(year, month);
  166. day = day > maxDay ? maxDay : day;
  167. var hour = 0;
  168. var minute = 0;
  169. if (type === 'datehour') {
  170. hour = getValue('hour');
  171. }
  172. if (type === 'datetime') {
  173. hour = getValue('hour');
  174. minute = getValue('minute');
  175. }
  176. var value = new Date(year, month - 1, day, hour, minute);
  177. this.innerValue = this.formatValue(value);
  178. },
  179. onChange: function onChange(picker) {
  180. var _this2 = this;
  181. this.updateInnerValue();
  182. this.$nextTick(function () {
  183. _this2.$nextTick(function () {
  184. _this2.$emit('change', picker);
  185. });
  186. });
  187. },
  188. updateColumnValue: function updateColumnValue() {
  189. var _this3 = this;
  190. var value = this.innerValue ? this.innerValue : this.minDate;
  191. var formatter = this.formatter;
  192. var values = this.originColumns.map(function (column) {
  193. switch (column.type) {
  194. case 'year':
  195. return formatter('year', "" + value.getFullYear());
  196. case 'month':
  197. return formatter('month', (0, _string.padZero)(value.getMonth() + 1));
  198. case 'day':
  199. return formatter('day', (0, _string.padZero)(value.getDate()));
  200. case 'hour':
  201. return formatter('hour', (0, _string.padZero)(value.getHours()));
  202. case 'minute':
  203. return formatter('minute', (0, _string.padZero)(value.getMinutes()));
  204. default:
  205. // no default
  206. return null;
  207. }
  208. });
  209. this.$nextTick(function () {
  210. _this3.getPicker().setValues(values);
  211. });
  212. }
  213. }
  214. });
  215. exports.default = _default2;