index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <template>
  2. <uni-shadow-root class="weapp-dist-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/dist/calendar/index'
  19. import { VantComponent } from '../common/component';
  20. import { ROW_HEIGHT, getPrevDay, getNextDay, getToday, compareDay, copyDates, calcDateNum, formatMonthTitle, compareMonth, getMonths, getDayByOffset, } from './utils';
  21. import Toast from '../toast/toast';
  22. import { requestAnimationFrame } from '../common/utils';
  23. const initialMinDate = getToday().getTime();
  24. const initialMaxDate = (() => {
  25. const now = getToday();
  26. return new Date(now.getFullYear(), now.getMonth() + 6, now.getDate()).getTime();
  27. })();
  28. const getTime = (date) => date instanceof Date ? date.getTime() : date;
  29. VantComponent({
  30. props: {
  31. title: {
  32. type: String,
  33. value: '日期选择',
  34. },
  35. color: String,
  36. show: {
  37. type: Boolean,
  38. observer(val) {
  39. if (val) {
  40. this.initRect();
  41. this.scrollIntoView();
  42. }
  43. },
  44. },
  45. formatter: null,
  46. confirmText: {
  47. type: String,
  48. value: '确定',
  49. },
  50. confirmDisabledText: {
  51. type: String,
  52. value: '确定',
  53. },
  54. rangePrompt: String,
  55. showRangePrompt: {
  56. type: Boolean,
  57. value: true,
  58. },
  59. defaultDate: {
  60. type: null,
  61. observer(val) {
  62. this.setData({ currentDate: val });
  63. this.scrollIntoView();
  64. },
  65. },
  66. allowSameDay: Boolean,
  67. type: {
  68. type: String,
  69. value: 'single',
  70. observer: 'reset',
  71. },
  72. minDate: {
  73. type: Number,
  74. value: initialMinDate,
  75. },
  76. maxDate: {
  77. type: Number,
  78. value: initialMaxDate,
  79. },
  80. position: {
  81. type: String,
  82. value: 'bottom',
  83. },
  84. rowHeight: {
  85. type: null,
  86. value: ROW_HEIGHT,
  87. },
  88. round: {
  89. type: Boolean,
  90. value: true,
  91. },
  92. poppable: {
  93. type: Boolean,
  94. value: true,
  95. },
  96. showMark: {
  97. type: Boolean,
  98. value: true,
  99. },
  100. showTitle: {
  101. type: Boolean,
  102. value: true,
  103. },
  104. showConfirm: {
  105. type: Boolean,
  106. value: true,
  107. },
  108. showSubtitle: {
  109. type: Boolean,
  110. value: true,
  111. },
  112. safeAreaInsetBottom: {
  113. type: Boolean,
  114. value: true,
  115. },
  116. closeOnClickOverlay: {
  117. type: Boolean,
  118. value: true,
  119. },
  120. maxRange: {
  121. type: null,
  122. value: null,
  123. },
  124. minRange: {
  125. type: Number,
  126. value: 1,
  127. },
  128. firstDayOfWeek: {
  129. type: Number,
  130. value: 0,
  131. },
  132. readonly: Boolean,
  133. },
  134. data: {
  135. subtitle: '',
  136. currentDate: null,
  137. scrollIntoView: '',
  138. },
  139. created() {
  140. this.setData({
  141. currentDate: this.getInitialDate(this.data.defaultDate),
  142. });
  143. },
  144. mounted() {
  145. if (this.data.show || !this.data.poppable) {
  146. this.initRect();
  147. this.scrollIntoView();
  148. }
  149. },
  150. methods: {
  151. reset() {
  152. this.setData({ currentDate: this.getInitialDate() });
  153. this.scrollIntoView();
  154. },
  155. initRect() {
  156. if (this.contentObserver != null) {
  157. this.contentObserver.disconnect();
  158. }
  159. const contentObserver = this.createIntersectionObserver({
  160. thresholds: [0, 0.1, 0.9, 1],
  161. observeAll: true,
  162. });
  163. this.contentObserver = contentObserver;
  164. contentObserver.relativeTo('.van-calendar__body');
  165. contentObserver.observe('.month', (res) => {
  166. if (res.boundingClientRect.top <= res.relativeRect.top) {
  167. // @ts-ignore
  168. this.setData({ subtitle: formatMonthTitle(res.dataset.date) });
  169. }
  170. });
  171. },
  172. limitDateRange(date, minDate = null, maxDate = null) {
  173. minDate = minDate || this.data.minDate;
  174. maxDate = maxDate || this.data.maxDate;
  175. if (compareDay(date, minDate) === -1) {
  176. return minDate;
  177. }
  178. if (compareDay(date, maxDate) === 1) {
  179. return maxDate;
  180. }
  181. return date;
  182. },
  183. getInitialDate(defaultDate = null) {
  184. const { type, minDate, maxDate, allowSameDay } = this.data;
  185. const now = getToday().getTime();
  186. if (type === 'range') {
  187. if (!Array.isArray(defaultDate)) {
  188. defaultDate = [];
  189. }
  190. const [startDay, endDay] = defaultDate || [];
  191. const start = this.limitDateRange(startDay || now, minDate, getPrevDay(new Date(maxDate)).getTime());
  192. const date = getTime(endDay || now);
  193. const end = this.limitDateRange(date, allowSameDay ? date : getNextDay(new Date(minDate)).getTime());
  194. return [start, end];
  195. }
  196. if (type === 'multiple') {
  197. if (Array.isArray(defaultDate)) {
  198. return defaultDate.map((date) => this.limitDateRange(date));
  199. }
  200. return [this.limitDateRange(now)];
  201. }
  202. if (!defaultDate || Array.isArray(defaultDate)) {
  203. defaultDate = now;
  204. }
  205. return this.limitDateRange(defaultDate);
  206. },
  207. scrollIntoView() {
  208. requestAnimationFrame(() => {
  209. const { currentDate, type, show, poppable, minDate, maxDate } = this.data;
  210. // @ts-ignore
  211. const targetDate = type === 'single' ? currentDate : currentDate[0];
  212. const displayed = show || !poppable;
  213. if (!targetDate || !displayed) {
  214. return;
  215. }
  216. const months = getMonths(minDate, maxDate);
  217. months.some((month, index) => {
  218. if (compareMonth(month, targetDate) === 0) {
  219. this.setData({ scrollIntoView: `month${index}` });
  220. return true;
  221. }
  222. return false;
  223. });
  224. });
  225. },
  226. onOpen() {
  227. this.$emit('open');
  228. },
  229. onOpened() {
  230. this.$emit('opened');
  231. },
  232. onClose() {
  233. this.$emit('close');
  234. },
  235. onClosed() {
  236. this.$emit('closed');
  237. },
  238. onClickDay(event) {
  239. if (this.data.readonly) {
  240. return;
  241. }
  242. let { date } = event.detail;
  243. const { type, currentDate, allowSameDay } = this.data;
  244. if (type === 'range') {
  245. // @ts-ignore
  246. const [startDay, endDay] = currentDate;
  247. if (startDay && !endDay) {
  248. const compareToStart = compareDay(date, startDay);
  249. if (compareToStart === 1) {
  250. const { days } = this.selectComponent('.month').data;
  251. days.some((day, index) => {
  252. const isDisabled = day.type === 'disabled' &&
  253. getTime(startDay) < getTime(day.date) &&
  254. getTime(day.date) < getTime(date);
  255. if (isDisabled) {
  256. ({ date } = days[index - 1]);
  257. }
  258. return isDisabled;
  259. });
  260. this.select([startDay, date], true);
  261. }
  262. else if (compareToStart === -1) {
  263. this.select([date, null]);
  264. }
  265. else if (allowSameDay) {
  266. this.select([date, date], true);
  267. }
  268. }
  269. else {
  270. this.select([date, null]);
  271. }
  272. }
  273. else if (type === 'multiple') {
  274. let selectedIndex;
  275. // @ts-ignore
  276. const selected = currentDate.some((dateItem, index) => {
  277. const equal = compareDay(dateItem, date) === 0;
  278. if (equal) {
  279. selectedIndex = index;
  280. }
  281. return equal;
  282. });
  283. if (selected) {
  284. // @ts-ignore
  285. const cancelDate = currentDate.splice(selectedIndex, 1);
  286. this.setData({ currentDate });
  287. this.unselect(cancelDate);
  288. }
  289. else {
  290. // @ts-ignore
  291. this.select([...currentDate, date]);
  292. }
  293. }
  294. else {
  295. this.select(date, true);
  296. }
  297. },
  298. unselect(dateArray) {
  299. const date = dateArray[0];
  300. if (date) {
  301. this.$emit('unselect', copyDates(date));
  302. }
  303. },
  304. select(date, complete) {
  305. if (complete && this.data.type === 'range') {
  306. const valid = this.checkRange(date);
  307. if (!valid) {
  308. // auto selected to max range if showConfirm
  309. if (this.data.showConfirm) {
  310. this.emit([
  311. date[0],
  312. getDayByOffset(date[0], this.data.maxRange - 1),
  313. ]);
  314. }
  315. else {
  316. this.emit(date);
  317. }
  318. return;
  319. }
  320. }
  321. this.emit(date);
  322. if (complete && !this.data.showConfirm) {
  323. this.onConfirm();
  324. }
  325. },
  326. emit(date) {
  327. this.setData({
  328. currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date),
  329. });
  330. this.$emit('select', copyDates(date));
  331. },
  332. checkRange(date) {
  333. const { maxRange, rangePrompt, showRangePrompt } = this.data;
  334. if (maxRange && calcDateNum(date) > maxRange) {
  335. if (showRangePrompt) {
  336. Toast({
  337. context: this,
  338. message: rangePrompt || `选择天数不能超过 ${maxRange} 天`,
  339. });
  340. }
  341. this.$emit('over-range');
  342. return false;
  343. }
  344. return true;
  345. },
  346. onConfirm() {
  347. if (this.data.type === 'range' &&
  348. !this.checkRange(this.data.currentDate)) {
  349. return;
  350. }
  351. wx.nextTick(() => {
  352. // @ts-ignore
  353. this.$emit('confirm', copyDates(this.data.currentDate));
  354. });
  355. },
  356. onClickSubtitle(event) {
  357. this.$emit('click-subtitle', event);
  358. },
  359. },
  360. });
  361. export default global['__wxComponents']['weapp/dist/calendar/index']
  362. </script>
  363. <style platform="mp-weixin">
  364. @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}
  365. </style>