calendar.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div class="demo-app">
  3. <FullCalendar
  4. ref="fullCalendar"
  5. locale="zh-cn"
  6. firstDay="1"
  7. :editable="false"
  8. :droppable="false"
  9. :eventLimit="eventLimit"
  10. :defaultDate="defaultDate"
  11. weekNumberCalculation="ISO"
  12. :customButtons="btn"
  13. :header="headers"
  14. :button-text="{
  15. today: '今天',
  16. dayGridMonth: '月视图',
  17. dayGridWeek: '周视图',
  18. listMonth: '月列表',
  19. }"
  20. :valid-range="{
  21. start: rangeStart,
  22. end: rangeEnd,
  23. }"
  24. :slot-event-overlap="false"
  25. :defaultView="view"
  26. :plugins="calendarPlugins"
  27. :selectable="useDraft"
  28. :events="event"
  29. @select="drawSelect"
  30. :eventOrder="`start`"
  31. @eventClick="clickEvent"
  32. :views="views"
  33. :displayEventEnd="true"
  34. :displayEventTime="true"
  35. />
  36. </div>
  37. </template>
  38. <script>
  39. import FullCalendar from '@fullcalendar/vue';
  40. import dayGridPlugin from '@fullcalendar/daygrid';
  41. import listPlugin from '@fullcalendar/list';
  42. import timeGridPlugin from '@fullcalendar/timegrid';
  43. import interactionPlugin from '@fullcalendar/interaction';
  44. var moment = require('moment');
  45. import _ from 'lodash';
  46. export default {
  47. props: {
  48. year: { type: null, default: `${new Date().getFullYear()}` }, //当前日历的年份
  49. events: { type: Array, default: () => [] },
  50. useDraft: { type: Boolean, default: true },
  51. useEvent: { type: Boolean, default: true },
  52. selfBtn: { type: Object, default: () => {} },
  53. eventLimit: { type: null, default: false },
  54. vacation: { type: Array, default: () => [] },
  55. },
  56. components: {
  57. FullCalendar, // make the <FullCalendar> tag available
  58. },
  59. data: function() {
  60. var that = this;
  61. return {
  62. view: 'dayGridMonth',
  63. form: { isAllDay: true },
  64. dialog: false,
  65. calendarPlugins: [
  66. // plugins must be defined in the JS
  67. dayGridPlugin,
  68. listPlugin,
  69. timeGridPlugin,
  70. interactionPlugin, // needed for dateClick
  71. ],
  72. planList: [],
  73. addTitle: '',
  74. headers: {
  75. left: 'prev',
  76. center: 'title',
  77. right: 'dayGridMonth,dayGridWeek, today ,next',
  78. },
  79. btn: {},
  80. views: {
  81. dayGridMonth: {
  82. eventLimit: _.clone(this.eventLimit),
  83. },
  84. dayGridWeek: {
  85. duration: { day: 7 },
  86. },
  87. listMonth: {
  88. eventLimit: _.clone(this.eventLimit),
  89. },
  90. },
  91. };
  92. },
  93. methods: {
  94. handleDateClick(arg) {
  95. this.$emit('click', arg);
  96. },
  97. drawSelect(arg) {
  98. arg.endStr = moment(arg.endStr)
  99. .subtract(1, 'days')
  100. .format('YYYY-MM-DD');
  101. // let is_possbile = this.$checkDate({ ...arg, vacation: this.vacation });
  102. let is_possbile = this.$checkDate(arg.startStr, arg.endStr, this.vacation);
  103. if (is_possbile == true) this.$emit('draft', arg);
  104. else this.$message.error('您选择的时间段与假期重合,请您重新选择');
  105. },
  106. clickEvent(arg) {
  107. if (this.useEvent) this.$emit('eventClick', arg);
  108. },
  109. setBtn() {
  110. let selfBtn = _.cloneDeep(this.selfBtn);
  111. let keys = Object.keys(selfBtn);
  112. for (const key of keys) {
  113. let { position, ...info } = selfBtn[key];
  114. selfBtn[key] = { ...info };
  115. if (position === 'left') this.headers.left = `${this.headers.left}, ${key}`;
  116. else this.headers.right = `${key}, ${this.headers.right} `;
  117. }
  118. this.$set(this, `btn`, selfBtn);
  119. },
  120. },
  121. computed: {
  122. defaultDate() {
  123. return `${this.year}-01-01`;
  124. },
  125. rangeStart() {
  126. return `${this.year}-01-01`;
  127. },
  128. rangeEnd() {
  129. return `${this.year}-12-31`;
  130. },
  131. event() {
  132. let nv = this.vacation.map(i => {
  133. let object = JSON.parse(JSON.stringify(i));
  134. object.end = moment(i.end)
  135. .add(1, 'days')
  136. .format('YYYY-MM-DD');
  137. return object;
  138. });
  139. let ne = this.events.map(i => {
  140. let object = JSON.parse(JSON.stringify(i));
  141. object.end = moment(i.end)
  142. .add(1, 'days')
  143. .format('YYYY-MM-DD');
  144. return object;
  145. });
  146. return nv.concat(ne);
  147. },
  148. },
  149. watch: {
  150. selfBtn: {
  151. immediate: true,
  152. handler(val) {
  153. if (val) this.setBtn();
  154. },
  155. },
  156. },
  157. };
  158. </script>
  159. <style lang="less">
  160. @import '~@fullcalendar/core/main.css';
  161. @import '~@fullcalendar/daygrid/main.css';
  162. </style>