PopSwitch.vue 2.24 KB
<template>
    <a-popconfirm :placement="placement" :title="titleText" :ok-text="okText" :cancel-text="cancelText"
                  @confirm="myConfirm()" @openChange="visibleChange">
        <a-switch v-model:checked="myChecked" :checked-children="checkedChildren" :un-checked-children="unCheckedChildren"/>
    </a-popconfirm>
</template>

<script lang="ts">
import {message} from 'ant-design-vue';
import {Component, Prop, Watch} from "vue-facing-decorator";
import {PopConfirm} from "@/components/popconfirm/PopConfirm";

/**
 * <PopSwitch @okConfirm="pagination.refresh()"
 *   :url="`notice/showTopNotice?noticeId=${record.noticeId}&showTop=`"
 *   :checked="record.showTop"/>
 */
@Component({
    name: "MPopSwitch",
    components: {}
})
export default class PopSwitch extends PopConfirm {

    // switch默认值
    @Prop({required: true}) checked: boolean;
    // switch缓存值
    private _checked: boolean;
    // switch显示文本
    myChecked = false;

    ok = false;

    created() {
        this.setMyTitle();
    }

    @Watch('title', {deep: true, immediate: true})
    titleChecked(val: any) {
        this.titleText = val;
    }

    @Watch('checked', {deep: true, immediate: true})
    watchChecked(val: any): void {
        if (val !== undefined) {
            this.myChecked = val;
            this._checked = JSON.parse(JSON.stringify(val));
            this.setMyTitle();
        }
    }

    visibleChange(visible: any): void {
        this.$emit('visibleChange', visible);
        if (visible) {
            this.setMyTitle();
        } else if (!this.ok) {
            this.myChecked = this._checked;
        }
    }

    setMyTitle(): void {
        if (!this.title || this.title === '') {
            this.titleText = this.myChecked ? `您确定取消${this.checkedChildren}吗?` : `您确定${this.checkedChildren}吗?`;
        }
    }

    /**
     * 置顶
     * @param record
     */
    myConfirm(): void {
        this.ok = true;
        // @ts-ignore
        this.axios.post(this.url + this.myChecked, null).then(() => {
            message.success(this.myChecked ? this.checkedChildren + '成功' : '取消' + this.checkedChildren);
            this.ok = false;
            this.$emit('okConfirm');
        });
    }
}
</script>

<style>
</style>