calendar.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. let is_possbile = this.$checkDate(arg.startStr, arg.endStr, this.vacation);
  90. if (is_possbile == true) this.$emit('draft', arg);
  91. else this.$message.error('您选择的时间段与假期重合,请您重新选择');
  92. },
  93. clickEvent(arg) {
  94. if (this.useEvent) this.$emit('eventClick', arg);
  95. },
  96. setBtn() {
  97. let selfBtn = _.cloneDeep(this.selfBtn);
  98. let keys = Object.keys(selfBtn);
  99. for (const key of keys) {
  100. let { position, ...info } = selfBtn[key];
  101. selfBtn[key] = { ...info };
  102. if (position === 'left') this.headers.left = `${this.headers.left}, ${key}`;
  103. else this.headers.right = `${this.headers.right}, ${key}`;
  104. }
  105. this.$set(this, `btn`, selfBtn);
  106. },
  107. },
  108. computed: {
  109. defaultDate() {
  110. return `${this.year}-01-01`;
  111. },
  112. rangeStart() {
  113. return `${this.year}-01-01`;
  114. },
  115. rangeEnd() {
  116. return `${this.year}-12-31`;
  117. },
  118. event() {
  119. let nv = this.vacation.map(i => {
  120. let object = JSON.parse(JSON.stringify(i));
  121. object.end = moment(i.end)
  122. .add(1, 'days')
  123. .format('YYYY-MM-DD');
  124. return object;
  125. });
  126. let ne = this.events.map(i => {
  127. let object = JSON.parse(JSON.stringify(i));
  128. object.end = moment(i.end)
  129. .add(1, 'days')
  130. .format('YYYY-MM-DD');
  131. return object;
  132. });
  133. return nv.concat(ne);
  134. },
  135. },
  136. watch: {
  137. selfBtn: {
  138. immediate: true,
  139. handler(val) {
  140. if (val) this.setBtn();
  141. },
  142. },
  143. },
  144. };
  145. </script>
  146. <style lang="less">
  147. @import '~@fullcalendar/core/main.css';
  148. @import '~@fullcalendar/daygrid/main.css';
  149. </style>