uni-calendar.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <template>
  2. <view class="uni-calendar" @touchstart="handleTouchstart" @touchend="handleTouchend">
  3. <view v-if="!insert && show" class="uni-calendar__mask" :class="{ 'uni-calendar--mask-show': aniMaskShow }" @click="clean"></view>
  4. <view v-if="insert || show" class="uni-calendar__content" :class="{ 'uni-calendar--fixed': !insert, 'uni-calendar--ani-show': aniMaskShow }">
  5. <view v-if="!insert" class="uni-calendar__header uni-calendar--fixed-top">
  6. <view class="uni-calendar__header-btn-box" @click="close">
  7. <text class="uni-calendar__header-text uni-calendar--fixed-width">{{ cancelText }}</text>
  8. </view>
  9. <view class="uni-calendar__header-btn-box" @click="confirm">
  10. <text class="uni-calendar__header-text uni-calendar--fixed-width">{{ okText }}</text>
  11. </view>
  12. </view>
  13. <!-- header -->
  14. <view class="uni-calendar__header">
  15. <view class="uni-calendar__header-btn-box" @click.stop="pre"><view class="uni-calendar__header-btn uni-calendar--left"></view></view>
  16. <picker mode="date" :value="date" fields="month" @change="bindDateChange">
  17. <text class="uni-calendar__header-text">{{ (nowDate.year || '') + ' / ' + (nowDate.month || '') }}</text>
  18. </picker>
  19. <view class="uni-calendar__header-btn-box" @click.stop="next"><view class="uni-calendar__header-btn uni-calendar--right"></view></view>
  20. <text class="uni-calendar__backtoday" @click="backtoday">{{ todayText }}</text>
  21. </view>
  22. <!-- content -->
  23. <view class="uni-calendar__box">
  24. <view v-if="showMonthed" class="uni-calendar__box-bg">
  25. <text class="uni-calendar__box-bg-text">{{ nowDate.month }}</text>
  26. </view>
  27. <view class="uni-calendar__weeks">
  28. <view class="uni-calendar__weeks-day">
  29. <text class="uni-calendar__weeks-day-text">{{ SUNText }}</text>
  30. </view>
  31. <view class="uni-calendar__weeks-day">
  32. <text class="uni-calendar__weeks-day-text">{{ monText }}</text>
  33. </view>
  34. <view class="uni-calendar__weeks-day">
  35. <text class="uni-calendar__weeks-day-text">{{ TUEText }}</text>
  36. </view>
  37. <view class="uni-calendar__weeks-day">
  38. <text class="uni-calendar__weeks-day-text">{{ WEDText }}</text>
  39. </view>
  40. <view class="uni-calendar__weeks-day">
  41. <text class="uni-calendar__weeks-day-text">{{ THUText }}</text>
  42. </view>
  43. <view class="uni-calendar__weeks-day">
  44. <text class="uni-calendar__weeks-day-text">{{ FRIText }}</text>
  45. </view>
  46. <view class="uni-calendar__weeks-day">
  47. <text class="uni-calendar__weeks-day-text">{{ SATText }}</text>
  48. </view>
  49. </view>
  50. <view class="uni-calendar__weeks" v-for="(item, weekIndex) in weeks" :key="weekIndex">
  51. <view v-if="fold" class="uni-calendar__weeks-box">
  52. <view v-if="foldIndex === weekIndex" class="uni-calendar__weeks-item" v-for="(weeks, weeksIndex) in item" :key="weeksIndex">
  53. <calendar-item
  54. class="uni-calendar-item--hook"
  55. :weeks="weeks"
  56. :calendar="calendar"
  57. :selected="selected"
  58. :lunar="lunar"
  59. @change="choiceDate"
  60. ></calendar-item>
  61. </view>
  62. </view>
  63. <view v-else class="uni-calendar__weeks-item" v-for="(weeks, weeksIndex) in item" :key="weeksIndex">
  64. <calendar-item class="uni-calendar-item--hook" :weeks="weeks" :calendar="calendar" :selected="selected" :lunar="lunar" @change="choiceDate"></calendar-item>
  65. </view>
  66. </view>
  67. </view>
  68. <!-- footer -->
  69. <view v-if="foldAble" class="uni-calendar__footer"><u-icon :name="fold ? 'arrow-down' : 'arrow-up'" @tap="foldBtnDidClick" size="20"></u-icon></view>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import Calendar from './util.js';
  75. import calendarItem from './uni-calendar-item.vue';
  76. /**
  77. * Calendar 日历
  78. * @description 日历组件可以查看日期,选择任意范围内的日期,打点操作。常用场景如:酒店日期预订、火车机票选择购买日期、上下班打卡等
  79. * @tutorial https://ext.dcloud.net.cn/plugin?id=56
  80. * @property {String} date 自定义当前时间,默认为今天
  81. * @property {Boolean} lunar 显示农历
  82. * @property {String} startDate 日期选择范围-开始日期
  83. * @property {String} endDate 日期选择范围-结束日期
  84. * @property {Boolean} range 范围选择
  85. * @property {Boolean} insert = [true|false] 插入模式,默认为false
  86. * @value true 弹窗模式
  87. * @value false 插入模式
  88. * @property {Boolean} clearDate = [true|false] 弹窗模式是否清空上次选择内容
  89. * @property {Array} selected 打点,期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
  90. * @property {Boolean} showMonth 是否选择月份为背景
  91. * @event {Function} change 日期改变,`insert :ture` 时生效
  92. * @event {Function} confirm 确认选择`insert :false` 时生效
  93. * @event {Function} monthSwitch 切换月份时触发
  94. * @example <uni-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
  95. */
  96. export default {
  97. components: {
  98. calendarItem
  99. },
  100. emits: ['close', 'confirm', 'change', 'monthSwitch'],
  101. props: {
  102. date: {
  103. type: String,
  104. default: ''
  105. },
  106. selected: {
  107. type: Array,
  108. default() {
  109. return [];
  110. }
  111. },
  112. lunar: {
  113. type: Boolean,
  114. default: false
  115. },
  116. startDate: {
  117. type: String,
  118. default: ''
  119. },
  120. endDate: {
  121. type: String,
  122. default: ''
  123. },
  124. range: {
  125. type: Boolean,
  126. default: false
  127. },
  128. insert: {
  129. type: Boolean,
  130. default: true
  131. },
  132. showMonth: {
  133. type: Boolean,
  134. default: false
  135. },
  136. clearDate: {
  137. type: Boolean,
  138. default: true
  139. },
  140. /// 是否能够折叠
  141. foldAble: {
  142. type: Boolean,
  143. default: false
  144. }
  145. },
  146. data() {
  147. return {
  148. show: false,
  149. weeks: [],
  150. calendar: {},
  151. nowDate: '',
  152. aniMaskShow: false,
  153. fold: true,
  154. showMonthed: false,
  155. foldIndex: -1,
  156. startTime: 0,
  157. startX: 0,
  158. startY: 0
  159. };
  160. },
  161. computed: {
  162. /**
  163. * for i18n
  164. */
  165. okText() {
  166. return this.$i18n.t('uni-calender.ok');
  167. },
  168. cancelText() {
  169. return this.$i18n.t('uni-calender.cancel');
  170. },
  171. todayText() {
  172. return this.$i18n.t('uni-calender.today');
  173. },
  174. monText() {
  175. return this.$i18n.t('uni-calender.MON');
  176. },
  177. TUEText() {
  178. return this.$i18n.t('uni-calender.TUE');
  179. },
  180. WEDText() {
  181. return this.$i18n.t('uni-calender.WED');
  182. },
  183. THUText() {
  184. return this.$i18n.t('uni-calender.THU');
  185. },
  186. FRIText() {
  187. return this.$i18n.t('uni-calender.FRI');
  188. },
  189. SATText() {
  190. return this.$i18n.t('uni-calender.SAT');
  191. },
  192. SUNText() {
  193. return this.$i18n.t('uni-calender.SUN');
  194. }
  195. },
  196. watch: {
  197. date(newVal) {
  198. // this.cale.setDate(newVal)
  199. this.init(newVal);
  200. },
  201. startDate(val) {
  202. this.cale.resetSatrtDate(val);
  203. this.cale.setDate(this.nowDate.fullDate);
  204. this.weeks = this.cale.weeks;
  205. },
  206. endDate(val) {
  207. this.cale.resetEndDate(val);
  208. this.cale.setDate(this.nowDate.fullDate);
  209. this.weeks = this.cale.weeks;
  210. },
  211. selected(newVal) {
  212. this.cale.setSelectInfo(this.nowDate.fullDate, newVal);
  213. this.weeks = this.cale.weeks;
  214. }
  215. },
  216. created() {
  217. // 获取日历方法实例
  218. this.cale = new Calendar({
  219. // date: new Date(),
  220. selected: this.selected,
  221. startDate: this.startDate,
  222. endDate: this.endDate,
  223. range: this.range
  224. });
  225. // 选中某一天
  226. // this.cale.setDate(this.date)
  227. this.init(this.date);
  228. this.showMonthed = this.showMonth;
  229. this.getFoldIndex();
  230. },
  231. methods: {
  232. // 取消穿透
  233. clean() {},
  234. bindDateChange(e) {
  235. const value = e.detail.value + '-1';
  236. console.log(this.cale.getDate(value));
  237. this.init(value);
  238. },
  239. /**
  240. * 初始化日期显示
  241. * @param {Object} date
  242. */
  243. init(date) {
  244. this.cale.setDate(date);
  245. this.weeks = this.cale.weeks;
  246. this.nowDate = this.calendar = this.cale.getInfo(date);
  247. },
  248. /**
  249. * 打开日历弹窗
  250. */
  251. open() {
  252. // 弹窗模式并且清理数据
  253. if (this.clearDate && !this.insert) {
  254. this.cale.cleanMultipleStatus();
  255. // this.cale.setDate(this.date)
  256. this.init(this.date);
  257. }
  258. this.show = true;
  259. this.$nextTick(() => {
  260. setTimeout(() => {
  261. this.aniMaskShow = true;
  262. }, 50);
  263. });
  264. },
  265. /**
  266. * 关闭日历弹窗
  267. */
  268. close() {
  269. this.aniMaskShow = false;
  270. this.$nextTick(() => {
  271. setTimeout(() => {
  272. this.show = false;
  273. this.$emit('close');
  274. }, 300);
  275. });
  276. },
  277. /**
  278. * 确认按钮
  279. */
  280. confirm() {
  281. this.setEmit('confirm');
  282. this.close();
  283. },
  284. /**
  285. * 变化触发
  286. */
  287. change() {
  288. if (!this.insert) return;
  289. this.getFoldIndex();
  290. this.setEmit('change');
  291. },
  292. /// 获取缩进后显示的行号
  293. getFoldIndex() {
  294. var _selectedData = this.nowDate.fullDate;
  295. if (this.calendar.fullDate === undefined) {
  296. _selectedData = this.calendar.fullDate;
  297. }
  298. for (let index in this.weeks) {
  299. let days = this.weeks[index].map(e => e.fullDate);
  300. if (days.indexOf(this.calendar.fullDate) !== -1) {
  301. this.foldIndex = index;
  302. }
  303. }
  304. },
  305. /**
  306. * 选择月份触发
  307. */
  308. monthSwitch() {
  309. let { year, month } = this.nowDate;
  310. this.$emit('monthSwitch', {
  311. year,
  312. month: Number(month)
  313. });
  314. },
  315. /**
  316. * 派发事件
  317. * @param {Object} name
  318. */
  319. setEmit(name) {
  320. let { year, month, date, fullDate, lunar, extraInfo } = this.calendar;
  321. this.$emit(name, {
  322. range: this.cale.multipleStatus,
  323. year,
  324. month,
  325. date,
  326. fulldate: fullDate,
  327. lunar,
  328. extraInfo: extraInfo || {}
  329. });
  330. },
  331. /**
  332. * 选择天触发
  333. * @param {Object} weeks
  334. */
  335. choiceDate(weeks) {
  336. console.log('fuck');
  337. if (weeks.disable) return;
  338. this.calendar = weeks;
  339. // 设置多选
  340. this.cale.setMultiple(this.calendar.fullDate);
  341. this.weeks = this.cale.weeks;
  342. this.change();
  343. },
  344. /**
  345. * 回到今天
  346. */
  347. backtoday() {
  348. console.log(this.cale.getDate(new Date()).fullDate);
  349. let date = this.cale.getDate(new Date()).fullDate;
  350. // this.cale.setDate(date)
  351. this.init(date);
  352. this.change();
  353. },
  354. /**
  355. * 上个月
  356. */
  357. pre() {
  358. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate;
  359. this.setDate(preDate);
  360. this.monthSwitch();
  361. this.fold = false;
  362. },
  363. /**
  364. * 下个月
  365. */
  366. next() {
  367. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate;
  368. this.setDate(nextDate);
  369. this.monthSwitch();
  370. this.fold = false;
  371. },
  372. /**
  373. * 设置日期
  374. * @param {Object} date
  375. */
  376. setDate(date) {
  377. this.cale.setDate(date);
  378. this.weeks = this.cale.weeks;
  379. this.nowDate = this.cale.getInfo(date);
  380. },
  381. /// 展开按钮点击事件
  382. foldBtnDidClick() {
  383. this.fold = !this.fold;
  384. this.showMonthed = !this.fold;
  385. },
  386. handleTouchstart(e) {
  387. //记录用户按下的时间
  388. this.startTime = Date.now();
  389. //记录用户按下的坐标
  390. this.startX = e.changedTouches[0].clientX;
  391. this.startY = e.changedTouches[0].clientY;
  392. },
  393. handleTouchend(e) {
  394. const endTime = Date.now();
  395. const endX = e.changedTouches[0].clientX;
  396. if (Math.abs(endX - this.startX) < 10) return;
  397. if (endX - this.startX < 0) {
  398. this.next();
  399. } else {
  400. this.pre();
  401. }
  402. }
  403. }
  404. };
  405. </script>
  406. <style lang="scss" scoped>
  407. .uni-calendar {
  408. /* #ifndef APP-NVUE */
  409. display: flex;
  410. /* #endif */
  411. flex-direction: column;
  412. }
  413. .uni-calendar__mask {
  414. position: fixed;
  415. bottom: 0;
  416. top: 0;
  417. left: 0;
  418. right: 0;
  419. background-color: $uni-bg-color-mask;
  420. transition-property: opacity;
  421. transition-duration: 0.3s;
  422. opacity: 0;
  423. /* #ifndef APP-NVUE */
  424. z-index: 99;
  425. /* #endif */
  426. }
  427. .uni-calendar--mask-show {
  428. opacity: 1;
  429. }
  430. .uni-calendar--fixed {
  431. position: fixed;
  432. bottom: calc(var(--window-bottom));
  433. left: 0;
  434. right: 0;
  435. transition-property: transform;
  436. transition-duration: 0.3s;
  437. transform: translateY(460px);
  438. /* #ifndef APP-NVUE */
  439. z-index: 99;
  440. /* #endif */
  441. }
  442. .uni-calendar--ani-show {
  443. transform: translateY(0);
  444. }
  445. .uni-calendar__content {
  446. background-color: #fff;
  447. }
  448. .uni-calendar__header {
  449. position: relative;
  450. /* #ifndef APP-NVUE */
  451. display: flex;
  452. /* #endif */
  453. flex-direction: row;
  454. justify-content: center;
  455. align-items: center;
  456. height: 50px;
  457. border-bottom-color: $uni-border-color;
  458. border-bottom-style: solid;
  459. border-bottom-width: 1px;
  460. }
  461. .uni-calendar--fixed-top {
  462. /* #ifndef APP-NVUE */
  463. display: flex;
  464. /* #endif */
  465. flex-direction: row;
  466. justify-content: space-between;
  467. border-top-color: $uni-border-color;
  468. border-top-style: solid;
  469. border-top-width: 1px;
  470. }
  471. .uni-calendar--fixed-width {
  472. width: 50px;
  473. // padding: 0 15px;
  474. }
  475. .uni-calendar__backtoday {
  476. position: absolute;
  477. right: 0;
  478. top: 25rpx;
  479. padding: 0 5px;
  480. padding-left: 10px;
  481. height: 25px;
  482. line-height: 25px;
  483. font-size: 12px;
  484. border-top-left-radius: 25px;
  485. border-bottom-left-radius: 25px;
  486. color: $uni-text-color;
  487. background-color: $uni-bg-color-hover;
  488. }
  489. .uni-calendar__header-text {
  490. text-align: center;
  491. width: 100px;
  492. font-size: $uni-font-size-base;
  493. color: $uni-text-color;
  494. }
  495. .uni-calendar__header-btn-box {
  496. /* #ifndef APP-NVUE */
  497. display: flex;
  498. /* #endif */
  499. flex-direction: row;
  500. align-items: center;
  501. justify-content: center;
  502. width: 50px;
  503. height: 50px;
  504. }
  505. .uni-calendar__header-btn {
  506. width: 10px;
  507. height: 10px;
  508. border-left-color: $uni-text-color-placeholder;
  509. border-left-style: solid;
  510. border-left-width: 2px;
  511. border-top-color: $uni-color-subtitle;
  512. border-top-style: solid;
  513. border-top-width: 2px;
  514. }
  515. .uni-calendar--left {
  516. transform: rotate(-45deg);
  517. }
  518. .uni-calendar--right {
  519. transform: rotate(135deg);
  520. }
  521. .uni-calendar__weeks {
  522. position: relative;
  523. /* #ifndef APP-NVUE */
  524. display: flex;
  525. /* #endif */
  526. flex-direction: row;
  527. }
  528. .uni-calendar__weeks-box {
  529. /* #ifndef APP-NVUE */
  530. display: flex;
  531. /* #endif */
  532. flex-direction: row;
  533. flex: 1;
  534. }
  535. .uni-calendar__weeks-item {
  536. flex: 1;
  537. }
  538. .uni-calendar__weeks-day {
  539. flex: 1;
  540. /* #ifndef APP-NVUE */
  541. display: flex;
  542. /* #endif */
  543. flex-direction: column;
  544. justify-content: center;
  545. align-items: center;
  546. height: 45px;
  547. border-bottom-color: #f5f5f5;
  548. border-bottom-style: solid;
  549. border-bottom-width: 1px;
  550. }
  551. .uni-calendar__weeks-day-text {
  552. font-size: 14px;
  553. }
  554. .uni-calendar__box {
  555. position: relative;
  556. }
  557. .uni-calendar__box-bg {
  558. /* #ifndef APP-NVUE */
  559. display: flex;
  560. /* #endif */
  561. justify-content: center;
  562. align-items: center;
  563. position: absolute;
  564. top: 0;
  565. left: 0;
  566. right: 0;
  567. bottom: 0;
  568. }
  569. .uni-calendar__box-bg-text {
  570. font-size: 200px;
  571. font-weight: bold;
  572. color: $uni-text-color-grey;
  573. opacity: 0.1;
  574. text-align: center;
  575. /* #ifndef APP-NVUE */
  576. line-height: 1;
  577. /* #endif */
  578. }
  579. .uni-calendar__footer {
  580. display: flex;
  581. flex-direction: row;
  582. justify-content: center;
  583. align-items: center;
  584. }
  585. </style>