index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. "use strict";
  2. var _component = require("../../../common/component");
  3. var _utils = require("../../utils");
  4. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  5. 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."); }
  6. 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); }
  7. 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; }
  8. 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; }
  9. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  10. (0, _component.VantComponent)({
  11. props: {
  12. date: {
  13. type: null,
  14. observer: "setDays"
  15. },
  16. type: {
  17. type: String,
  18. observer: "setDays"
  19. },
  20. color: String,
  21. minDate: {
  22. type: null,
  23. observer: "setDays"
  24. },
  25. maxDate: {
  26. type: null,
  27. observer: "setDays"
  28. },
  29. showMark: Boolean,
  30. rowHeight: [Number, String],
  31. formatter: {
  32. type: null,
  33. observer: "setDays"
  34. },
  35. currentDate: {
  36. type: [null, Array],
  37. observer: "setDays"
  38. },
  39. allowSameDay: Boolean,
  40. showSubtitle: Boolean,
  41. showMonthTitle: Boolean
  42. },
  43. data: {
  44. visible: true,
  45. days: []
  46. },
  47. created: function created() {
  48. if (this.props.onFormatter) {
  49. this.setDays();
  50. }
  51. },
  52. methods: {
  53. onClick: function onClick(event) {
  54. var index = event.currentTarget.dataset.index;
  55. var item = this.data.days[index];
  56. if (item.type !== "disabled") {
  57. this.$emit("click", item);
  58. }
  59. },
  60. setDays: function setDays() {
  61. var days = [];
  62. var startDate = new Date(this.data.date);
  63. var year = startDate.getFullYear();
  64. var month = startDate.getMonth();
  65. var totalDay = (0, _utils.getMonthEndDay)(startDate.getFullYear(), startDate.getMonth() + 1);
  66. for (var day = 1; day <= totalDay; day++) {
  67. var date = new Date(year, month, day);
  68. var type = this.getDayType(date);
  69. var config = {
  70. date: date,
  71. type: type,
  72. text: day,
  73. bottomInfo: this.getBottomInfo(type)
  74. };
  75. if (this.data.formatter) {
  76. config = this.data.formatter(config);
  77. }
  78. if (this.props.onFormatter && typeof this.props.onFormatter === "function") {
  79. config = this.props.onFormatter(config);
  80. }
  81. days.push(config);
  82. }
  83. this.setData({
  84. days: days
  85. });
  86. },
  87. getMultipleDayType: function getMultipleDayType(day) {
  88. var currentDate = this.data.currentDate;
  89. if (!Array.isArray(currentDate)) {
  90. return "";
  91. }
  92. var isSelected = function isSelected(date) {
  93. return currentDate.some(function (item) {
  94. return (0, _utils.compareDay)(item, date) === 0;
  95. });
  96. };
  97. if (isSelected(day)) {
  98. var prevDay = (0, _utils.getPrevDay)(day);
  99. var nextDay = (0, _utils.getNextDay)(day);
  100. var prevSelected = isSelected(prevDay);
  101. var nextSelected = isSelected(nextDay);
  102. if (prevSelected && nextSelected) {
  103. return "multiple-middle";
  104. }
  105. if (prevSelected) {
  106. return "end";
  107. }
  108. return nextSelected ? "start" : "multiple-selected";
  109. }
  110. return "";
  111. },
  112. getRangeDayType: function getRangeDayType(day) {
  113. var _this$data = this.data,
  114. currentDate = _this$data.currentDate,
  115. allowSameDay = _this$data.allowSameDay;
  116. if (!Array.isArray(currentDate)) {
  117. return;
  118. }
  119. var _currentDate = _slicedToArray(currentDate, 2),
  120. startDay = _currentDate[0],
  121. endDay = _currentDate[1];
  122. if (!startDay) {
  123. return;
  124. }
  125. var compareToStart = (0, _utils.compareDay)(day, startDay);
  126. if (!endDay) {
  127. return compareToStart === 0 ? "start" : "";
  128. }
  129. var compareToEnd = (0, _utils.compareDay)(day, endDay);
  130. if (compareToStart === 0 && compareToEnd === 0 && allowSameDay) {
  131. return "start-end";
  132. }
  133. if (compareToStart === 0) {
  134. return "start";
  135. }
  136. if (compareToEnd === 0) {
  137. return "end";
  138. }
  139. if (compareToStart > 0 && compareToEnd < 0) {
  140. return "middle";
  141. }
  142. },
  143. getDayType: function getDayType(day) {
  144. var _this$data2 = this.data,
  145. type = _this$data2.type,
  146. minDate = _this$data2.minDate,
  147. maxDate = _this$data2.maxDate,
  148. currentDate = _this$data2.currentDate;
  149. if ((0, _utils.compareDay)(day, minDate) < 0 || (0, _utils.compareDay)(day, maxDate) > 0) {
  150. return "disabled";
  151. }
  152. if (type === "single") {
  153. return (0, _utils.compareDay)(day, currentDate) === 0 ? "selected" : "";
  154. }
  155. if (type === "multiple") {
  156. return this.getMultipleDayType(day);
  157. }
  158. /* istanbul ignore else */
  159. if (type === "range") {
  160. return this.getRangeDayType(day);
  161. }
  162. },
  163. getBottomInfo: function getBottomInfo(type) {
  164. if (this.data.type === "range") {
  165. if (type === "start") {
  166. return "开始";
  167. }
  168. if (type === "end") {
  169. return "结束";
  170. }
  171. if (type === "start-end") {
  172. return "开始/结束";
  173. }
  174. }
  175. }
  176. }
  177. });