base.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @Author: drfu*
  3. * @Description: 基础功能
  4. * @Date: 2020-10-08 21:22:09*
  5. * @Last Modified by: drfu
  6. * @Last Modified time: 2020-10-11 13:28:52
  7. * */
  8. import { calcJumpData } from '../../core'
  9. import { renderCalendar } from '../../render'
  10. import {
  11. dateUtil,
  12. getCalendarData,
  13. setCalendarData,
  14. getCalendarConfig
  15. } from '../../utils/index'
  16. export default () => {
  17. return {
  18. name: 'base',
  19. beforeRender(calendarData = {}, calendarConfig) {
  20. const calendar = calendarData
  21. const { selectedDates = [], dates } = calendar
  22. let _dates = [...dates]
  23. if (selectedDates.length) {
  24. const selectedDatesStr = selectedDates.map(date =>
  25. dateUtil.toTimeStr(date)
  26. )
  27. _dates.forEach(date => {
  28. const dateStr = dateUtil.toTimeStr(date)
  29. if (selectedDatesStr.includes(dateStr)) {
  30. date.choosed = true
  31. }
  32. })
  33. }
  34. return {
  35. calendarData: {
  36. ...calendarData,
  37. dates: _dates
  38. },
  39. calendarConfig
  40. }
  41. },
  42. onTapDate(tapedDate, calendarData = {}, calendarConfig = {}) {
  43. const calendar = {
  44. ...calendarData
  45. }
  46. const dateIndex = dateUtil.findDateIndexInArray(
  47. tapedDate,
  48. calendarData.dates
  49. )
  50. const { multi, inverse } = calendarConfig
  51. let dates = [...calendar.dates]
  52. const { selectedDates = [] } = calendar
  53. if (!multi) {
  54. let preSelectedDate = {}
  55. if (selectedDates.length) {
  56. preSelectedDate = [...selectedDates].pop() || {}
  57. }
  58. const timeStr = dateUtil.toTimeStr
  59. if (!inverse && timeStr(preSelectedDate) === timeStr(tapedDate)) {
  60. return calendar
  61. }
  62. let _tapedDate = { ...tapedDate, choosed: !tapedDate.choosed }
  63. dates[dateIndex] = _tapedDate
  64. if (preSelectedDate.date) {
  65. const idx = dateUtil.findDateIndexInArray(preSelectedDate, dates)
  66. const date = dates[idx]
  67. if (date) {
  68. date.choosed = false
  69. }
  70. }
  71. if (dates[dateIndex].choosed) {
  72. calendar.selectedDates = [dates[dateIndex]]
  73. } else {
  74. calendar.selectedDates = []
  75. }
  76. } else {
  77. dates[dateIndex] = {
  78. ...dates[dateIndex],
  79. choosed: !dates[dateIndex].choosed
  80. }
  81. if (!calendar.selectedDates) {
  82. calendar.selectedDates = []
  83. }
  84. if (dates[dateIndex].choosed) {
  85. calendar.selectedDates.push(dates[dateIndex])
  86. } else {
  87. calendar.selectedDates = calendar.selectedDates.filter(
  88. date =>
  89. dateUtil.toTimeStr(date) !== dateUtil.toTimeStr(dates[dateIndex])
  90. )
  91. }
  92. }
  93. return {
  94. calendarData: {
  95. ...calendar,
  96. dates
  97. },
  98. calendarConfig
  99. }
  100. },
  101. onSwitchCalendar(date, calendarData = {}, component) {
  102. const calendarConfig = getCalendarConfig(component)
  103. if (calendarConfig.weekMode) {
  104. return calendarData
  105. }
  106. const updatedRenderData = calcJumpData({
  107. dateInfo: date,
  108. config: calendarConfig
  109. })
  110. return {
  111. ...calendarData,
  112. ...updatedRenderData
  113. }
  114. },
  115. methods(component) {
  116. return {
  117. jump: dateInfo => {
  118. if (Object.prototype.toString.call(dateInfo) !== '[object Object]')
  119. return
  120. const updatedRenderData = calcJumpData({
  121. dateInfo,
  122. component
  123. })
  124. const existCalendarData = getCalendarData('calendar', component)
  125. const config = getCalendarConfig(component)
  126. if (config.autoChoosedWhenJump) {
  127. const target = updatedRenderData.dates[dateInfo.date - 1]
  128. if (!updatedRenderData.selectedDates) {
  129. updatedRenderData.selectedDates = [target]
  130. } else {
  131. updatedRenderData.selectedDates.push(target)
  132. }
  133. }
  134. return renderCalendar.call(component, {
  135. ...existCalendarData,
  136. ...updatedRenderData
  137. })
  138. },
  139. getCalendarConfig() {
  140. return getCalendarConfig(component)
  141. },
  142. setCalendarConfig(config) {
  143. return new Promise((resolve, reject) => {
  144. if (!component || !component.data.config) {
  145. reject('异常:未找到组件配置信息')
  146. return
  147. }
  148. let conf = { ...component.config, ...config }
  149. component.config = conf
  150. setCalendarData({ config: conf }, component)
  151. .then(resolve)
  152. .catch(reject)
  153. })
  154. },
  155. cancelSelectedDates(cancelDates = []) {
  156. const existCalendarData = getCalendarData('calendar', component) || {}
  157. const { dates = [], selectedDates = [] } = existCalendarData
  158. let updatedRenderData = {}
  159. const config = getCalendarConfig(component)
  160. let chooseAreaData = {}
  161. if (config.chooseAreaMode) {
  162. chooseAreaData = {
  163. chooseAreaTimestamp: [],
  164. tempChooseAreaTimestamp: []
  165. }
  166. }
  167. if (!cancelDates.length) {
  168. dates.forEach(item => {
  169. item.choosed = false
  170. })
  171. updatedRenderData = {
  172. dates,
  173. selectedDates: []
  174. }
  175. } else {
  176. const cancelDatesStr = cancelDates.map(date =>
  177. dateUtil.toTimeStr(date)
  178. )
  179. const filterSelectedDates = selectedDates.filter(
  180. date => !cancelDatesStr.includes(dateUtil.toTimeStr(date))
  181. )
  182. dates.forEach(date => {
  183. if (cancelDatesStr.includes(dateUtil.toTimeStr(date))) {
  184. date.choosed = false
  185. }
  186. })
  187. updatedRenderData = {
  188. dates,
  189. selectedDates: filterSelectedDates
  190. }
  191. }
  192. return renderCalendar.call(component, {
  193. ...existCalendarData,
  194. ...updatedRenderData,
  195. ...chooseAreaData
  196. })
  197. },
  198. setSelectedDates: targetDates => {
  199. const existCalendarData = getCalendarData('calendar', component)
  200. let { dates, selectedDates = [] } = existCalendarData || {}
  201. let __selectedDates = []
  202. let __dates = dates
  203. if (!targetDates) {
  204. __dates = dates.map(item => {
  205. const date = { ...item }
  206. date.choosed = true
  207. if (existCalendarData.showLabelAlways && date.showTodoLabel) {
  208. date.showTodoLabel = true
  209. } else {
  210. date.showTodoLabel = false
  211. }
  212. return date
  213. })
  214. __selectedDates = dates
  215. } else if (targetDates && targetDates.length) {
  216. const allSelected = dateUtil.uniqueArrayByDate(
  217. selectedDates.concat(targetDates)
  218. )
  219. const allSelectedDateStr = allSelected.map(d =>
  220. dateUtil.toTimeStr(d)
  221. )
  222. __dates = dates.map(item => {
  223. const date = { ...item }
  224. if (allSelectedDateStr.includes(dateUtil.toTimeStr(date))) {
  225. date.choosed = true
  226. __selectedDates.push(date)
  227. }
  228. if (existCalendarData.showLabelAlways && date.showTodoLabel) {
  229. date.showTodoLabel = true
  230. } else {
  231. date.showTodoLabel = false
  232. }
  233. return date
  234. })
  235. }
  236. return renderCalendar.call(component, {
  237. ...existCalendarData,
  238. dates: __dates,
  239. selectedDates: __selectedDates
  240. })
  241. },
  242. setDateStyle: toSetDates => {
  243. if (!Array.isArray(toSetDates)) return Promise.reject()
  244. const existCalendarData = getCalendarData('calendar', component)
  245. const { dates = [], specialStyleDates } = existCalendarData || {}
  246. if (Array.isArray(specialStyleDates)) {
  247. toSetDates = dateUtil.uniqueArrayByDate([
  248. ...specialStyleDates,
  249. ...toSetDates
  250. ])
  251. }
  252. const toSetDatesStr = toSetDates.map(item => dateUtil.toTimeStr(item))
  253. const _dates = dates.map(item => {
  254. const idx = toSetDatesStr.indexOf(dateUtil.toTimeStr(item))
  255. if (idx > -1) {
  256. return {
  257. ...item,
  258. class: toSetDates[idx].class
  259. }
  260. } else {
  261. return item
  262. }
  263. })
  264. return renderCalendar.call(component, {
  265. ...existCalendarData,
  266. dates: _dates,
  267. specialStyleDates: toSetDates
  268. })
  269. }
  270. }
  271. }
  272. }
  273. }