index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <template>
  2. <uni-shadow-root class="weapp-lib-calendar-index"><van-popup v-if="poppable" :custom-class="'van-calendar__popup--'+(position)" close-icon-class="van-calendar__close-icon" :show="show" :round="round" :position="position" :closeable="showTitle || showSubtitle" :close-on-click-overlay="closeOnClickOverlay" :safe-area-inset-bottom="safeAreaInsetBottom" @enter="onOpen" @close="onClose" @after-enter="onOpened" @after-leave="onClosed">
  3. <include src="./calendar.wxml"></include>
  4. </van-popup>
  5. <include v-else src="./calendar.wxml"></include>
  6. <van-toast id="van-toast"></van-toast></uni-shadow-root>
  7. </template>
  8. <wxs src="./index.wxs" module="computed"></wxs><wxs src="../wxs/utils.wxs" module="utils"></wxs>
  9. <script>
  10. const __wxTemplateComponentProps = {}
  11. import __wxTemplateComponent0 from './calendar.vue'
  12. import Header from './components/header/index.vue'
  13. import Month from './components/month/index.vue'
  14. import VanButton from '../button/index.vue'
  15. import VanPopup from '../popup/index.vue'
  16. import VanToast from '../toast/index.vue'
  17. global['__wxVueOptions'] = {components:{'header': Header,'month': Month,'van-button': VanButton,'van-popup': VanPopup,'van-toast': VanToast,}}
  18. global['__wxRoute'] = 'weapp/lib/calendar/index'
  19. "use strict";
  20. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  21. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  22. if (ar || !(i in from)) {
  23. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  24. ar[i] = from[i];
  25. }
  26. }
  27. return to.concat(ar || Array.prototype.slice.call(from));
  28. };
  29. var __importDefault = (this && this.__importDefault) || function (mod) {
  30. return (mod && mod.__esModule) ? mod : { "default": mod };
  31. };
  32. Object.defineProperty(exports, "__esModule", { value: true });
  33. var component_1 = require("../common/component");
  34. var utils_1 = require("./utils");
  35. var toast_1 = __importDefault(require("../toast/toast"));
  36. var utils_2 = require("../common/utils");
  37. var initialMinDate = (0, utils_1.getToday)().getTime();
  38. var initialMaxDate = (function () {
  39. var now = (0, utils_1.getToday)();
  40. return new Date(now.getFullYear(), now.getMonth() + 6, now.getDate()).getTime();
  41. })();
  42. var getTime = function (date) {
  43. return date instanceof Date ? date.getTime() : date;
  44. };
  45. (0, component_1.VantComponent)({
  46. props: {
  47. title: {
  48. type: String,
  49. value: '日期选择',
  50. },
  51. color: String,
  52. show: {
  53. type: Boolean,
  54. observer: function (val) {
  55. if (val) {
  56. this.initRect();
  57. this.scrollIntoView();
  58. }
  59. },
  60. },
  61. formatter: null,
  62. confirmText: {
  63. type: String,
  64. value: '确定',
  65. },
  66. confirmDisabledText: {
  67. type: String,
  68. value: '确定',
  69. },
  70. rangePrompt: String,
  71. showRangePrompt: {
  72. type: Boolean,
  73. value: true,
  74. },
  75. defaultDate: {
  76. type: null,
  77. observer: function (val) {
  78. this.setData({ currentDate: val });
  79. this.scrollIntoView();
  80. },
  81. },
  82. allowSameDay: Boolean,
  83. type: {
  84. type: String,
  85. value: 'single',
  86. observer: 'reset',
  87. },
  88. minDate: {
  89. type: Number,
  90. value: initialMinDate,
  91. },
  92. maxDate: {
  93. type: Number,
  94. value: initialMaxDate,
  95. },
  96. position: {
  97. type: String,
  98. value: 'bottom',
  99. },
  100. rowHeight: {
  101. type: null,
  102. value: utils_1.ROW_HEIGHT,
  103. },
  104. round: {
  105. type: Boolean,
  106. value: true,
  107. },
  108. poppable: {
  109. type: Boolean,
  110. value: true,
  111. },
  112. showMark: {
  113. type: Boolean,
  114. value: true,
  115. },
  116. showTitle: {
  117. type: Boolean,
  118. value: true,
  119. },
  120. showConfirm: {
  121. type: Boolean,
  122. value: true,
  123. },
  124. showSubtitle: {
  125. type: Boolean,
  126. value: true,
  127. },
  128. safeAreaInsetBottom: {
  129. type: Boolean,
  130. value: true,
  131. },
  132. closeOnClickOverlay: {
  133. type: Boolean,
  134. value: true,
  135. },
  136. maxRange: {
  137. type: null,
  138. value: null,
  139. },
  140. minRange: {
  141. type: Number,
  142. value: 1,
  143. },
  144. firstDayOfWeek: {
  145. type: Number,
  146. value: 0,
  147. },
  148. readonly: Boolean,
  149. },
  150. data: {
  151. subtitle: '',
  152. currentDate: null,
  153. scrollIntoView: '',
  154. },
  155. created: function () {
  156. this.setData({
  157. currentDate: this.getInitialDate(this.data.defaultDate),
  158. });
  159. },
  160. mounted: function () {
  161. if (this.data.show || !this.data.poppable) {
  162. this.initRect();
  163. this.scrollIntoView();
  164. }
  165. },
  166. methods: {
  167. reset: function () {
  168. this.setData({ currentDate: this.getInitialDate() });
  169. this.scrollIntoView();
  170. },
  171. initRect: function () {
  172. var _this = this;
  173. if (this.contentObserver != null) {
  174. this.contentObserver.disconnect();
  175. }
  176. var contentObserver = this.createIntersectionObserver({
  177. thresholds: [0, 0.1, 0.9, 1],
  178. observeAll: true,
  179. });
  180. this.contentObserver = contentObserver;
  181. contentObserver.relativeTo('.van-calendar__body');
  182. contentObserver.observe('.month', function (res) {
  183. if (res.boundingClientRect.top <= res.relativeRect.top) {
  184. // @ts-ignore
  185. _this.setData({ subtitle: (0, utils_1.formatMonthTitle)(res.dataset.date) });
  186. }
  187. });
  188. },
  189. limitDateRange: function (date, minDate, maxDate) {
  190. if (minDate === void 0) { minDate = null; }
  191. if (maxDate === void 0) { maxDate = null; }
  192. minDate = minDate || this.data.minDate;
  193. maxDate = maxDate || this.data.maxDate;
  194. if ((0, utils_1.compareDay)(date, minDate) === -1) {
  195. return minDate;
  196. }
  197. if ((0, utils_1.compareDay)(date, maxDate) === 1) {
  198. return maxDate;
  199. }
  200. return date;
  201. },
  202. getInitialDate: function (defaultDate) {
  203. var _this = this;
  204. if (defaultDate === void 0) { defaultDate = null; }
  205. var _a = this.data, type = _a.type, minDate = _a.minDate, maxDate = _a.maxDate, allowSameDay = _a.allowSameDay;
  206. var now = (0, utils_1.getToday)().getTime();
  207. if (type === 'range') {
  208. if (!Array.isArray(defaultDate)) {
  209. defaultDate = [];
  210. }
  211. var _b = defaultDate || [], startDay = _b[0], endDay = _b[1];
  212. var start = this.limitDateRange(startDay || now, minDate, (0, utils_1.getPrevDay)(new Date(maxDate)).getTime());
  213. var date = getTime(endDay || now);
  214. var end = this.limitDateRange(date, allowSameDay ? date : (0, utils_1.getNextDay)(new Date(minDate)).getTime());
  215. return [start, end];
  216. }
  217. if (type === 'multiple') {
  218. if (Array.isArray(defaultDate)) {
  219. return defaultDate.map(function (date) { return _this.limitDateRange(date); });
  220. }
  221. return [this.limitDateRange(now)];
  222. }
  223. if (!defaultDate || Array.isArray(defaultDate)) {
  224. defaultDate = now;
  225. }
  226. return this.limitDateRange(defaultDate);
  227. },
  228. scrollIntoView: function () {
  229. var _this = this;
  230. (0, utils_2.requestAnimationFrame)(function () {
  231. var _a = _this.data, currentDate = _a.currentDate, type = _a.type, show = _a.show, poppable = _a.poppable, minDate = _a.minDate, maxDate = _a.maxDate;
  232. // @ts-ignore
  233. var targetDate = type === 'single' ? currentDate : currentDate[0];
  234. var displayed = show || !poppable;
  235. if (!targetDate || !displayed) {
  236. return;
  237. }
  238. var months = (0, utils_1.getMonths)(minDate, maxDate);
  239. months.some(function (month, index) {
  240. if ((0, utils_1.compareMonth)(month, targetDate) === 0) {
  241. _this.setData({ scrollIntoView: "month".concat(index) });
  242. return true;
  243. }
  244. return false;
  245. });
  246. });
  247. },
  248. onOpen: function () {
  249. this.$emit('open');
  250. },
  251. onOpened: function () {
  252. this.$emit('opened');
  253. },
  254. onClose: function () {
  255. this.$emit('close');
  256. },
  257. onClosed: function () {
  258. this.$emit('closed');
  259. },
  260. onClickDay: function (event) {
  261. if (this.data.readonly) {
  262. return;
  263. }
  264. var date = event.detail.date;
  265. var _a = this.data, type = _a.type, currentDate = _a.currentDate, allowSameDay = _a.allowSameDay;
  266. if (type === 'range') {
  267. // @ts-ignore
  268. var startDay_1 = currentDate[0], endDay = currentDate[1];
  269. if (startDay_1 && !endDay) {
  270. var compareToStart = (0, utils_1.compareDay)(date, startDay_1);
  271. if (compareToStart === 1) {
  272. var days_1 = this.selectComponent('.month').data.days;
  273. days_1.some(function (day, index) {
  274. var isDisabled = day.type === 'disabled' &&
  275. getTime(startDay_1) < getTime(day.date) &&
  276. getTime(day.date) < getTime(date);
  277. if (isDisabled) {
  278. (date = days_1[index - 1].date);
  279. }
  280. return isDisabled;
  281. });
  282. this.select([startDay_1, date], true);
  283. }
  284. else if (compareToStart === -1) {
  285. this.select([date, null]);
  286. }
  287. else if (allowSameDay) {
  288. this.select([date, date], true);
  289. }
  290. }
  291. else {
  292. this.select([date, null]);
  293. }
  294. }
  295. else if (type === 'multiple') {
  296. var selectedIndex_1;
  297. // @ts-ignore
  298. var selected = currentDate.some(function (dateItem, index) {
  299. var equal = (0, utils_1.compareDay)(dateItem, date) === 0;
  300. if (equal) {
  301. selectedIndex_1 = index;
  302. }
  303. return equal;
  304. });
  305. if (selected) {
  306. // @ts-ignore
  307. var cancelDate = currentDate.splice(selectedIndex_1, 1);
  308. this.setData({ currentDate: currentDate });
  309. this.unselect(cancelDate);
  310. }
  311. else {
  312. // @ts-ignore
  313. this.select(__spreadArray(__spreadArray([], currentDate, true), [date], false));
  314. }
  315. }
  316. else {
  317. this.select(date, true);
  318. }
  319. },
  320. unselect: function (dateArray) {
  321. var date = dateArray[0];
  322. if (date) {
  323. this.$emit('unselect', (0, utils_1.copyDates)(date));
  324. }
  325. },
  326. select: function (date, complete) {
  327. if (complete && this.data.type === 'range') {
  328. var valid = this.checkRange(date);
  329. if (!valid) {
  330. // auto selected to max range if showConfirm
  331. if (this.data.showConfirm) {
  332. this.emit([
  333. date[0],
  334. (0, utils_1.getDayByOffset)(date[0], this.data.maxRange - 1),
  335. ]);
  336. }
  337. else {
  338. this.emit(date);
  339. }
  340. return;
  341. }
  342. }
  343. this.emit(date);
  344. if (complete && !this.data.showConfirm) {
  345. this.onConfirm();
  346. }
  347. },
  348. emit: function (date) {
  349. this.setData({
  350. currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date),
  351. });
  352. this.$emit('select', (0, utils_1.copyDates)(date));
  353. },
  354. checkRange: function (date) {
  355. var _a = this.data, maxRange = _a.maxRange, rangePrompt = _a.rangePrompt, showRangePrompt = _a.showRangePrompt;
  356. if (maxRange && (0, utils_1.calcDateNum)(date) > maxRange) {
  357. if (showRangePrompt) {
  358. (0, toast_1.default)({
  359. context: this,
  360. message: rangePrompt || "\u9009\u62E9\u5929\u6570\u4E0D\u80FD\u8D85\u8FC7 ".concat(maxRange, " \u5929"),
  361. });
  362. }
  363. this.$emit('over-range');
  364. return false;
  365. }
  366. return true;
  367. },
  368. onConfirm: function () {
  369. var _this = this;
  370. if (this.data.type === 'range' &&
  371. !this.checkRange(this.data.currentDate)) {
  372. return;
  373. }
  374. wx.nextTick(function () {
  375. // @ts-ignore
  376. _this.$emit('confirm', (0, utils_1.copyDates)(_this.data.currentDate));
  377. });
  378. },
  379. onClickSubtitle: function (event) {
  380. this.$emit('click-subtitle', event);
  381. },
  382. },
  383. });
  384. export default global['__wxComponents']['weapp/lib/calendar/index']
  385. </script>
  386. <style platform="mp-weixin">
  387. @import '../common/index.css';.van-calendar{background-color:var(--calendar-background-color,#fff);display:flex;flex-direction:column;height:var(--calendar-height,100%)}.van-calendar__close-icon{top:11px}.van-calendar__popup--bottom,.van-calendar__popup--top{height:var(--calendar-popup-height,80%)}.van-calendar__popup--left,.van-calendar__popup--right{height:100%}.van-calendar__body{-webkit-overflow-scrolling:touch;flex:1;overflow:auto}.van-calendar__footer{flex-shrink:0;padding:0 var(--padding-md,16px)}.van-calendar__footer--safe-area-inset-bottom{padding-bottom:env(safe-area-inset-bottom)}.van-calendar__footer+.van-calendar__footer,.van-calendar__footer:empty{display:none}.van-calendar__footer:empty+.van-calendar__footer{display:block!important}.van-calendar__confirm{height:var(--calendar-confirm-button-height,36px)!important;line-height:var(--calendar-confirm-button-line-height,34px)!important;margin:var(--calendar-confirm-button-margin,7px 0)!important}
  388. </style>