index.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. import {
  2. Weeks,
  3. Today,
  4. CorrectDate,
  5. MonthOnly,
  6. MonthDaysDetailFull,
  7. initCurrInfo,
  8. LunarDetail,
  9. WeekFirstDay,
  10. YearWeekOrder,
  11. Months,
  12. EnOrders,
  13. DayDetail,
  14. ConsoleStyle
  15. } from './config'
  16. const CalendarPositions = ['relative', 'absolute', 'fixed']
  17. const CalendarHeight = 820 //日历高度,单位rpx
  18. const Version = '1.0.1'
  19. Component({
  20. behaviors: ['wx://component-export'],
  21. options: {
  22. pureDataPattern: /^_/,
  23. // multipleSlots: true
  24. },
  25. properties: {
  26. /**
  27. * 是否选择日期时震动
  28. */
  29. _vibrate: {
  30. type: Boolean,
  31. value: true
  32. },
  33. /**
  34. * 初始化视图 month, week
  35. */
  36. view: {
  37. type: String,
  38. value: 'month'
  39. },
  40. /**
  41. * 定位
  42. */
  43. _position: {
  44. type: String,
  45. value: 'relative'
  46. },
  47. /**
  48. * 绝对定位有用
  49. */
  50. _top: {
  51. type: String,
  52. optionalTypes: [Number],
  53. value: '0rpx'
  54. },
  55. /**
  56. * { year: 2021, month: 4, day: 1, type: 'holiday', mark: '愚人节', color: '#2a97ff', bgColor: '#cce6ff' }
  57. * type: 角标corner,节假日holiday,日程schedule
  58. */
  59. _markers: {
  60. type: Array,
  61. value: [
  62. // { year: 2021, month: 4, day: 1, type: 'holiday', mark: '愚人节', color: '#2a97ff', bgColor: '#cce6ff' },
  63. // { year: 2021, month: 4, day: 4, type: 'holiday', mark: '清明', color: '#2a97ff', bgColor: '#cce6ff' },
  64. // { year: 2021, month: 4, day: 3, type: 'corner', mark: '休', color: '#61b057' },
  65. // { year: 2021, month: 4, day: 4, type: 'corner', mark: '休', color: '#61b057' },
  66. // { year: 2021, month: 4, day: 5, type: 'corner', mark: '休', color: '#61b057' },
  67. // { year: 2021, month: 4, day: 6, type: 'schedule', mark: '测试一下', color: '#2a97ff', bgColor: '#cce6ff' },
  68. // { year: 2021, month: 4, day: 6, type: 'schedule', mark: '测试一下哈哈哈', color: '#2a97ff', bgColor: '#cce6ff' },
  69. // { year: 2021, month: 4, day: 6, type: 'schedule', mark: '测试一下哈哈哈', color: '#2a97ff', bgColor: '#cce6ff' }
  70. ]
  71. },
  72. _markerKey: {
  73. type: String,
  74. value: 'id'
  75. },
  76. darkmode: {
  77. type: Boolean,
  78. value: false
  79. },
  80. _date: {
  81. type: Number,
  82. optionalTypes: [String],
  83. value: (new Date).getTime()
  84. },
  85. checkedShow: {
  86. type: Boolean,
  87. value: true
  88. }
  89. },
  90. data: {
  91. Weeks,
  92. style: 0,
  93. currTab: 2,
  94. months: [],
  95. titleInfo: '',
  96. minHeight: 0,
  97. maxHeight: 0,
  98. panelHeight: 0,
  99. calendarHeight: 0,
  100. currView: 1,
  101. tdOpShow: false,
  102. monthchange: false,
  103. barAni: true,
  104. weektabchange: -1,
  105. viewchange: '',
  106. solidDay: true,
  107. loading: true,
  108. yearMs: [],
  109. currYmTab: 2,
  110. yearPanelShow: false,
  111. needInitTrans: false,
  112. _currWeekIdx: 0,
  113. _markerdays: {},
  114. _selDay: null,
  115. _selWeek: 0,
  116. _rects: [],
  117. _today: {}
  118. },
  119. attached() {
  120. this._rectsLoading = true
  121. this._year_panel_show = false
  122. this._curr_view = this.data.view == 'week' ? 2 : 1
  123. const current = this.data.currTab
  124. this.initialize(current, _date => {
  125. this.getRects().then(() => {
  126. if (this._curr_view == 2) {
  127. this.setWeeks(_date, current).then(() => {
  128. this.getDayCurr(current, _date.month)
  129. this.bindLoad(true)
  130. })
  131. } else {
  132. this.setMonths(_date, current).then(() => {
  133. this.getDayCurr(current, _date.month)
  134. this.bindLoad()
  135. })
  136. }
  137. })
  138. })
  139. getApp().globalData.Language.getLanguagePackage(this)
  140. this.setData({
  141. languagecode: wx.getStorageSync('languagecode')
  142. })
  143. console.log("language",this.data.language)
  144. },
  145. methods: {
  146. initialize(current, callback) {
  147. const _today = Today()
  148. const system = wx.getSystemInfoSync()
  149. const maxRate = this.judgeScreen(system) ? 0.8 : 0.9
  150. const _clientWidth = system.windowWidth
  151. const _otherHeight = Math.floor(200 * _clientWidth / 750)
  152. const calendarHeight = Math.floor(CalendarHeight * _clientWidth / 750)
  153. const panelHeight = calendarHeight - _otherHeight
  154. const maxHeight = Math.floor(system.windowHeight * maxRate)
  155. const minHeight = panelHeight / 5 + _otherHeight
  156. let _date = this.getCorrectDate(this.data._date)
  157. _date = _date ? DayDetail(_date.year, _date.month, _date.day) : _today
  158. this.setData({
  159. _today,
  160. style: this.initStyle(),
  161. maxHeight,
  162. minHeight,
  163. calendarHeight,
  164. panelHeight,
  165. currView: this._curr_view,
  166. _selDay: _date,
  167. _selWeek: _date.week,
  168. titleInfo: this.setTitleInfo(_date, _today),
  169. yearMs: this.setYearMs(_today.year, current, _today),
  170. _markerdays: this.initMarkDays()
  171. }, () => {
  172. typeof callback === 'function' && callback.call(this, _date)
  173. })
  174. },
  175. judgeScreen(system) {
  176. const rate = system.windowHeight / system.windowWidth
  177. const limit = system.windowHeight == system.screenHeight ? 1.8 : 1.65
  178. return rate > limit
  179. },
  180. initStyle() {
  181. const position = CalendarPositions.includes(this.data._position) ? this.data._position : 'relative'
  182. const top = typeof this.data._top === 'number' ? this.data._top + 'px' : this.data._top
  183. let style = { position }
  184. if (position != 'relative') style.top = top
  185. return Object.keys(style).map(attr => `${ attr }:${ style[attr] };`).join('')
  186. },
  187. trigger(event, detail = {}, view = '', eventOptions = {}) {
  188. let _o = view ? { view } : {}
  189. detail = Object.assign({}, detail, _o)
  190. this.triggerEvent(event, detail, eventOptions)
  191. },
  192. bindLoad(needInitTrans = false) {
  193. let setData = { loading: false }
  194. if (needInitTrans) setData.needInitTrans = true
  195. const view = this.data.currView == 2 ? 'week' : 'month'
  196. this.setData(setData, () => {
  197. this.console('欢迎到%chttps:\/\/github.com\/lspriv\/wx-calendar\/issues%c提出建议或Bug或✭', 'info', 'font-weight:bold;margin: 0 2px;', 'color: #8cc5ff')
  198. this.trigger('load', { date: this.data._selDay }, view)
  199. this.triggerChange(view)
  200. })
  201. },
  202. triggerChange(type) {
  203. const detail = this.getTriggerDetail()
  204. detail.markerCommit = markers => this.handleDynamicMarkers(markers)
  205. this.trigger('rangechange', detail, type)
  206. },
  207. triggerSel(d) {
  208. this.trigger('datechange', { date: d })
  209. },
  210. triggerView(v) {
  211. this.trigger('viewchange', { view: v == 2 ? 'week' : 'month' })
  212. },
  213. getTriggerDetail() {
  214. const currTab = this.data.currTab
  215. const _first = this.data.months[(currTab + 3) % 5]
  216. const _last = this.data.months[(currTab + 2) % 5]
  217. const _curr = this.data.months[currTab]
  218. const _detail = this._curr_view == 2 ? this.getTriggerWeekDetail(_curr, _first, _last) : this.getTriggerMonthDetail(_curr, _first, _last)
  219. return Object.assign({}, _detail, { curr: this.data._selDay })
  220. },
  221. getTriggerWeekDetail(_curr, _first, _last) {
  222. const _l = CorrectDate(_last.wf.year, _last.wf.month, _last.wf.day + 6)
  223. const _c = CorrectDate(_curr.wf.year, _curr.wf.month, _curr.wf.day + 6)
  224. const range = [
  225. { year: _first.wf.year, month: _first.wf.month, day: _first.wf.day },
  226. { year: _l.year, month: _l.month, day: _l.day }
  227. ]
  228. const visual = [
  229. { year: _curr.wf.year, month: _curr.wf.month, day: _curr.wf.day },
  230. { year: _c.year, month: _c.month, day: _c.day }
  231. ]
  232. return { range, visual }
  233. },
  234. getTriggerMonthDetail(_curr, _first, _last) {
  235. const _f = _first.idays[0]
  236. const _l = _last.idays[_last.idays.length - 1]
  237. const range = [
  238. { year: _f.year, month: _f.month, day: _f.day },
  239. { year: _l.year, month: _l.month, day: _l.day }
  240. ]
  241. const _cf = _curr.idays[0]
  242. const _cl = _curr.idays[_curr.idays.length - 1]
  243. const visual = [
  244. { year: _cf.year, month: _cf.month, day: _cf.day },
  245. { year: _cl.year, month: _cl.month, day: _cl.day }
  246. ]
  247. return { range, visual }
  248. },
  249. initMarkDays(_markdays) {
  250. _markdays = _markdays ? _markdays : this.data._markers
  251. const _marks = (_ => {
  252. for (let i = 0; i < _markdays.length; i++) {
  253. _ = this.setMarkerItem(_markdays[i], _)
  254. }
  255. return _
  256. })(new Object)
  257. return _marks
  258. },
  259. initMarker(year, month, day) {
  260. return { year, month, day, corner: [], holiday: [], schedule: [] }
  261. },
  262. getMarker(key) {
  263. const marker = this.data._markerdays[key]
  264. return marker ? marker : null
  265. },
  266. setMarkerItem(marker, markers) {
  267. let { year, month, day, type, mark, color, bgColor } = marker
  268. let _key = `d_${ year }_${ month }_${ day }`
  269. if (!markers.hasOwnProperty(_key)) markers[_key] = this.initMarker(year, month, day)
  270. if (type == 'corner') mark = mark.substring(0, 2)
  271. let key = marker[this.data._markerKey] || markers[_key].length
  272. markers[_key][type].push({ mark, color, bgColor, key })
  273. return markers
  274. },
  275. addMarker(marker) {
  276. let _markers = this.data._markerdays
  277. let { year, month, day } = marker
  278. _markers = this.setMarkerItem(marker, _markers)
  279. let _key = `d_${ year }_${ month }_${ day }`
  280. let keys = this.findDateInCurrDateSwiper(year, month, day)
  281. if (keys.length > 0) {
  282. this.setData((_ => {
  283. for (let i = 0; i < keys.length; i++) {
  284. _[keys[i]] = _markers[_key]
  285. }
  286. return _
  287. })(new Object))
  288. }
  289. },
  290. editMarker(marker) {
  291. let _markers = this.data._markerdays
  292. let { year, month, day, type, mark, color, bgColor } = marker
  293. let _mkey = `d_${ year }_${ month }_${ day }`
  294. if (!_markers.hasOwnProperty(_mkey)) return
  295. let _markerInfo = _markers[_mkey]
  296. let key = this.data._markerKey
  297. let setData = new Object
  298. _markerInfo[type] = _markerInfo[type].map(_ => {
  299. if (_.key == marker[key]) {
  300. _ = { mark, color, bgColor, key }
  301. }
  302. return _
  303. })
  304. setData[`_markerdays.${ _mkey }.${ type }`] = _markerInfo[type]
  305. let keys = this.findDateInCurrDateSwiper(year, month, day)
  306. if (keys.length > 0) {
  307. for (let i = 0; i < keys.length; i++) {
  308. setData[keys[i]] = _markerInfo
  309. }
  310. }
  311. this.setData(setData)
  312. },
  313. delMarker({ year, month, day }, type = '', key = '') {
  314. let setData = new Object
  315. let _markers = this.data._markerdays
  316. let _mkey = `d_${ year }_${ month }_${ day }`
  317. if (!_markers.hasOwnProperty(_mkey)) return
  318. let _markerInfo = _markers[_mkey]
  319. if (type != '' && key != '') {
  320. _markerInfo[type] = _markerInfo[type].filter(_ => _.key != key)
  321. setData[`_markerdays.${ _mkey }.${ type }`] = _markerInfo[type]
  322. } else {
  323. if (type == '') {
  324. _markerInfo = null
  325. setData[`_markerdays.${ _mkey }`] = null
  326. } else {
  327. _markerInfo[type] = []
  328. setData[`_markerdays.${ _mkey }.${ type }`] = []
  329. }
  330. }
  331. let keys = this.findDateInCurrDateSwiper(year, month, day)
  332. if (keys.length > 0) {
  333. for (let i = 0; i < keys.length; i++) {
  334. setData[keys[i]] = _markerInfo
  335. }
  336. }
  337. this.setData(setData)
  338. },
  339. findDateInCurrDateSwiper(year, month, day) {
  340. let keys = []
  341. for (let i = 0; i < this.data.months.length; i++) {
  342. let _month = this.data.months[i]
  343. if (
  344. Math.abs(_month.year - year) < 2 &&
  345. Math.abs(_month.month - month) < 2
  346. ) {
  347. let _idx = _month.idays.findIndex(_ => _.year == year && _.month == month && _.day == day)
  348. if (_idx >= 0) {
  349. let _wdx = Math.floor(_idx / 7)
  350. let _widx = _idx % 7
  351. keys.push(`months[${ i }].idays[${ _idx }].marker`)
  352. keys.push(`months[${ i }].days[${ _wdx }].days[${ _widx }].marker`)
  353. }
  354. }
  355. }
  356. return keys
  357. },
  358. handleDynamicMarkers(markers) {
  359. if (Array.isArray(markers) && markers.length > 0) {
  360. this.clearMarkers()
  361. const _ms = markers.filter(_ => _.year && _.month && _.day)
  362. const _markers = this.initMarkDays(_ms)
  363. this.setData({ _markerdays: _markers })
  364. this.setDynamicMarkers(_markers)
  365. }
  366. },
  367. setDynamicMarkers(markers) {
  368. const _markers = Object.keys(markers)
  369. const months = this.data.months
  370. for (let i = 0; i < _markers.length; i++) {
  371. let _marker = markers[_markers[i]]
  372. for (let j = 0; j < months.length; j++) {
  373. let _month = months[j]
  374. if (
  375. Math.abs(_month.year - _marker.year) < 2 &&
  376. Math.abs(_month.month - _marker.month) < 2
  377. ) {
  378. let _idx = _month.idays.findIndex(_ => _.year == _marker.year && _.month == _marker.month && _.day == _marker.day)
  379. if (_idx >= 0) {
  380. let _wdx = Math.floor(_idx / 7)
  381. let _widx = _idx % 7
  382. months[j].idays[_idx].marker = _marker
  383. months[j].days[_wdx].days[_widx].marker = _marker
  384. }
  385. }
  386. }
  387. }
  388. this.setData({ months })
  389. },
  390. clearMarkers() {
  391. const markers = Object.values(this.data._markerdays)
  392. const months = this.data.months
  393. let setData = new Object
  394. for (let i = 0; i < markers.length; i++) {
  395. let _marker = markers[i]
  396. for (let j = 0; j < months.length; j++) {
  397. let _month = months[j]
  398. if (
  399. Math.abs(_month.year - _marker.year) < 2 &&
  400. Math.abs(_month.month - _marker.month) < 2
  401. ) {
  402. let _idx = _month.idays.findIndex(_ => _.year == _marker.year && _.month == _marker.month && _.day == _marker.day)
  403. if (_idx >= 0) {
  404. let _wdx = Math.floor(_idx / 7)
  405. let _widx = _idx % 7
  406. setData[`months[${ j }].idays[${ _idx }].marker`] = null
  407. setData[`months[${ j }].days[${ _wdx }].days[${ _widx }].marker`] = null
  408. }
  409. }
  410. }
  411. }
  412. this.setData(setData)
  413. },
  414. initCalendarMonth(d, trans = '', wf = null) {
  415. const { year, month, lunar_order, lunar_year } = d
  416. const _ds = this.getMonthDays(d)
  417. const _wds = this.getMonthWeekDays(_ds.days, year, month)
  418. return {
  419. key: `m_${ year }_${ month }`,
  420. year,
  421. month,
  422. lunar_order,
  423. lunar_year,
  424. idays: _ds.days,
  425. bar: JSON.parse(JSON.stringify(initCurrInfo)),
  426. days: _wds,
  427. count: _ds.count,
  428. trans: typeof trans === 'function' ? trans.call(this, _ds, _wds) : trans,
  429. wf
  430. }
  431. },
  432. setMonths(d, current) {
  433. return new Promise((resolve, reject) => {
  434. const _trans = this.getMonthsTrans(d, current)
  435. const months = this.getSwiperMonths(d, current, _trans)
  436. this.setData({ months }, () => {
  437. resolve()
  438. })
  439. })
  440. },
  441. getSwiperMonths(d, current, trans) {
  442. return Array.apply(null, { length: 5 }).map((_, i) => {
  443. if (i === current) return this.initCalendarMonth(d, trans[i])
  444. return this.initCalendarMonth(DayDetail(d.year, d.month + i - current, 1), trans[i])
  445. })
  446. },
  447. getMonthDays(d) {
  448. const today = this.data._today
  449. const { count, days } = MonthDaysDetailFull(d.year, d.month)
  450. return {
  451. count,
  452. days: days.map(item => this.replenishDateInfo(item, today))
  453. }
  454. },
  455. getMonthWeekDays(idays, year, month) {
  456. return Array.apply(null, { length: idays.length / 7 }).map((w, idx) => {
  457. return {
  458. key: `w_${ year }_${ month }_${ idx + 1 }`,
  459. days: Array.apply(null, { length: 7 }).map((_, _i) => idays[idx * 7 + _i])
  460. }
  461. })
  462. },
  463. replenishDateInfo(d, t) {
  464. d.isToday = (t.year == d.year && t.month == d.month && t.day == d.day)
  465. d.key = `d_${ d.year }_${ d.month }_${ d.day }`
  466. d.marker = this.getMarker(d.key)
  467. return d
  468. },
  469. setWeeks(d, current) {
  470. return new Promise((resolve, reject) => {
  471. const { year, month, day } = d
  472. const months = Array.apply(null, { length: 5 }).map((_, i) => {
  473. if (i == current) return this.getMonthByWeekDayInIdx(d)
  474. let _d = DayDetail(year, month, day + (i - 2) * 7)
  475. return this.getMonthByWeekDayInIdx(_d)
  476. })
  477. this.setData({ months }, () => {
  478. resolve()
  479. })
  480. })
  481. },
  482. getRects() {
  483. this._rectsLoading = true
  484. return new Promise(resolve => {
  485. Promise.all([this.getPos('#calendar'), this.getPos('.wd-calendar-week-item')])
  486. .then(([calendar, rects]) => {
  487. const _initX = calendar[0].left
  488. const _rects = rects.map(item => {
  489. item.center = item.left + item.width / 2 - _initX
  490. return item
  491. })
  492. this.setData({
  493. _rects
  494. }, () => {
  495. this._rectsLoading = false
  496. resolve()
  497. })
  498. })
  499. })
  500. },
  501. getPos(selector) {
  502. return new Promise((resolve, reject) => {
  503. const query = this.createSelectorQuery()
  504. query.selectAll(selector).boundingClientRect(rects => {
  505. if (rects.length > 0) resolve(rects)
  506. else reject(rects)
  507. }).exec()
  508. })
  509. },
  510. reloadPos() {
  511. return new Promise((resolve, reject) => {
  512. this.getRects().then(() => {
  513. this.getDayCurr(this.data.currTab, this.data._selDay.month)
  514. resolve()
  515. })
  516. })
  517. },
  518. initSelBar(i, d, l, w, v) {
  519. const r = this.data._rects[i % 7]
  520. return {
  521. i,
  522. x: r.center,
  523. y: `calc(100% / ${ l } * ${ w + 0.5 })`,
  524. t: d.isToday,
  525. d: d.day,
  526. a: true,
  527. s: true,
  528. v
  529. }
  530. },
  531. getDayCurr(idx, month, callback = null, vibrate = false) {
  532. const _month = this.data.months[idx]
  533. const day = this.data._selDay.day <= _month.count ? this.data._selDay.day : _month.count
  534. const seekIdx = _month.idays.findIndex(_d => (_d.month == month && _d.day == day))
  535. if (seekIdx >= 0) {
  536. const seekDay = _month.idays[seekIdx]
  537. const wdx = this.getDayInCurrMonthWeek(seekDay)
  538. let setData = {
  539. _currWeekIdx: wdx,
  540. [`months[${ idx }].bar`]: this.initSelBar(seekIdx, seekDay, _month.days.length, wdx, vibrate)
  541. }
  542. typeof callback === 'function' && (setData = callback(setData, seekDay, seekIdx))
  543. this.setData(setData, () => {
  544. this.setTitleInfo(seekDay)
  545. this.triggerSel(seekDay)
  546. })
  547. }
  548. },
  549. swiperTo(type, d, current) {
  550. current = current ? current : this.data.currTab
  551. const _current = type == 'prev' ? (current + 4) % 5 : (current + 1) % 5
  552. this.setData({
  553. _selDay: d,
  554. _selWeek: d.week,
  555. currTab: _current
  556. })
  557. },
  558. selDate(e) {
  559. if (this._rectsLoading) return
  560. const currTab = this.data.currTab
  561. const { wdx, ddx } = e.currentTarget.dataset
  562. const idx = wdx * 7 + ddx
  563. const { idays, month } = this.data.months[currTab]
  564. const seek = idays[idx]
  565. if (this.data.currView != 2) {
  566. if (seek.type == 'prev' || seek.type == 'next') {
  567. this.swiperTo(seek.type, seek, currTab)
  568. } else {
  569. this.setDate(idx, currTab, (setData, _s, _w) => {
  570. if (_w === this.data._currWeekIdx) return setData
  571. setData = Object.assign({}, setData, this.refreshAllTrans(_s))
  572. return setData
  573. })
  574. }
  575. } else {
  576. if (seek.month != month) {
  577. let newMonth = this.getMonthByWeekDayInIdx(seek)
  578. this.setData({
  579. [`months[${ currTab }]`]: this.handelWeekMonthChange(this.data._selDay, newMonth),
  580. monthchange: true,
  581. barAni: false
  582. }, () => {
  583. this.handleWeekMonthChangeSel(seek, currTab, newMonth)
  584. })
  585. } else {
  586. this.setDate(idx, currTab)
  587. }
  588. }
  589. },
  590. handelWeekMonthChange(d, newMonth) {
  591. const seekIdx = newMonth.idays.findIndex(_d => (_d.month == d.month && _d.day == d.day))
  592. if (seekIdx >= 0) {
  593. const seekDay = newMonth.idays[seekIdx]
  594. newMonth.bar = this.initSelBar(seekIdx, seekDay, newMonth.days.length, Math.floor(seekIdx / 7), false)
  595. }
  596. return newMonth
  597. },
  598. handleWeekMonthChangeSel(seek, currTab, month) {
  599. const seekIdx = month.idays.findIndex(_d => (_d.month == seek.month && _d.day == seek.day))
  600. if (seekIdx >= 0) {
  601. const seekDay = month.idays[seekIdx]
  602. const wdx = Math.floor(seekIdx / 7)
  603. let setData = {
  604. _currWeekIdx: wdx,
  605. _selDay: seekDay,
  606. _selWeek: seekDay.week,
  607. barAni: true,
  608. [`months[${ currTab }].bar`]: this.initSelBar(seekIdx, seekDay, month.days.length, wdx, true)
  609. }
  610. this.setData(setData, () => {
  611. this.setTitleInfo(seekDay)
  612. this.triggerSel(seekDay)
  613. })
  614. }
  615. },
  616. setDate(idx, currTab, callback) {
  617. const month = this.data.months[currTab]
  618. const seek = month.idays[idx]
  619. const wdx = this.getDayInCurrMonthWeek(seek)
  620. let setData = {
  621. _selDay: seek,
  622. _selWeek: seek.week,
  623. _currWeekIdx: wdx,
  624. [`months[${ currTab }].bar`]: this.initSelBar(idx, seek, month.days.length, wdx, true)
  625. }
  626. if (typeof callback === 'function') setData = callback(setData, seek, wdx)
  627. this.setData(setData, () => {
  628. this.setTitleInfo(seek)
  629. this.triggerSel(seek)
  630. })
  631. },
  632. refreshAllTrans(seek) {
  633. const _trans = this.getMonthsTrans(seek, this.data.currTab)
  634. return (_ => {
  635. for (let i = 0; i < 5; i++) {
  636. _[`months[${ i }].trans`] = _trans[i]
  637. }
  638. return _
  639. })(new Object)
  640. },
  641. handleSelBarAniEnd(e) {
  642. const currTab = this.data.currTab
  643. const key = `months[${ currTab }].bar.a`
  644. this.setData({
  645. [key]: false,
  646. tdOpShow: !this.data.months[currTab].bar.t
  647. })
  648. if (this.data.months[currTab].bar.v && this.data._vibrate) {
  649. wx.vibrateShort({
  650. type: 'light'
  651. })
  652. }
  653. },
  654. handleSwiperAniEnd(e) {
  655. const { current, source } = e.detail
  656. if (source == 'touch') {
  657. if (current != this.data.currTab) {
  658. this.setCurrTab(current)
  659. }
  660. } else {
  661. this.refrenMonths(current, true)
  662. }
  663. },
  664. setCurrTab(currTab) {
  665. this.setData({ currTab }, () => {
  666. this.refrenMonths(currTab)
  667. })
  668. },
  669. refrenMonths(current, v = false) {
  670. if (this.data.currView == 2) {
  671. this.refreshMonthWeekStatus(current, v)
  672. } else {
  673. this.refreshMonthStatus(current, v)
  674. }
  675. },
  676. refreshMonthStatus(current, v = false) {
  677. const currMonth = this.data.months[current]
  678. this.getDayCurr(current, currMonth.month, (setData, seekday) => {
  679. setData._selDay = seekday
  680. return setData
  681. }, v)
  682. const _trans = this.getMonthsTrans(this.data._selDay, current)
  683. this.refreshAllMonth(currMonth, current, _trans, setData => {
  684. setData[`months[${ current }].trans`] = _trans[current]
  685. return setData
  686. }, () => {
  687. this.triggerChange('month')
  688. })
  689. },
  690. refreshAllMonth(currMonth, current, _trans, _scallback, _acallback) {
  691. const { year, month } = currMonth
  692. const idx_1 = (current + 3) % 5
  693. const idx_2 = (current + 4) % 5
  694. const idx_3 = (current + 1) % 5
  695. const idx_4 = (current + 2) % 5
  696. const d1 = DayDetail(year, month - 2, 1)
  697. const d2 = DayDetail(year, month - 1, 1)
  698. const d3 = DayDetail(year, month + 1, 1)
  699. const d4 = DayDetail(year, month + 2, 1)
  700. let setData = {
  701. [`months[${ idx_1 }]`]: this.refreshMonth(d1, idx_1, _trans),
  702. [`months[${ idx_2 }]`]: this.refreshMonth(d2, idx_2, _trans),
  703. [`months[${ idx_3 }]`]: this.refreshMonth(d3, idx_3, _trans),
  704. [`months[${ idx_4 }]`]: this.refreshMonth(d4, idx_4, _trans)
  705. }
  706. if (typeof _scallback === 'function') setData = _scallback(setData)
  707. this.setData(setData, () => {
  708. typeof _acallback === 'function' && _acallback()
  709. })
  710. },
  711. refreshMonth(d, idx, _trans) {
  712. return this.initCalendarMonth(d, _trans[idx])
  713. },
  714. refreshMonthWeekStatus(current, v = false) {
  715. this.getDayCurrForWeek(current, () => {
  716. this.setMonthsForWeek(current, setData => {
  717. setData.weektabchange = current
  718. return setData
  719. }, () => {
  720. this.triggerChange('week')
  721. })
  722. }, v)
  723. },
  724. getDayCurrForWeek(current, callback, v = false) {
  725. const month = this.data.months[current]
  726. const wf = month.wf
  727. const newSelDay = CorrectDate(wf.year, wf.month, wf.day + this.data._selDay.week)
  728. const seekIdx = month.idays.findIndex(_d => (_d.month == newSelDay.month && _d.day == newSelDay.day))
  729. if (seekIdx >= 0) {
  730. const seekDay = month.idays[seekIdx]
  731. const wdx = this.getDayInCurrMonthWeek(seekDay)
  732. this.setData({
  733. _selDay: seekDay,
  734. _selWeek: seekDay.week,
  735. _currWeekIdx: wdx,
  736. [`months[${ current }].bar`]: this.initSelBar(seekIdx, seekDay, month.days.length, wdx, v)
  737. }, () => {
  738. this.setTitleInfo(seekDay)
  739. this.triggerSel(seekDay)
  740. typeof callback === 'function' && callback()
  741. })
  742. }
  743. },
  744. setTitleInfo(d, today = null) {
  745. today = today ? today : this.data._today
  746. let titleInfo = this.data.titleInfo
  747. const count = this.getDateDiff(new Date(today.year, today.month - 1, today.day), new Date(d.year, d.month - 1, d.day))
  748. if (d.year == today.year && d.month == today.month && d.day == today.day) {
  749. titleInfo = this._curr_view == 2 ? `第${ YearWeekOrder(today.year, today.month, today.day) }周 ${ today.week_name }` : today.week_name
  750. } else {
  751. titleInfo = `${ this._curr_view == 2 ? '第' + YearWeekOrder(d.year, d.month, d.day) + '周 ' : ''}${ Math.abs(count) }天${ count < 0 ? '前' : '后' }`
  752. }
  753. if(wx.getStorageSync('languagecode')!='ZH') {
  754. titleInfo = count < 0 ?`${ Math.abs(count) } days ago`: `After ${ Math.abs(count) } days`
  755. if(count==0) titleInfo = 'today'
  756. }
  757. this.setData({ titleInfo })
  758. },
  759. getDateDiff(startDate, endDate) {
  760. return Math.floor((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24))
  761. },
  762. toggleView({ state }) {
  763. this._curr_view = state
  764. },
  765. handleCalendarTransEnd() {
  766. const noChanged = ((this.data.currView == 1 || this.data.currView == 3) && (this._curr_view == 1 || this._curr_view == 3) || this._curr_view == this.data.currView)
  767. if (!noChanged) {
  768. this.triggerView(this._curr_view)
  769. this.setData({
  770. currView: this._curr_view
  771. }, () => {
  772. const { _selDay, currTab } = this.data
  773. if (this._curr_view == 2) {
  774. this.setMonthsForWeek(currTab, setData => {
  775. setData.weektabchange = currTab
  776. if (this._curr_view == 1) setData.yearPanelShow = this._year_panel_show
  777. return setData
  778. }, () => {
  779. this.triggerChange('week')
  780. })
  781. } else {
  782. const _trans = this.getMonthsTrans(_selDay, this.data.months[currTab].trans, true)
  783. this.refreshAllMonth(_selDay, currTab, _trans, setData => {
  784. if (this._curr_view == 1) setData.yearPanelShow = this._year_panel_show
  785. return setData
  786. }, () => {
  787. this.triggerChange('month')
  788. })
  789. }
  790. this.setTitleInfo(_selDay)
  791. })
  792. } else if (this._curr_view != this.data.currView) {
  793. let setData = { currView: this._curr_view }
  794. if (this._curr_view == 1) setData.yearPanelShow = this._year_panel_show
  795. this.setData(setData)
  796. }
  797. },
  798. handleOpBarTransEnd() {
  799. this.setData({
  800. solidDay: this._curr_view != 2
  801. })
  802. },
  803. toToday() {
  804. if (this.data.tdOpShow) {
  805. if (this.data.currView == 2) {
  806. this.handleWeekToDate(this.data._today)
  807. } else {
  808. this.handleMonthToToday(this.data._today)
  809. }
  810. }
  811. },
  812. handleDayInOtherMonth(day, tab, callback) {
  813. let month = this.data.months[tab]
  814. if (day.month != month.month) {
  815. let newMonth = this.getMonthByWeekDayInIdx(day)
  816. this.setData({
  817. [`months[${ tab }]`]: this.handelWeekMonthChange(this.data._selDay, newMonth),
  818. monthchange: true,
  819. barAni: false
  820. }, () => {
  821. typeof callback === 'function' && callback()
  822. })
  823. } else {
  824. typeof callback === 'function' && callback()
  825. }
  826. },
  827. handleWeekToDate(date) {
  828. const todayDate = (new Date(date.year, date.month - 1, date.day)).getTime()
  829. const isInSwiper = this.data.months.findIndex(_m => {
  830. let startDate = (new Date(_m.wf.year, _m.wf.month - 1, _m.wf.day)).getTime()
  831. let endDate = (new Date(_m.wf.year, _m.wf.month - 1, _m.wf.day + 7)).getTime()
  832. return startDate <= todayDate && endDate > todayDate
  833. })
  834. const currTab = this.data.currTab
  835. if (isInSwiper >= 0) {
  836. if (isInSwiper == currTab) {
  837. this.handleDayInOtherMonth(date, isInSwiper, () => {
  838. const idx = this.getDayInCurrMonth(date)
  839. if (idx >= 0) this.setDate(idx, currTab, (setData, _s, _w) => {
  840. if (_w === this.data._currWeekIdx) return setData
  841. setData = Object.assign({}, setData, this.refreshAllTrans(_s))
  842. setData.barAni = true
  843. return setData
  844. })
  845. })
  846. } else {
  847. this.setData({
  848. currTab: isInSwiper
  849. }, () => {
  850. this.handleDayInOtherMonth(date, isInSwiper, () => {
  851. const _idx = this.data.months[isInSwiper].idays.findIndex(_d => (_d.month == date.month && _d.day == date.day))
  852. this.setDate(_idx, isInSwiper, (setData, _s, _w) => {
  853. setData = Object.assign({}, setData, this.refreshAllTrans(_s))
  854. setData.barAni = true
  855. return setData
  856. })
  857. })
  858. })
  859. }
  860. } else {
  861. this.setData({
  862. _selDay: date,
  863. _selWeek: date.week
  864. }, () => {
  865. this.setMonthsForWeek(currTab, setData => {
  866. let month = this.getMonthByWeekDayInIdx(date)
  867. month.wf = setData[`months[${ currTab }].wf`]
  868. delete(setData[`months[${ currTab }].wf`])
  869. setData[`months[${ currTab }]`] = month
  870. setData.monthchange = true
  871. setData.weektabchange = this.data.currTab
  872. return setData
  873. }, () => {
  874. this.getDayCurr(currTab, date.month, null, true)
  875. this.triggerChange('week')
  876. })
  877. })
  878. }
  879. },
  880. handleMonthToToday(date) {
  881. const isInSwiper = this.data.months.findIndex(_m => (_m.year == date.year && _m.month == date.month))
  882. const currTab = this.data.currTab
  883. if (isInSwiper >= 0) {
  884. if (currTab == isInSwiper) {
  885. const idx = this.getDayInCurrMonth(date)
  886. if (idx >= 0) this.setDate(idx, currTab, (setData, _s, _w) => {
  887. if (_w === this.data._currWeekIdx) return setData
  888. setData = Object.assign({}, setData, this.refreshAllTrans(_s))
  889. return setData
  890. })
  891. } else {
  892. this.setData({
  893. currTab: isInSwiper
  894. }, () => {
  895. const _idx = this.data.months[isInSwiper].idays.findIndex(_d => (_d.month == date.month && _d.day == date.day))
  896. this.setDate(_idx, isInSwiper, (setData, _s, _w) => {
  897. setData = Object.assign({}, setData, this.refreshAllTrans(_s))
  898. return setData
  899. })
  900. })
  901. }
  902. } else {
  903. const d = CorrectDate(date.year, date.month, 1)
  904. const _trans = this.getMonthsTrans(date, currTab)
  905. this.setData({
  906. [`months[${ currTab }]`]: this.refreshMonth(d, currTab, _trans),
  907. _selDay: date,
  908. _selWeek: date.week
  909. }, () => {
  910. this.getDayCurr(currTab, date.month, null, true)
  911. })
  912. this.refreshAllMonth(date, currTab, _trans, null, () => {
  913. this.triggerChange('month')
  914. })
  915. }
  916. },
  917. getDayInCurrMonth(d) {
  918. const _ = this.data.months[this.data.currTab].idays
  919. return _.findIndex(_d => (d.year == _d.year && d.month == _d.month && d.day == _d.day))
  920. },
  921. getDayInCurrMonthWeek(d) {
  922. const _ = this.data.months[this.data.currTab].idays
  923. return this.getDayWeekIdxInMonth(d, _)
  924. },
  925. getDayWeekIdxInMonth(d, _) {
  926. const idx = _.findIndex(_d => (d.month == _d.month && d.day == _d.day))
  927. return Math.floor(idx / 7)
  928. },
  929. setMonthsForWeek(current, callback, userFunc) {
  930. const currDay = this.data._selDay
  931. this.setWeekMonths(currDay, current, callback, userFunc)
  932. },
  933. setWeekMonths(d, current, callback, userFunc) {
  934. const { year, month, day } = d
  935. current = current ? current : this.data.currTab
  936. const idx_1 = (current + 3) % 5
  937. const idx_2 = (current + 4) % 5
  938. const idx_3 = (current + 1) % 5
  939. const idx_4 = (current + 2) % 5
  940. const d1 = DayDetail(year, month, day - 14)
  941. const d2 = DayDetail(year, month, day - 7)
  942. const d3 = DayDetail(year, month, day + 7)
  943. const d4 = DayDetail(year, month, day + 14)
  944. let setData = {
  945. [`months[${ idx_1 }]`]: this.getMonthByWeekDayInIdx(d1),
  946. [`months[${ idx_2 }]`]: this.getMonthByWeekDayInIdx(d2),
  947. [`months[${ current }].wf`]: WeekFirstDay(d.year, d.month, d.day),
  948. [`months[${ idx_3 }]`]: this.getMonthByWeekDayInIdx(d3),
  949. [`months[${ idx_4 }]`]: this.getMonthByWeekDayInIdx(d4),
  950. }
  951. if (typeof callback === 'function') setData = callback(setData)
  952. this.setData(setData, () => {
  953. typeof userFunc === 'function' && userFunc()
  954. })
  955. },
  956. getMonthByWeekDayInIdx(d) {
  957. return this.initCalendarMonth(
  958. d,
  959. (ids, ds) => this.calcTrans(this.getDayWeekIdxInMonth(d, ids.days), ds.length),
  960. WeekFirstDay(d.year, d.month, d.day)
  961. )
  962. },
  963. calcTrans(wdx, wlength) {
  964. const defaultDayHeight = this.data.panelHeight / 5
  965. const currentDayHeight = wlength == 5 ? defaultDayHeight : this.data.panelHeight / wlength
  966. const dayHeightDifference = wlength == 5 ? 0 : (defaultDayHeight - currentDayHeight) / 2
  967. return Math.floor((wdx * currentDayHeight - dayHeightDifference) * 100) / 100
  968. },
  969. getMonthsTrans(d, current, isCurrTrans = false) {
  970. const { year, month, day } = d
  971. const idx_1 = (current + 3) % 5
  972. const idx_2 = (current + 4) % 5
  973. const idx_3 = (current + 1) % 5
  974. const idx_4 = (current + 2) % 5
  975. const d1 = CorrectDate(year, month, day - 14)
  976. const d2 = CorrectDate(year, month, day - 7)
  977. const d3 = CorrectDate(year, month, day + 7)
  978. const d4 = CorrectDate(year, month, day + 14)
  979. const arr = Array.apply(null, { length: 5 }).map(_ => 0)
  980. arr[idx_1] = this.getTransByDayInMonth(d1)
  981. arr[idx_2] = this.getTransByDayInMonth(d2)
  982. arr[current] = isCurrTrans ? current : this.getTransByDayInMonth(d)
  983. arr[idx_3] = this.getTransByDayInMonth(d3)
  984. arr[idx_4] = this.getTransByDayInMonth(d4)
  985. return arr
  986. },
  987. getTransByDayInMonth(d) {
  988. const idays = this.getMonthDays(d).days
  989. return this.calcTrans(this.getDayWeekIdxInMonth(d, idays), idays.length / 7)
  990. },
  991. setYearMs(year, current, today) {
  992. today = today ? today : this.data._today
  993. let years = Array.apply(null, { length: 5 })
  994. years[(current + 3) % 5] = year - 2
  995. years[(current + 4) % 5] = year - 1
  996. years[current] = year
  997. years[(current + 1) % 5] = year + 1
  998. years[(current + 2) % 5] = year + 2
  999. return Array.apply(null, { length: 5 }).map((_, i) => this.getYearMonth(years[i], today))
  1000. },
  1001. refreshYearMs(year, current, callback) {
  1002. const today = this.data._today
  1003. const idx_1 = (current + 3) % 5
  1004. const idx_2 = (current + 4) % 5
  1005. const idx_3 = (current + 1) % 5
  1006. const idx_4 = (current + 2) % 5
  1007. const y1 = year - 2
  1008. const y2 = year - 1
  1009. const y3 = year + 1
  1010. const y4 = year + 2
  1011. this.setData({
  1012. [`yearMs[${ idx_1 }]`]: this.getYearMonth(y1, today),
  1013. [`yearMs[${ idx_2 }]`]: this.getYearMonth(y2, today),
  1014. [`yearMs[${ idx_3 }]`]: this.getYearMonth(y3, today),
  1015. [`yearMs[${ idx_4 }]`]: this.getYearMonth(y4, today)
  1016. }, () => {
  1017. typeof callback === 'function' && callback()
  1018. })
  1019. },
  1020. getYearMonth(year, today) {
  1021. today = today ? today : this.data._today
  1022. let { lunar_year } = LunarDetail(year, 6, 1)
  1023. return {
  1024. year,
  1025. lunar_year,
  1026. key: `yp_${ year }`,
  1027. months: Months.map(m => {
  1028. return {
  1029. month: m,
  1030. curr: today.year == year && today.month == m,
  1031. lunar: this.getMonthLunarMonthFirst(year, m)
  1032. }
  1033. })
  1034. }
  1035. },
  1036. getMonthLunarMonthFirst(year, month) {
  1037. const monthDays = MonthOnly(year, month)
  1038. return monthDays.filter(d => d.ld == 1).map(d => {
  1039. let { day, lunar_month } = d
  1040. return { day, lunar: lunar_month, order: day > 3 ? EnOrders[4] : EnOrders[day] }
  1041. })
  1042. },
  1043. handleYmSwiperAniEnd(e) {
  1044. const { current, source } = e.detail
  1045. const year = this.data.yearMs[current].year
  1046. if (source == 'touch') {
  1047. if (current != this.data.currYmTab) {
  1048. this.setData({
  1049. currYmTab: current
  1050. }, () => {
  1051. this.refreshYearMs(year, current)
  1052. })
  1053. }
  1054. } else {
  1055. this.refreshYearMs(year, current)
  1056. }
  1057. },
  1058. justYearPanelShow() {
  1059. this._year_panel_show = true
  1060. this.setData({
  1061. yearPanelShow: true
  1062. })
  1063. },
  1064. handleYearPanelShow() {
  1065. this._year_panel_show = true
  1066. },
  1067. handleYearPanelDayClick(e) {
  1068. const { year, month } = e.currentTarget.dataset
  1069. const currTab = this.data.currTab
  1070. const currMonth = this.data.months[currTab]
  1071. if (currMonth.year == year && currMonth.month == month) {
  1072. this._year_panel_show = false
  1073. this.setData({
  1074. yearPanelShow: false
  1075. })
  1076. } else {
  1077. const d = this.getMonthDay(year, month, this.data._selDay.day)
  1078. const _trans = this.getMonthsTrans(d, currTab)
  1079. this._year_panel_show = false
  1080. this.setData({
  1081. [`months[${ currTab }]`]: this.refreshMonth(d, currTab, _trans),
  1082. _selDay: d,
  1083. _selWeek: d.week,
  1084. yearPanelShow: false
  1085. }, () => {
  1086. this.getDayCurr(currTab, d.month)
  1087. })
  1088. this.refreshAllMonth(d, currTab, _trans, null, () => {
  1089. this.triggerChange('month')
  1090. })
  1091. }
  1092. },
  1093. getMonthDay(year, month, day) {
  1094. const dayCount = MonthOnly(year, month).length
  1095. const today = this.data._today
  1096. day = day > dayCount ? dayCount : day
  1097. let date = this.replenishDateInfo(DayDetail(year, month, day), today)
  1098. return date
  1099. },
  1100. getCorrectDate() {
  1101. if (arguments.length == 1) {
  1102. let date
  1103. if (typeof arguments[0] === 'string') {
  1104. const strDates = arguments[0].split('-').map(_ => parseInt(_))
  1105. if (strDates.length < 3) {
  1106. this.console('日期格式错误', 'warn')
  1107. return null
  1108. }
  1109. strDates[1] = strDates[1] - 1
  1110. date = new Date(...strDates)
  1111. } else {
  1112. date = new Date(arguments[0])
  1113. }
  1114. if (isNaN(date.getTime())) {
  1115. this.console('日期格式错误', 'warn')
  1116. return null
  1117. }
  1118. const year = date.getFullYear()
  1119. const month = date.getMonth() + 1
  1120. const day = date.getDate()
  1121. return { year, month, day }
  1122. } else if (arguments.length == 3) {
  1123. const [_y, _m, _d] = arguments
  1124. const [y, m, d] = [_y, _m, _d].map(_ => parseInt(_))
  1125. const { year, month, day } = CorrectDate(y, m, d)
  1126. return { year, month, day }
  1127. } else {
  1128. this.console('日期格式错误', 'warn')
  1129. return null
  1130. }
  1131. },
  1132. calendarToDate() {
  1133. const date = this.getCorrectDate(...arguments)
  1134. if (date) {
  1135. const today = this.data._today
  1136. let d = this.replenishDateInfo(DayDetail(date.year, date.month, date.day), today)
  1137. if (this.data.currView == 2) {
  1138. this.handleWeekToDate(d)
  1139. } else {
  1140. this.handleMonthToToday(d)
  1141. }
  1142. }
  1143. },
  1144. calendarToMonth(year, month) {
  1145. const d = DayDetail(year, month, 1)
  1146. if (this.data.currView == 2) {
  1147. this.handleWeekToDate(d)
  1148. } else {
  1149. this.handleMonthToToday(d)
  1150. }
  1151. },
  1152. calendarPrev() {
  1153. const _prev = (this.data.currTab + 4) % 5
  1154. this.setCurrTab(_prev)
  1155. },
  1156. calendarNext() {
  1157. const _next = (this.data.currTab + 1) % 5
  1158. this.setCurrTab(_next)
  1159. },
  1160. switchCalendarView(view) {
  1161. view = view ? view : 'month'
  1162. view = (view != 'month' && view != 'week') ? 'month' : view
  1163. this.setData({
  1164. viewchange: view
  1165. })
  1166. },
  1167. getCalendarDateInfo() {
  1168. const { year, month, day } = this.getCorrectDate(...arguments)
  1169. return this.replenishDateInfo(DayDetail(year, month, day), this.data._today)
  1170. },
  1171. console(tips, type = 'info', ...args) {
  1172. const { label, content, title } = ConsoleStyle[type]
  1173. console.log(`%cWxCalendar${ title } %c${ tips }`, label, content, ...args)
  1174. }
  1175. },
  1176. export () {
  1177. if (this.data.loading) {
  1178. this.console('请在bindload回调后执行selectComponent', 'warn')
  1179. return null
  1180. }
  1181. const { minHeight, maxHeight, calendarHeight } = this.data
  1182. const calendarInstance = this
  1183. return {
  1184. name: 'wx-calendar',
  1185. version: Version,
  1186. minHeight,
  1187. maxHeight,
  1188. height: calendarHeight,
  1189. getDateInfo() {
  1190. return calendarInstance.getCalendarDateInfo(...arguments)
  1191. },
  1192. setMarkers() {
  1193. calendarInstance.handleDynamicMarkers(arguments[0])
  1194. },
  1195. addMarker() {
  1196. calendarInstance.addMarker(arguments[0])
  1197. },
  1198. editMarker() {
  1199. calendarInstance.editMarker(arguments[0])
  1200. },
  1201. delMarker() {
  1202. const [date, type, key] = arguments
  1203. calendarInstance.delMarker(date, type, key)
  1204. },
  1205. reloadMarkers() {
  1206. calendarInstance.handleDynamicMarkers(calendarInstance.data._markers)
  1207. },
  1208. toDate() {
  1209. calendarInstance.calendarToDate(...arguments)
  1210. },
  1211. toMonth() {
  1212. const [year, month] = arguments
  1213. calendarInstance.calendarToMonth(year, month)
  1214. },
  1215. toggleView() {
  1216. calendarInstance.switchCalendarView(arguments[0])
  1217. },
  1218. prev() {
  1219. calendarInstance.calendarPrev()
  1220. },
  1221. next() {
  1222. calendarInstance.calendarNext()
  1223. },
  1224. reloadPos() {
  1225. return calendarInstance.reloadPos()
  1226. }
  1227. }
  1228. }
  1229. })