utils.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import dayjs from 'dayjs';
  2. import { isoWeekday } from '../_util/dayjs/iso-weekday';
  3. export function getMonthListFromRange(start, end) {
  4. if (start.isAfter(end))
  5. throw new Error("Start time can't be later than end time.");
  6. var result = [];
  7. var current = start.date(1);
  8. while (!current.isAfter(end)) {
  9. result.push(current.date(1));
  10. current = current.add(1, 'month');
  11. }
  12. return result;
  13. }
  14. export function defaultMonthRange() {
  15. var start = dayjs().startOf('date');
  16. var end = dayjs().startOf('date').add(2, 'month');
  17. return [start.toDate().getTime(), end.toDate().getTime()];
  18. }
  19. /**
  20. *
  21. * @param month 月份的某一天
  22. * @param weekStartsOn 日历以星期几开始
  23. * @returns 获取当月日历所有的日子
  24. */
  25. export function getDate(month, weekStartsOn) {
  26. var startOfMonth = month.date(1);
  27. var cells = [];
  28. var iterator = startOfMonth
  29. .subtract(isoWeekday(startOfMonth) % 7, 'day')
  30. .startOf('day');
  31. if (weekStartsOn === 'Monday') {
  32. iterator = iterator.add(1, 'day');
  33. if (iterator.isSame(startOfMonth, 'month') &&
  34. !iterator.isSame(startOfMonth.startOf('date'), 'date')) {
  35. iterator = iterator.add(-7, 'days');
  36. }
  37. }
  38. var diffDay = startOfMonth.date(1).add(1, 'month').diff(iterator, 'day');
  39. var lintCount = Math.ceil(diffDay / 7);
  40. while (cells.length < lintCount * 7) {
  41. cells.push(iterator);
  42. iterator = iterator.add(1, 'day');
  43. }
  44. return cells;
  45. }
  46. export function renderCells(cellsMonth, weekStartsOn, value, localeText) {
  47. var _a;
  48. var rowBeginDay = 0;
  49. var rowEndDay = 6;
  50. if (weekStartsOn === 'Monday') {
  51. rowBeginDay = 1;
  52. rowEndDay = 0;
  53. }
  54. var dates = getDate(cellsMonth, weekStartsOn);
  55. if (!value) {
  56. return dates.map(function (d) {
  57. var isToday = dayjs().isSame(d, 'day');
  58. var isRowBegin = d.isSame(cellsMonth.startOf('month'), 'date') ||
  59. d.day() === rowBeginDay;
  60. var isRowEnd = d.isSame(cellsMonth.endOf('month'), 'date') || d.day() === rowEndDay;
  61. var top;
  62. if (isToday) {
  63. top = {
  64. label: localeText.today,
  65. };
  66. }
  67. return {
  68. disabled: false,
  69. time: d.toDate().getTime(),
  70. date: d.get('date'),
  71. isSelected: false,
  72. isSelectedBegin: false,
  73. top: top,
  74. isSelectedEnd: false,
  75. inThisMonth: d.month() === cellsMonth.month(),
  76. isRowBegin: isRowBegin,
  77. isRowEnd: isRowEnd,
  78. };
  79. });
  80. }
  81. var selectBegin;
  82. var selectEnd;
  83. if (Array.isArray(value)) {
  84. selectBegin = dayjs(value[0]);
  85. selectEnd = dayjs((_a = value[1]) !== null && _a !== void 0 ? _a : value[0]);
  86. }
  87. else {
  88. selectBegin = dayjs(value);
  89. selectEnd = dayjs(value);
  90. }
  91. return dates.map(function (d) {
  92. var isToday = dayjs().isSame(d, 'day');
  93. var isRowBegin = d.isSame(cellsMonth.startOf('month'), 'date') || d.day() === rowBeginDay;
  94. var isRowEnd = d.isSame(cellsMonth.endOf('month'), 'date') || d.day() === rowEndDay;
  95. var isSelectedBegin = selectBegin.isSame(d, 'day');
  96. var isSelectedEnd = selectEnd.isSame(d, 'day');
  97. var isSelected = (!!selectBegin.isBefore(d, 'day') && !!selectEnd.isAfter(d, 'day')) ||
  98. isSelectedBegin ||
  99. isSelectedEnd;
  100. var inThisMonth = d.month() === cellsMonth.month();
  101. var time = d.toDate().getTime();
  102. var topLabel = isToday ? localeText.today : '';
  103. if (Array.isArray(value)) {
  104. if (isSelectedBegin) {
  105. if (isSelectedEnd && value.length === 2) {
  106. topLabel = localeText.startAndEnd;
  107. }
  108. else {
  109. topLabel = localeText.start;
  110. }
  111. }
  112. else {
  113. if (isSelectedEnd) {
  114. topLabel = localeText.end;
  115. }
  116. }
  117. }
  118. return {
  119. disabled: false,
  120. time: time,
  121. date: d.get('date'),
  122. isSelected: isSelected,
  123. isSelectedBegin: isSelectedBegin,
  124. top: { label: topLabel },
  125. isSelectedEnd: isSelectedEnd,
  126. inThisMonth: inThisMonth,
  127. isRowBegin: isRowBegin,
  128. isRowEnd: isRowEnd,
  129. };
  130. });
  131. }
  132. export function getSelectionModeFromValue(value) {
  133. if (Array.isArray(value)) {
  134. return 'range';
  135. }
  136. if (typeof value === 'number') {
  137. return 'single';
  138. }
  139. return null;
  140. }
  141. // 获取滚动视图的元素id
  142. export function getScrollIntoViewId(value) {
  143. // 已选中时间滚动到可视区域内(微信不支持id为数字开头)
  144. return "id_".concat(value &&
  145. dayjs(Array.isArray(value) ? value[0] : value)
  146. .startOf('d')
  147. .subtract(7, 'd') // 需要定位的地方往前推7天,让已选中时间定位到中间位置
  148. .toDate()
  149. .getTime());
  150. }