uni-calendar.vue 9.3 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}" @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">取消</text>
  8. </view>
  9. <view class="uni-calendar__header-btn-box" @click="confirm">
  10. <text class="uni-calendar__header-text uni-calendar--fixed-width">确定</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||'') +'年'+( nowDate.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">{{returnName}}</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">日</text>
  30. </view>
  31. <view class="uni-calendar__weeks-day">
  32. <text class="uni-calendar__weeks-day-text">一</text>
  33. </view>
  34. <view class="uni-calendar__weeks-day">
  35. <text class="uni-calendar__weeks-day-text">二</text>
  36. </view>
  37. <view class="uni-calendar__weeks-day">
  38. <text class="uni-calendar__weeks-day-text">三</text>
  39. </view>
  40. <view class="uni-calendar__weeks-day">
  41. <text class="uni-calendar__weeks-day-text">四</text>
  42. </view>
  43. <view class="uni-calendar__weeks-day">
  44. <text class="uni-calendar__weeks-day-text">五</text>
  45. </view>
  46. <view class="uni-calendar__weeks-day">
  47. <text class="uni-calendar__weeks-day-text">六</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. * 回到今天按钮名称
  127. */
  128. returnName: {
  129. type: String,
  130. default: '回到今天'
  131. },
  132. },
  133. data() {
  134. return {
  135. show: false,
  136. weeks: [],
  137. calendar: {},
  138. nowDate: '',
  139. aniMaskShow: false
  140. }
  141. },
  142. watch: {
  143. selected(newVal) {
  144. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  145. this.weeks = this.cale.weeks
  146. }
  147. },
  148. created() {
  149. // 获取日历方法实例
  150. this.cale = new Calendar({
  151. date: this.date,
  152. selected: this.selected,
  153. startDate: this.startDate,
  154. endDate: this.endDate,
  155. range: this.range,
  156. })
  157. this.init(this.cale.date.fullDate)
  158. },
  159. methods: {
  160. // 取消穿透
  161. clean() {},
  162. init(date) {
  163. this.weeks = this.cale.weeks
  164. this.nowDate = this.calendar = this.cale.getInfo(date)
  165. },
  166. open() {
  167. this.show = true
  168. this.$nextTick(() => {
  169. setTimeout(()=>{
  170. this.aniMaskShow = true
  171. },50)
  172. })
  173. },
  174. close() {
  175. this.aniMaskShow = false
  176. this.$nextTick(() => {
  177. setTimeout(() => {
  178. this.show = false
  179. }, 300)
  180. })
  181. },
  182. confirm() {
  183. this.setEmit('confirm')
  184. this.close()
  185. },
  186. change() {
  187. if (!this.insert) return
  188. this.setEmit('change')
  189. },
  190. monthSwitch() {
  191. let {
  192. year,
  193. month
  194. } = this.nowDate
  195. this.$emit('monthSwitch', {
  196. year,
  197. month: Number(month)
  198. })
  199. },
  200. setEmit(name) {
  201. let {
  202. year,
  203. month,
  204. date,
  205. fullDate,
  206. lunar,
  207. extraInfo
  208. } = this.calendar
  209. this.$emit(name, {
  210. range: this.cale.multipleStatus,
  211. year,
  212. month,
  213. date,
  214. fulldate: fullDate,
  215. lunar,
  216. extraInfo: extraInfo || {}
  217. })
  218. },
  219. choiceDate(weeks) {
  220. if (weeks.disable) return
  221. this.calendar = weeks
  222. // 设置多选
  223. this.cale.setMultiple(this.calendar.fullDate)
  224. this.weeks = this.cale.weeks
  225. this.change()
  226. },
  227. backtoday() {
  228. this.cale.setDate(this.date)
  229. this.weeks = this.cale.weeks
  230. this.nowDate = this.calendar = this.cale.getInfo(this.date)
  231. this.change()
  232. },
  233. pre() {
  234. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  235. this.setDate(preDate)
  236. this.monthSwitch()
  237. },
  238. next() {
  239. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  240. this.setDate(nextDate)
  241. this.monthSwitch()
  242. },
  243. setDate(date) {
  244. this.cale.setDate(date)
  245. this.weeks = this.cale.weeks
  246. this.nowDate = this.cale.getInfo(date)
  247. }
  248. }
  249. }
  250. </script>
  251. <style lang="scss" scoped>
  252. .uni-calendar {
  253. /* #ifndef APP-NVUE */
  254. display: flex;
  255. /* #endif */
  256. flex-direction: column;
  257. }
  258. .uni-calendar__mask {
  259. position: fixed;
  260. bottom: 0;
  261. top: 0;
  262. left: 0;
  263. right: 0;
  264. background-color: $uni-bg-color-mask;
  265. transition-property: opacity;
  266. transition-duration: 0.3s;
  267. opacity: 0;
  268. /* #ifndef APP-NVUE */
  269. z-index: 99;
  270. /* #endif */
  271. }
  272. .uni-calendar--mask-show {
  273. opacity: 1
  274. }
  275. .uni-calendar--fixed {
  276. position: fixed;
  277. bottom: 0;
  278. left: 0;
  279. right: 0;
  280. transition-property: transform;
  281. transition-duration: 0.3s;
  282. transform: translateY(460px);
  283. /* #ifndef APP-NVUE */
  284. z-index: 99;
  285. /* #endif */
  286. }
  287. .uni-calendar--ani-show {
  288. transform: translateY(0);
  289. }
  290. .uni-calendar__content {
  291. background-color: #fff;
  292. }
  293. .uni-calendar__header {
  294. position: relative;
  295. /* #ifndef APP-NVUE */
  296. display: flex;
  297. /* #endif */
  298. flex-direction: row;
  299. justify-content: center;
  300. align-items: center;
  301. height: 50px;
  302. border-bottom-color: $uni-border-color;
  303. border-bottom-style: solid;
  304. border-bottom-width: 1px;
  305. }
  306. .uni-calendar--fixed-top {
  307. /* #ifndef APP-NVUE */
  308. display: flex;
  309. /* #endif */
  310. flex-direction: row;
  311. justify-content: space-between;
  312. border-top-color: $uni-border-color;
  313. border-top-style: solid;
  314. border-top-width: 1px;
  315. }
  316. .uni-calendar--fixed-width {
  317. width: 50px;
  318. // padding: 0 15px;
  319. }
  320. .uni-calendar__backtoday {
  321. position: absolute;
  322. right: 0;
  323. top: 25rpx;
  324. padding: 0 5px;
  325. padding-left: 10px;
  326. height: 25px;
  327. line-height: 25px;
  328. font-size: 12px;
  329. border-top-left-radius: 25px;
  330. border-bottom-left-radius: 25px;
  331. color: $uni-text-color;
  332. background-color: $uni-bg-color-hover;
  333. }
  334. .uni-calendar__header-text {
  335. text-align: center;
  336. width: 100px;
  337. font-size: $uni-font-size-base;
  338. color: $uni-text-color;
  339. }
  340. .uni-calendar__header-btn-box {
  341. /* #ifndef APP-NVUE */
  342. display: flex;
  343. /* #endif */
  344. flex-direction: row;
  345. align-items: center;
  346. justify-content: center;
  347. width: 50px;
  348. height: 50px;
  349. }
  350. .uni-calendar__header-btn {
  351. width: 10px;
  352. height: 10px;
  353. border-left-color: $uni-text-color-placeholder;
  354. border-left-style: solid;
  355. border-left-width: 2px;
  356. border-top-color: $uni-color-subtitle;
  357. border-top-style: solid;
  358. border-top-width: 2px;
  359. }
  360. .uni-calendar--left {
  361. transform: rotate(-45deg);
  362. }
  363. .uni-calendar--right {
  364. transform: rotate(135deg);
  365. }
  366. .uni-calendar__weeks {
  367. position: relative;
  368. /* #ifndef APP-NVUE */
  369. display: flex;
  370. /* #endif */
  371. flex-direction: row;
  372. }
  373. .uni-calendar__weeks-item {
  374. flex: 1;
  375. }
  376. .uni-calendar__weeks-day {
  377. flex: 1;
  378. /* #ifndef APP-NVUE */
  379. display: flex;
  380. /* #endif */
  381. flex-direction: column;
  382. justify-content: center;
  383. align-items: center;
  384. height: 45px;
  385. border-bottom-color: #F5F5F5;
  386. border-bottom-style: solid;
  387. border-bottom-width: 1px;
  388. }
  389. .uni-calendar__weeks-day-text {
  390. font-size: 14px;
  391. }
  392. .uni-calendar__box {
  393. position: relative;
  394. }
  395. .uni-calendar__box-bg {
  396. /* #ifndef APP-NVUE */
  397. display: flex;
  398. /* #endif */
  399. justify-content: center;
  400. align-items: center;
  401. position: absolute;
  402. top: 0;
  403. left: 0;
  404. right: 0;
  405. bottom: 0;
  406. }
  407. .uni-calendar__box-bg-text {
  408. font-size: 200px;
  409. font-weight: bold;
  410. color: $uni-text-color-grey;
  411. opacity: 0.1;
  412. text-align: center;
  413. /* #ifndef APP-NVUE */
  414. line-height: 1;
  415. /* #endif */
  416. }
  417. </style>