index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. firstDayOfWeek: {
  35. type: Number,
  36. observer: 'setDays',
  37. },
  38. allowSameDay: Boolean,
  39. showSubtitle: Boolean,
  40. showMonthTitle: Boolean,
  41. },
  42. data: {
  43. visible: true,
  44. days: [],
  45. },
  46. methods: {
  47. onClick: function (event) {
  48. var index = event.currentTarget.dataset.index;
  49. var item = this.data.days[index];
  50. if (item.type !== 'disabled') {
  51. this.$emit('click', item);
  52. }
  53. },
  54. setDays: function () {
  55. var days = [];
  56. var startDate = new Date(this.data.date);
  57. var year = startDate.getFullYear();
  58. var month = startDate.getMonth();
  59. var totalDay = utils_1.getMonthEndDay(
  60. startDate.getFullYear(),
  61. startDate.getMonth() + 1
  62. );
  63. for (var day = 1; day <= totalDay; day++) {
  64. var date = new Date(year, month, day);
  65. var type = this.getDayType(date);
  66. var config = {
  67. date: date,
  68. type: type,
  69. text: day,
  70. bottomInfo: this.getBottomInfo(type),
  71. };
  72. if (this.data.formatter) {
  73. config = this.data.formatter(config);
  74. }
  75. days.push(config);
  76. }
  77. this.setData({ days: days });
  78. },
  79. getMultipleDayType: function (day) {
  80. var currentDate = this.data.currentDate;
  81. if (!Array.isArray(currentDate)) {
  82. return '';
  83. }
  84. var isSelected = function (date) {
  85. return currentDate.some(function (item) {
  86. return utils_1.compareDay(item, date) === 0;
  87. });
  88. };
  89. if (isSelected(day)) {
  90. var prevDay = utils_1.getPrevDay(day);
  91. var nextDay = utils_1.getNextDay(day);
  92. var prevSelected = isSelected(prevDay);
  93. var nextSelected = isSelected(nextDay);
  94. if (prevSelected && nextSelected) {
  95. return 'multiple-middle';
  96. }
  97. if (prevSelected) {
  98. return 'end';
  99. }
  100. return nextSelected ? 'start' : 'multiple-selected';
  101. }
  102. return '';
  103. },
  104. getRangeDayType: function (day) {
  105. var _a = this.data,
  106. currentDate = _a.currentDate,
  107. allowSameDay = _a.allowSameDay;
  108. if (!Array.isArray(currentDate)) {
  109. return '';
  110. }
  111. var startDay = currentDate[0],
  112. endDay = currentDate[1];
  113. if (!startDay) {
  114. return '';
  115. }
  116. var compareToStart = utils_1.compareDay(day, startDay);
  117. if (!endDay) {
  118. return compareToStart === 0 ? 'start' : '';
  119. }
  120. var compareToEnd = utils_1.compareDay(day, endDay);
  121. if (compareToStart === 0 && compareToEnd === 0 && allowSameDay) {
  122. return 'start-end';
  123. }
  124. if (compareToStart === 0) {
  125. return 'start';
  126. }
  127. if (compareToEnd === 0) {
  128. return 'end';
  129. }
  130. if (compareToStart > 0 && compareToEnd < 0) {
  131. return 'middle';
  132. }
  133. return '';
  134. },
  135. getDayType: function (day) {
  136. var _a = this.data,
  137. type = _a.type,
  138. minDate = _a.minDate,
  139. maxDate = _a.maxDate,
  140. currentDate = _a.currentDate;
  141. if (
  142. utils_1.compareDay(day, minDate) < 0 ||
  143. utils_1.compareDay(day, maxDate) > 0
  144. ) {
  145. return 'disabled';
  146. }
  147. if (type === 'single') {
  148. return utils_1.compareDay(day, currentDate) === 0 ? 'selected' : '';
  149. }
  150. if (type === 'multiple') {
  151. return this.getMultipleDayType(day);
  152. }
  153. /* istanbul ignore else */
  154. if (type === 'range') {
  155. return this.getRangeDayType(day);
  156. }
  157. return '';
  158. },
  159. getBottomInfo: function (type) {
  160. if (this.data.type === 'range') {
  161. if (type === 'start') {
  162. return '开始';
  163. }
  164. if (type === 'end') {
  165. return '结束';
  166. }
  167. if (type === 'start-end') {
  168. return '开始/结束';
  169. }
  170. }
  171. },
  172. },
  173. });