92857bc886407666a64ce578cbbbdb38385e0f0f5b22651d42ba663cf63cee0ae27eb46c9a2e22017c9d0fd14a3fc8427bb279cf4ca96d43677983bcf942e7 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var dayjs = require('dayjs');
  5. var index = require('../../../hooks/use-locale/index.js');
  6. var event = require('../../../constants/event.js');
  7. var shared = require('@vue/shared');
  8. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  9. var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
  10. const adjacentMonth = (start, end) => {
  11. const firstMonthLastDay = start.endOf("month");
  12. const lastMonthFirstDay = end.startOf("month");
  13. const isSameWeek = firstMonthLastDay.isSame(lastMonthFirstDay, "week");
  14. const lastMonthStartDay = isSameWeek ? lastMonthFirstDay.add(1, "week") : lastMonthFirstDay;
  15. return [
  16. [start, firstMonthLastDay],
  17. [lastMonthStartDay.startOf("week"), end]
  18. ];
  19. };
  20. const threeConsecutiveMonth = (start, end) => {
  21. const firstMonthLastDay = start.endOf("month");
  22. const secondMonthFirstDay = start.add(1, "month").startOf("month");
  23. const secondMonthStartDay = firstMonthLastDay.isSame(secondMonthFirstDay, "week") ? secondMonthFirstDay.add(1, "week") : secondMonthFirstDay;
  24. const secondMonthLastDay = secondMonthStartDay.endOf("month");
  25. const lastMonthFirstDay = end.startOf("month");
  26. const lastMonthStartDay = secondMonthLastDay.isSame(lastMonthFirstDay, "week") ? lastMonthFirstDay.add(1, "week") : lastMonthFirstDay;
  27. return [
  28. [start, firstMonthLastDay],
  29. [secondMonthStartDay.startOf("week"), secondMonthLastDay],
  30. [lastMonthStartDay.startOf("week"), end]
  31. ];
  32. };
  33. const useCalendar = (props, emit, componentName) => {
  34. const { lang } = index.useLocale();
  35. const selectedDay = vue.ref();
  36. const now = dayjs__default["default"]().locale(lang.value);
  37. const realSelectedDay = vue.computed({
  38. get() {
  39. if (!props.modelValue)
  40. return selectedDay.value;
  41. return date.value;
  42. },
  43. set(val) {
  44. if (!val)
  45. return;
  46. selectedDay.value = val;
  47. const result = val.toDate();
  48. emit(event.INPUT_EVENT, result);
  49. emit(event.UPDATE_MODEL_EVENT, result);
  50. }
  51. });
  52. const validatedRange = vue.computed(() => {
  53. if (!props.range || !shared.isArray(props.range) || props.range.length !== 2 || props.range.some((item) => !shared.isDate(item)))
  54. return [];
  55. const rangeArrDayjs = props.range.map((_) => dayjs__default["default"](_).locale(lang.value));
  56. const [startDayjs, endDayjs] = rangeArrDayjs;
  57. if (startDayjs.isAfter(endDayjs)) {
  58. return [];
  59. }
  60. if (startDayjs.isSame(endDayjs, "month")) {
  61. return calculateValidatedDateRange(startDayjs, endDayjs);
  62. } else {
  63. if (startDayjs.add(1, "month").month() !== endDayjs.month()) {
  64. return [];
  65. }
  66. return calculateValidatedDateRange(startDayjs, endDayjs);
  67. }
  68. });
  69. const date = vue.computed(() => {
  70. if (!props.modelValue) {
  71. return realSelectedDay.value || (validatedRange.value.length ? validatedRange.value[0][0] : now);
  72. } else {
  73. return dayjs__default["default"](props.modelValue).locale(lang.value);
  74. }
  75. });
  76. const prevMonthDayjs = vue.computed(() => date.value.subtract(1, "month").date(1));
  77. const nextMonthDayjs = vue.computed(() => date.value.add(1, "month").date(1));
  78. const prevYearDayjs = vue.computed(() => date.value.subtract(1, "year").date(1));
  79. const nextYearDayjs = vue.computed(() => date.value.add(1, "year").date(1));
  80. const calculateValidatedDateRange = (startDayjs, endDayjs) => {
  81. const firstDay = startDayjs.startOf("week");
  82. const lastDay = endDayjs.endOf("week");
  83. const firstMonth = firstDay.get("month");
  84. const lastMonth = lastDay.get("month");
  85. if (firstMonth === lastMonth) {
  86. return [[firstDay, lastDay]];
  87. } else if ((firstMonth + 1) % 12 === lastMonth) {
  88. return adjacentMonth(firstDay, lastDay);
  89. } else if (firstMonth + 2 === lastMonth || (firstMonth + 1) % 11 === lastMonth) {
  90. return threeConsecutiveMonth(firstDay, lastDay);
  91. } else {
  92. return [];
  93. }
  94. };
  95. const pickDay = (day) => {
  96. realSelectedDay.value = day;
  97. };
  98. const selectDate = (type) => {
  99. const dateMap = {
  100. "prev-month": prevMonthDayjs.value,
  101. "next-month": nextMonthDayjs.value,
  102. "prev-year": prevYearDayjs.value,
  103. "next-year": nextYearDayjs.value,
  104. today: now
  105. };
  106. const day = dateMap[type];
  107. if (!day.isSame(date.value, "day")) {
  108. pickDay(day);
  109. }
  110. };
  111. return {
  112. calculateValidatedDateRange,
  113. date,
  114. realSelectedDay,
  115. pickDay,
  116. selectDate,
  117. validatedRange
  118. };
  119. };
  120. exports.useCalendar = useCalendar;
  121. //# sourceMappingURL=use-calendar.js.map