studio-drawer.tsx 12.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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
import { Vue, Component, Watch } from 'vue-property-decorator';
import { CreateElement } from 'vue/types/umd';
import { Subject, Observable } from 'rxjs';
import { Util } from '@/utils';
import { on } from '@/utils/dom/dom';
import store from '@/store';
import i18n from '@/locale';
import './studio-drawer.less';

/**
 * 模态承载组件
 *
 * @export
 * @class StudioDrawer
 * @extends {Vue}
 */
@Component({})
export class StudioDrawer extends Vue {

    /**
     * 已呈现视图列表
     *
     * @protected
     * @type {any[]}
     * @memberof StudioDrawer
     */
    protected viewList: any[] = [];

    /**
     * 为了视觉效果,当前列表为显示的视图
     *
     * @protected
     * @type {any[]}
     * @memberof StudioDrawer
     */
    protected showViewList: any[] = [];

    /**
     * 关闭模态数据传递
     *
     * @protected
     * @type {*}
     * @memberof StudioDrawer
     */
    protected closeModalData: any;

    /**
     * 待关闭视图
     *
     * @protected
     * @type {any[]}
     * @memberof StudioDrawer
     */
    protected toBeClosedViews: any[] = [];

    /**
     * 是否展示模态
     *
     * @protected
     * @type {boolean}
     * @memberof StudioDrawer
     */
    protected isShow: boolean = false;

    /**
     * 监控模态展示状态变更
     *
     * @memberof StudioDrawer
     */
    @Watch('isShow')
    public isShowWatch(newVal: boolean, oldVal: boolean): void {
        if (newVal !== oldVal && newVal === false) {
            this.zIndex -= 1;
            this.$store.commit('updateZIndex', this.zIndex);
        }
    }

    /**
     * 视图层级
     *
     * @protected
     * @type {number}
     * @memberof StudioDrawer
     */
    protected zIndex: number = 100;

    /**
     * 当前激活项下标
     *
     * @protected
     * @type {number}
     * @memberof StudioDrawer
     */
    protected activeIndex: number = 0;

    /**
     * 组件创建完毕
     *
     * @protected
     * @memberof StudioDrawer
     */
    protected created(): void {
        on(document, 'keydown', ($event: KeyboardEvent) => {
            if ($event && $event.keyCode === 27 && this.viewList.length > 0) {
                const zIndex = this.$store.getters.getZIndex()
                if (zIndex !== this.zIndex) {
                    return;
                }
                this.refCloseView(this.viewList[this.viewList.length - 1], this.viewList.length - 1);
            }
        });
    }

