index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <uni-shadow-root class="weapp-lib-calendar-components-month-index"><view class="van-calendar__month" :style="computed.getMonthStyle(visible, date, rowHeight)">
  3. <view v-if="showMonthTitle" class="van-calendar__month-title">
  4. {{ computed.formatMonthTitle(date) }}
  5. </view>
  6. <view v-if="visible" class="van-calendar__days">
  7. <view v-if="showMark" class="van-calendar__month-mark">
  8. {{ computed.getMark(date) }}
  9. </view>
  10. <view v-for="(item,index) in (days)" :key="item.index" :style="computed.getDayStyle(item.type, index, date, rowHeight, color, firstDayOfWeek)" :class="(utils.bem('calendar__day', [item.type]))+' '+(item.className)" :data-index="index" @click="onClick">
  11. <view v-if="item.type === 'selected'" class="van-calendar__selected-day" :style="'width: '+(rowHeight)+'px; height: '+(rowHeight)+'px; background: '+(color)">
  12. <view v-if="item.topInfo" class="van-calendar__top-info">{{ item.topInfo }}</view>
  13. {{ item.text }}
  14. <view v-if="item.bottomInfo" class="van-calendar__bottom-info">
  15. {{ item.bottomInfo }}
  16. </view>
  17. </view>
  18. <view v-else>
  19. <view v-if="item.topInfo" class="van-calendar__top-info">{{ item.topInfo }}</view>
  20. {{ item.text }}
  21. <view v-if="item.bottomInfo" class="van-calendar__bottom-info">
  22. {{ item.bottomInfo }}
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. </view></uni-shadow-root>
  28. </template>
  29. <wxs src="./index.wxs" module="computed"></wxs><wxs src="../../../wxs/utils.wxs" module="utils"></wxs>
  30. <script>
  31. global['__wxRoute'] = 'weapp/lib/calendar/components/month/index'
  32. "use strict";
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. var component_1 = require("../../../common/component");
  35. var utils_1 = require("../../utils");
  36. (0, component_1.VantComponent)({
  37. props: {
  38. date: {
  39. type: null,
  40. observer: 'setDays',
  41. },
  42. type: {
  43. type: String,
  44. observer: 'setDays',
  45. },
  46. color: String,
  47. minDate: {
  48. type: null,
  49. observer: 'setDays',
  50. },
  51. maxDate: {
  52. type: null,
  53. observer: 'setDays',
  54. },
  55. showMark: Boolean,
  56. rowHeight: null,
  57. formatter: {
  58. type: null,
  59. observer: 'setDays',
  60. },
  61. currentDate: {
  62. type: null,
  63. observer: 'setDays',
  64. },
  65. firstDayOfWeek: {
  66. type: Number,
  67. observer: 'setDays',
  68. },
  69. allowSameDay: Boolean,
  70. showSubtitle: Boolean,
  71. showMonthTitle: Boolean,
  72. },
  73. data: {
  74. visible: true,
  75. days: [],
  76. },
  77. methods: {
  78. onClick: function (event) {
  79. var index = event.currentTarget.dataset.index;
  80. var item = this.data.days[index];
  81. if (item.type !== 'disabled') {
  82. this.$emit('click', item);
  83. }
  84. },
  85. setDays: function () {
  86. var days = [];
  87. var startDate = new Date(this.data.date);
  88. var year = startDate.getFullYear();
  89. var month = startDate.getMonth();
  90. var totalDay = (0, utils_1.getMonthEndDay)(startDate.getFullYear(), startDate.getMonth() + 1);
  91. for (var day = 1; day <= totalDay; day++) {
  92. var date = new Date(year, month, day);
  93. var type = this.getDayType(date);
  94. var config = {
  95. date: date,
  96. type: type,
  97. text: day,
  98. bottomInfo: this.getBottomInfo(type),
  99. };
  100. if (this.data.formatter) {
  101. config = this.data.formatter(config);
  102. }
  103. days.push(config);
  104. }
  105. this.setData({ days: days });
  106. },
  107. getMultipleDayType: function (day) {
  108. var currentDate = this.data.currentDate;
  109. if (!Array.isArray(currentDate)) {
  110. return '';
  111. }
  112. var isSelected = function (date) {
  113. return currentDate.some(function (item) { return (0, utils_1.compareDay)(item, date) === 0; });
  114. };
  115. if (isSelected(day)) {
  116. var prevDay = (0, utils_1.getPrevDay)(day);
  117. var nextDay = (0, utils_1.getNextDay)(day);
  118. var prevSelected = isSelected(prevDay);
  119. var nextSelected = isSelected(nextDay);
  120. if (prevSelected && nextSelected) {
  121. return 'multiple-middle';
  122. }
  123. if (prevSelected) {
  124. return 'end';
  125. }
  126. return nextSelected ? 'start' : 'multiple-selected';
  127. }
  128. return '';
  129. },
  130. getRangeDayType: function (day) {
  131. var _a = this.data, currentDate = _a.currentDate, allowSameDay = _a.allowSameDay;
  132. if (!Array.isArray(currentDate)) {
  133. return '';
  134. }
  135. var startDay = currentDate[0], endDay = currentDate[1];
  136. if (!startDay) {
  137. return '';
  138. }
  139. var compareToStart = (0, utils_1.compareDay)(day, startDay);
  140. if (!endDay) {
  141. return compareToStart === 0 ? 'start' : '';
  142. }
  143. var compareToEnd = (0, utils_1.compareDay)(day, endDay);
  144. if (compareToStart === 0 && compareToEnd === 0 && allowSameDay) {
  145. return 'start-end';
  146. }
  147. if (compareToStart === 0) {
  148. return 'start';
  149. }
  150. if (compareToEnd === 0) {
  151. return 'end';
  152. }
  153. if (compareToStart > 0 && compareToEnd < 0) {
  154. return 'middle';
  155. }
  156. return '';
  157. },
  158. getDayType: function (day) {
  159. var _a = this.data, type = _a.type, minDate = _a.minDate, maxDate = _a.maxDate, currentDate = _a.currentDate;
  160. if ((0, utils_1.compareDay)(day, minDate) < 0 || (0, utils_1.compareDay)(day, maxDate) > 0) {
  161. return 'disabled';
  162. }
  163. if (type === 'single') {
  164. return (0, utils_1.compareDay)(day, currentDate) === 0 ? 'selected' : '';
  165. }
  166. if (type === 'multiple') {
  167. return this.getMultipleDayType(day);
  168. }
  169. /* istanbul ignore else */
  170. if (type === 'range') {
  171. return this.getRangeDayType(day);
  172. }
  173. return '';
  174. },
  175. getBottomInfo: function (type) {
  176. if (this.data.type === 'range') {
  177. if (type === 'start') {
  178. return '开始';
  179. }
  180. if (type === 'end') {
  181. return '结束';
  182. }
  183. if (type === 'start-end') {
  184. return '开始/结束';
  185. }
  186. }
  187. },
  188. },
  189. });
  190. export default global['__wxComponents']['weapp/lib/calendar/components/month/index']
  191. </script>
  192. <style platform="mp-weixin">
  193. @import '../../../common/index.css';.van-calendar{background-color:var(--calendar-background-color,#fff);display:flex;flex-direction:column;height:100%}.van-calendar__month-title{font-size:var(--calendar-month-title-font-size,14px);font-weight:var(--font-weight-bold,500);height:var(--calendar-header-title-height,44px);line-height:var(--calendar-header-title-height,44px);text-align:center}.van-calendar__days{display:flex;flex-wrap:wrap;position:relative;-webkit-user-select:none;user-select:none}.van-calendar__month-mark{color:var(--calendar-month-mark-color,rgba(242,243,245,.8));font-size:var(--calendar-month-mark-font-size,160px);left:50%;pointer-events:none;position:absolute;top:50%;transform:translate(-50%,-50%);z-index:0}.van-calendar__day,.van-calendar__selected-day{align-items:center;display:flex;justify-content:center;text-align:center}.van-calendar__day{font-size:var(--calendar-day-font-size,16px);height:var(--calendar-day-height,64px);position:relative;width:14.285%}.van-calendar__day--end,.van-calendar__day--multiple-middle,.van-calendar__day--multiple-selected,.van-calendar__day--start,.van-calendar__day--start-end{background-color:var(--calendar-range-edge-background-color,#ee0a24);color:var(--calendar-range-edge-color,#fff)}.van-calendar__day--start{border-radius:4px 0 0 4px}.van-calendar__day--end{border-radius:0 4px 4px 0}.van-calendar__day--multiple-selected,.van-calendar__day--start-end{border-radius:4px}.van-calendar__day--middle{color:var(--calendar-range-middle-color,#ee0a24)}.van-calendar__day--middle:after{background-color:currentColor;bottom:0;content:"";left:0;opacity:var(--calendar-range-middle-background-opacity,.1);position:absolute;right:0;top:0}.van-calendar__day--disabled{color:var(--calendar-day-disabled-color,#c8c9cc);cursor:default}.van-calendar__bottom-info,.van-calendar__top-info{font-size:var(--calendar-info-font-size,10px);left:0;line-height:var(--calendar-info-line-height,14px);position:absolute;right:0}@media (max-width:350px){.van-calendar__bottom-info,.van-calendar__top-info{font-size:9px}}.van-calendar__top-info{top:6px}.van-calendar__bottom-info{bottom:6px}.van-calendar__selected-day{background-color:var(--calendar-selected-day-background-color,#ee0a24);border-radius:4px;color:var(--calendar-selected-day-color,#fff);height:var(--calendar-selected-day-size,54px);width:var(--calendar-selected-day-size,54px)}
  194. </style>