mpvue-calendar.vue 43 KB

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