    /**
     * 使用实例方法关闭视图
     *
     * @protected
     * @param {*} view
     * @param {number} i
     * @returns {*}
     * @memberof StudioDrawer
     */
    protected refCloseView(view: any, i: number): any {
        const ref: any = this.$refs[view.viewname + i];
        if (ref) {
            const appview = this.$store.getters['viewaction/getAppView'](ref.viewtag);
            if (appview && appview.viewdatachange) {
                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/setViewDataChange', { viewtag: ref.viewtag, viewdatachange: false });
                        ref.closeView();
                    }
                });
            } else {
                ref.closeView();
            }
        }
    }

    /**
     * 打开模态视图
     *
     * @param {*} [param={}]
     * @returns {Promise<any>}
     * @memberof StudioDrawer
     */
    public openModal(param: any = {}): Promise<any> {
        return new Promise((resolve: (res: any) => void) => {
            if (!this.isShow) {
                const zIndex: number = this.$store.getters.getZIndex();
                if (zIndex) {
                    this.zIndex = zIndex + 1;
                    this.$store.commit('updateZIndex', this.zIndex);
                }
                setTimeout(() => this.isShow = true, 50);
            }
            this.viewList.push(Object.assign(Util.deepCopy(param), { resolve }));
            this.showViewList.push(this.viewList[this.viewList.length - 1]);
            this.activeIndex = this.viewList.length - 1;
        });
    }

    /**
     * 关闭模态视图
     *
     * @memberof StudioDrawer
     */
    public closeView(item: any): void {
        if (this.closeModalData && this.closeModalData.length > 0 && this.closeModalData[0] !== undefined) {
            item.resolve({ ret: 'OK', datas: this.closeModalData });
        } else {
            item.resolve({ ret: '', datas: [] });
        }
        this.viewList.pop();
        this.activeIndex = this.viewList.length - 1;
        setTimeout(() => {
            this.showViewList.pop();
        }, 300);
        if (this.toBeClosedViews.length > 0) {
            const view: any = this.toBeClosedViews[this.toBeClosedViews.length - 1];
            const viewname: string = view.viewname;
            const i: number = view.index;
            this.toBeClosedViews.pop();
            this.refCloseView({ viewname }, i);
        }
    }

    /**
     * 关闭指定下标之前的所有页面
     *
     * @protected
     * @memberof StudioDrawer
     */
    protected closeByIndex(i: number): void {
        this.toBeClosedViews = [];
        for (let index = i + 1; index < this.viewList.length - 1; index++) {
            this.toBeClosedViews.push({
                viewname: this.viewList[index].viewname,
                index: index
            });
        }
        this.refCloseView(this.viewList[this.viewList.length - 1], this.viewList.length - 1);
    }

    /**
     * 绘制标题
     *
     * @protected
     * @returns {*}
     * @memberof StudioDrawer
     */
    protected renderHeader(): any {
        return <div class="studio-drawer-header">
            <div class="studio-drawer-breadcrumb">
                <div class="studio-drawer-back" on-click={() => this.closeByIndex(this.viewList.length - 1)}>
                    <icon type="ios-arrow-back"/>
                    返回
                </div>
                {this.showViewList.map((item, i) => {
                    const ref: any = this.$refs[item.viewname + i];
                    if (!ref) {
                        setTimeout(() => {
                            this.$forceUpdate();
                        }, 300);
                        return;
                    }
                    return <span key={i}>
                        {i !== 0 ? <span class="studio-drawer-breadcrumb-item-separator">&gt;</span> : null}
                        <span class={{ 'text': true, 'active': (i === (this.showViewList.length - 1)) }} on-click={() => {
                            if (this.showViewList.length === (i + 1)) {
                                return;
                            }
                            this.closeByIndex(i);
                        }}>
                            {this.$t(ref.model.srfTitle)}
                        </span>
                    </span>;
                })}
            </div>
            {
                this.viewList.length > 1 ?
                    <poptip
                        class="close"
                        confirm
                        placement="left-start"
                        title="确认关闭所有界面?"
                        on-on-ok={() => this.closeByIndex(-1)}
                    >
                        <icon title="关闭所有视图" type="md-close" />
                    </poptip>
                    :
                    <div class="close" on-click={() => this.closeByIndex(-1)}>
                        <icon title="关闭所有视图" type="md-close" />
                    </div>
            }
        </div>;
    }

    /**
     * 绘制视图
     *
     * @protected
     * @param {CreateElement} h
     * @returns {*}
     * @memberof StudioDrawer
     */
    protected renderViews(h: CreateElement): any {
        return this.showViewList.map((view: any, i: number) => {
            const props: any = { openMode: 'MODAL', viewUsage: 2, viewDefaultUsage: false, viewdata: JSON.stringify(view.viewdata), viewparams: view.viewparams };
            const style: any = { 'z-index': i + 1 };
            return <div class={{ 'studio-drawer-item': true, 'active': this.activeIndex === i }}>
                {h(view.viewname, {
                    key: view.viewname + i,
                    ref: view.viewname + i,
                    style,
                    props,
                    on: {
                        viewdataschange: (data: any) => {
                            this.closeModalData = data;
                        },
                        close: () => {
                            if ((this.viewList.length - 1) < i) {
                                return;
                            }
                            if (this.viewList.length === 1) {
                                this.isShow = false;
                                setTimeout(() => this.closeView(view), 500);
                            } else {
                                this.closeView(view);
                            }
                        },
                        viewModelChange: () => {
                            this.$forceUpdate();
                        }
                    }
                })}
            </div>;
        });
    }

    /**
     * 绘制模态内容
     *
     * @returns {*}
     * @memberof StudioDrawer
     */
    public render(h: CreateElement): any {
        return <div class="studio-drawer" key="studio-drawer" style={{ 'z-index': this.zIndex, 'margin-top': this.isShow ? '0px' : '-100vh' }}>
            {this.renderHeader()}
            <div class="studio-drawer-content" style={`transform: translateX(${this.activeIndex > 0 ? this.activeIndex * - 100 : 0}%) translateZ(0px);`}>
                {this.renderViews(h)}
            </div>
        </div>;
    }

}

/**
 * 模态
 *
 * @export
 * @class StudioDrawerController
 */
export class StudioDrawerController {

    /**
     * 唯一实例
     *
     * @protected
     * @static
     * @type {StudioDrawerController}
     * @memberof StudioDrawerController
     */
    protected static readonly instance: StudioDrawerController = new StudioDrawerController();

    /**
     * 模态承载容器
     *
     * @protected
     * @type {HTMLDivElement}
     * @memberof StudioDrawerController
     */
    protected modalContainer: HTMLDivElement;

    /**
     * Vue实例
     *
     * @protected
     * @type {*}
     * @memberof StudioDrawerController
     */
    protected vueInstance!: any;

    /**
     * Creates an instance of StudioDrawerController.
     * @memberof StudioDrawerController
     */
    constructor() {
        this.modalContainer = document.createElement('div');
        document.body.appendChild(this.modalContainer);
    }

    /**
     * 初始化
     *
     * @protected
     * @memberof StudioDrawerController
     */
    protected init(): void {
        this.vueInstance = new Vue({
            i18n,
            store,
            render: (h: any) => h(StudioDrawer, { ref: 'StudioDrawer' })
        }).$mount(this.modalContainer);
    }

    /**
     * 顶部抽屉模式打开视图
     *
     * @param {{ viewname: string, title: string, width?: number, height?: number }} view 视图
     * @param {*} [viewParam={}] 视图参数
     * @param {any[]} deResParameters 关系实体参数对象
     * @param {any[]} parameters 当前应用视图参数对象
     * @param {any[]} args 多项数据
     * @param {*} [data={}] 行为参数
     * @returns {Observable<any>}
     * @memberof StudioDrawerController
     */
    public openDrawer(view: { viewname: string, title: string, width?: number, height?: number, placement?: string }, context: any, data: any = {}): Observable<any> {
        const subject: Subject<any> = new Subject();
        this.getVueInstance().$refs.StudioDrawer.openModal({ ...view, viewdata: Util.deepCopy(context), viewparams: data }).then((data: any) => {
            subject.next(data);
            subject.complete();
            subject.unsubscribe();
        });
        return subject.asObservable();
    }

    /**
     * 获取Vue容器实例
     *
     * @protected
     * @returns {*}
     * @memberof StudioDrawerController
     */
    protected getVueInstance(): any {
        if (!this.vueInstance) {
            this.init();
        }
        return this.vueInstance;
    }

}

// 模态服务控制器实例
export const studioDrawerController: StudioDrawerController = new StudioDrawerController();