app-drawer.vue 8.5 KB
Newer Older
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
<template>
    <drawer
        class="app-drawer"
        :placement="placement"
        :closable="false"
        v-model="isShow"
        :width="width"
        :before-close="beforeClose"
        @on-visible-change="onVisibleChange($event)"
        @on-close="close(tempResult)"
    >
        <component
            :is="viewname"
            class="view-container2"
            :staticProps="{ ...this.staticProps, viewDefaultUsage:false}"
            :dynamicProps="{ ...this.dynamicProps }"
            @viewdataschange="dataChange($event)"
            @viewdatasactivated="viewDatasActivated($event)"
            @close="close($event)"
            :ref="viewname">
        </component>
    </drawer>
</template>
<script lang="ts">
import { Vue, Component, Prop } from "vue-property-decorator";
import { Subject } from "rxjs";
import { AppDrawer } from './app-drawer';
import { Util } from 'ibiz-core'
@Component({
    components: {}
})
export default class AppDrawerCompponent extends Vue {

    /**
     * 视图UI参数
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */ 
    @Prop() public view !:any;

    /**
     * 视图动态参数
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */ 
    @Prop({default:{}}) public dynamicProps?:any;

    /**
     * 视图静态参数
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */ 
    @Prop({default:{}}) public staticProps?:any;

    /**
     * 数据传递对象
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */ 
    public subject: null | Subject<any> = new Subject<any>();

    /**
     * 抽屉弹出位置
     *
     * @type {string}
     * @memberof AppDrawerCompponent
     */
    public placement:string = '';

    /**
     * 是否显示
     *
     * @type {boolean}
     * @memberof AppDrawerCompponent
     */
    public isShow:boolean = false;

    /**
     * 零时结果
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */  
    public tempResult:any = { ret: '' };

    /**
     * 视图名称
     *
     * @type {string}
     * @memberof AppDrawerCompponent
     */ 
    public viewname:string = '';

    /**
     * 视图宽度
     *
     * @type {number}
     * @memberof AppDrawerCompponent
     */ 
    public width:number = 256;

    /**
     * 视图层级
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */ 
    public zIndex:any = null;

    /**
     * 是否在抽屉区按下鼠标
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */ 
    public isMouseDown: boolean = false;

    /**
     * 获取数据传递对象
     *
     * @memberof AppDrawerCompponent
     */ 
    public getSubject(){
        return this.subject;
    }

    /**
     * Vue生命周期created
     *
     * @memberof AppDrawerCompponent
     */ 
    public created() {
        this.viewname = this.view.viewname;
        this.placement = this.view.placement === 'DRAWER_LEFT' ? 'left' : 'right';
        if (this.view.width) {
            if (this.view.width.toString().indexOf('px') > 0) {
                if (!Object.is(this.view.width, '0px')) {
                    this.width = parseInt(this.view.width.toString().slice(0, this.view.width.toString().length - 2));
                } else {
                    this.width = 800;
                }
            } else {
                if (this.view.width !== 0) {
                    this.width = this.view.width;
                } else {
                    this.width = 800;
                }
            }
        } else {
            this.width = 800;
        }
        document.onkeydown = (e: any) => {
            var keyCode = e.keyCode || e.which || e.charCode;
            if (keyCode == 27) {
                const zIndex = this.$store.getters.getZIndex();
                if (zIndex !== this.zIndex) {
                    return;
                }
                this.isShow = false;
            }
        }
        // 监听鼠标是否在抽屉内容区按下
        document.onmousedown = (e: any) => {
            const documentPaths = e.path;
            this.isMouseDown = false;
            if (documentPaths?.length > 0) {
                documentPaths.forEach((path: any) => {
                    if (path.className == 'ivu-drawer-content') {
                        this.isMouseDown = true;
                    }
                })
            }
        }
    }

