app-searchbar-base.tsx 8.7 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
import { Prop, Watch, Emit } from 'vue-property-decorator';
import { throttle, Util } from 'ibiz-core';
import { SearchBarControlBase } from '../../../widgets/searchbar-control-base';
import { IPSEditor, IPSSearchBarFilter } from '@ibiz/dynamic-model-api';

/**
 * 搜索栏部件基类
 *
 * @export
 * @class AppSearchBarBase
 * @extends {SearchBarControlBase}
 */
export class AppSearchBarBase extends SearchBarControlBase {

    /**
     * 部件动态参数
     *
     * @memberof AppSearchBarBase
     */
    @Prop() public declare dynamicProps: any;

    /**
     * 部件静态参数
     *
     * @memberof AppSearchBarBase
     */
    @Prop() public declare staticProps: any;

    /**
     * 监听动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppSearchBarBase
     */
    @Watch('dynamicProps', {
        immediate: true,
    })
    public onDynamicPropsChange(newVal: any, oldVal: any) {
        if (newVal && !Util.isFieldsSame(newVal, oldVal)) {
            super.onDynamicPropsChange(newVal, oldVal);
        }
    }

    /**
     * 监听静态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppSearchBarBase
     */
    @Watch('staticProps', {
        immediate: true,
    })
    public onStaticPropsChange(newVal: any, oldVal: any) {
        if (newVal && !Util.isFieldsSame(newVal, oldVal)) {
            super.onStaticPropsChange(newVal, oldVal);
        }
    }

    /**
     * 销毁视图回调
     *
     * @memberof AppSearchBarBase
     */
    public destroyed() {
        this.ctrlDestroyed();
    }

    /**
     * 部件事件
     *
     * @param {{ controlname: string; action: string; data: any }} { controlname 部件名称, action 事件名称, data 事件参数 }
     * @memberof AppSearchBarBase
     */
    @Emit('ctrl-event')
    public ctrlEvent({ controlname, action, data }: { controlname: string; action: string; data: any }): void { }

    /**
     * 绘制过滤栏
     *
     * @param {{ controlname: string; action: string; data: any }} { controlname 部件名称, action 事件名称, data 事件参数 }
     * @memberof AppSearchBarBase
     */
    public renderBarFilters(data: any) {
        const barFilters: Array<IPSSearchBarFilter> = this.controlInstance.getPSSearchBarFilters() || [];
        if(barFilters.length === 0 || !data.editor) {
            return null;
        }
        return barFilters.map((filter: IPSSearchBarFilter) => {
            if(!filter.name || !Object.is(data.editor, filter.name)) {
                return null;
            }
            const editor: IPSEditor | null = filter.getPSEditor();
            if(editor) {
                let filterItem: any = this.filterItems.find((item: any) => {
                    return item.editor && Object.is(item.editor, editor.name);
                });
                if(filterItem) {
                    filterItem[editor.name] = data[editor.name];
                }
                return (
                    <app-default-editor
                        editorInstance={editor}
                        value={data[editor.name]}
                        contextData={data}
                        context={this.context}
                        viewparams={this.viewparams}
                        service={this.service}
                        disabled={false}
                        ignorefieldvaluechange={false}
                        on-change={this.editorChange.bind(this)}
                    />
                );
            }
        })
    }

    /**
     * 编辑器值变化
     *
     * @memberof AppSearchBarBase
     */
    public editorChange({ name, value }: any) {
        if(this.filterItems.length == 0) {
            return;
        }
        this.filterItems.forEach((item: any) => {
            if(item.editor && Object.is(name, item.editor)) {
                item[name] = value;
            }
        })
    }

    /**
     * 绘制过滤树
     *
     * @memberof AppSearchBarBase
     */
    public renderFilterTree() {
        if(!this.filterFields || this.filterFields.length==0) {
            return null;
        }
        return (
            <div class="filter-group">
                <filter-tree
                    datas={this.filterItems}
                    fields={this.filterFields}
                    scopedSlots={{
                        default: ({data}: any) => { return this.renderBarFilters(data) }
                    }}>
                </filter-tree>
            </div>
        );
    }

