app-calendar.vue 3.2 KB
Newer Older
ibizdev's avatar
ibizdev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<template>
    <FullCalendar ref="appCalendar" height="parent" :locales="locales" locale="zh-cn" :header="header" class="app-calendar" :events="items" defaultView="dayGridMonth" :plugins="calendarPlugins" @eventClick="onClick"/>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";
import FullCalendar from '@fullcalendar/vue';
import dayGridPlugin from '@fullcalendar/daygrid';
import allLocales from '@fullcalendar/core/locales-all';

import '@fullcalendar/core/main.css';
import '@fullcalendar/daygrid/main.css';

@Component({
    components: {
        FullCalendar
    }
})
export default class AppCalendar extends Vue {

    /**
     * 数据集
     *
     * @type {string}
     * @memberof AppCalendar
     */
    @Prop() datas?: string;

    /**
     * 类型
     *
     * @type {string}
     * @memberof aaa
     */
    @Prop() type?: string;

    /**
     * 数据集对象
     *
     * @protected
     * @type {any[]}
     * @memberof AppCalendar
     */
    protected items: any[] = [];

    /**
     * 头部设置
     *
     * @protected
     * @memberof AppCalendar
     */
    protected header: any = {
        left: 'prev,next today',
        center: 'title',
        right: false
    };

    /**
     * 语言资源
     *
     * @protected
     * @memberof AppCalendar
     */
    public locales = allLocales;

    /**
     * 日历插件
     *
     * @protected
     * @type {any[]}
     * @memberof aaa
     */
    protected calendarPlugins: any[] = [dayGridPlugin];

    /**
     * 数据集监听
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppCalendar
     */
    @Watch('datas')
    onDatasChange(newVal: any, oldVal: any) {
        if(newVal) {
            this.items = this.prepareItems(JSON.parse(newVal));
        }
    }

    /**
     * 类型监听
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppCalendar
     */
    @Watch('type')
    onTypeChange(newVal: any, oldVal: any) {
        if(Object.is(newVal, 'calendar')) {
            let appCalendar: any = this.$refs.appCalendar;
            let api = appCalendar.getApi();
            api.updateSize()
        }
    }

    /**
     * 生命周期
     *
     * @memberof AppCalendar
     */
    public mounted() {
        if(this.datas) {
            this.items = this.prepareItems(JSON.parse(this.datas));
        }
    }

    /**
     * 准备数据
     *
     * @param {*} datas
     * @memberof AppCalendar
     */
    public prepareItems(datas: any[]): any[] {
        let items: any[] = [];
        datas.forEach((data: any) => {
            if(!data.hasOwnProperty('date')) {
                data.date = data.create_date;
                data.id = data.srfkey;
            }
            items.push(data);
        })
        return items;
    }

    /**
     * 数据点击
     *
     * @param {*} datas
     * @memberof AppCalendar
     */
    public onClick($event: any) {
        let item = this.items.find((item: any) => item.srfkey == $event.event.id);
        if(item) {
            this.$emit('click', item);
        }
    }
}
</script>

<style lang="less">
.app-calendar {
    height: 100%;
    width: 100%;
}
</style>