app-popover.tsx 10.5 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 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
import Vue, { CreateElement } from 'vue';
import PopperJs from 'popper.js';
import { on } from '../dom/dom';
import './app-popover.less';

import { ViewTool } from '../view-tool/view-tool';
import store from '../../store';
import i18n from '@/locale';

import { Subject } from 'rxjs';

/**
 * 悬浮窗控制器实例
 *
 * @export
 * @class AppPopover
 */
export class AppPopover {
    /**
     * 实例
     *
     * @private
     * @static
     * @memberof AppPopover
     */
    private static readonly $popover = new AppPopover();
    /**
     * vue实例
     *
     * @private
     * @type {Vue}
     * @memberof AppPopover
     */
    private vueExample!: Vue;
    /**
     * PopperJs实例
     *
     * @private
     * @type {PopperJs}
     * @memberof AppPopover
     */
    private popperExample?: PopperJs;
    /**
     * 是否显示悬浮窗
     *
     * @private
     * @type {boolean}
     * @memberof AppPopover
     */
    private showPopper: boolean = false;
    /**
     * 是否在点击空白区域时自动关闭
     *
     * @private
     * @type {boolean}
     * @memberof AppPopover
     */
    private isAutoClose: boolean = true;
    /**
     * 当前激活popver标识
     *
     * @private
     * @type {string}
     * @memberof AppPopover
     */
    private activePopoverTag?: string;

    /**
     * 返回数据
     */
    private tempResult = { ret: '' };

    /**
     * 数据传递对象
     */
    private subject: any;

    /**
     * 气泡卡片层级
     *
     * @private
     * @type {(number | null)}
     * @memberof AppPopover
     */
    private zIndex: number | null = null;

    /**
     * Creates an instance of AppPopover.
     * @memberof AppPopover
     */
    constructor() {
        if (AppPopover.$popover) {
            return AppPopover.$popover;
        }
        on(document, 'click', () => {
            if (!this.showPopper || !this.isAutoClose) {
                return;
            }
            this.destroy();
        });
    }

    /**
     * 初始化vue实例
     *
     * @private
     * @returns {void}
     * @memberof AppPopover
     */
    private initVueExample(): void {
        const div = document.createElement('div');
        document.body.appendChild(div);
        this.vueExample = new Vue({
            el: div,
            store: store,
            i18n: i18n,
            data: {
                title: null,
                content: null,
                isShow: false
            },
            render(h: CreateElement) {
                const content: any = this.content;
                const style: string = `display: ${this.isShow ? 'block' : 'none'};`;
                return <div style={style} class="el-popover el-popper panel-design-popover">
                    <el-card ref="datacard">
                        {
                            this.title ? <div slot="header" class="clearfix">
                                <span>{this.title}</span>
                                <span class="cancel" ref="cancel"><i class="fa fa-times" aria-hidden="true" style="cursor: pointer;"></i></span>
                            </div> : null
                        }
                        <div style="height:100%;">
                            {content ? content(h) : null}
                        </div>
                    </el-card>
                    <div x-arrow class="popper__arrow"></div>
                </div>;
            }
        });
        on(this.vueExample.$el, 'click', (event: any) => {
            if (Object.is(event.target.outerHTML, '<i aria-hidden="true" class="fa fa-times" style="cursor: pointer;"></i>')) {
                this.destroy();
            } else {
                event.stopPropagation();
            }
        });
    }

    /** 
     * 打开悬浮窗
     *
     * @param {MouseEvent} event 事件
     * @param {*} view 视图
     * @param {*} [context={}] 应用上下文参数
     * @param {any[]} deResParameters 关系实体参数对象
     * @param {any[]} parameters 当前应用视图参数对象
     * @param {any[]} args 多项数据
     * @param {*} data 行为参数
     * @param {string} [title] 标题
     * @param {PopperJs.Placement} [position='left'] 悬浮窗位置
     * @param {boolean} [isAutoClose=true] 是否自动关闭
     * @param {number} [width] 宽度
     * @param {number} [height] 高度
     * @returns {Subject<any>}
     * @memberof AppPopover
     */
    public openPop(event: MouseEvent, view: any, context: any = {}, data: any, title?: string, position: PopperJs.Placement = 'left', isAutoClose: boolean = true, width?: number, height?: number): Subject<any> {
        let viewdata: any = {};
        Object.assign(viewdata, JSON.parse(JSON.stringify(context)));
        this.subject = new Subject<any>();
        this.openPopover(event, (h: CreateElement) => {
            return h(view.viewname, {
                class: {
                    viewcontainer2: true,
                },
                props: {
                    viewdata: JSON.stringify(viewdata),
                    viewDefaultUsage: false,
                    viewparam:JSON.stringify(data)
                },
                on: {
                    viewdataschange: ($event: any) => this.dataChange($event),
                    close: ($event: any) => this.viewclose($event)
                },
            })
        }, view.title, undefined, false, view.width, view.height);
        return this.subject;
    }

