uni-calendar.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <template>
  2. <view class="uni-calendar" @touchmove.stop.prevent="clean">
  3. <view v-if="!insert&&show" class="uni-calendar__mask" :class="{'uni-calendar--mask-show':aniMaskShow}"
  4. @click="clean"></view>
  5. <view v-if="insert || show" class="uni-calendar__content"
  6. :class="{'uni-calendar--fixed':!insert,'uni-calendar--ani-show':aniMaskShow}">
  7. <view v-if="!insert" class="uni-calendar__header uni-calendar--fixed-top">
  8. <view class="uni-calendar__header-btn-box" @click="close">
  9. <text class="uni-calendar__header-text uni-calendar--fixed-width">{{$t(`取消`)}}</text>
  10. </view>
  11. <view class="uni-calendar__header-btn-box" @click="confirm">
  12. <text class="uni-calendar__header-text uni-calendar--fixed-width">{{$t(`确定`)}}</text>
  13. </view>
  14. </view>
  15. <view class="uni-calendar__header">
  16. <view class="uni-calendar__header-btn-box" @click="pre">
  17. <view class="uni-calendar__header-btn uni-calendar--left"></view>
  18. </view>
  19. <text
  20. class="uni-calendar__header-text">{{ (nowDate.year||'') +$t(`年`)+( nowDate.month||'') +$t(`月`)}}</text>
  21. <view class="uni-calendar__header-btn-box" @click="next">
  22. <view class="uni-calendar__header-btn uni-calendar--right"></view>
  23. </view>
  24. <text class="uni-calendar__backtoday" @click="backtoday">{{$t(`回到当天`)}}</text>
  25. </view>
  26. <view class="uni-calendar__box">
  27. <view v-if="showMonth" class="uni-calendar__box-bg">
  28. <text class="uni-calendar__box-bg-text">{{nowDate.month}}</text>
  29. </view>
  30. <view class="uni-calendar__weeks">
  31. <view class="uni-calendar__weeks-day">
  32. <text class="uni-calendar__weeks-day-text">{{$t(`天`)}}</text>
  33. </view>
  34. <view class="uni-calendar__weeks-day">
  35. <text class="uni-calendar__weeks-day-text">{{$t(`一`)}}</text>
  36. </view>
  37. <view class="uni-calendar__weeks-day">
  38. <text class="uni-calendar__weeks-day-text">{{$t(`二`)}}</text>
  39. </view>
  40. <view class="uni-calendar__weeks-day">
  41. <text class="uni-calendar__weeks-day-text">{{$t(`三`)}}</text>
  42. </view>
  43. <view class="uni-calendar__weeks-day">
  44. <text class="uni-calendar__weeks-day-text">{{$t(`四`)}}</text>
  45. </view>
  46. <view class="uni-calendar__weeks-day">
  47. <text class="uni-calendar__weeks-day-text">{{$t(`五`)}}</text>
  48. </view>
  49. <view class="uni-calendar__weeks-day">
  50. <text class="uni-calendar__weeks-day-text">{{$t(`六`)}}</text>
  51. </view>
  52. </view>
  53. <view class="uni-calendar__weeks" v-for="(item,weekIndex) in weeks" :key="weekIndex">
  54. <view class="uni-calendar__weeks-item" v-for="(weeks,weeksIndex) in item" :key="weeksIndex">
  55. <uni-calendar-item :weeks="weeks" :calendar="calendar" :selected="selected" :lunar="lunar"
  56. @change="choiceDate"></uni-calendar-item>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. import Calendar from './util.js';
  65. import uniCalendarItem from './uni-calendar-item.vue'
  66. export default {
  67. components: {
  68. uniCalendarItem
  69. },
  70. props: {
  71. /**
  72. * 当前日期
  73. */
  74. date: {
  75. type: String,
  76. default: ''
  77. },
  78. /**
  79. * 打点日期
  80. */
  81. selected: {
  82. type: Array,
  83. default () {
  84. return []
  85. }
  86. },
  87. /**
  88. * 是否开启阴历日期
  89. */
  90. lunar: {
  91. type: Boolean,
  92. default: false
  93. },
  94. /**
  95. * 开始时间
  96. */
  97. startDate: {
  98. type: String,
  99. default: ''
  100. },
  101. /**
  102. * 结束时间
  103. */
  104. endDate: {
  105. type: String,
  106. default: ''
  107. },
  108. /**
  109. * 范围
  110. */
  111. range: {
  112. type: Boolean,
  113. default: false
  114. },
  115. /**
  116. * 插入
  117. */
  118. insert: {
  119. type: Boolean,
  120. default: true
  121. },
  122. /**
  123. * 是否显示月份背景
  124. */
  125. showMonth: {
  126. type: Boolean,
  127. default: true
  128. }
  129. },
  130. data() {
  131. return {
  132. show: false,
  133. weeks: [],
  134. calendar: {},
  135. nowDate: '',
  136. aniMaskShow: false
  137. }
  138. },
  139. watch: {
  140. selected(newVal) {
  141. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  142. this.weeks = this.cale.weeks
  143. }
  144. },
  145. created() {
  146. // 获取日历方法实例
  147. this.cale = new Calendar({
  148. date: this.date,
  149. selected: this.selected,
  150. startDate: this.startDate,
  151. endDate: this.endDate,
  152. range: this.range,
  153. })
  154. this.init(this.cale.date.fullDate)
  155. },
  156. methods: {
  157. // 取消穿透
  158. clean() {},
  159. init(date) {
  160. this.weeks = this.cale.weeks
  161. this.nowDate = this.calendar = this.cale.getInfo(date)
  162. },
  163. open() {
  164. this.show = true
  165. this.$nextTick(() => {
  166. setTimeout(() => {
  167. this.aniMaskShow = true
  168. }, 50)
  169. })
  170. },
  171. close() {
  172. this.aniMaskShow = false
  173. this.$nextTick(() => {
  174. setTimeout(() => {
  175. this.show = false
  176. }, 300)
  177. })
  178. },
  179. confirm() {
  180. this.setEmit('confirm')
  181. this.close()
  182. },
  183. change() {
  184. if (!this.insert) return
  185. this.setEmit('change')
  186. },
  187. monthSwitch() {
  188. let {
  189. year,
  190. month
  191. } = this.nowDate
  192. this.$emit('monthSwitch', {
  193. year,
  194. month: Number(month)
  195. })
  196. },
  197. setEmit(name) {
  198. let {
  199. year,
  200. month,
  201. date,
  202. fullDate,
  203. lunar,
  204. extraInfo
  205. } = this.calendar
  206. this.$emit(name, {
  207. range: this.cale.multipleStatus,
  208. year,
  209. month,
  210. date,
  211. fulldate: fullDate,
  212. lunar,
  213. extraInfo: extraInfo || {}
  214. })
  215. },
  216. choiceDate(weeks) {
  217. if (weeks.disable) return
  218. this.calendar = weeks
  219. // 设置多选
  220. this.cale.setMultiple(this.calendar.fullDate)
  221. this.weeks = this.cale.weeks
  222. this.change()
  223. },
  224. backtoday() {
  225. this.cale.setDate(this.date)
  226. this.weeks = this.cale.weeks
  227. this.nowDate = this.calendar = this.cale.getInfo(this.date)
  228. this.cale.multipleStatus.before = this.nowDate.fullDate
  229. this.cale.multipleStatus.after = this.nowDate.fullDate
  230. this.change()
  231. },
  232. pre() {
  233. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  234. this.setDate(preDate)
  235. this.monthSwitch()
  236. },
  237. next() {
  238. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  239. this.setDate(nextDate)
  240. this.monthSwitch()
  241. },
  242. setDate(date) {
  243. this.cale.setDate(date)
  244. this.weeks = this.cale.weeks
  245. this.nowDate = this.cale.getInfo(date)
  246. }
  247. }
  248. }
  249. </script>
  250. <style lang="scss" scoped>
  251. .uni-calendar {
  252. /* #ifndef APP-NVUE */
  253. display: flex;
  254. /* #endif */
  255. flex-direction: column;
  256. }
  257. .uni-calendar__mask {
  258. position: fixed;
  259. bottom: 0;
  260. top: 0;
  261. left: 0;
  262. right: 0;
  263. background-color: $uni-bg-color-mask;
  264. transition-property: opacity;
  265. transition-duration: 0.3s;
  266. opacity: 0;
  267. /* #ifndef APP-NVUE */
  268. z-index: 99;
  269. /* #endif */
  270. }
  271. .uni-calendar--mask-show {
  272. opacity: 1
  273. }
  274. .uni-calendar--fixed {
  275. position: fixed;
  276. bottom: 0;
  277. left: 0;
  278. right: 0;
  279. transition-property: transform;
  280. transition-duration: 0.3s;
  281. transform: translateY(460px);
  282. /* #ifndef APP-NVUE */
  283. z-index: 99;
  284. /* #endif */
  285. }
  286. .uni-calendar--ani-show {
  287. transform: translateY(0);
  288. }
  289. .uni-calendar__content {
  290. background-color: #fff;
  291. }
  292. .uni-calendar__header {
  293. position: relative;
  294. /* #ifndef APP-NVUE */
  295. display: flex;
  296. /* #endif */
  297. flex-direction: row;
  298. justify-content: center;
  299. align-items: center;
  300. height: 50px;
  301. border-bottom-color: $uni-border-color;
  302. border-bottom-style: solid;
  303. border-bottom-width: 1px;
  304. }
  305. .uni-calendar--fixed-top {
  306. /* #ifndef APP-NVUE */
  307. display: flex;
  308. /* #endif */
  309. flex-direction: row;
  310. justify-content: space-between;
  311. border-top-color: $uni-border-color;
  312. border-top-style: solid;
  313. border-top-width: 1px;
  314. }
  315. .uni-calendar--fixed-width {
  316. width: 50px;
  317. // padding: 0 15px;
  318. }
  319. .uni-calendar__backtoday {
  320. position: absolute;
  321. right: 0;
  322. top: 25rpx;
  323. padding: 0 5px;
  324. padding-left: 10px;
  325. height: 25px;
  326. line-height: 25px;
  327. font-size: 12px;
  328. border-top-left-radius: 25px;
  329. border-bottom-left-radius: 25px;
  330. color: $uni-text-color;
  331. background-color: $uni-bg-color-hover;
  332. }
  333. .uni-calendar__header-text {
  334. text-align: center;
  335. width: 100px;
  336. font-size: $uni-font-size-base;
  337. color: $uni-text-color;
  338. }
  339. .uni-calendar__header-btn-box {
  340. /* #ifndef APP-NVUE */
  341. display: flex;
  342. /* #endif */
  343. flex-direction: row;
  344. align-items: center;
  345. justify-content: center;
  346. width: 50px;
  347. height: 50px;
  348. }
  349. .uni-calendar__header-btn {
  350. width: 10px;
  351. height: 10px;
  352. border-left-color: $uni-text-color-placeholder;
  353. border-left-style: solid;
  354. border-left-width: 2px;
  355. border-top-color: $uni-color-subtitle;
  356. border-top-style: solid;
  357. border-top-width: 2px;
  358. }
  359. .uni-calendar--left {
  360. transform: rotate(-45deg);
  361. }
  362. .uni-calendar--right {
  363. transform: rotate(135deg);
  364. }
  365. .uni-calendar__weeks {
  366. position: relative;
  367. /* #ifndef APP-NVUE */
  368. display: flex;
  369. /* #endif */
  370. flex-direction: row;
  371. }
  372. .uni-calendar__weeks-item {
  373. flex: 1;
  374. }
  375. .uni-calendar__weeks-day {
  376. flex: 1;
  377. /* #ifndef APP-NVUE */
  378. display: flex;
  379. /* #endif */
  380. flex-direction: column;
  381. justify-content: center;
  382. align-items: center;
  383. height: 45px;
  384. border-bottom-color: #F5F5F5;
  385. border-bottom-style: solid;
  386. border-bottom-width: 1px;
  387. }
  388. .uni-calendar__weeks-day-text {
  389. font-size: 14px;
  390. }
  391. .uni-calendar__box {
  392. position: relative;
  393. }
  394. .uni-calendar__box-bg {
  395. /* #ifndef APP-NVUE */
  396. display: flex;
  397. /* #endif */
  398. justify-content: center;
  399. align-items: center;
  400. position: absolute;
  401. top: 0;
  402. left: 0;
  403. right: 0;
  404. bottom: 0;
  405. }
  406. .uni-calendar__box-bg-text {
  407. font-size: 200px;
  408. font-weight: bold;
  409. color: $uni-text-color-grey;
  410. opacity: 0.1;
  411. text-align: center;
  412. /* #ifndef APP-NVUE */
  413. line-height: 1;
  414. /* #endif */
  415. }
  416. </style>