use-calendar.js 4.9 KB

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