index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../../../common/component");
  4. var utils_1 = require("../../utils");
  5. (0, 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 = (0, utils_1.getMonthEndDay)(startDate.getFullYear(), startDate.getMonth() + 1);
  60. for (var day = 1; day <= totalDay; day++) {
  61. var date = new Date(year, month, day);
  62. var type = this.getDayType(date);
  63. var config = {
  64. date: date,
  65. type: type,
  66. text: day,
  67. bottomInfo: this.getBottomInfo(type),
  68. };
  69. if (this.data.formatter) {
  70. config = this.data.formatter(config);
  71. }
  72. days.push(config);
  73. }
  74. this.setData({ days: days });
  75. },
  76. getMultipleDayType: function (day) {
  77. var currentDate = this.data.currentDate;
  78. if (!Array.isArray(currentDate)) {
  79. return '';
  80. }
  81. var isSelected = function (date) {
  82. return currentDate.some(function (item) { return (0, utils_1.compareDay)(item, date) === 0; });
  83. };
  84. if (isSelected(day)) {
  85. var prevDay = (0, utils_1.getPrevDay)(day);
  86. var nextDay = (0, utils_1.getNextDay)(day);
  87. var prevSelected = isSelected(prevDay);
  88. var nextSelected = isSelected(nextDay);
  89. if (prevSelected && nextSelected) {
  90. return 'multiple-middle';
  91. }
  92. if (prevSelected) {
  93. return 'end';
  94. }
  95. return nextSelected ? 'start' : 'multiple-selected';
  96. }
  97. return '';
  98. },
  99. getRangeDayType: function (day) {
  100. var _a = this.data, currentDate = _a.currentDate, allowSameDay = _a.allowSameDay;
  101. if (!Array.isArray(currentDate)) {
  102. return '';
  103. }
  104. var startDay = currentDate[0], endDay = currentDate[1];
  105. if (!startDay) {
  106. return '';
  107. }
  108. var compareToStart = (0, utils_1.compareDay)(day, startDay);
  109. if (!endDay) {
  110. return compareToStart === 0 ? 'start' : '';
  111. }
  112. var compareToEnd = (0, utils_1.compareDay)(day, endDay);
  113. if (compareToStart === 0 && compareToEnd === 0 && allowSameDay) {
  114. return 'start-end';
  115. }
  116. if (compareToStart === 0) {
  117. return 'start';
  118. }
  119. if (compareToEnd === 0) {
  120. return 'end';
  121. }
  122. if (compareToStart > 0 && compareToEnd < 0) {
  123. return 'middle';
  124. }
  125. return '';
  126. },
  127. getDayType: function (day) {
  128. var _a = this.data, type = _a.type, minDate = _a.minDate, maxDate = _a.maxDate, currentDate = _a.currentDate;
  129. if ((0, utils_1.compareDay)(day, minDate) < 0 || (0, utils_1.compareDay)(day, maxDate) > 0) {
  130. return 'disabled';
  131. }
  132. if (type === 'single') {
  133. return (0, utils_1.compareDay)(day, currentDate) === 0 ? 'selected' : '';
  134. }
  135. if (type === 'multiple') {
  136. return this.getMultipleDayType(day);
  137. }
  138. /* istanbul ignore else */
  139. if (type === 'range') {
  140. return this.getRangeDayType(day);
  141. }
  142. return '';
  143. },
  144. getBottomInfo: function (type) {
  145. if (this.data.type === 'range') {
  146. if (type === 'start') {
  147. return '开始';
  148. }
  149. if (type === 'end') {
  150. return '结束';
  151. }
  152. if (type === 'start-end') {
  153. return '开始/结束';
  154. }
  155. }
  156. },
  157. },
  158. });