header.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <div class="full-calendar-header">
  3. <div class="header-left">
  4. <slot name="header-left"></slot>
  5. </div>
  6. <div class="header-center">
  7. <span class="prev-month" @click.stop="goPrev">{{leftArrow}}</span>
  8. <span class="title">{{title}}</span>
  9. <span class="next-month" @click.stop="goNext">{{rightArrow}}</span>
  10. </div>
  11. <div class="header-right">
  12. <slot name="header-right"></slot>
  13. </div>
  14. </div>
  15. </template>
  16. <script type="text/babel">
  17. import dateFunc from './dateFunc'
  18. import bus from './bus'
  19. export default {
  20. created() {
  21. this.dispatchEvent(this.tableType)
  22. },
  23. props: {
  24. currentDate: {},
  25. titleFormat: {},
  26. firstDay: {},
  27. monthNames: {},
  28. tableType: ''
  29. },
  30. data() {
  31. return {
  32. title: '',
  33. leftArrow: '<',
  34. rightArrow: '>',
  35. headDate: new Date()
  36. }
  37. },
  38. watch: {
  39. currentDate(val) {
  40. if (!val) return
  41. this.headDate = val
  42. },
  43. tableType(val) {
  44. this.dispatchEvent(this.tableType)
  45. }
  46. },
  47. methods: {
  48. goPrev() {
  49. this.headDate = this.changeDateRange(this.headDate, -1)
  50. this.dispatchEvent(this.tableType)
  51. },
  52. goNext() {
  53. this.headDate = this.changeDateRange(this.headDate, 1)
  54. this.dispatchEvent(this.tableType)
  55. },
  56. changeDateRange(date, num) {
  57. let dt = new Date(date)
  58. if (this.tableType == 'month') {
  59. return new Date(dt.setMonth(dt.getMonth() + num))
  60. } else {
  61. return new Date(dt.valueOf() + num * 7 * 24 * 60 * 60 * 1000)
  62. }
  63. },
  64. dispatchEvent(type) {
  65. if (type == 'month') {
  66. this.title = dateFunc.format(this.headDate, this.titleFormat, this.monthNames)
  67. let startDate = dateFunc.getStartDate(this.headDate)
  68. let curWeekDay = startDate.getDay()
  69. // 1st day of this monthView
  70. let diff = parseInt(this.firstDay) - curWeekDay
  71. if (diff) diff -= 7
  72. startDate.setDate(startDate.getDate() + diff)
  73. // the month view is 6*7
  74. let endDate = dateFunc.changeDay(startDate, 41)
  75. // 1st day of current month
  76. let currentDate = dateFunc.getStartDate(this.headDate)
  77. this.$emit('change',
  78. dateFunc.format(startDate, 'yyyy-MM-dd'),
  79. dateFunc.format(endDate, 'yyyy-MM-dd'),
  80. dateFunc.format(currentDate, 'yyyy-MM-dd'),
  81. this.headDate
  82. )
  83. } else if (type == 'week') {
  84. let weekDays = dateFunc.getDates(this.headDate)
  85. this.title = dateFunc.format(weekDays[0], 'MM-dd') + ` 至 ` + dateFunc.format(weekDays[6], 'MM-dd')
  86. this.$emit('change',
  87. dateFunc.format(weekDays[0], 'yyyy-MM-dd'),
  88. dateFunc.format(weekDays[6], 'yyyy-MM-dd'),
  89. dateFunc.format(weekDays[0], 'yyyy-MM-dd'),
  90. this.headDate,
  91. weekDays
  92. )
  93. bus.$emit('changeWeekDays', weekDays)
  94. }
  95. },
  96. }
  97. }
  98. </script>
  99. <style lang="scss">
  100. .full-calendar-header {
  101. display: flex;
  102. align-items: center;
  103. .header-left, .header-right {
  104. flex: 1;
  105. }
  106. .header-center {
  107. flex: 3;
  108. text-align: center;
  109. user-select: none;
  110. font-weight: bold;
  111. .title {
  112. margin: 0 5px;
  113. width: 150px;
  114. }
  115. .prev-month, .next-month {
  116. cursor: pointer;
  117. padding: 10px 15px;
  118. }
  119. }
  120. }
  121. </style>