body.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <template>
  2. <div class="full-calendar-body">
  3. <div class="right-body">
  4. <div class="weeks">
  5. <div class="blank" v-if="tableType=='week'" style="width: 60px"></div>
  6. <strong class="week" v-for="(week,index) in weekNames">
  7. {{week}}
  8. <span v-if="tableType=='week' && weekDate.length">({{weekDate[index].showDate}})</span>
  9. </strong>
  10. </div>
  11. <div class="dates" ref="dates" v-if="tableType=='month'">
  12. <Spin v-if="loading" fix></Spin>
  13. <!-- absolute so we can make dynamic td -->
  14. <div class="dates-events">
  15. <div class="events-week" v-for="week in currentDates"
  16. v-if="week[0].isCurMonth || week[week.length-1].isCurMonth">
  17. <div class="events-day" v-for="day in week" track-by="$index"
  18. :class="{'today': day.isToday, 'not-cur-month': !day.isCurMonth}">
  19. <p class="day-number">{{day.monthDay}}</p>
  20. <div class="event-box" v-if="day.events.length">
  21. <div class="event-item"
  22. v-for="event in day.events"
  23. :class="{selected: showCard == event.id}"
  24. :style="{
  25. 'color': event.color||'#333333',
  26. 'background-color': showCard == event.id ? (event.selectedColor||'#C7E6FD') : (event.backgroundColor||'#C7E6FD'),
  27. }"
  28. @click="eventClick(event,$event)">
  29. <img class="avatar" v-if="event.avatar" :src="event.avatar" @error="($set(event, 'avatar', null))"/>
  30. <span v-else class="icon" :style="iconStyle(event.name)">{{event.name}}</span>
  31. <p class="info" v-html="isBegin(event, day.date, day.weekDay)"></p>
  32. <div id="card" :class="cardClass" v-if="$slots['body-card'] && event && showCard == event.id" @click.stop>
  33. <slot name="body-card"></slot>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </div>
  41. <div class="time" ref="time" v-else-if="tableType=='week'">
  42. <Spin v-if="loading" fix></Spin>
  43. <!-- <div class="column" v-for="day in weekDate" v-if="weekDate.length">
  44. <div class="single" v-for="time in timeDivide"></div>
  45. </div> -->
  46. <div class="row" v-for="(time,index) in timeDivide">
  47. <div class="left-info" v-if="tableType=='week'">
  48. <div class="time-info first" v-if="index==0">
  49. <span class="center">{{$L('上午')}}</span>
  50. </div>
  51. <div class="time-info" v-if="index==1">
  52. <span class="top">12:00</span>
  53. <span class="center">{{$L('下午')}}</span>
  54. </div>
  55. <div class="time-info" v-if="index==2">
  56. <span class="top">18:00</span>
  57. <span class="center">{{$L('晚上')}}</span>
  58. </div>
  59. </div>
  60. <div class="events-day" v-for="item in weekDate" v-if="weekDate.length"
  61. :class="{today: item.isToday}">
  62. <div class="event-box" v-if="item.events.length">
  63. <div class="event-item"
  64. v-for="event in item.events"
  65. :class="{selected: showCard == event.id}"
  66. :style="{
  67. 'color': event.color||'#333333',
  68. 'background-color': showCard == event.id ? (event.selectedColor||'#C7E6FD') : (event.backgroundColor||'#C7E6FD'),
  69. }"
  70. v-if="isTheday(item.date, event.start) && isInTime(time, event.start)"
  71. @click="eventClick(event,$event)">
  72. <img class="avatar" v-if="event.avatar" :src="event.avatar" @error="($set(event, 'avatar', null))"/>
  73. <span v-else class="icon" :style="iconStyle(event.name)">{{event.name}}</span>
  74. <p class="info" v-html="event.title"></p>
  75. <div id="card" :class="cardClass" v-if="$slots['body-card'] && event && showCard == event.id" @click.stop>
  76. <slot name="body-card"></slot>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </template>
  86. <script type="text/babel">
  87. import dateFunc from './dateFunc'
  88. import bus from './bus'
  89. export default {
  90. props: {
  91. currentDate: {},
  92. events: {
  93. type: Array
  94. },
  95. weekNames: {
  96. type: Array,
  97. default: []
  98. },
  99. monthNames: {},
  100. firstDay: {},
  101. tableType: '',
  102. weekDays: {
  103. type: Array,
  104. default() {
  105. return dateFunc.getDates(new Date())
  106. }
  107. },
  108. isLimit: {
  109. type: Boolean,
  110. default: false
  111. },
  112. loading: {
  113. type: Boolean,
  114. default: true
  115. }
  116. },
  117. created() {
  118. let _this = this
  119. document.addEventListener('click', function (e) {
  120. _this.showCard = -1
  121. });
  122. // 监听header组件事件
  123. bus.$on('changeWeekDays', res => {
  124. });
  125. //
  126. this.initLanguage()
  127. },
  128. data() {
  129. return {
  130. // weekNames : DAY_NAMES,
  131. weekMask: [1, 2, 3, 4, 5, 6, 7],
  132. // events : [],
  133. eventLimit: 18,
  134. showMore: false,
  135. morePos: {
  136. top: 0,
  137. left: 0
  138. },
  139. selectDay: {},
  140. timeDivide: [],
  141. showCard: -1,
  142. cardLeft: 0,
  143. cardRight: 0,
  144. }
  145. },
  146. watch: {
  147. // events(val){
  148. // this.getCalendar()
  149. // },
  150. currentDate() {
  151. this.getCalendar()
  152. }
  153. },
  154. computed: {
  155. currentDates() {
  156. return this.getCalendar()
  157. },
  158. weekDate() {
  159. return this.getWeekDate()
  160. }
  161. },
  162. methods: {
  163. initLanguage() {
  164. this.timeDivide = [{
  165. start: 0,
  166. end: 12,
  167. label: this.$L('上午')
  168. }, {
  169. start: 12,
  170. end: 18,
  171. label: this.$L('下午')
  172. }, {
  173. start: 18,
  174. end: 23,
  175. label: this.$L('晚上')
  176. }];
  177. },
  178. isBegin(event, date, index) {
  179. let st = new Date(event.start)
  180. if (index == 0 || st.toDateString() == date.toDateString()) {
  181. return event.title
  182. }
  183. return ' '
  184. },
  185. moreTitle(date) {
  186. let dt = new Date(date)
  187. return this.weekNames[dt.getDay()] + ', ' + this.monthNames[dt.getMonth()] + dt.getDate()
  188. },
  189. classNames(cssClass) {
  190. if (!cssClass) return ''
  191. // string
  192. if (typeof cssClass == 'string') return cssClass
  193. // Array
  194. if (Array.isArray(cssClass)) return cssClass.join(' ')
  195. // else
  196. return ''
  197. },
  198. getCalendar() {
  199. // calculate 2d-array of each month
  200. // first day of this month
  201. let now = new Date() // today
  202. let current = new Date(this.currentDate)
  203. let startDate = dateFunc.getStartDate(current) // 1st day of this month
  204. let curWeekDay = startDate.getDay()
  205. // begin date of this table may be some day of last month
  206. let diff = parseInt(this.firstDay) - curWeekDay + 1
  207. diff = diff > 0 ? (diff - 7) : diff
  208. startDate.setDate(startDate.getDate() + diff)
  209. let calendar = []
  210. for (let perWeek = 0; perWeek < 6; perWeek++) {
  211. let week = []
  212. for (let perDay = 0; perDay < 7; perDay++) {
  213. // console.log(startDate)
  214. week.push({
  215. monthDay: startDate.getDate(),
  216. isToday: now.toDateString() == startDate.toDateString(),
  217. isCurMonth: startDate.getMonth() == current.getMonth(),
  218. weekDay: perDay,
  219. date: new Date(startDate),
  220. events: this.slotEvents(new Date(startDate))
  221. })
  222. startDate.setDate(startDate.getDate() + 1)
  223. }
  224. calendar.push(week)
  225. }
  226. return calendar
  227. },
  228. slotEvents(date) {
  229. // console.log(date)
  230. let thisDayEvents = []
  231. this.events.filter(event => {
  232. let day = new Date(event.start)
  233. if (date.toLocaleDateString() === day.toLocaleDateString()) {
  234. thisDayEvents.push(event)
  235. }
  236. })
  237. this.judgeTime(thisDayEvents)
  238. return thisDayEvents
  239. },
  240. // 获取周视图的天元素格式化
  241. getWeekDate() {
  242. let newWeekDays = this.weekDays
  243. newWeekDays.forEach((e, index) => {
  244. e.showDate = dateFunc.format(e, 'MM-dd');
  245. e.date = dateFunc.format(e, 'yyyy-MM-dd');
  246. e.isToday = (new Date().toDateString() == e.toDateString())
  247. e.events = this.slotTimeEvents(e) // 整理事件集合 (拿事件去比较时间,分发事件到时间插槽内)
  248. })
  249. return newWeekDays
  250. },
  251. // 发现该时间段所有的事件
  252. slotTimeEvents(date) {
  253. let thisDayEvents = this.events
  254. thisDayEvents.filter(event => {
  255. let day = new Date(event.start)
  256. return date.toLocaleDateString() == day.toLocaleDateString()
  257. })
  258. this.judgeTime(thisDayEvents)
  259. return thisDayEvents
  260. },
  261. judgeTime(arr) {
  262. arr.forEach(event => {
  263. let day = new Date(event.start)
  264. // 加上时间戳后判断时间段
  265. let hour = day.getHours()
  266. let week = day.getDay()
  267. week == 0 ? week = 7 : ''
  268. if (this.timeDivide[0].start < hour < this.timeDivide[0].end) {
  269. event.time = 0
  270. } else if (this.timeDivide[1].start < hour < this.timeDivide[1].end) {
  271. event.time = 1
  272. } else if (this.timeDivide[2].start < hour < this.timeDivide[2].end) {
  273. event.time = 2
  274. }
  275. event.weekDay = this.weekNames[Number(week) - 1]
  276. event.weekDayIndex = week
  277. })
  278. },
  279. isTheday(day1, day2) {
  280. return new Date(day1).toDateString() === new Date(day2).toDateString()
  281. },
  282. isStart(eventDate, date) {
  283. let st = new Date(eventDate)
  284. return st.toDateString() == date.toDateString()
  285. },
  286. isEnd(eventDate, date) {
  287. let ed = new Date(eventDate)
  288. return ed.toDateString() == date.toDateString()
  289. },
  290. isInTime(time, date) {
  291. let hour = new Date(date).getHours()
  292. return (time.start <= hour) && (hour < time.end)
  293. },
  294. selectThisDay(day, jsEvent) {
  295. this.selectDay = day
  296. this.showMore = true
  297. this.morePos = this.computePos(event.target)
  298. this.morePos.top -= 100
  299. let events = day.events.filter(item => {
  300. return item.isShow == true
  301. })
  302. this.$emit('moreclick', day.date, events, jsEvent)
  303. },
  304. computePos(target) {
  305. let eventRect = target.getBoundingClientRect()
  306. let pageRect = this.$refs.dates.getBoundingClientRect()
  307. return {
  308. left: eventRect.left - pageRect.left,
  309. top: eventRect.top + eventRect.height - pageRect.top
  310. }
  311. },
  312. dayClick(day, jsEvent) {
  313. // console.log('dayClick')
  314. // this.$emit('dayclick', day, jsEvent)
  315. },
  316. eventClick(event, jsEvent) {
  317. // console.log(event,jsEvent, 'evenvet')
  318. this.showCard = event.id
  319. jsEvent.stopPropagation()
  320. // let pos = this.computePos(jsEvent.target)
  321. this.$emit('eventclick', event, jsEvent)
  322. let x = jsEvent.x
  323. let y = jsEvent.y
  324. // console.log(jsEvent)
  325. // 判断出左右中三边界的取值范围进而分配class
  326. if (x > 400 && x < window.innerWidth - 200) {
  327. this.cardClass = "center-card"
  328. } else if (x <= 400) {
  329. this.cardClass = "left-card"
  330. } else {
  331. this.cardClass = "right-card"
  332. }
  333. if (y > window.innerHeight - 300) {
  334. this.cardClass += ' ' + 'bottom-card'
  335. }
  336. },
  337. isEmojiPrefix(text) {
  338. return /^[\uD800-\uDBFF][\uDC00-\uDFFF]/.test(text);
  339. },
  340. iconStyle(name) {
  341. const style = {
  342. backgroundColor: '#A0D7F1'
  343. };
  344. if (name) {
  345. let bgColor = '';
  346. for (let i = 0; i < name.length; i++) {
  347. bgColor += parseInt(name[i].charCodeAt(0), 10).toString(16);
  348. }
  349. style.backgroundColor = '#' + bgColor.slice(1, 4);
  350. }
  351. return style;
  352. }
  353. }
  354. }
  355. </script>
  356. <style lang="scss">
  357. .full-calendar-body {
  358. background-color: #fff;
  359. display: flex;
  360. margin-top: 12px;
  361. border-radius: 4px;
  362. border: 1px solid #EEEEEE;
  363. .left-info {
  364. width: 60px;
  365. .time-info {
  366. height: 100%;
  367. position: relative;
  368. &:nth-child(2) {
  369. border-top: 1px solid #EFF2FF;
  370. border-bottom: 1px solid #EFF2FF;
  371. }
  372. .center {
  373. position: absolute;
  374. top: 50%;
  375. left: 50%;
  376. transform: translate(-50%, -50%);
  377. width: 14px;
  378. font-size: 14px;
  379. word-wrap: break-word;
  380. letter-spacing: 10px;
  381. }
  382. .top {
  383. position: absolute;
  384. top: -8px;
  385. width: 100%;
  386. text-align: center;
  387. }
  388. }
  389. }
  390. .right-body {
  391. flex: 1;
  392. width: 100%;
  393. position: relative;
  394. .weeks {
  395. display: flex;
  396. border-bottom: 1px solid #EEEEEE;
  397. .week {
  398. flex: 1;
  399. text-align: center;
  400. height: 40px;
  401. display: flex;
  402. align-items: center;
  403. justify-content: center;
  404. font-weight: normal;
  405. }
  406. }
  407. .dates {
  408. position: relative;
  409. .dates-events {
  410. z-index: 1;
  411. width: 100%;
  412. .events-week {
  413. display: flex;
  414. min-height: 180px;
  415. .events-day {
  416. text-overflow: ellipsis;
  417. flex: 1;
  418. width: 0;
  419. height: auto;
  420. padding: 4px;
  421. border-right: 1px solid #EFF2FF;
  422. border-bottom: 1px solid #EFF2FF;
  423. background-color: #fff;
  424. .day-number {
  425. text-align: left;
  426. padding: 4px 5px 4px 4px;
  427. }
  428. &.not-cur-month {
  429. .day-number {
  430. color: #ECECED;
  431. }
  432. }
  433. &.today {
  434. background-color: #F9FAFB;
  435. }
  436. &:last-child {
  437. border-right: 0;
  438. }
  439. .event-box {
  440. max-height: 280px;
  441. overflow: auto;
  442. .event-item {
  443. cursor: pointer;
  444. font-size: 12px;
  445. background-color: #C7E6FD;
  446. margin-bottom: 4px;
  447. color: rgba(0, 0, 0, .87);
  448. padding: 6px 0 6px 4px;
  449. height: auto;
  450. line-height: 30px;
  451. display: flex;
  452. // transform:translate(0,0);
  453. align-items: flex-start;
  454. position: relative;
  455. border-radius: 4px;
  456. &.is-end {
  457. display: none;
  458. }
  459. &.is-start {
  460. display: block;
  461. }
  462. &.is-opacity {
  463. display: none;
  464. }
  465. .avatar {
  466. width: 18px;
  467. height: 18px;
  468. border: 0;
  469. border-radius: 50%;
  470. overflow: hidden;
  471. display: inline-block;
  472. }
  473. .icon {
  474. width: 18px;
  475. height: 18px;
  476. line-height: 18px;
  477. border-radius: 10px;
  478. text-align: center;
  479. color: #fff;
  480. display: inline-block;
  481. overflow: hidden;
  482. }
  483. .info {
  484. width: calc(100% - 30px);
  485. display: inline-block;
  486. margin-left: 5px;
  487. line-height: 18px;
  488. word-break: break-all;
  489. word-wrap: break-word;
  490. font-size: 12px;
  491. }
  492. #card {
  493. cursor: initial;
  494. position: absolute;
  495. z-index: 999;
  496. min-width: 250px;
  497. height: auto;
  498. left: 50%;
  499. top: calc(100% + 10px);
  500. transform: translate(-50%, 0);
  501. min-height: 100px;
  502. background: #fff;
  503. // border: 1px solid #eee;
  504. box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.1);
  505. border-radius: 4px;
  506. overflow: hidden;
  507. &.left-card {
  508. left: 0;
  509. transform: translate(0, 0);
  510. }
  511. &.right-card {
  512. right: 0;
  513. left: auto;
  514. transform: translate(0, 0);
  515. }
  516. &.bottom-card {
  517. top: auto;
  518. bottom: calc(100% + 10px);
  519. }
  520. }
  521. &:hover {
  522. .info {
  523. // font-weight: bold;
  524. }
  525. }
  526. &.selected {
  527. /*.info {
  528. color: #fff;
  529. font-weight: normal;
  530. }
  531. .icon {
  532. background-color: transparent !important;
  533. }*/
  534. }
  535. }
  536. .more-link {
  537. cursor: pointer;
  538. // text-align: right;
  539. padding-left: 8px;
  540. padding-right: 2px;
  541. color: rgba(0, 0, 0, .38);
  542. font-size: 12px;
  543. }
  544. }
  545. }
  546. &:last-child {
  547. .events-day {
  548. border-bottom: 0;
  549. }
  550. }
  551. }
  552. }
  553. .more-events {
  554. position: absolute;
  555. width: 150px;
  556. z-index: 2;
  557. border: 1px solid #eee;
  558. box-shadow: 0 2px 6px rgba(0, 0, 0, .15);
  559. .more-header {
  560. background-color: #eee;
  561. padding: 5px;
  562. display: flex;
  563. align-items: center;
  564. font-size: 14px;
  565. .title {
  566. flex: 1;
  567. }
  568. .close {
  569. margin-right: 2px;
  570. cursor: pointer;
  571. font-size: 16px;
  572. }
  573. }
  574. .more-body {
  575. height: 125px;
  576. overflow: hidden;
  577. background: #fff;
  578. .body-list {
  579. height: 120px;
  580. padding: 5px;
  581. overflow: auto;
  582. background-color: #fff;
  583. .body-item {
  584. cursor: pointer;
  585. font-size: 12px;
  586. background-color: #C7E6FD;
  587. margin-bottom: 2px;
  588. color: rgba(0, 0, 0, .87);
  589. padding: 0 0 0 4px;
  590. height: 18px;
  591. line-height: 18px;
  592. white-space: nowrap;
  593. overflow: hidden;
  594. text-overflow: ellipsis;
  595. }
  596. }
  597. }
  598. }
  599. }
  600. .time {
  601. position: relative;
  602. .row {
  603. width: 100%;
  604. display: flex;
  605. min-height: 180px;
  606. .events-day {
  607. border-bottom: 1px solid #EFF2FF;
  608. border-left: 1px solid #EFF2FF;
  609. background-color: #fff;
  610. height: auto;
  611. text-overflow: ellipsis;
  612. flex: 1;
  613. width: 0;
  614. padding: 4px;
  615. .event-box {
  616. max-height: 280px;
  617. overflow: auto;
  618. }
  619. &.today {
  620. background-color: #F9FAFB;
  621. }
  622. }
  623. .event-item {
  624. cursor: pointer;
  625. font-size: 12px;
  626. background-color: #C7E6FD;
  627. margin-bottom: 4px;
  628. color: rgba(0, 0, 0, .87);
  629. padding: 6px 0 6px 4px;
  630. height: auto;
  631. line-height: 30px;
  632. display: flex;
  633. align-items: flex-start;
  634. position: relative;
  635. border-radius: 4px;
  636. .avatar {
  637. width: 18px;
  638. height: 18px;
  639. border: 0;
  640. border-radius: 50%;
  641. overflow: hidden;
  642. display: inline-block;
  643. }
  644. .icon {
  645. width: 18px;
  646. height: 18px;
  647. line-height: 18px;
  648. border-radius: 10px;
  649. text-align: center;
  650. color: #fff;
  651. display: inline-block;
  652. padding: 0 2px;
  653. overflow: hidden;
  654. }
  655. .info {
  656. width: calc(100% - 30px);
  657. display: inline-block;
  658. margin-left: 5px;
  659. line-height: 18px;
  660. word-break: break-all;
  661. word-wrap: break-word;
  662. font-size: 12px;
  663. }
  664. #card {
  665. cursor: initial;
  666. position: absolute;
  667. z-index: 999;
  668. min-width: 250px;
  669. height: auto;
  670. left: 50%;
  671. top: calc(100% + 10px);
  672. transform: translate(-50%, 0);
  673. min-height: 100px;
  674. background: #fff;
  675. box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.1);
  676. border-radius: 4px;
  677. overflow: hidden;
  678. &.left-card {
  679. left: 0;
  680. transform: translate(0, 0);
  681. }
  682. &.right-card {
  683. right: 0;
  684. left: auto;
  685. transform: translate(0, 0);
  686. }
  687. &.bottom-card {
  688. top: auto;
  689. bottom: calc(100% + 10px);
  690. }
  691. }
  692. &:hover {
  693. .info {
  694. // font-weight: bold;
  695. }
  696. }
  697. &.selected {
  698. /*.info {
  699. color: #fff;
  700. font-weight: normal;
  701. }
  702. // background-color: #5272FF !important;
  703. .icon {
  704. background-color: transparent !important;
  705. }*/
  706. }
  707. }
  708. &:last-child {
  709. .events-day {
  710. border-bottom: 0;
  711. }
  712. .single {
  713. border-bottom: 0;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. }
  720. </style>