    /**
     * 数据变化回调
     * @param $event 
     */
    public dataChange(result: any) {
        this.tempResult = { ret: '' };
        if (result && Array.isArray(result) && result.length > 0) {
            Object.assign(this.tempResult, { ret: 'OK' }, { datas: JSON.parse(JSON.stringify(result)) });
        }
        this.close();
        this.destroy();
    }

    /**
     * 视图关闭
     * @param result 
     */
    public viewclose(result: any) {
        if (result && Array.isArray(result) && result.length > 0) {
            Object.assign(this.tempResult, { datas: JSON.parse(JSON.stringify(result)) });
        }
        this.destroy();
    }

    /**
     * 关闭回调(吐值)
     */
    public close() {
        if (this.tempResult && Object.is(this.tempResult.ret, 'OK')) {
            this.subject.next(this.tempResult);
        }
    }

    /**
     * 打开悬浮窗
     *
     * @param {MouseEvent} event
     * @param {(h: CreateElement) => any} [content]
     * @param {string} [title]
     * @param {PopperJs.Placement} [position='left']
     * @param {boolean} [isAutoClose=true]
     * @param {number} [width]
     * @param {number} [height]
     * @returns {void}
     * @memberof AppPopover
     */
    public openPopover(event: any, content?: (h: CreateElement) => any, title?: string, position: PopperJs.Placement = 'left', isAutoClose: boolean = true, width?: number, height?: number): void {
        // 阻止事件冒泡

        event.stopPropagation();
        const element: Element = event.toElement || event.srcElement;
        if (element.hasAttribute('app-popover-tag')) {
            const tag: any = element.getAttribute('app-popover-tag');
            if (Object.is(this.activePopoverTag, tag)) {
                this.destroy();
                return;
            }
            this.activePopoverTag = tag;
        } else {
            const tag: string = 'app-popover-tag-' + new Date().getTime();
            element.setAttribute('app-popover-tag', tag);
            this.activePopoverTag = tag;
        }
        // if (!this.vueExample) {
        //     this.initVueExample();
        // }
        if (this.vueExample) {
            this.destroy();
        }
        this.initVueExample();
        const _element: any = this.vueExample.$el;

        const zIndex = this.vueExample.$store.getters.getZIndex();
        if (zIndex) {
            this.zIndex = zIndex + 100;
            this.vueExample.$store.commit('updateZIndex', this.zIndex);
        }
        _element.style.zIndex = this.zIndex;

        const datacard: any = this.vueExample.$refs.datacard;
        datacard.$el.style.width = width !== 0 ? width + 'px' : '240px';
        datacard.$el.getElementsByClassName('el-card__body')[0].style.height = height !== 0 ? height + 'px' : '562px';
        // if (width !== '0px' && width) {
        //     this.vueExample.$refs.datacard.$el.style.width = width;
        // } else {
        //     this.vueExample.$refs.datacard.$el.style.width = '240px';
        // }
        // if (height !== '0px' && height) {
        //     this.vueExample.$refs.datacard.$el.getElementsByClassName('el-card__body')[0].style.height = height;
        // } else {
        //     this.vueExample.$refs.datacard.$el.getElementsByClassName('el-card__body')[0].style.height = '562px';
        // }
        // this.destroy();
        // 是否可自动关闭
        this.isAutoClose = isAutoClose;
        // 更新vue实例内容
        this.vueExample.$data.title = title;
        this.vueExample.$data.content = content;
        this.vueExample.$data.isShow = true;
        this.vueExample.$forceUpdate();
        // 绘制popover
        const config: PopperJs.PopperOptions = {
            placement: position,
            onCreate: () => {
                this.showPopper = true;
            },
            onUpdate: (arg) => {
                const popper: any = arg.instance.popper;
                popper.style.display = 'none';
            },
            modifiers: {
                flip: {
                    boundariesElement: 'viewport'
                },
                preventOverflow: {
                    boundariesElement: document.getElementsByClassName('app-index')[0]
                }
            }
        };
        this.popperExample = new PopperJs(element, this.vueExample.$el, config);
        this.popperExample.update();
    }

    /**
     * 销毁popper(带回填数据)
     *
     * @memberof AppPopover
     */
    public destroy(): void {
        if (this.popperExample) {
            this.popperExample.destroy();

            if (this.zIndex) {
                const zIndex: any = this.zIndex;
                this.vueExample.$store.commit('updateZIndex', zIndex - 100);
                this.zIndex = null;
            }

            this.vueExample.$destroy();
            document.body.removeChild(this.vueExample.$el);
            this.popperExample = undefined;
            this.showPopper = false;
            this.activePopoverTag = '';
            this.vueExample.$data.isShow = false;
            this.vueExample.$forceUpdate();
        }
    }

    /**
     * 获取实例
     *
     * @static
     * @memberof AppPopover
     */
    public static getInstance() {
        return AppPopover.$popover;
    }

}