index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import plugins from './plugins/index'
  2. import { calcJumpData } from './core'
  3. import { renderCalendar } from './render'
  4. import { calcTargetYMInfo } from './helper'
  5. import { dateUtil, calendarGesture, logger } from './utils/index'
  6. Component({
  7. options: {
  8. styleIsolation: 'apply-shared',
  9. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  10. },
  11. properties: {
  12. config: {
  13. type: Object,
  14. value: {}
  15. },
  16. header:{
  17. type:Object,
  18. value: {}
  19. }//用来定义显示我的收入及我的服务单
  20. },
  21. lifetimes: {
  22. attached: function() {
  23. this.initComp()
  24. }
  25. },
  26. methods: {
  27. initComp() {
  28. const calendarConfig = this.setDefaultDisableDate()
  29. this.setConfig(calendarConfig)
  30. },
  31. // 禁用某天日期配置默认为今天
  32. setDefaultDisableDate() {
  33. const calendarConfig = this.properties.config || {}
  34. if (calendarConfig.disableMode && !calendarConfig.disableMode.date) {
  35. calendarConfig.disableMode.date = dateUtil.toTimeStr(
  36. dateUtil.todayFMD()
  37. )
  38. }
  39. return calendarConfig
  40. },
  41. initCalendar(config) {
  42. const { defaultDate } = config
  43. let date = dateUtil.todayFMD()
  44. if (defaultDate && typeof defaultDate === 'string') {
  45. const dateInfo = defaultDate.split('-')
  46. if (dateInfo.length < 3) {
  47. return logger.warn('defaultDate配置格式应为: 2018-4-2 或 2018-04-02')
  48. } else {
  49. date = {
  50. year: +dateInfo[0],
  51. month: +dateInfo[1],
  52. date: +dateInfo[2]
  53. }
  54. }
  55. }
  56. const waitRenderData = calcJumpData({
  57. dateInfo: date,
  58. config
  59. })
  60. const timestamp = dateUtil.todayTimestamp()
  61. if (config.autoChoosedWhenJump) {
  62. const target = waitRenderData.dates.filter(
  63. item => dateUtil.toTimeStr(item) === dateUtil.toTimeStr(date)
  64. )
  65. if (target && target.length) {
  66. if (!waitRenderData.selectedDates) {
  67. waitRenderData.selectedDates = target
  68. } else {
  69. waitRenderData.selectedDates.push(target[0])
  70. }
  71. }
  72. }
  73. return {
  74. ...waitRenderData,
  75. todayTimestamp: timestamp,
  76. weeksCh: dateUtil.getWeekHeader(config.firstDayOfWeek)
  77. }
  78. },
  79. setConfig(config) {
  80. if (config.markToday && typeof config.markToday === 'string') {
  81. config.highlightToday = true
  82. }
  83. config.theme = config.theme || 'default'
  84. this.setData(
  85. {
  86. config
  87. },
  88. () => {
  89. for (let plugin of plugins.installed) {
  90. const [, p] = plugin
  91. if (typeof p.install === 'function') {
  92. p.install(this)
  93. }
  94. if (typeof p.methods === 'function') {
  95. const methods = p.methods(this)
  96. for (let fnName in methods) {
  97. if (fnName.startsWith('__')) continue
  98. const fn = methods[fnName]
  99. if (typeof fn === 'function') {
  100. if (!this.calendar) this.calendar = {}
  101. this.calendar[fnName] = fn
  102. }
  103. }
  104. }
  105. }
  106. const initData = this.initCalendar(config)
  107. renderCalendar.call(this, initData, config)
  108. }
  109. )
  110. },
  111. tapDate(e) {
  112. const { info } = e.currentTarget.dataset
  113. const { date, disable } = info || {}
  114. if (disable || !date) return
  115. const { calendar, config } = this.data
  116. let calendarData = calendar
  117. let calendarConfig = config
  118. if (config.takeoverTap) {
  119. return this.triggerEvent('takeoverTap', info)
  120. }
  121. for (let plugin of plugins.installed) {
  122. const [, p] = plugin
  123. if (typeof p.onTapDate === 'function') {
  124. const {
  125. calendarData: __calendarData,
  126. calendarConfig: __calendarConfig
  127. } = p.onTapDate(info, calendarData, calendarConfig)
  128. calendarData = __calendarData
  129. calendarConfig = __calendarConfig
  130. }
  131. }
  132. renderCalendar.call(this, calendarData, calendarConfig).then(() => {
  133. this.triggerEvent('afterTapDate', info)
  134. })
  135. },
  136. /**
  137. * 日历滑动开始
  138. * @param {object} e
  139. */
  140. calendarTouchstart(e) {
  141. console.info(e)
  142. const t = e.touches[0]
  143. const startX = t.clientX
  144. const startY = t.clientY
  145. this.swipeLock = true
  146. this.setData({
  147. 'gesture.startX': startX,
  148. 'gesture.startY': startY
  149. })
  150. },
  151. /**
  152. * 日历滑动中
  153. * @param {object} e
  154. */
  155. calendarTouchmove(e) {
  156. const { gesture } = this.data
  157. const { preventSwipe } = this.properties.config
  158. if (!this.swipeLock || preventSwipe) return
  159. if (calendarGesture.isLeft(gesture, e.touches[0])) {
  160. this.handleSwipe('left')
  161. this.swipeLock = false
  162. }
  163. if (calendarGesture.isRight(gesture, e.touches[0])) {
  164. this.handleSwipe('right')
  165. this.swipeLock = false
  166. }
  167. },
  168. calendarTouchend(e) {
  169. this.setData({
  170. 'calendar.leftSwipe': 0,
  171. 'calendar.rightSwipe': 0
  172. })
  173. },
  174. handleSwipe(direction) {
  175. let swipeKey = 'calendar.leftSwipe'
  176. if (direction === 'right') {
  177. swipeKey = 'calendar.rightSwipe'
  178. }
  179. this.setData({
  180. [swipeKey]: 1
  181. })
  182. const { calendar } = this.data
  183. let calendarData = calendar
  184. const { curYear, curMonth } = calendarData
  185. const getMonthInfo = calcTargetYMInfo()[direction]
  186. const target = getMonthInfo({
  187. year: +curYear,
  188. month: +curMonth
  189. })
  190. target.direction = direction
  191. this.renderCalendar(target)
  192. },
  193. changeDate(e) {
  194. const { type } = e.currentTarget.dataset
  195. const { calendar: calendarData } = this.data
  196. const { curYear, curMonth } = calendarData
  197. const getMonthInfo = calcTargetYMInfo()[type]
  198. const target = getMonthInfo({
  199. year: +curYear,
  200. month: +curMonth
  201. })
  202. target.direction = type
  203. this.renderCalendar(target)
  204. },
  205. renderCalendar(target) {
  206. let { calendar: calendarData, config } = this.data
  207. const { curYear, curMonth } = calendarData || {}
  208. for (let plugin of plugins.installed) {
  209. const [, p] = plugin
  210. if (typeof p.onSwitchCalendar === 'function') {
  211. calendarData = p.onSwitchCalendar(target, calendarData, this)
  212. }
  213. }
  214. return renderCalendar.call(this, calendarData, config).then(() => {
  215. let triggerEventName = 'whenChangeMonth'
  216. if (config.weekMode) {
  217. triggerEventName = 'whenChangeWeek'
  218. }
  219. this.triggerEvent(triggerEventName, {
  220. current: {
  221. year: +curYear,
  222. month: +curMonth
  223. },
  224. next: target
  225. })
  226. })
  227. }
  228. }
  229. })