calendar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. import { getPrevMonth, getPrevYear, getNextMonth, getNextYear } from './utils';
  13. const { prefix } = config;
  14. const name = `${prefix}-calendar`;
  15. const defaultLocaleText = {
  16. title: '请选择日期',
  17. weekdays: ['日', '一', '二', '三', '四', '五', '六'],
  18. monthTitle: '{year} 年 {month}',
  19. months: ['1 月', '2 月', '3 月', '4 月', '5 月', '6 月', '7 月', '8 月', '9 月', '10 月', '11 月', '12 月'],
  20. confirm: '确认',
  21. };
  22. let Calendar = class Calendar extends SuperComponent {
  23. constructor() {
  24. super(...arguments);
  25. this.behaviors = [useCustomNavbar];
  26. this.externalClasses = [`${prefix}-class`];
  27. this.options = {
  28. multipleSlots: true,
  29. };
  30. this.properties = props;
  31. this.data = {
  32. prefix,
  33. classPrefix: name,
  34. months: [],
  35. scrollIntoView: '',
  36. innerConfirmBtn: {},
  37. realLocalText: {},
  38. currentMonth: {},
  39. actionButtons: {
  40. preYearBtnDisable: false,
  41. prevMonthBtnDisable: false,
  42. nextMonthBtnDisable: false,
  43. nextYearBtnDisable: false,
  44. },
  45. };
  46. this.controlledProps = [
  47. {
  48. key: 'value',
  49. event: 'confirm',
  50. },
  51. {
  52. key: 'value',
  53. event: 'change',
  54. },
  55. ];
  56. this.lifetimes = {
  57. created() {
  58. this.base = new TCalendar(this.properties);
  59. },
  60. ready() {
  61. const realLocalText = Object.assign(Object.assign({}, defaultLocaleText), this.properties.localeText);
  62. this.initialValue();
  63. this.setData({
  64. days: this.base.getDays(realLocalText.weekdays),
  65. realLocalText,
  66. });
  67. this.calcMonths();
  68. if (this.data.switchMode !== 'none') {
  69. this.calcCurrentMonth();
  70. }
  71. if (!this.data.usePopup) {
  72. this.scrollIntoView();
  73. }
  74. },
  75. };
  76. this.observers = {
  77. type(v) {
  78. this.base.type = v;
  79. },
  80. confirmBtn(v) {
  81. if (typeof v === 'string') {
  82. this.setData({ innerConfirmBtn: v === 'slot' ? 'slot' : { content: v } });
  83. }
  84. else if (typeof v === 'object') {
  85. this.setData({ innerConfirmBtn: v });
  86. }
  87. },
  88. 'firstDayOfWeek,minDate,maxDate'(firstDayOfWeek, minDate, maxDate) {
  89. firstDayOfWeek && (this.base.firstDayOfWeek = firstDayOfWeek);
  90. minDate && (this.base.minDate = minDate);
  91. maxDate && (this.base.maxDate = maxDate);
  92. this.calcMonths();
  93. },
  94. value(v) {
  95. this.base.value = v;
  96. this.calcMonths();
  97. },
  98. visible(v) {
  99. if (v) {
  100. this.scrollIntoView();
  101. this.base.value = this.data.value;
  102. this.calcMonths();
  103. }
  104. },
  105. format(v) {
  106. const { usePopup, visible } = this.data;
  107. this.base.format = v;
  108. if (!usePopup || visible) {
  109. this.calcMonths();
  110. }
  111. },
  112. };
  113. this.methods = {
  114. initialValue() {
  115. const { value, type, minDate } = this.data;
  116. if (!value) {
  117. const today = new Date();
  118. const now = minDate || new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime();
  119. const initialValue = type === 'single' ? now : [now];
  120. if (type === 'range') {
  121. initialValue[1] = now + 24 * 3600 * 1000;
  122. }
  123. this.setData({
  124. value: initialValue,
  125. });
  126. this.base.value = initialValue;
  127. }
  128. },
  129. scrollIntoView() {
  130. const { value } = this.data;
  131. if (!value)
  132. return;
  133. const date = new Date(Array.isArray(value) ? value[0] : value);
  134. if (date) {
  135. this.setData({
  136. scrollIntoView: `year_${date.getFullYear()}_month_${date.getMonth()}`,
  137. });
  138. }
  139. },
  140. getCurrentYearAndMonth(v) {
  141. const date = new Date(v);
  142. return { year: date.getFullYear(), month: date.getMonth() };
  143. },
  144. updateActionButton(value) {
  145. const _min = this.getCurrentYearAndMonth(this.base.minDate);
  146. const _max = this.getCurrentYearAndMonth(this.base.maxDate);
  147. const _minTimestamp = new Date(_min.year, _min.month, 1).getTime();
  148. const _maxTimestamp = new Date(_max.year, _max.month, 1).getTime();
  149. const _prevYearTimestamp = getPrevYear(value).getTime();
  150. const _prevMonthTimestamp = getPrevMonth(value).getTime();
  151. const _nextMonthTimestamp = getNextMonth(value).getTime();
  152. const _nextYearTimestamp = getNextYear(value).getTime();
  153. const preYearBtnDisable = _prevYearTimestamp < _minTimestamp || _prevMonthTimestamp < _minTimestamp;
  154. const prevMonthBtnDisable = _prevMonthTimestamp < _minTimestamp;
  155. const nextYearBtnDisable = _nextMonthTimestamp > _maxTimestamp || _nextYearTimestamp > _maxTimestamp;
  156. const nextMonthBtnDisable = _nextMonthTimestamp > _maxTimestamp;
  157. this.setData({
  158. actionButtons: {
  159. preYearBtnDisable,
  160. prevMonthBtnDisable,
  161. nextYearBtnDisable,
  162. nextMonthBtnDisable,
  163. },
  164. });
  165. },
  166. calcCurrentMonth(newValue) {
  167. const date = newValue || this.getCurrentDate();
  168. const { year, month } = this.getCurrentYearAndMonth(date);
  169. const currentMonth = this.data.months.filter((item) => item.year === year && item.month === month);
  170. this.updateActionButton(date);
  171. this.setData({
  172. currentMonth: currentMonth.length > 0 ? currentMonth : [this.data.months[0]],
  173. });
  174. },
  175. calcMonths() {
  176. const months = this.base.getMonths();
  177. this.setData({
  178. months,
  179. });
  180. },
  181. close(trigger) {
  182. if (this.data.autoClose) {
  183. this.setData({ visible: false });
  184. }
  185. this.triggerEvent('close', { trigger });
  186. },
  187. onVisibleChange() {
  188. this.close('overlay');
  189. },
  190. handleClose() {
  191. this.close('close-btn');
  192. },
  193. handleSelect(e) {
  194. const { date, year, month } = e.currentTarget.dataset;
  195. if (date.type === 'disabled')
  196. return;
  197. const rawValue = this.base.select({ cellType: date.type, year, month, date: date.day });
  198. const value = this.toTime(rawValue);
  199. this.calcMonths();
  200. if (this.data.switchMode !== 'none') {
  201. const date = this.getCurrentDate();
  202. this.calcCurrentMonth(date);
  203. }
  204. if (this.data.confirmBtn == null) {
  205. if (this.data.type === 'single' || rawValue.length === 2) {
  206. this.setData({ visible: false });
  207. this._trigger('change', { value });
  208. }
  209. }
  210. this.triggerEvent('select', { value });
  211. },
  212. onTplButtonTap() {
  213. const rawValue = this.base.getTrimValue();
  214. const value = this.toTime(rawValue);
  215. this.close('confirm-btn');
  216. this._trigger('confirm', { value });
  217. },
  218. toTime(val) {
  219. if (Array.isArray(val)) {
  220. return val.map((item) => item.getTime());
  221. }
  222. return val.getTime();
  223. },
  224. onScroll(e) {
  225. this.triggerEvent('scroll', e.detail);
  226. },
  227. getCurrentDate() {
  228. var _a, _b;
  229. let time = Array.isArray(this.base.value) ? this.base.value[0] : this.base.value;
  230. if (this.data.currentMonth.length > 0) {
  231. const year = (_a = this.data.currentMonth[0]) === null || _a === void 0 ? void 0 : _a.year;
  232. const month = (_b = this.data.currentMonth[0]) === null || _b === void 0 ? void 0 : _b.month;
  233. time = new Date(year, month, 1).getTime();
  234. }
  235. return time;
  236. },
  237. handleSwitchModeChange(e) {
  238. const { type, disabled } = e.currentTarget.dataset;
  239. if (disabled)
  240. return;
  241. const date = this.getCurrentDate();
  242. const funcMap = {
  243. 'pre-year': () => getPrevYear(date),
  244. 'pre-month': () => getPrevMonth(date),
  245. 'next-month': () => getNextMonth(date),
  246. 'next-year': () => getNextYear(date),
  247. };
  248. const newValue = funcMap[type]();
  249. if (!newValue)
  250. return;
  251. const { year, month } = this.getCurrentYearAndMonth(newValue);
  252. this.triggerEvent('panel-change', { year, month: month + 1 });
  253. this.calcCurrentMonth(newValue);
  254. },
  255. };
  256. }
  257. };
  258. Calendar = __decorate([
  259. wxComponent()
  260. ], Calendar);
  261. export default Calendar;