You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1219 lines
38 KiB

3 months ago
  1. <template>
  2. <div class="mpvue-calendar" ref="calendar">
  3. <div class="calendar-tools" v-if="!isMonthRange">
  4. <div class="calendar-prev" @click="prev">
  5. <img :src="arrowLeft" v-if="!!arrowLeft">
  6. <i class="iconfont icon-arrow-left" v-else></i>
  7. </div>
  8. <div class="calendar-next" @click="next">
  9. <img :src="arrowRight" v-if="!!arrowRight">
  10. <i class="iconfont icon-arrow-right" v-else></i>
  11. </div>
  12. <div class="calendar-info" @click.stop="changeYear">
  13. <div class="mc-month">
  14. <div :class="['mc-month-inner', oversliding ? '' : 'month-transition']"
  15. :style="{'top': monthPosition + unit}" v-if="isIos">
  16. <span v-for="(m, i) in monthsLoop" :key="i">{{m}}</span>
  17. </div>
  18. <div class="mc-month-text" v-else>{{monthText}}</div>
  19. </div>
  20. <div class="mc-year">{{year}}</div>
  21. </div>
  22. </div>
  23. <table cellpadding="5">
  24. <div class="mc-head" :class="['mc-head', {'mc-month-range-mode-head': isMonthRange}]">
  25. <div class="mc-head-box">
  26. <div v-for="(week, index) in weeks" :key="index" class="mc-week">{{week}}</div>
  27. </div>
  28. </div>
  29. <div :class="['mc-body', {'mc-range-mode': range, 'week-switch': weekSwitch && !isMonthRange, 'month-range-mode': isMonthRange}]"
  30. v-for="(days, index) in monthRangeDays" :key='index'>
  31. <div class="month-rang-head" v-if="isMonthRange">{{rangeOfMonths[index][2]}}</div>
  32. <tr v-for="(day,k1) in days" :key="k1" :class="{'gregorianStyle': !lunar}">
  33. <td v-for="(child,k2) in day" :key="k2"
  34. :class="[{'selected': child.selected, 'mc-today-element': child.isToday, 'disabled': child.disabled, 'mc-range-select-one': rangeBgHide && child.selected, 'lunarStyle': lunar, 'mc-range-row-first': k2 === 0 && child.selected, 'month-last-date': child.lastDay, 'month-first-date': 1 === child.day, 'mc-range-row-last': k2 === 6 && child.selected, 'mc-last-month': child.lastMonth, 'mc-next-month': child.nextMonth}, child.className, child.rangeClassName]"
  35. @click="select(k1, k2, child, $event, index)" class="mc-day" :style="itemStyle">
  36. <span v-if="showToday.show && child.isToday"
  37. class="mc-today calendar-date">{{showToday.text}}</span>
  38. <span :class="[{'mc-date-red': k2 === (monFirst ? 5 : 0) || k2 === 6}, 'calendar-date']"
  39. v-else>{{child.day}}</span>
  40. <div class="slot-element" v-if="!!child.content">{{child.content}}</div>
  41. <div class="mc-text remark-text" v-if="child.eventName && !clean">{{child.eventName}}</div>
  42. <div class="mc-dot" v-if="child.eventName && clean"></div>
  43. <div class="mc-text"
  44. :class="{'isLunarFestival': child.isAlmanac || child.isLunarFestival,'isGregorianFestival': child.isGregorianFestival,'isTerm': child.isTerm}"
  45. v-if="lunar && (!child.eventName || clean)">{{child.almanac || child.lunar}}</div>
  46. <div class="mc-range-bg" v-if="range && child.selected" />
  47. </td>
  48. </tr>
  49. </div>
  50. </table>
  51. <div class="mpvue-calendar-change" :class="{'show': yearsShow}">
  52. <div class="calendar-years" v-if="!weekSwitch">
  53. <span v-for="y in years" :key="y" @click.stop="selectYear(y)"
  54. :class="{'active': y === year}">{{y}}</span>
  55. </div>
  56. <div :class="['calendar-months', {'calendar-week-switch-months': weekSwitch}]">
  57. <span v-for="(m, i) in months" :key="m" @click.stop="changeMonth(i)"
  58. :class="{'active': i === month}">{{m}}</span>
  59. </div>
  60. </div>
  61. </div>
  62. </template>
  63. <script>
  64. import calendar from './calendarinit.js';
  65. import './icon.css';
  66. const isBrowser = !!window;
  67. const now = new Date();
  68. const todayString = [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('-');
  69. export default {
  70. props: {
  71. multi: {
  72. type: Boolean,
  73. default: false
  74. },
  75. arrowLeft: {
  76. type: String,
  77. default: ''
  78. },
  79. arrowRight: {
  80. type: String,
  81. default: ''
  82. },
  83. clean: {
  84. type: Boolean,
  85. default: false
  86. },
  87. now: {
  88. type: [String, Boolean],
  89. default: true
  90. },
  91. range: {
  92. type: Boolean,
  93. default: false
  94. },
  95. completion: {
  96. type: Boolean,
  97. default: false
  98. },
  99. value: {
  100. type: Array,
  101. default: function() {
  102. return []
  103. }
  104. },
  105. begin: {
  106. type: Array,
  107. default: function() {
  108. return []
  109. }
  110. },
  111. end: {
  112. type: Array,
  113. default: function() {
  114. return []
  115. }
  116. },
  117. zero: {
  118. type: Boolean,
  119. default: false
  120. },
  121. disabled: {
  122. type: Array,
  123. default: function() {
  124. return []
  125. }
  126. },
  127. almanacs: {
  128. type: Object,
  129. default: function() {
  130. return {}
  131. }
  132. },
  133. tileContent: {
  134. type: Array,
  135. default: function() {
  136. return []
  137. }
  138. },
  139. lunar: {
  140. type: Boolean,
  141. default: false
  142. },
  143. monFirst: {
  144. type: Boolean,
  145. default: false
  146. },
  147. weeks: {
  148. type: Array,
  149. default: function() {
  150. return this.monFirst ? ['一', '二', '三', '四', '五', '六', '日'] : ['日', '一', '二', '三', '四', '五', '六']
  151. }
  152. },
  153. months: {
  154. type: Array,
  155. default: function() {
  156. return ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
  157. }
  158. },
  159. events: {
  160. type: Object,
  161. default: function() {
  162. return {}
  163. }
  164. },
  165. weekSwitch: {
  166. type: Boolean,
  167. default: false
  168. },
  169. monthRange: {
  170. type: Array,
  171. default: function() {
  172. return []
  173. }
  174. },
  175. responsive: {
  176. type: Boolean,
  177. default: false
  178. },
  179. rangeMonthFormat: {
  180. type: String,
  181. default: ''
  182. }
  183. },
  184. data() {
  185. return {
  186. years: [],
  187. yearsShow: false,
  188. year: 0,
  189. month: 0,
  190. monthPosition: 0,
  191. day: 0,
  192. days: [],
  193. multiDays: [],
  194. today: [],
  195. handleMultiDay: [],
  196. firstRender: true,
  197. isIos: true,
  198. showToday: {},
  199. monthText: '',
  200. festival: {
  201. lunar: {
  202. "1-1": "春节",
  203. "1-15": "元宵节",
  204. "2-2": "龙头节",
  205. "5-5": "端午节",
  206. "7-7": "七夕节",
  207. "7-15": "中元节",
  208. "8-15": "中秋节",
  209. "9-9": "重阳节",
  210. "10-1": "寒衣节",
  211. "10-15": "下元节",
  212. "12-8": "腊八节",
  213. "12-23": "小年",
  214. },
  215. gregorian: {
  216. "1-1": "元旦",
  217. "2-14": "情人节",
  218. "3-8": "妇女节",
  219. "3-12": "植树节",
  220. "5-1": "劳动节",
  221. "5-4": "青年节",
  222. "6-1": "儿童节",
  223. "7-1": "建党节",
  224. "8-1": "建军节",
  225. "9-10": "教师节",
  226. "10-1": "国庆节",
  227. "12-24": "平安夜",
  228. "12-25": "圣诞节",
  229. },
  230. },
  231. rangeBegin: [],
  232. rangeEnd: [],
  233. multiDaysData: [],
  234. monthsLoop: [],
  235. itemWidth: 50,
  236. unit: isBrowser ? 'px' : 'rpx',
  237. positionH: isBrowser ? -24 : -40,
  238. monthIndex: 0,
  239. oversliding: false,
  240. rangeBgHide: false,
  241. monthRangeDays: [],
  242. rangeOfMonths: [],
  243. monthDays: [],
  244. weekIndex: 0,
  245. startWeekIndex: 0,
  246. positionWeek: true,
  247. isMonthRange: false,
  248. }
  249. },
  250. computed: {
  251. itemStyle() {
  252. return {
  253. width: this.itemWidth + 'px',
  254. height: this.itemWidth + 'px',
  255. fontSize: this.itemWidth / 4 + 'px',
  256. lineHeight: this.lunar ? (this.itemWidth / 1.5 + 'px') : (this.itemWidth + 'px')
  257. }
  258. }
  259. },
  260. watch: {
  261. events() {
  262. if (this.isRendeRangeMode()) return;
  263. this.render(this.year, this.month, '_WATCHRENDER_', 'events');
  264. },
  265. disabled() {
  266. if (this.isRendeRangeMode()) return;
  267. this.render(this.year, this.month, '_WATCHRENDER_', 'disabled');
  268. },
  269. value() {
  270. if (this.isRendeRangeMode('_WATCHRENDERVALUE_')) return;
  271. const value = this.value;
  272. let year = value[0],
  273. month = value[1] - 1;
  274. if (this.multi) {
  275. year = value[value.length - 1][0];
  276. month = value[value.length - 1][1] - 1;
  277. } else if (this.range) {
  278. if (this.isUserSelect) {
  279. year = this.year;
  280. month = this.month;
  281. this.isUserSelect = false;
  282. } else {
  283. year = value[0][0];
  284. month = value[0][1] - 1;
  285. const day = value[0][2];
  286. return this.render(year, month, '_WATCHRENDERVALUE_', [year, month, day]);
  287. }
  288. }
  289. this.render(year, month, '_WATCHRENDERVALUE_');
  290. },
  291. tileContent() {
  292. if (this.isRendeRangeMode()) return;
  293. this.render(this.year, this.month, '_WATCHRENDER_', 'tileContent');
  294. },
  295. almanacs() {
  296. if (this.isRendeRangeMode()) return;
  297. this.render(this.year, this.month, '_WATCHRENDER_', 'almanacs');
  298. },
  299. monthRange() {
  300. if (this.isRendeRangeMode()) return;
  301. this.render(this.year, this.month, '_WATCHRENDER_', 'almanacs');
  302. },
  303. responsive() {
  304. if (this.responsive) this.addResponsiveListener();
  305. }
  306. },
  307. created() {
  308. this.isMonthRange = !!this.monthRange.length;
  309. const loopArray = this.months.concat();
  310. loopArray.unshift(this.months[this.months.length - 1]);
  311. loopArray.push(this.months[0]);
  312. this.monthsLoop = loopArray;
  313. this.monthsLoopCopy = this.monthsLoop.concat();
  314. },
  315. mounted() {
  316. const self = this;
  317. this.resize();
  318. if (!isBrowser) {
  319. wx.getSystemInfo({
  320. success: function(res) {
  321. self.isIos = (res.system.split(' ') || [])[0] === 'iOS';
  322. }
  323. });
  324. } else if (this.responsive) {
  325. this.addResponsiveListener();
  326. }
  327. this.oversliding = true;
  328. this.initRender = true;
  329. this.init();
  330. },
  331. beforeDestroy() {
  332. if (isBrowser) {
  333. window.removeEventListener('resize', this.resize)
  334. }
  335. },
  336. methods: {
  337. init() {
  338. let now = new Date();
  339. this.year = now.getFullYear();
  340. this.month = now.getMonth();
  341. this.day = now.getDate();
  342. this.monthIndex = this.month + 1;
  343. if (this.value.length || this.multi) {
  344. if (this.range) {
  345. this.year = Number(this.value[0][0]);
  346. this.month = this.value[0][1] - 1;
  347. this.day = Number(this.value[0][2]);
  348. const yearEnd = Number(this.value[1][0]);
  349. const monthEnd = this.value[1][1] - 1;
  350. const dayEnd = this.value[1][2];
  351. this.rangeBegin = [this.year, this.month, this.day];
  352. this.rangeEnd = [yearEnd, monthEnd, dayEnd];
  353. } else if (this.multi) {
  354. this.multiDays = this.value;
  355. const handleMultiDay = this.handleMultiDay;
  356. if (this.firstRender) {
  357. this.firstRender = false;
  358. const thatYear = (this.value[0] || [])[0];
  359. const thatMonth = (this.value[0] || [])[1];
  360. if (isFinite(thatYear) && isFinite(thatMonth)) {
  361. this.month = parseInt(thatMonth) - 1;
  362. this.year = parseInt(thatYear);
  363. }
  364. } else if (this.handleMultiDay.length) {
  365. this.month = parseInt(handleMultiDay[handleMultiDay.length - 1][1]) - 1;
  366. this.year = parseInt(handleMultiDay[handleMultiDay.length - 1][0]);
  367. this.handleMultiDay = [];
  368. } else {
  369. this.month = parseInt(this.value[this.value.length - 1][1]) - 1;
  370. this.year = parseInt(this.value[this.value.length - 1][0]);
  371. }
  372. this.day = parseInt((this.value[0] || [])[2]);
  373. } else {
  374. this.year = parseInt(this.value[0]);
  375. this.month = parseInt(this.value[1]) - 1;
  376. this.day = parseInt(this.value[2]);
  377. }
  378. }
  379. this.updateHeadMonth();
  380. if (this.isRendeRangeMode()) return;
  381. this.render(this.year, this.month);
  382. },
  383. renderOption(year, month, i, playload) {
  384. const weekSwitch = this.monthRange.length ? false : this.weekSwitch;
  385. const seletSplit = this.value;
  386. const isMonthModeCurrentMonth = !weekSwitch && !playload;
  387. const disabledFilter = (disabled) => {
  388. return disabled.find(v => {
  389. const dayArr = v.split('-');
  390. return year === Number(dayArr[0]) && month === (dayArr[1] - 1) && i === Number(dayArr[
  391. 2]);
  392. });
  393. };
  394. if (this.range) {
  395. const lastDay = new Date(year, month + 1, 0).getDate() === i ? {
  396. lastDay: true
  397. } : null;
  398. const options = Object.assign({
  399. day: i
  400. },
  401. this.getLunarInfo(year, month + 1, i),
  402. this.getEvents(year, month + 1, i),
  403. lastDay
  404. );
  405. const {
  406. date,
  407. day
  408. } = options;
  409. const copyRangeBegin = this.rangeBegin.concat();
  410. const copyRangeEnd = this.rangeEnd.concat();
  411. copyRangeBegin[1] = copyRangeBegin[1] + 1;
  412. copyRangeEnd[1] = copyRangeEnd[1] + 1;
  413. if (weekSwitch || isMonthModeCurrentMonth) {
  414. (copyRangeEnd.join('-') === date) && (options.rangeClassName = 'mc-range-end');
  415. (copyRangeBegin.join('-') === date) && (options.rangeClassName = 'mc-range-begin');
  416. }
  417. if (year === copyRangeEnd[0] && (month + 1) === copyRangeEnd[1] && day === (copyRangeEnd[2] - 1)) {
  418. options.rangeClassName = options.rangeClassName ? ['mc-range-begin', 'mc-range-second-to-last'] :
  419. 'mc-range-second-to-last';
  420. }
  421. if (this.rangeBegin.length) {
  422. const beginTime = +new Date(this.rangeBegin[0], this.rangeBegin[1], this.rangeBegin[2]);
  423. const endTime = +new Date(this.rangeEnd[0], this.rangeEnd[1], this.rangeEnd[2]);
  424. const stepTime = +new Date(year, month, i);
  425. if (beginTime <= stepTime && endTime >= stepTime) {
  426. options.selected = true;
  427. }
  428. }
  429. if (this.begin.length) {
  430. const beginTime = +new Date(parseInt(this.begin[0]), parseInt(this.begin[1]) - 1, parseInt(this
  431. .begin[2]));
  432. if (beginTime > +new Date(year, month, i)) {
  433. options.disabled = true;
  434. }
  435. }
  436. if (this.end.length) {
  437. let endTime = Number(new Date(parseInt(this.end[0]), parseInt(this.end[1]) - 1, parseInt(this.end[
  438. 2])));
  439. if (endTime < Number(new Date(year, month, i))) {
  440. options.disabled = true;
  441. }
  442. }
  443. if (playload && !weekSwitch) {
  444. options.disabled = true;
  445. } else if (this.disabled.length && disabledFilter(this.disabled)) {
  446. options.disabled = true;
  447. }
  448. const monthFirstDay = year + '-' + (month + 1) + '-' + 1;
  449. const monthLastDay = year + '-' + (month + 1) + '-' + new Date(year, month + 1, 0).getDate();
  450. (monthFirstDay === date && options.selected && !options.rangeClassName) && (options.rangeClassName =
  451. 'mc-range-month-first');
  452. (monthLastDay === date && options.selected && !options.rangeClassName) && (options.rangeClassName =
  453. 'mc-range-month-last');
  454. this.isCurrentMonthToday(options) && (options.isToday = true);
  455. (!weekSwitch && playload) && (options.selected = false);
  456. return options;
  457. } else if (this.multi) {
  458. let options;
  459. if (this.value.find(v => year === v[0] && month === v[1] - 1 && i === v[2])) {
  460. options = Object.assign({
  461. day: i,
  462. selected: true
  463. },
  464. this.getLunarInfo(year, month + 1, i),
  465. this.getEvents(year, month + 1, i)
  466. );
  467. } else {
  468. options = Object.assign({
  469. day: i,
  470. selected: false
  471. },
  472. this.getLunarInfo(year, month + 1, i),
  473. this.getEvents(year, month + 1, i)
  474. );
  475. if (this.begin.length) {
  476. const beginTime = +new Date(parseInt(this.begin[0]), parseInt(this.begin[1]) - 1, parseInt(this
  477. .begin[2]));
  478. if (beginTime > +(new Date(year, month, i))) {
  479. options.disabled = true;
  480. }
  481. }
  482. if (this.end.length) {
  483. const endTime = +new Date(parseInt(this.end[0]), parseInt(this.end[1]) - 1, parseInt(this.end[
  484. 2]));
  485. if (endTime < +(new Date(year, month, i))) {
  486. options.disabled = true;
  487. }
  488. }
  489. if (playload && !weekSwitch) {
  490. options.disabled = true;
  491. } else if (this.disabled.length && disabledFilter(this.disabled)) {
  492. options.disabled = true;
  493. }
  494. }
  495. if (options.selected && this.multiDaysData.length !== this.value.length) {
  496. this.multiDaysData.push(options);
  497. }
  498. this.isCurrentMonthToday(options) && (options.isToday = true);
  499. (!weekSwitch && playload) && (options.selected = false);
  500. return options;
  501. } else {
  502. const options = {};
  503. const monthHuman = month + 1;
  504. if (seletSplit[0] === year && seletSplit[1] === monthHuman && seletSplit[2] === i) {
  505. Object.assign(
  506. options, {
  507. day: i,
  508. selected: true
  509. },
  510. this.getLunarInfo(year, monthHuman, i),
  511. this.getEvents(year, monthHuman, i)
  512. );
  513. } else {
  514. Object.assign(
  515. options, {
  516. day: i,
  517. selected: false
  518. },
  519. this.getLunarInfo(year, monthHuman, i),
  520. this.getEvents(year, monthHuman, i)
  521. );
  522. if (this.begin.length) {
  523. const beginTime = +new Date(parseInt(this.begin[0]), parseInt(this.begin[1]) - 1, parseInt(this
  524. .begin[2]));
  525. if (beginTime > Number(new Date(year, month, i))) {
  526. options.disabled = true;
  527. }
  528. }
  529. if (this.end.length) {
  530. const endTime = +new Date(parseInt(this.end[0]), parseInt(this.end[1]) - 1, parseInt(this.end[
  531. 2]));
  532. if (endTime < +(new Date(year, month, i))) {
  533. options.disabled = true;
  534. }
  535. }
  536. if (playload && !weekSwitch) {
  537. options.disabled = true;
  538. } else if (this.disabled.length && disabledFilter(this.disabled)) {
  539. options.disabled = true;
  540. }
  541. }
  542. this.isCurrentMonthToday(options) && (options.isToday = true);
  543. (!weekSwitch && playload) && (options.selected = false);
  544. return options;
  545. }
  546. },
  547. isCurrentMonthToday(options) {
  548. const isToday = todayString === options.date;
  549. if (!isToday) return false;
  550. return this.weekSwitch ? isToday : (Number(todayString.split('-')[1]) === this.month + 1);
  551. },
  552. watchRender(type) {
  553. const weekSwitch = this.weekSwitch;
  554. const daysDeepCopy = JSON.parse(JSON.stringify(this.monthDays));
  555. if (type === 'events') {
  556. const events = this.events || {};
  557. Object.keys(events).forEach(value => {
  558. daysDeepCopy.some(v => v.some(vv => {
  559. if (vv.date === value) {
  560. vv.eventName = events[value];
  561. return true;
  562. }
  563. }))
  564. });
  565. this.monthDays = daysDeepCopy;
  566. } else if (type === 'disabled') {
  567. const disabled = this.disabled || [];
  568. disabled.forEach(value => {
  569. daysDeepCopy.some(v => v.some(vv => {
  570. if (vv.date === value) {
  571. vv.disabled = true;
  572. return true;
  573. }
  574. }))
  575. });
  576. } else if (type === 'almanacs') {
  577. const almanacs = this.almanacs || {};
  578. Object.keys(almanacs).forEach(value => {
  579. daysDeepCopy.some(v => v.some(vv => {
  580. if (vv.date.slice(5, 20) === value) {
  581. vv.lunar = almanacs[value];
  582. return true;
  583. }
  584. }))
  585. });
  586. } else if (type === 'tileContent') {
  587. const tileContent = this.tileContent || [];
  588. tileContent.forEach(value => {
  589. daysDeepCopy.some(v => v.some(vv => {
  590. if (vv.date === value.date) {
  591. vv.className = value.className;
  592. vv.content = value.content;
  593. return true;
  594. }
  595. }))
  596. });
  597. }
  598. if (weekSwitch) {
  599. this.monthDays = daysDeepCopy;
  600. this.days = [daysDeepCopy[this.weekIndex]];
  601. this.monthRangeDays = [this.days];
  602. } else {
  603. this.days = daysDeepCopy;
  604. this.monthRangeDays = [this.days];
  605. }
  606. },
  607. render(y, m, renderer, payload) {
  608. const weekSwitch = this.weekSwitch;
  609. const isCustomRender = renderer === 'CUSTOMRENDER';
  610. const isWatchRenderValue = renderer === '_WATCHRENDERVALUE_';
  611. this.year = y;
  612. this.month = m;
  613. if (renderer === '_WATCHRENDER_') return this.watchRender(payload);
  614. if (this.range && isWatchRenderValue) {
  615. if (!Array.isArray((this.value || [])[0])) {
  616. this.rangeBegin = [];
  617. this.rangeEnd = [];
  618. } else {
  619. this.rangeBegin = [this.value[0][0], this.value[0][1] - 1, this.value[0][2]];
  620. this.rangeEnd = [this.value[1][0], this.value[1][1] - 1, this.value[1][2]];
  621. }
  622. }
  623. if (isWatchRenderValue && weekSwitch) {
  624. this.positionWeek = true;
  625. }
  626. if (isCustomRender) {
  627. this.year = y;
  628. this.month = m;
  629. this.positionWeek = true;
  630. if (weekSwitch && !payload) {
  631. this.startWeekIndex = 0;
  632. this.weekIndex = 0;
  633. }
  634. this.updateHeadMonth();
  635. }
  636. let firstDayOfMonth = new Date(y, m, 1).getDay();
  637. const lastDateOfMonth = new Date(y, m + 1, 0).getDate();
  638. let lastDayOfLastMonth = new Date(y, m, 0).getDate();
  639. this.year = y;
  640. let i = 1,
  641. line = 0,
  642. temp = [],
  643. nextMonthPushDays = 1;
  644. for (i; i <= lastDateOfMonth; i++) {
  645. let day = new Date(y, m, i).getDay();
  646. let k;
  647. if (day === 0) {
  648. temp[line] = [];
  649. } else if (i === 1) {
  650. temp[line] = [];
  651. k = lastDayOfLastMonth - firstDayOfMonth + 1;
  652. for (let j = 0; j < firstDayOfMonth; j++) { //generate prev month surplus option
  653. temp[line].push(Object.assign(
  654. this.renderOption(this.computedPrevYear(y, m), this.computedPrevMonth(false, m), k,
  655. 'prevMonth'), {
  656. lastMonth: true
  657. }
  658. ));
  659. k++;
  660. }
  661. }
  662. temp[line].push(this.renderOption(y, m, i)); //generate current month option
  663. if (day === 6 && i < lastDateOfMonth) {
  664. line++;
  665. } else if (i === lastDateOfMonth) {
  666. let k = 1;
  667. const lastDateOfMonthLength = this.monFirst ? 7 : 6;
  668. for (let d = day; d < lastDateOfMonthLength; d++) { //generate next month surplus option
  669. temp[line].push(Object.assign(
  670. this.renderOption(this.computedNextYear(y, m), this.computedNextMonth(false, m), k,
  671. 'nextMonth'), {
  672. nextMonth: true
  673. }
  674. ));
  675. k++;
  676. }
  677. nextMonthPushDays = k;
  678. }
  679. }
  680. const completion = this.completion;
  681. if (this.monFirst) {
  682. if (!firstDayOfMonth) {
  683. let lastMonthDay = lastDayOfLastMonth;
  684. const LastMonthItems = [];
  685. for (let i = 1; i <= 7; i++) {
  686. LastMonthItems.unshift(Object.assign(
  687. this.renderOption(this.computedPrevYear(y, m), this.computedPrevMonth(false, m),
  688. lastMonthDay, 'prevMonth'), {
  689. lastMonth: true
  690. }
  691. ));
  692. lastMonthDay--;
  693. }
  694. temp.unshift(LastMonthItems);
  695. }
  696. temp.forEach((item, index) => {
  697. if (!index) {
  698. return item.splice(0, 1);
  699. };
  700. temp[index - 1].length < 7 && temp[index - 1].push(item.splice(0, 1)[0]);
  701. });
  702. if (this.isMonthRange && temp[temp.length - 1][0].nextMonth) {
  703. temp.splice(temp.length - 1, 1); //if the first day of last line is nextMonth, delete this line
  704. }
  705. if (!completion && !weekSwitch) {
  706. const lastIndex = temp.length - 1;
  707. const secondToLastIndex = lastIndex - 1;
  708. const differentMonth = temp[lastIndex][0].date.split('-')[1] !== temp[secondToLastIndex][6].date
  709. .split('-')[1];
  710. differentMonth && temp.splice(lastIndex, 1);
  711. }
  712. }
  713. if (completion && !weekSwitch && temp.length <= 5 && nextMonthPushDays > 0) {
  714. for (let i = temp.length; i <= 5; i++) {
  715. temp[i] = [];
  716. let start = nextMonthPushDays + (i - line - 1) * 7;
  717. for (let d = start; d <= start + 6; d++) {
  718. temp[i].push(Object.assign({
  719. day: d,
  720. disabled: true,
  721. nextMonth: true
  722. },
  723. this.getLunarInfo(this.computedNextYear(), this.computedNextMonth(true), d),
  724. this.getEvents(this.computedNextYear(), this.computedNextMonth(true), d)
  725. ));
  726. }
  727. }
  728. }
  729. if (this.tileContent.length) {
  730. temp.forEach((item, index) => {
  731. item.forEach((v, i) => {
  732. const contents = this.tileContent.find(val => val.date === v.date);
  733. if (contents) {
  734. const {
  735. className,
  736. content
  737. } = contents || {};
  738. v.className = className;
  739. v.content = content;
  740. }
  741. });
  742. });
  743. }
  744. if (weekSwitch) {
  745. const tempLength = temp.length;
  746. const lastLineMonth = temp[tempLength - 1][0].date.split('-')[1]; // last line month
  747. const secondLastMonth = temp[tempLength - 2][0].date.split('-')[1]; // second-to-last line month
  748. lastLineMonth !== secondLastMonth && temp.splice(tempLength - 1, 1);
  749. }
  750. this.monthDays = temp;
  751. if (weekSwitch && !this.isMonthRange) {
  752. if (this.positionWeek) {
  753. let payloadDay = '';
  754. let searchIndex = true;
  755. if (Array.isArray(payload)) { //range
  756. payloadDay = [payload[0], payload[1] + 1, payload[2]].join('-');
  757. } else if (this.multi || isWatchRenderValue) {
  758. if (this.thisTimeSelect) {
  759. payloadDay = this.thisTimeSelect;
  760. } else {
  761. payloadDay = this.multi ? this.value[this.value.length - 1].join('-') : this.value.join(
  762. '-');
  763. }
  764. }
  765. if (payload === 'SETTODAY') {
  766. payloadDay = todayString;
  767. } else if (isCustomRender) {
  768. if (typeof payload === 'string') {
  769. payloadDay = [y, Number(m) + 1, payload].join('-');
  770. searchIndex = true;
  771. } else if (typeof payload === 'number') {
  772. const setIndex = payload > temp.length ? temp.length - 1 : payload;
  773. this.startWeekIndex = setIndex;
  774. this.weekIndex = setIndex;
  775. this.positionWeek = false;
  776. searchIndex = false;
  777. }
  778. }
  779. const positionDay = payloadDay || todayString;
  780. if (searchIndex) {
  781. temp.some((v, i) => {
  782. const isWeekNow = v.find(vv => vv.date === positionDay);
  783. if (isWeekNow) {
  784. this.startWeekIndex = i;
  785. this.weekIndex = i;
  786. return true;
  787. }
  788. });
  789. }
  790. this.positionWeek = false;
  791. }
  792. this.days = [temp[this.startWeekIndex]];
  793. if (this.initRender) {
  794. this.setMonthRangeofWeekSwitch();
  795. this.initRender = false;
  796. }
  797. } else {
  798. this.days = temp;
  799. }
  800. const todayText = '今';
  801. if (typeof this.now === 'boolean' && !this.now) {
  802. this.showToday = {
  803. show: false
  804. };
  805. } else if (typeof this.now === 'string') {
  806. this.showToday = {
  807. show: true,
  808. text: this.now || todayText
  809. };
  810. } else {
  811. this.showToday = {
  812. show: true,
  813. text: todayText
  814. };
  815. }
  816. this.monthRangeDays = [this.days];
  817. isWatchRenderValue && this.updateHeadMonth();
  818. return this.days;
  819. },
  820. rendeRange(renderer) {
  821. const range = [];
  822. const self = this;
  823. const monthRange = this.monthRange;
  824. function formatDateText(fYear, fMonth) {
  825. const reg = /([y]+)(.*?)([M]+)(.*?)$/i;
  826. const rangeMonthFormat = self.rangeMonthFormat || 'yyyy-MM';
  827. reg.exec(rangeMonthFormat);
  828. return String(fYear).substring(4 - RegExp.$1.length) + RegExp.$2 + String(fMonth).substring(2 - RegExp
  829. .$3.length) + RegExp.$4;
  830. }
  831. if (monthRange[0] === monthRange[1]) {
  832. const [y, m] = monthRange[0].split('-');
  833. range.push([Number(y), Number(m), formatDateText(y, m)])
  834. } else {
  835. const monthRangeOfStart = monthRange[0].split('-');
  836. const monthRangeOfEnd = monthRange[1].split('-');
  837. let startYear = +monthRangeOfStart[0];
  838. let startMonth = +monthRangeOfStart[1];
  839. let endYear = +monthRangeOfEnd[0];
  840. let endtMonth = +monthRangeOfEnd[1] > 12 ? 12 : +monthRangeOfEnd[1];
  841. while (startYear < endYear || startMonth <= endtMonth) {
  842. range.push([startYear, startMonth, formatDateText(startYear, startMonth)]);
  843. if (startMonth === 12 && startYear !== endYear) {
  844. startYear++;
  845. startMonth = 0;
  846. }
  847. startMonth++;
  848. }
  849. }
  850. this.rangeOfMonths = range;
  851. const monthsRange = range.map(item => {
  852. const [yearParam, monthParam] = item;
  853. return this.render(yearParam, monthParam - 1, renderer);
  854. });
  855. this.monthRangeDays = monthsRange;
  856. },
  857. isRendeRangeMode(renderer) {
  858. this.isMonthRange = !!this.monthRange.length;
  859. if (this.isMonthRange) {
  860. this.rendeRange(renderer);
  861. return true;
  862. }
  863. },
  864. renderer(y, m, w) {
  865. const renderY = y || this.year;
  866. const renderM = typeof parseInt(m) === 'number' ? (m - 1) : this.month;
  867. this.initRender = true;
  868. this.render(renderY, renderM, 'CUSTOMRENDER', w);
  869. !this.weekSwitch && (this.monthsLoop = this.monthsLoopCopy.concat());
  870. },
  871. computedPrevYear(year, month) {
  872. let value = year;
  873. if ((month - 1) < 0) {
  874. value--;
  875. }
  876. return value;
  877. },
  878. computedPrevMonth(isString, month) {
  879. let value = month;
  880. if ((month - 1) < 0) {
  881. value = 11;
  882. } else {
  883. value--;
  884. }
  885. if (isString) {
  886. return value + 1;
  887. }
  888. return value;
  889. },
  890. computedNextYear(year, month) {
  891. let value = year;
  892. if ((month + 1) > 11) {
  893. value++;
  894. }
  895. return value;
  896. },
  897. computedNextMonth(isString, month) {
  898. let value = month;
  899. if ((month + 1) > 11) {
  900. value = 0;
  901. } else {
  902. value++;
  903. }
  904. if (isString) {
  905. return value + 1;
  906. }
  907. return value;
  908. },
  909. getLunarInfo(y, m, d) {
  910. let lunarInfo = calendar.solar2lunar(y, m, d);
  911. let yearEve = '';
  912. if (lunarInfo.lMonth === 12 && lunarInfo.lDay === calendar.monthDays(lunarInfo.lYear, 12)) {
  913. yearEve = '除夕';
  914. }
  915. let lunarValue = lunarInfo.IDayCn;
  916. let Term = lunarInfo.Term;
  917. let isLunarFestival = false;
  918. let isGregorianFestival = false;
  919. if (this.festival.lunar[lunarInfo.lMonth + "-" + lunarInfo.lDay]) {
  920. lunarValue = this.festival.lunar[lunarInfo.lMonth + "-" + lunarInfo.lDay];
  921. isLunarFestival = true;
  922. } else if (this.festival.gregorian[m + "-" + d]) {
  923. lunarValue = this.festival.gregorian[m + "-" + d];
  924. isGregorianFestival = true;
  925. }
  926. const lunarInfoObj = {
  927. date: `${y}-${m}-${d}`,
  928. lunar: yearEve || Term || lunarValue,
  929. isLunarFestival: isLunarFestival,
  930. isGregorianFestival: isGregorianFestival,
  931. isTerm: !!yearEve || lunarInfo.isTerm
  932. };
  933. if (Object.keys(this.almanacs).length) {
  934. Object.assign(lunarInfoObj, {
  935. almanac: this.almanacs[m + "-" + d] || '',
  936. isAlmanac: !!this.almanacs[m + "-" + d]
  937. });
  938. }
  939. return lunarInfoObj;
  940. },
  941. getEvents(y, m, d) {
  942. if (Object.keys(this.events).length == 0) return false;
  943. let eventName = this.events[y + "-" + m + "-" + d];
  944. let data = {};
  945. if (eventName != undefined) {
  946. data.eventName = eventName;
  947. }
  948. return data;
  949. },
  950. prev(e) {
  951. e && e.stopPropagation();
  952. if (this.isMonthRange) return;
  953. const weekSwitch = this.weekSwitch;
  954. const changeMonth = (changed) => {
  955. if (this.monthIndex === 1) {
  956. this.oversliding = false;
  957. this.month = 11;
  958. this.year = parseInt(this.year) - 1;
  959. this.monthIndex = this.monthIndex - 1;
  960. } else if (this.monthIndex === 0) {
  961. this.oversliding = true;
  962. this.monthIndex = 12;
  963. setTimeout(() => this.prev(e), 50);
  964. return this.updateHeadMonth('custom');
  965. } else if (this.monthIndex === 13) {
  966. this.month = 11;
  967. this.year = parseInt(this.year) - 1;
  968. this.monthIndex = this.monthIndex - 1;
  969. } else {
  970. this.oversliding = false;
  971. this.month = parseInt(this.month) - 1;
  972. this.monthIndex = this.monthIndex - 1;
  973. }
  974. this.updateHeadMonth('custom');
  975. this.render(this.year, this.month);
  976. (typeof changed === 'function') && changed();
  977. const weekIndex = weekSwitch ? this.weekIndex : undefined;
  978. this.$emit('prev', this.year, this.month + 1, weekIndex);
  979. }
  980. if (!this.weekSwitch) return changeMonth();
  981. const changeWeek = () => {
  982. this.weekIndex = this.weekIndex - 1;
  983. this.days = [this.monthDays[this.weekIndex]];
  984. this.monthRangeDays = [this.days];
  985. this.setMonthRangeofWeekSwitch();
  986. this.$emit('prev', this.year, this.month + 1, this.weekIndex);
  987. }
  988. const currentWeek = (this.days[0] || [])[0] || {};
  989. if (currentWeek.lastMonth || currentWeek.day === 1) {
  990. const monthChenged = () => {
  991. const lastMonthLength = this.monthDays.length;
  992. const startWeekIndex = currentWeek.lastMonth ? lastMonthLength - 1 : lastMonthLength;
  993. this.startWeekIndex = startWeekIndex;
  994. this.weekIndex = startWeekIndex;
  995. changeWeek();
  996. }
  997. changeMonth(monthChenged);
  998. } else {
  999. changeWeek();
  1000. }
  1001. },
  1002. next(e) {
  1003. e && e.stopPropagation();
  1004. if (this.isMonthRange) return;
  1005. const weekSwitch = this.weekSwitch;
  1006. const changeMonth = () => {
  1007. if (this.monthIndex === 12) {
  1008. this.oversliding = false;
  1009. this.month = 0;
  1010. this.year = parseInt(this.year) + 1;
  1011. this.monthIndex = this.monthIndex + 1;
  1012. } else if (this.monthIndex === 0 && this.month === 11) {
  1013. this.oversliding = false;
  1014. this.month = 0;
  1015. this.year = parseInt(this.year) + 1;
  1016. this.monthIndex = this.monthIndex + 1;
  1017. } else if (this.monthIndex === 13) {
  1018. this.oversliding = true;
  1019. this.monthIndex = 1;
  1020. setTimeout(() => this.next(e), 50);
  1021. return this.updateHeadMonth('custom');
  1022. } else {
  1023. this.oversliding = false;
  1024. this.month = parseInt(this.month) + 1;
  1025. this.monthIndex = this.monthIndex + 1;
  1026. }
  1027. this.updateHeadMonth('custom');
  1028. this.render(this.year, this.month);
  1029. const weekIndex = weekSwitch ? this.weekIndex : undefined;
  1030. this.$emit('next', this.year, this.month + 1, weekIndex);
  1031. }
  1032. if (!this.weekSwitch) return changeMonth();
  1033. const changeWeek = () => {
  1034. this.weekIndex = this.weekIndex + 1;
  1035. this.days = [this.monthDays[this.weekIndex]];
  1036. this.monthRangeDays = [this.days];
  1037. this.setMonthRangeofWeekSwitch();
  1038. this.$emit('next', this.year, this.month + 1, this.weekIndex);
  1039. }
  1040. const currentWeek = (this.days[0] || [])[6] || {};
  1041. if (currentWeek.nextMonth || currentWeek.day === (new Date(this.year, this.month + 1, 0).getDate())) {
  1042. const startWeekIndex = currentWeek.nextMonth ? 1 : 0;
  1043. this.startWeekIndex = startWeekIndex;
  1044. this.weekIndex = startWeekIndex;
  1045. changeMonth();
  1046. } else {
  1047. changeWeek();
  1048. }
  1049. },
  1050. select(k1, k2, data, e, monthIndex) {
  1051. e && e.stopPropagation();
  1052. const weekSwitch = this.weekSwitch;
  1053. if (data.lastMonth && !weekSwitch) {
  1054. return this.prev(e);
  1055. } else if (data.nextMonth && !weekSwitch) {
  1056. return this.next(e);
  1057. }
  1058. if (data.disabled) return;
  1059. (data || {}).event = (this.events || {})[data.date] || '';
  1060. const {
  1061. selected,
  1062. day,
  1063. date
  1064. } = data;
  1065. const selectedDates = date.split('-');
  1066. const selectYear = Number(selectedDates[0]);
  1067. const selectMonth = selectedDates[1] - 1;
  1068. const selectMonthHuman = Number(selectedDates[1]);
  1069. const selectDay = Number(selectedDates[2]);;
  1070. if (this.range) {
  1071. this.isUserSelect = true;
  1072. if (this.rangeBegin.length === 0 || this.rangeEndTemp !== 0) {
  1073. this.rangeBegin = [selectYear, selectMonth, selectDay];
  1074. this.rangeBeginTemp = this.rangeBegin;
  1075. this.rangeEnd = [selectYear, selectMonth, selectDay];
  1076. this.thisTimeSelect = this.rangeEnd;
  1077. this.rangeEndTemp = 0;
  1078. } else {
  1079. this.rangeEnd = [selectYear, selectMonth, selectDay];
  1080. this.thisTimeSelect = [selectYear, selectMonth, selectDay];
  1081. if (this.rangeBegin.join('-') === this.rangeEnd.join('-')) {
  1082. return this.rangeEndTemp = 0;
  1083. }
  1084. this.rangeEndTemp = 1;
  1085. if (+new Date(this.rangeEnd[0], this.rangeEnd[1], this.rangeEnd[2]) < +new Date(this.rangeBegin[0],
  1086. this.rangeBegin[1], this.rangeBegin[2])) {
  1087. this.rangeBegin = this.rangeEnd;
  1088. this.rangeEnd = this.rangeBeginTemp;
  1089. }
  1090. const rangeDate = (date) => {
  1091. return date.map((v, k) => {
  1092. const value = k === 1 ? v + 1 : v;
  1093. return this.zero ? this.zeroPad(value) : value;
  1094. });
  1095. }
  1096. const begin = rangeDate(this.rangeBegin);
  1097. const end = rangeDate(this.rangeEnd);
  1098. this.value.splice(0, 1, begin)
  1099. this.value.splice(1, 1, end)
  1100. this.$emit('select', begin, end);
  1101. }
  1102. this.rangeBgHide = !this.rangeEndTemp || (this.rangeBegin.join('-') === this.rangeEnd.join('-'));
  1103. this.positionWeek = true;
  1104. if (this.isMonthRange) {
  1105. this.rendeRange();
  1106. } else {
  1107. this.render(this.year, this.month, undefined, this.thisTimeSelect);
  1108. }
  1109. } else if (this.multi) {
  1110. const filterDayIndex = this.value.findIndex(v => v.join('-') === date);
  1111. if (~filterDayIndex) {
  1112. this.handleMultiDay = this.value.splice(filterDayIndex, 1);
  1113. } else {
  1114. this.value.push([Number(Number(selectedDates[0])), Number(selectedDates[1]), day]);
  1115. }
  1116. this.days[k1][k2].selected = !selected;
  1117. if (this.monthDays[k1][k2].selected) {
  1118. this.multiDaysData.push(data);
  1119. } else {
  1120. this.multiDaysData = this.multiDaysData.filter(item => item.date !== date);
  1121. }
  1122. this.thisTimeSelect = date;
  1123. this.$emit('select', this.value, this.multiDaysData);
  1124. } else {
  1125. const currentSelected = this.value.join('-');
  1126. this.monthRangeDays.some(value => value.some(v => !!v.find(vv => {
  1127. if (vv.date === currentSelected) {
  1128. vv.selected = false;
  1129. return true;
  1130. }
  1131. })));
  1132. this.monthRangeDays[monthIndex][k1][k2].selected = true;
  1133. this.day = day;
  1134. const selectDate = [selectYear, selectMonthHuman, selectDay];
  1135. this.value[0] = selectYear;
  1136. this.value[1] = selectMonthHuman;
  1137. this.value[2] = selectDay;
  1138. this.today = [k1, k2];
  1139. this.$emit('select', selectDate, data);
  1140. }
  1141. },
  1142. changeYear() {
  1143. if (this.yearsShow) {
  1144. this.yearsShow = false;
  1145. return false;
  1146. }
  1147. this.yearsShow = true;
  1148. this.years = [];
  1149. for (let i = this.year - 5; i < this.year + 7; i++) {
  1150. this.years.push(i);
  1151. }
  1152. },
  1153. changeMonth(value) {
  1154. this.oversliding && (this.oversliding = false);
  1155. this.yearsShow = false;
  1156. this.month = value;
  1157. this.render(this.year, this.month, 'CUSTOMRENDER', 0);
  1158. this.updateHeadMonth();
  1159. this.weekSwitch && this.setMonthRangeofWeekSwitch();
  1160. this.$emit('selectMonth', this.month + 1, this.year);
  1161. },
  1162. selectYear(value) {
  1163. this.yearsShow = false;
  1164. this.year = value;
  1165. this.render(this.year, this.month);
  1166. this.$emit('selectYear', value);
  1167. },
  1168. setToday() {
  1169. const now = new Date();
  1170. this.year = now.getFullYear();
  1171. this.month = now.getMonth();
  1172. this.day = now.getDate();
  1173. this.positionWeek = true;
  1174. this.render(this.year, this.month, undefined, 'SETTODAY');
  1175. this.updateHeadMonth();
  1176. },
  1177. setMonthRangeofWeekSwitch() {
  1178. this.monthsLoop = this.monthsLoopCopy.concat();
  1179. this.days[0].reduce((prev, current) => {
  1180. if (!prev) return;
  1181. const prveDate = ((prev || {}).date || '').split('-');
  1182. const prevYear = prveDate[0];
  1183. const prevMonth = prveDate[1];
  1184. const currentMonth = ((current || {}).date || '').split('-')[1];
  1185. if (prevMonth === currentMonth) {
  1186. return current;
  1187. } else {
  1188. const prevMonthText = this.months[prevMonth - 1];
  1189. const currentMonthText = this.months[currentMonth - 1];
  1190. this.monthsLoop[this.monthIndex] = prevMonthText + '~' + currentMonthText;
  1191. }
  1192. });
  1193. },
  1194. dateInfo(y, m, d) {
  1195. return calendar.solar2lunar(y, m, d);
  1196. },
  1197. zeroPad(n) {
  1198. return String(n < 10 ? '0' + n : n)
  1199. },
  1200. updateHeadMonth(type) {
  1201. if (!type) this.monthIndex = this.month + 1;
  1202. this.monthPosition = this.monthIndex * this.positionH;
  1203. this.monthText = this.months[this.month];
  1204. },
  1205. addResponsiveListener() {
  1206. window.addEventListener('resize', this.resize);
  1207. },
  1208. resize() {
  1209. const calendar = this.$refs.calendar;
  1210. this.itemWidth = (calendar.clientWidth / 7 - 4).toFixed(5);
  1211. }
  1212. }
  1213. }
  1214. </script>