电速宝
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import TCalendar from '../common/shared/calendar/index';
  11. import useCustomNavbar from '../mixins/using-custom-navbar';
  12. const { prefix } = config;
  13. const name = `${prefix}-calendar`;
  14. let Calendar = class Calendar extends SuperComponent {
  15. constructor() {
  16. super(...arguments);
  17. this.behaviors = [useCustomNavbar];
  18. this.externalClasses = [`${prefix}-class`];
  19. this.options = {
  20. multipleSlots: true,
  21. };
  22. this.properties = props;
  23. this.data = {
  24. prefix,
  25. classPrefix: name,
  26. months: [],
  27. scrollIntoView: '',
  28. innerConfirmBtn: { content: '确定' },
  29. };
  30. this.controlledProps = [
  31. {
  32. key: 'value',
  33. event: 'confirm',
  34. },
  35. {
  36. key: 'value',
  37. event: 'change',
  38. },
  39. ];
  40. this.lifetimes = {
  41. created() {
  42. this.base = new TCalendar(this.properties);
  43. },
  44. ready() {
  45. this.initialValue();
  46. this.setData({
  47. days: this.base.getDays(),
  48. });
  49. this.calcMonths();
  50. if (!this.data.usePopup) {
  51. this.scrollIntoView();
  52. }
  53. },
  54. };
  55. this.observers = {
  56. type(v) {
  57. this.base.type = v;
  58. },
  59. confirmBtn(v) {
  60. if (typeof v === 'string') {
  61. this.setData({ innerConfirmBtn: v === 'slot' ? 'slot' : { content: v } });
  62. }
  63. else if (typeof v === 'object') {
  64. this.setData({ innerConfirmBtn: v });
  65. }
  66. },
  67. 'firstDayOfWeek,minDate,maxDate'(firstDayOfWeek, minDate, maxDate) {
  68. firstDayOfWeek && (this.base.firstDayOfWeek = firstDayOfWeek);
  69. minDate && (this.base.minDate = minDate);
  70. maxDate && (this.base.maxDate = maxDate);
  71. this.calcMonths();
  72. },
  73. value(v) {
  74. this.base.value = v;
  75. this.calcMonths();
  76. },
  77. visible(v) {
  78. if (v) {
  79. this.scrollIntoView();
  80. this.base.value = this.data.value;
  81. this.calcMonths();
  82. }
  83. },
  84. format(v) {
  85. const { usePopup, visible } = this.data;
  86. this.base.format = v;
  87. if (!usePopup || visible) {
  88. this.calcMonths();
  89. }
  90. },
  91. };
  92. this.methods = {
  93. initialValue() {
  94. const { value, type, minDate } = this.data;
  95. if (!value) {
  96. const today = new Date();
  97. const now = minDate || new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime();
  98. const initialValue = type === 'single' ? now : [now];
  99. if (type === 'range') {
  100. initialValue[1] = now + 24 * 3600 * 1000;
  101. }
  102. this.setData({
  103. value: initialValue,
  104. });
  105. this.base.value = initialValue;
  106. }
  107. },
  108. scrollIntoView() {
  109. const { value } = this.data;
  110. if (!value)
  111. return;
  112. const date = new Date(Array.isArray(value) ? value[0] : value);
  113. if (date) {
  114. this.setData({
  115. scrollIntoView: `year_${date.getFullYear()}_month_${date.getMonth()}`,
  116. });
  117. }
  118. },
  119. calcMonths() {
  120. const months = this.base.getMonths();
  121. this.setData({
  122. months,
  123. });
  124. },
  125. close(trigger) {
  126. if (this.data.autoClose) {
  127. this.setData({ visible: false });
  128. }
  129. this.triggerEvent('close', { trigger });
  130. },
  131. onVisibleChange() {
  132. this.close('overlay');
  133. },
  134. handleClose() {
  135. this.close('close-btn');
  136. },
  137. handleSelect(e) {
  138. const { date, year, month } = e.currentTarget.dataset;
  139. if (date.type === 'disabled')
  140. return;
  141. const rawValue = this.base.select({ cellType: date.type, year, month, date: date.day });
  142. const value = this.toTime(rawValue);
  143. this.calcMonths();
  144. if (this.data.confirmBtn == null) {
  145. if (this.data.type === 'single' || rawValue.length === 2) {
  146. this.setData({ visible: false });
  147. this._trigger('change', { value });
  148. }
  149. }
  150. this.triggerEvent('select', { value });
  151. },
  152. onTplButtonTap() {
  153. const rawValue = this.base.getTrimValue();
  154. const value = this.toTime(rawValue);
  155. this.close('confirm-btn');
  156. this._trigger('confirm', { value });
  157. },
  158. toTime(val) {
  159. if (Array.isArray(val)) {
  160. return val.map((item) => item.getTime());
  161. }
  162. return val.getTime();
  163. },
  164. };
  165. }
  166. };
  167. Calendar = __decorate([
  168. wxComponent()
  169. ], Calendar);
  170. export default Calendar;