uni-calendar.vue 9.3 KB

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