app-breadcrumb.tsx 4.6 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
import { Vue, Component } from 'vue-property-decorator';
import qs from 'qs';
import { AppService } from '@/studio-core/service/app-service/AppService';
import { HistoryItem } from '@/studio-core/service/app-nav-history/AppNavHistoryBase';
import './app-breadcrumb.less';

/**
 * 面包屑导航组件
 *
 * @export
 * @class AppBreadcrumb
 * @extends {Vue}
 */
@Component({})
export class AppBreadcrumb extends Vue {

    /**
     * 应用服务(单例)
     *
     * @protected
     * @type {AppService}
     * @memberof AppHeader
     */
    protected appService: AppService = new AppService();

    /**
     * 跳转点击
     *
     * @protected
     * @param {*} to
     * @memberof AppBreadcrumb
     */
    protected click(to: any) {
        const i = this.$appService.navHistory.findHistoryIndex(to);
        this.$router.push({ path: to.path, params: to.params, query: to.query });
        this.$appService.navHistory.reset(i + 1);
    }

    /**
     * 跳转首页
     *
     * @protected
     * @param {*} meta
     * @memberof AppBreadcrumb
     */
    protected toIndex(meta: any): void {
        let path = '';
        if (meta && meta.parameters) {
            meta.parameters.forEach((item: any) => {
                const val = this.$route.params[item.parameterName];
                path += `/${item.pathName}${val ? `/${val}` : ''}`;
            });
        }
        this.$router.push(path);
        this.$appService.navHistory.reset();
        location.reload();
    }

    /**
     * 根据下标从父导航查询是否有列表数据
     *
     * @protected
     * @param {*} context
     * @param {string} [tag]
     * @returns {(any[] | null)}
     * @memberof AppBreadcrumb
     */
    protected getItems(context: any, tag?: string): any[] | null {
        if (tag) {
            const view = this.$appService.viewStore.findParentByTag(tag);
            if (view) {
                const data = this.appService.contextStore.getContextData(view.context, context.srfappdename);
                if (data && data.items) {
                    return data.items;
                }
            }
        }
        return null;
    }

    /**
     * 选中数据变更
     *
     * @protected
     * @param {*} c
     * @param {HistoryItem} history
     * @param {*} val
     * @memberof AppBreadcrumb
     */
    protected itemChange(c: any, history: HistoryItem, val: any): void {
        const to = history.to;
        to.params[c.srfappdename] = val;
        let path = '';
        history.meta.parameters.forEach((item: any) => {
            const pam = to.params[item.parameterName];
            path += (`/${item.pathName}` + (pam ? `/${pam}` : ''));
        });
        if (to.query && Object.keys(to.query).length > 0) {
            path += ('?' + qs.stringify(to.query));
        }
        this.$router.push(path);
        if (to.fullPath !== path) {
            this.$appService.navHistory.pop();
        }
        this.$forceUpdate();
    }

    /**
     * 绘制面包屑
     *
     * @returns {*}
     * @memberof AppBreadcrumb
     */
    public render(): any {
        const items: any[] = [];
        const indexMeta = this.appService.navHistory.indexMeta;
        if (indexMeta) {
            items.push(<span class="app-breadcrumb-item">
                <span class="content" on-click={() => this.toIndex(indexMeta)}>首页</span>
            </span>);
        }
        const arr = this.appService.navHistory.historyList;
        arr.forEach((item, i) => {
            let dropdown: any = null;
            if (arr.length === (i + 1)) {
                const list = this.getItems(item.context, item.tag);
                if (list && list.length > 0) {
                    const c = item.context;
                    dropdown = <i-select v-model={c[c.srfappdename]} on-on-change={(val: any) => this.itemChange(c, item, val)} size="small">
                        {list.map((item: any) => {
                            return <i-option value={item.srfkey} key={item.srfkey}>{item.srfmajortext}</i-option>;
                        })}
                    </i-select>;
                }
            }
            items.push(<span class={{ 'app-breadcrumb-item': true, 'last': i === (arr.length - 1) }}>
                {(!indexMeta && i === 0) ? null : <span class="separator">/</span>}
                <span class="content" on-click={() => this.click(item.to)}>{this.$t(item.meta?.caption)}{dropdown ? null : (item.meta?.info && item.meta?.info !== '') ? ' - ' + item.meta?.info : ''}</span>
                {dropdown ? <span class="select"> - {dropdown}</span> : null}
            </span>);
        });
        return <div class="app-breadcrumb">{items}</div>;
    }
}