    /**
     * 绘制过滤存储集合
     *
     * @memberof AppSearchBarBase
     */
    public renderFilterFooter() {
        return (
            <div class="search-bar-footer">
                <div class="search-bar-action">
                    {this.historyItems?.length>0 ? 
                        <el-select
                            size="small" 
                            value={this.selectItem}
                            on-change={this.onFilterChange.bind(this)}>
                                {this.historyItems.map((item: any)=> {
                                    return (
                                        <el-option
                                            key={item.value}
                                            label={item.name}
                                            value={item.value}
                                            ></el-option>
                                    )
                                })}
                            </el-select> : null}
                    <i-button type="primary" on-click={(...params: any[]) => throttle(this.search,params,this)}>{this.$t('app.searchbutton.search')}</i-button>
                    <i-button on-click={this.reset.bind(this)}>{this.$t('app.searchbutton.reset')}</i-button>
                    <poptip
                        ref="propip"
                        trigger="hover"
                        transfer
                        placement="top-end"
                        title="存储自定义查询"
                        popper-class="searchbar-poptip"
                        op-on-popper-show={this.openPoper.bind(this)}>
                            <i-button><i class="fa fa-floppy-o" aria-hidden="true"></i></i-button>
                            <div slot="content">
                                <i-input v-model={this.saveItemName} placeholder=""></i-input>
                                <div class="save-action">
                                    <i-button on-click={(...params: any[]) => throttle(this.onCancel,params,this)}>{this.$t('app.commonwords.cancel')}</i-button>
                                    <i-button type="primary" on-click={(...params: any[]) => throttle(this.onOk,params,this)}>{this.$t('app.commonwords.save')}</i-button>
                                </div>
                            </div>
                    </poptip>
                </div>
            </div>
        );
    }

    /**
     * 渲染快速搜索(视图代理模式下)
     *
     * @protected
     * @return {*} 
     * @memberof AppSearchBarBase
     */
    protected renderQuickSearch() {
        return (
            <app-quick-search v-model={this.queryParams.quickSearchValue} appDataEntity={this.controlInstance.getPSAppDataEntity?.()} on-search={(value: any) => this.searchValueChange(value)} />
        )
    }

    /**
     * 渲染搜索栏分组(视图代理模式下)
     *
     * @protected
     * @return {*} 
     * @memberof AppSearchBarBase
     */
    protected renderSearchBarGroups() {
        //  TODO 等待后续支持代码表配置、计数器
        if (this.quickGroupItems && this.quickGroupItems.length) {
            return (
                <app-quick-group class="app-searchbar-quick-group" context={this.context} viewparams={this.viewparams} items={this.quickGroupItems} type="ITEMS" on-valuechange={(item: any) => this.searchValueChange(item, 'QUICKGROUP') }></app-quick-group>
            )
        }
    }

    /**
     * 渲染视图代理模式下搜索栏部件
     *
     * @memberof AppSearchBarBase
     */
    renderByViewProxyMode() {
        return [
            this.renderSearchBarGroups(),
            this.controlInstance.enableQuickSearch ? this.renderQuickSearch() : null
        ]
    }

    /**
     * 绘制搜索栏部件
     *
     * @memberof AppSearchBarBase
     */
    public render() {
        if (!this.controlIsLoaded || (!this.isExpandSearchForm && !this.viewIsProxyMode)) {
            return null;
        }
        const { controlClassNames } = this.renderOptions;
        return <div class={{ ...controlClassNames, 'app-searchbar': true, 'app-searchbar__proxy-mode': this.viewIsProxyMode }}>
            {this.viewIsProxyMode ? this.renderByViewProxyMode() : [this.renderFilterTree(), this.renderFilterFooter()]}
        </div>
    }

}