index.js 4.3 KB

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