util.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. class Calendar {
  2. constructor({
  3. date,
  4. selected,
  5. startDate,
  6. endDate,
  7. range,
  8. // multipleStatus
  9. } = {}) {
  10. // 当前日期
  11. this.date = this.getDate(new Date()) // 当前初入日期
  12. // 打点信息
  13. this.selected = selected || [];
  14. // 范围开始
  15. this.startDate = startDate
  16. // 范围结束
  17. this.endDate = endDate
  18. this.range = range
  19. // 多选状态
  20. this.cleanMultipleStatus()
  21. // 每周日期
  22. this.weeks = {}
  23. // this._getWeek(this.date.fullDate)
  24. // this.multipleStatus = multipleStatus
  25. this.lastHover = false
  26. }
  27. /**
  28. * 设置日期
  29. * @param {Object} date
  30. */
  31. setDate(date) {
  32. this.selectDate = this.getDate(date)
  33. this._getWeek(this.selectDate.fullDate)
  34. }
  35. /**
  36. * 清理多选状态
  37. */
  38. cleanMultipleStatus() {
  39. this.multipleStatus = {
  40. before: '',
  41. after: '',
  42. data: []
  43. }
  44. }
  45. /**
  46. * 重置开始日期
  47. */
  48. resetSatrtDate(startDate) {
  49. // 范围开始
  50. this.startDate = startDate
  51. }
  52. /**
  53. * 重置结束日期
  54. */
  55. resetEndDate(endDate) {
  56. // 范围结束
  57. this.endDate = endDate
  58. }
  59. /**
  60. * 获取任意时间
  61. */
  62. getDate(date, AddDayCount = 0, str = 'day') {
  63. if (!date) {
  64. date = new Date()
  65. }
  66. if (typeof date !== 'object') {
  67. date = date.replace(/-/g, '/')
  68. }
  69. const dd = new Date(date)
  70. switch (str) {
  71. case 'day':
  72. dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
  73. break
  74. case 'month':
  75. if (dd.getDate() === 31) {
  76. dd.setDate(dd.getDate() + AddDayCount)
  77. } else {
  78. dd.setMonth(dd.getMonth() + AddDayCount) // 获取AddDayCount天后的日期
  79. }
  80. break
  81. case 'year':
  82. dd.setFullYear(dd.getFullYear() + AddDayCount) // 获取AddDayCount天后的日期
  83. break
  84. }
  85. const y = dd.getFullYear()
  86. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  87. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  88. return {
  89. fullDate: y + '-' + m + '-' + d,
  90. year: y,
  91. month: m,
  92. date: d,
  93. day: dd.getDay()
  94. }
  95. }
  96. /**
  97. * 获取上月剩余天数
  98. */
  99. _getLastMonthDays(firstDay, full) {
  100. let dateArr = []
  101. for (let i = firstDay; i > 0; i--) {
  102. const beforeDate = new Date(full.year, full.month - 1, -i + 1).getDate()
  103. dateArr.push({
  104. date: beforeDate,
  105. month: full.month - 1,
  106. disable: true
  107. })
  108. }
  109. return dateArr
  110. }
  111. /**
  112. * 获取本月天数
  113. */
  114. _currentMonthDys(dateData, full) {
  115. let dateArr = []
  116. let fullDate = this.date.fullDate
  117. for (let i = 1; i <= dateData; i++) {
  118. let isinfo = false
  119. let nowDate = full.year + '-' + (full.month < 10 ?
  120. full.month : full.month) + '-' + (i < 10 ?
  121. '0' + i : i)
  122. // 是否今天
  123. let isDay = fullDate === nowDate
  124. // 获取打点信息
  125. let info = this.selected && this.selected.find((item) => {
  126. if (this.dateEqual(nowDate, item.date)) {
  127. return item
  128. }
  129. })
  130. // 日期禁用
  131. let disableBefore = true
  132. let disableAfter = true
  133. if (this.startDate) {
  134. // let dateCompBefore = this.dateCompare(this.startDate, fullDate)
  135. // disableBefore = this.dateCompare(dateCompBefore ? this.startDate : fullDate, nowDate)
  136. disableBefore = this.dateCompare(this.startDate, nowDate)
  137. }
  138. if (this.endDate) {
  139. // let dateCompAfter = this.dateCompare(fullDate, this.endDate)
  140. // disableAfter = this.dateCompare(nowDate, dateCompAfter ? this.endDate : fullDate)
  141. disableAfter = this.dateCompare(nowDate, this.endDate)
  142. }
  143. let multiples = this.multipleStatus.data
  144. let checked = false
  145. let multiplesStatus = -1
  146. if (this.range) {
  147. if (multiples) {
  148. multiplesStatus = multiples.findIndex((item) => {
  149. return this.dateEqual(item, nowDate)
  150. })
  151. }
  152. if (multiplesStatus !== -1) {
  153. checked = true
  154. }
  155. }
  156. let data = {
  157. fullDate: nowDate,
  158. year: full.year,
  159. date: i,
  160. multiple: this.range ? checked : false,
  161. beforeMultiple: this.isLogicBefore(nowDate, this.multipleStatus.before, this.multipleStatus.after),
  162. afterMultiple: this.isLogicAfter(nowDate, this.multipleStatus.before, this.multipleStatus.after),
  163. month: full.month,
  164. disable: !(disableBefore && disableAfter),
  165. isDay,
  166. userChecked: false
  167. }
  168. if (info) {
  169. data.extraInfo = info
  170. }
  171. dateArr.push(data)
  172. }
  173. return dateArr
  174. }
  175. /**
  176. * 获取下月天数
  177. */
  178. _getNextMonthDays(surplus, full) {
  179. let dateArr = []
  180. for (let i = 1; i < surplus + 1; i++) {
  181. dateArr.push({
  182. date: i,
  183. month: Number(full.month) + 1,
  184. disable: true
  185. })
  186. }
  187. return dateArr
  188. }
  189. /**
  190. * 获取当前日期详情
  191. * @param {Object} date
  192. */
  193. getInfo(date) {
  194. if (!date) {
  195. date = new Date()
  196. }
  197. const dateInfo = this.canlender.find(item => item.fullDate === this.getDate(date).fullDate)
  198. return dateInfo
  199. }
  200. /**
  201. * 比较时间大小
  202. */
  203. dateCompare(startDate, endDate) {
  204. // 计算截止时间
  205. startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
  206. // 计算详细项的截止时间
  207. endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
  208. if (startDate <= endDate) {
  209. return true
  210. } else {
  211. return false
  212. }
  213. }
  214. /**
  215. * 比较时间是否相等
  216. */
  217. dateEqual(before, after) {
  218. // 计算截止时间
  219. before = new Date(before.replace('-', '/').replace('-', '/'))
  220. // 计算详细项的截止时间
  221. after = new Date(after.replace('-', '/').replace('-', '/'))
  222. if (before.getTime() - after.getTime() === 0) {
  223. return true
  224. } else {
  225. return false
  226. }
  227. }
  228. /**
  229. * 比较真实起始日期
  230. */
  231. isLogicBefore(currentDay, before, after) {
  232. let logicBefore = before
  233. if (before && after) {
  234. logicBefore = this.dateCompare(before, after) ? before : after
  235. }
  236. return this.dateEqual(logicBefore, currentDay)
  237. }
  238. isLogicAfter(currentDay, before, after) {
  239. let logicAfter = after
  240. if (before && after) {
  241. logicAfter = this.dateCompare(before, after) ? after : before
  242. }
  243. return this.dateEqual(logicAfter, currentDay)
  244. }
  245. /**
  246. * 获取日期范围内所有日期
  247. * @param {Object} begin
  248. * @param {Object} end
  249. */
  250. geDateAll(begin, end) {
  251. var arr = []
  252. var ab = begin.split('-')
  253. var ae = end.split('-')
  254. var db = new Date()
  255. db.setFullYear(ab[0], ab[1] - 1, ab[2])
  256. var de = new Date()
  257. de.setFullYear(ae[0], ae[1] - 1, ae[2])
  258. var unixDb = db.getTime() - 24 * 60 * 60 * 1000
  259. var unixDe = de.getTime() - 24 * 60 * 60 * 1000
  260. for (var k = unixDb; k <= unixDe;) {
  261. k = k + 24 * 60 * 60 * 1000
  262. arr.push(this.getDate(new Date(parseInt(k))).fullDate)
  263. }
  264. return arr
  265. }
  266. /**
  267. * 获取多选状态
  268. */
  269. setMultiple(fullDate) {
  270. let {
  271. before,
  272. after
  273. } = this.multipleStatus
  274. if (!this.range) return
  275. if (before && after) {
  276. if (!this.lastHover) {
  277. this.lastHover = true
  278. return
  279. }
  280. this.multipleStatus.before = fullDate
  281. this.multipleStatus.after = ''
  282. this.multipleStatus.data = []
  283. this.multipleStatus.fulldate = ''
  284. this.lastHover = false
  285. } else {
  286. if (!before) {
  287. this.multipleStatus.before = fullDate
  288. this.lastHover = false
  289. } else {
  290. this.multipleStatus.after = fullDate
  291. if (this.dateCompare(this.multipleStatus.before, this.multipleStatus.after)) {
  292. this.multipleStatus.data = this.geDateAll(this.multipleStatus.before, this.multipleStatus
  293. .after);
  294. } else {
  295. this.multipleStatus.data = this.geDateAll(this.multipleStatus.after, this.multipleStatus
  296. .before);
  297. }
  298. this.lastHover = true
  299. }
  300. }
  301. this._getWeek(fullDate)
  302. }
  303. /**
  304. * 鼠标 hover 更新多选状态
  305. */
  306. setHoverMultiple(fullDate) {
  307. let {
  308. before,
  309. after
  310. } = this.multipleStatus
  311. if (!this.range) return
  312. if (this.lastHover) return
  313. if (!before) {
  314. this.multipleStatus.before = fullDate
  315. } else {
  316. this.multipleStatus.after = fullDate
  317. if (this.dateCompare(this.multipleStatus.before, this.multipleStatus.after)) {
  318. this.multipleStatus.data = this.geDateAll(this.multipleStatus.before, this.multipleStatus.after);
  319. } else {
  320. this.multipleStatus.data = this.geDateAll(this.multipleStatus.after, this.multipleStatus.before);
  321. }
  322. }
  323. this._getWeek(fullDate)
  324. }
  325. /**
  326. * 更新默认值多选状态
  327. */
  328. setDefaultMultiple(before, after) {
  329. this.multipleStatus.before = before
  330. this.multipleStatus.after = after
  331. if (before && after) {
  332. if (this.dateCompare(before, after)) {
  333. this.multipleStatus.data = this.geDateAll(before, after);
  334. this._getWeek(after)
  335. } else {
  336. this.multipleStatus.data = this.geDateAll(after, before);
  337. this._getWeek(before)
  338. }
  339. }
  340. }
  341. /**
  342. * 获取每周数据
  343. * @param {Object} dateData
  344. */
  345. _getWeek(dateData) {
  346. const {
  347. fullDate,
  348. year,
  349. month,
  350. date,
  351. day
  352. } = this.getDate(dateData)
  353. let firstDay = new Date(year, month - 1, 1).getDay()
  354. let currentDay = new Date(year, month, 0).getDate()
  355. let dates = {
  356. lastMonthDays: this._getLastMonthDays(firstDay, this.getDate(dateData)), // 上个月末尾几天
  357. currentMonthDys: this._currentMonthDys(currentDay, this.getDate(dateData)), // 本月天数
  358. nextMonthDays: [], // 下个月开始几天
  359. weeks: []
  360. }
  361. let canlender = []
  362. const surplus = 42 - (dates.lastMonthDays.length + dates.currentMonthDys.length)
  363. dates.nextMonthDays = this._getNextMonthDays(surplus, this.getDate(dateData))
  364. canlender = canlender.concat(dates.lastMonthDays, dates.currentMonthDys, dates.nextMonthDays)
  365. let weeks = {}
  366. // 拼接数组 上个月开始几天 + 本月天数+ 下个月开始几天
  367. for (let i = 0; i < canlender.length; i++) {
  368. if (i % 7 === 0) {
  369. weeks[parseInt(i / 7)] = new Array(7)
  370. }
  371. weeks[parseInt(i / 7)][i % 7] = canlender[i]
  372. }
  373. this.canlender = canlender
  374. this.weeks = weeks
  375. }
  376. //静态方法
  377. // static init(date) {
  378. // if (!this.instance) {
  379. // this.instance = new Calendar(date);
  380. // }
  381. // return this.instance;
  382. // }
  383. }
  384. export default Calendar