    /**
     * Vue生命周期mounted
     *
     * @memberof AppDrawerCompponent
     */ 
    public mounted() {
        this.isShow = true;
        this.handleZIndex('ivu-drawer-mask', 'ivu-drawer-wrap');
    }

    /**
     * Vue生命周期beforeDestroy
     *
     * @memberof AppDrawerCompponent
     */ 
    public beforeDestroy() {
        if (this.zIndex) {
            const zIndex: any = this.zIndex;
            this.$store.commit('updateZIndex', zIndex - 100);
        }
    }

    /**
     * 处理 z-index
     *
     * @memberof AppDrawerCompponent
     */ 
    public handleZIndex(mask: string, wrap: string) {
        const zIndex = this.$store.getters.getZIndex();
        if (zIndex) {
            this.zIndex = zIndex + 100;
            this.$store.commit('updateZIndex', this.zIndex);
        }
        const element: Element = this.$el;
        const maskTag: any = element.getElementsByClassName(mask)[0];
        const warpTag: any = element.getElementsByClassName(wrap)[0];
        maskTag.style.zIndex = this.zIndex;
        warpTag.style.zIndex = this.zIndex;
    }

    /**
     * 关闭之前
     * 在内容区按下鼠标,在其他地方松开鼠标时不关闭抽屉
     * @memberof AppDrawerCompponent
     */ 
    public beforeClose() {
        return new Promise((resolve: any, reject: any) => {
            if (this.isMouseDown) {
                this.isMouseDown = false;
                return
            }
            resolve(true)
        })
    }

    /**
     * 关闭回调
     *
     * @memberof AppDrawerCompponent
     */ 
    public close(result: any) {
        if (result && Array.isArray(result)) {
            Object.assign(this.tempResult, { ret: 'OK' }, { datas: Util.deepCopy(result) });
        }
        this.isShow = false;
    }

    /**
     * 数据变化回调
     *
     * @memberof AppDrawerCompponent
     */ 
    public dataChange(result: any) {
        this.tempResult = { ret: '' };
        if (result && Array.isArray(result) && result.length > 0) {
            Object.assign(this.tempResult, { ret: 'OK' }, { datas: Util.deepCopy(result) });
        }
    }

    /**
     * 激活数据回调
     *
     * @memberof AppDrawerCompponent
     */ 
    public viewDatasActivated(result: any) {
        if (result && Array.isArray(result) && result.length > 0) {
            this.close(result);
        }
    }

    /**
     * 抽屉显示隐藏回调
     *
     * @memberof AppDrawerCompponent
     */ 
    public onVisibleChange($event: any) {
        const component: any = this.$refs[this.viewname];
        if (component) {
            const viewInstance = component.$children[0];
            const viewDataChange = this.$store.getters["viewAction/getStateByViewTag"](viewInstance?.viewtag);
            if (viewDataChange) {
                this.isShow = true;
                const title: any = this.$t('app.tabpage.sureclosetip.title');
                const contant: any = this.$t('app.tabpage.sureclosetip.content');
                this.$Modal.confirm({
                    title: title,
                    content: contant,
                    onOk: () => {
                        this.$store.commit('viewAction/removeViewByViewTag', viewInstance.viewtag);
                        this.isShow = false;
                    },
                    onCancel: () => {
                        this.isShow = true;
                    }
                });
            } else {
              this.handleShowState($event);
            } 
        }
    }

    /**
     * 处理显示隐藏状态
     *
     * @memberof AppDrawerCompponent
     */ 
    public handleShowState($event: any) {
        if ($event) {
            return;
        }
        
        setTimeout(() => {
            if (this.subject) {
                if (this.tempResult && Object.is(this.tempResult.ret, 'OK')) {
                    this.subject.next(this.tempResult);
                }
            }
            document.body.removeChild(this.$el);
            this.$destroy();
            AppDrawer.getInstance().destroyVueExample();
            this.subject = null;
        }, 500)
    }

}
</script>
<style lang="less">
@import './app-drawer.less';
</style>