index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { use, range } from '../utils';
  3. import Picker from '../picker';
  4. import { pickerProps } from '../picker/shared';
  5. import { times, padZero, isValidDate, getTrueValue, getMonthEndDay } from './utils';
  6. var _use = use('datetime-picker'),
  7. sfc = _use[0],
  8. bem = _use[1];
  9. var currentYear = new Date().getFullYear();
  10. export default sfc({
  11. props: _extends({}, pickerProps, {
  12. value: null,
  13. minHour: Number,
  14. minMinute: Number,
  15. type: {
  16. type: String,
  17. "default": 'datetime'
  18. },
  19. showToolbar: {
  20. type: Boolean,
  21. "default": true
  22. },
  23. format: {
  24. type: String,
  25. "default": 'YYYY.MM.DD HH时 mm分'
  26. },
  27. formatter: {
  28. type: Function,
  29. "default": function _default(type, value) {
  30. return value;
  31. }
  32. },
  33. minDate: {
  34. type: Date,
  35. "default": function _default() {
  36. return new Date(currentYear - 10, 0, 1);
  37. },
  38. validator: isValidDate
  39. },
  40. maxDate: {
  41. type: Date,
  42. "default": function _default() {
  43. return new Date(currentYear + 10, 11, 31);
  44. },
  45. validator: isValidDate
  46. },
  47. maxHour: {
  48. type: Number,
  49. "default": 23
  50. },
  51. maxMinute: {
  52. type: Number,
  53. "default": 59
  54. }
  55. }),
  56. data: function data() {
  57. return {
  58. innerValue: this.correctValue(this.value)
  59. };
  60. },
  61. watch: {
  62. value: function value(val) {
  63. val = this.correctValue(val);
  64. var isEqual = this.type === 'time' ? val === this.innerValue : val.valueOf() === this.innerValue.valueOf();
  65. if (!isEqual) {
  66. this.innerValue = val;
  67. if (this.type === 'time') {
  68. this.updateColumnValue(val);
  69. }
  70. }
  71. },
  72. innerValue: function innerValue(val) {
  73. this.$emit('input', val);
  74. },
  75. columns: function columns() {
  76. this.updateColumnValue(this.innerValue);
  77. }
  78. },
  79. computed: {
  80. ranges: function ranges() {
  81. if (this.type === 'time') {
  82. return [{
  83. type: 'hour',
  84. range: [this.minHour, this.maxHour]
  85. }, {
  86. type: 'minute',
  87. range: [this.minMinute, this.maxMinute]
  88. }];
  89. }
  90. var _this$getBoundary = this.getBoundary('max', this.innerValue),
  91. maxYear = _this$getBoundary.maxYear,
  92. maxDate = _this$getBoundary.maxDate,
  93. maxMonth = _this$getBoundary.maxMonth,
  94. maxHour = _this$getBoundary.maxHour,
  95. maxMinute = _this$getBoundary.maxMinute;
  96. var _this$getBoundary2 = this.getBoundary('min', this.innerValue),
  97. minYear = _this$getBoundary2.minYear,
  98. minDate = _this$getBoundary2.minDate,
  99. minMonth = _this$getBoundary2.minMonth,
  100. minHour = _this$getBoundary2.minHour,
  101. minMinute = _this$getBoundary2.minMinute;
  102. var result = [{
  103. type: 'year',
  104. range: [minYear, maxYear]
  105. }, {
  106. type: 'month',
  107. range: [minMonth, maxMonth]
  108. }, {
  109. type: 'day',
  110. range: [minDate, maxDate]
  111. }, {
  112. type: 'hour',
  113. range: [minHour, maxHour]
  114. }, {
  115. type: 'minute',
  116. range: [minMinute, maxMinute]
  117. }];
  118. if (this.type === 'date') result.splice(3, 2);
  119. if (this.type === 'year-month') result.splice(2, 3);
  120. return result;
  121. },
  122. columns: function columns() {
  123. var _this = this;
  124. var results = this.ranges.map(function (_ref) {
  125. var type = _ref.type,
  126. rangeArr = _ref.range;
  127. var values = times(rangeArr[1] - rangeArr[0] + 1, function (index) {
  128. var value = rangeArr[0] + index;
  129. value = value < 10 ? "0" + value : "" + value;
  130. return _this.formatter(type, value);
  131. });
  132. return {
  133. values: values
  134. };
  135. });
  136. return results;
  137. }
  138. },
  139. mounted: function mounted() {
  140. this.updateColumnValue(this.innerValue);
  141. },
  142. methods: {
  143. correctValue: function correctValue(value) {
  144. // validate value
  145. var isDateType = this.type !== 'time';
  146. if (isDateType && !isValidDate(value)) {
  147. value = this.minDate;
  148. } else if (!value) {
  149. var minHour = this.minHour;
  150. value = (minHour > 10 ? minHour : '0' + minHour) + ":00";
  151. } // time type
  152. if (!isDateType) {
  153. var _value$split = value.split(':'),
  154. hour = _value$split[0],
  155. minute = _value$split[1];
  156. hour = padZero(range(hour, this.minHour, this.maxHour));
  157. minute = padZero(range(minute, this.minMinute, this.maxMinute));
  158. return hour + ":" + minute;
  159. } // date type
  160. value = Math.max(value, this.minDate.getTime());
  161. value = Math.min(value, this.maxDate.getTime());
  162. return new Date(value);
  163. },
  164. getBoundary: function getBoundary(type, value) {
  165. var _ref2;
  166. var boundary = this[type + "Date"];
  167. var year = boundary.getFullYear();
  168. var month = 1;
  169. var date = 1;
  170. var hour = 0;
  171. var minute = 0;
  172. if (type === 'max') {
  173. month = 12;
  174. date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
  175. hour = 23;
  176. minute = 59;
  177. }
  178. if (value.getFullYear() === year) {
  179. month = boundary.getMonth() + 1;
  180. if (value.getMonth() + 1 === month) {
  181. date = boundary.getDate();
  182. if (value.getDate() === date) {
  183. hour = boundary.getHours();
  184. if (value.getHours() === hour) {
  185. minute = boundary.getMinutes();
  186. }
  187. }
  188. }
  189. }
  190. return _ref2 = {}, _ref2[type + "Year"] = year, _ref2[type + "Month"] = month, _ref2[type + "Date"] = date, _ref2[type + "Hour"] = hour, _ref2[type + "Minute"] = minute, _ref2;
  191. },
  192. onConfirm: function onConfirm() {
  193. this.$emit('confirm', this.innerValue);
  194. },
  195. onChange: function onChange(picker) {
  196. var _this2 = this;
  197. var value;
  198. if (this.type === 'time') {
  199. var indexes = picker.getIndexes();
  200. value = indexes[0] + this.minHour + ":" + (indexes[1] + this.minMinute);
  201. } else {
  202. var values = picker.getValues();
  203. var year = getTrueValue(values[0]);
  204. var month = getTrueValue(values[1]);
  205. var maxDate = getMonthEndDay(year, month);
  206. var date = getTrueValue(values[2]);
  207. if (this.type === 'year-month') {
  208. date = 1;
  209. }
  210. date = date > maxDate ? maxDate : date;
  211. var hour = 0;
  212. var minute = 0;
  213. if (this.type === 'datetime') {
  214. hour = getTrueValue(values[3]);
  215. minute = getTrueValue(values[4]);
  216. }
  217. value = new Date(year, month - 1, date, hour, minute);
  218. }
  219. this.innerValue = this.correctValue(value);
  220. this.$nextTick(function () {
  221. _this2.$nextTick(function () {
  222. _this2.$emit('change', picker);
  223. });
  224. });
  225. },
  226. updateColumnValue: function updateColumnValue(value) {
  227. var _this3 = this;
  228. var values = [];
  229. var formatter = this.formatter;
  230. if (this.type === 'time') {
  231. var pair = value.split(':');
  232. values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
  233. } else {
  234. values = [formatter('year', "" + value.getFullYear()), formatter('month', padZero(value.getMonth() + 1)), formatter('day', padZero(value.getDate()))];
  235. if (this.type === 'datetime') {
  236. values.push(formatter('hour', padZero(value.getHours())), formatter('minute', padZero(value.getMinutes())));
  237. }
  238. if (this.type === 'year-month') {
  239. values = values.slice(0, 2);
  240. }
  241. }
  242. this.$nextTick(function () {
  243. _this3.$refs.picker.setValues(values);
  244. });
  245. }
  246. },
  247. render: function render(h) {
  248. var _this4 = this;
  249. var props = {};
  250. Object.keys(pickerProps).forEach(function (key) {
  251. props[key] = _this4[key];
  252. });
  253. return h(Picker, {
  254. "class": bem(),
  255. "ref": "picker",
  256. "attrs": {
  257. "columns": this.columns
  258. },
  259. "on": {
  260. "change": this.onChange,
  261. "confirm": this.onConfirm,
  262. "cancel": function cancel() {
  263. _this4.$emit('cancel');
  264. }
  265. },
  266. "props": _extends({}, props)
  267. });
  268. }
  269. });