calendar.vue 3.9 KB

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