import { ComponentRef } from '@angular/core'; import { DataTableComponent } from '@farris/ui-datatable'; import { TreeNode, TreeTableComponent } from '@farris/ui-treetable'; import { of } from 'rxjs'; import { debounceTime, switchMap } from 'rxjs/operators'; import { ComponentInstanceType } from '../lookup-displaytype'; import { LookupGridComponent } from '../lookup-grid.component'; import { LookupLeftComponent } from '../lookup-left.component'; export class LookupComponentManager { constructor(private ins: LookupGridComponent) {} getComponentInstance(type: ComponentInstanceType = 'datatable') { if (!this.ins.componentRef || !this.ins.componentRef.instance) { return null; } if (type === 'selected') { return this.ins.multiSelectDT; } let ins = this.ins.componentRef.instance; if (type === 'leftDataTable' || type === 'leftTree') { const leftRef = this.ins.leftComponentRef; if (!leftRef || !leftRef.instance || !leftRef.instance.cmpRef || !leftRef.instance.cmpRef.instance) { return null; } ins = this.ins.leftComponentRef.instance.cmpRef.instance; } if (type === 'fav') { ins = this.ins.favoritesComponentRef.instance; } switch (type) { case 'leftDataTable': case 'fav': case 'datatable': return ins as DataTableComponent; case 'leftTree': case 'treetable': return ins as TreeTableComponent; default: if (this.ins.isTree()) { return ins as TreeTableComponent; } return ins as DataTableComponent; } } createComponentWithServerData(data: any) { if (this.ins.componentRef) { return; } this.ins.idField = data.idField || this.ins.idField; this.ins.textField = data.textField || this.ins.textField; this.ins.valueField = data.valueField || this.ins.valueField; this.ins.displayType = (data && data.displayType) || this.ins.displayType || 'LIST'; this.ins.componentRef = this.createContent(this.ins.gridOptions); this.createFavoriteComponent(); this.resizeComponent(); } createFavoriteComponent() { if (this.ins.useFavorite && !this.ins.favoritesComponentRef) { this.ins.favoriteColumns = this.ins.favHelper.getFavoriteColumns(); const favoritesOptions = Object.assign({}, this.ins.gridOptions, { showFilterBar: false, pagination: false, columns: this.ins.favoriteColumns || [] }); this.ins.favoritesComponentRef = this.createFavoritesContent(favoritesOptions); this.resizeComponent('fav'); } } private reloadTreeDataForExpand(expandLevel) { const uri = this.ins.gridOptions.uri; const tt = this.ins.componentRef.instance as TreeTableComponent; const { field = '*', value = '' } = { ...tt.searchData }; const search = { category: 'all', searchValue: value, searchField: field, layerNum: expandLevel }; const param = { searchValue: JSON.stringify(search), customData: this.ins.customData, enableFullTree: this.ins.enableFullTree, loadTreeDataType: this.ins.loadTreeDataType }; if (this.ins.helpId) { param['helpId'] = this.ins.helpId; } if (tt && tt.sortName) { Object.assign(param, { sortName: tt.sortName, sortOrder: tt.sortOrder }); } return this.ins.http.getData(uri, param).subscribe((data: any) => { const nodes = this.ins.checkNodeCanBeSelect(data.items, expandLevel === -1); this.ins.items = nodes; tt.loadData(nodes); }); } private setTreeNodeExpandBy(nodes: TreeNode[], level) { if (!nodes || !nodes.length) { return; } nodes.forEach((n: TreeNode) => { const layer = n.data[this.ins.treeInfo.dataField].layer; const expandLayer = layer + level - 1; n.expanded = layer <= expandLayer ? true : n.expanded; if (n.children && n.children.length && layer + 1 <= expandLayer) { this.setTreeNodeExpandBy(n.children, level); } }); } // 启用树帮助右键菜单功能 private useContextMenuForTree(cmpRef: ComponentRef) { const { maxLevel, enableContextMenu, language } = this.ins.treeTableOptions; if (enableContextMenu && maxLevel) { const levelMenus = []; for (let i = 1; i <= maxLevel; i++) { const title = language['expand' + i]; levelMenus.push({ id: 30 + 1, code: i, title, handle: (t) => { // console.log(t); // 发送请求获取数据 const level = t.menu.code; if (isLoadAllTreeData() ) { this.setTreeNodeExpandBy(this.ins.items, level); cmpRef.instance.updateSerializedValue(); } else { const { dataField, layerField } = this.ins.treeInfo; const firstNodeData = (this.ins.items || [])[0]; if (firstNodeData && firstNodeData.data) { const minLayer = this.ins.utils.getValue(`${dataField}.${layerField}`, firstNodeData.data); let _level = minLayer + level - 1; this.reloadTreeDataForExpand(_level); } } } }); } const isLoadAllTreeData = () => { if (this.ins.loadTreeDataType === 'default') { return cmpRef.instance.loadDataType === 'all'; } else { return this.ins.loadTreeDataType === 'loadall'; } }; const contextMenus = [ { id: 1, code: 'expandall', title: language.expandall, handle: (t) => { if (cmpRef && isLoadAllTreeData()) { cmpRef.instance.expandAll(); } else { // 发送请求获取数据 this.reloadTreeDataForExpand(-1); } } }, { id: 2, code: 'collapseall', title: language.collapseall, handle: (t) => { if (cmpRef) { cmpRef.instance.collapseAll(); } } }, '-', { id: 3, title: language.expandByLayer, children: levelMenus } ]; this.ins.treeTableOptions.contextMenuItems = contextMenus; cmpRef.instance.beforeShowContextMenu = () => { return of({show: !cmpRef.instance.state.searched}); }; } } createContent(opts: any): ComponentRef { if (this.ins.componentRef) { return; } const type = this.ins.getComponentType(); const dtFac = this.ins.cfr.resolveComponentFactory(type); let cmpRef: ComponentRef = null; if (this.ins.isDoublleList()) { cmpRef = this.ins.centerContainer.createComponent(dtFac); } else { cmpRef = this.ins.contentContainer.createComponent(dtFac); } if (this.ins.isTree()) { opts.fit = true; opts.pagination = false; this.useContextMenuForTree(cmpRef); } else { opts.fill = true; } const ttOpts = this.ins.treeTableOptions || {}; Object.assign(cmpRef.instance, opts, { allColumnsTitle: this.ins.allColumnsTitle, ...ttOpts }); this.ins.componentRef = cmpRef; this.resizeComponent(); return cmpRef; } // 创建收藏CMP createFavoritesContent(opts: any): ComponentRef { const type = this.ins.getComponentType(); const dtFac = this.ins.cfr.resolveComponentFactory(type); let cmpRef = null; cmpRef = this.ins.favoritesContainer.createComponent(dtFac); if (this.ins.isTree()) { opts.fit = true; opts.pagination = false; } else { opts.fill = true; } Object.assign(cmpRef.instance, opts, { width: this.ins.dialog.size.width - 20, height: this.ins.dialogMgr.getHeight() }); // 订阅收藏夹列表中组件的相关事件 this.ins.favHelper.initFavoriteComponentEvent(cmpRef); return cmpRef; } resizeComponent(type: ComponentInstanceType = 'datatable') { const size = { width: this.ins.dialog.size.width - 20, height: this.ins.dialogMgr.getHeight() }; if (this.ins.isDoublleList() && (type === 'datatable' || type === 'treetable')) { size.width = this.ins.dialog.size.width - this.ins.leftPanelWidth - 27; } this.getComponentInstance(type).resize(size); } /** 创建左侧组件 */ createLeftComponent(ops: any) { let dtFac = null; if (this.ins.isDoublleList()) { dtFac = this.ins.cfr.resolveComponentFactory(LookupLeftComponent); } this.ins.leftComponentRef = this.ins.leftContainer.createComponent(dtFac); ops.height = this.ins.dialogMgr.getHeight(); if (this.ins.dialogWidth < this.ins.navLookupDialogMinWidth) { this.ins.dialogWidth = this.ins.navLookupDialogMinWidth; this.ins.dialog.reSize({ width: this.ins.dialogWidth }); this.ins.resizeCmp({ width: this.ins.dialog.size.width }); } if (ops.width !== this.ins.leftPanel.width) { // 默认 1 : 2 this.ins.leftPanel.resize({ width: this.ins.leftPanel.width, height: ops.height }); this.ins.resizeCmp({ width: this.ins.dialog.size.width }); } // this.resizeComponent(); this.ins.leftComponentRef.instance.lookupCmp = this.ins; this.ins.leftComponentRef.instance.navOptions = ops; this.ins.leftComponentRef.instance.selected .pipe( debounceTime(100), switchMap((d: any) => { if (d && d.data) { this.ins.navigationFilter = { selected: d.data, idValue: this.getNavigationFilter(d.data), searchField: '', searchValue: '' }; } else { this.ins.navigationFilter = undefined; } // 加载右侧数据 const p = { pageInfo: { pageIndex: this.ins.gridOptions.pageIndex, pageSize: this.ins.gridOptions.pageSize } }; Object.assign(p, { search: this.ins._searchState }); return this.ins.httpMgr.getData(p, 'list'); }) ) .subscribe(res => { this.ins.closeLoading(); this.ins.loadDataWhenOpen = true; if (this.ins.useFavorite && !this.ins.isTree()) { this.ins.favHelper.updateFavoritesStatus(res.items); } this.ins.loadDataTableData(res); setTimeout(() => { // 选中数据 this.ins.selectionMgr.selectCurrentValue(); this.ins.changeDetector.detectChanges(); }); }); return this.ins.leftComponentRef.instance.createComponent(); } // 获取关联数据, 右侧数据中 关联各字段的值 private getNavigationFilter(navRow: any) { if (this.ins.navigationOptions.relations && this.ins.navigationOptions.relations.length) { const result = []; this.ins.navigationOptions.relations.forEach(r => { const k = r.groupField; const dField = r.helpField; const rf = { fieldName: dField, fieldValue: '' }; rf.fieldValue = k.split('.').reduce((o, c) => { return o[c]; }, navRow); result.push(rf); }); return result; } return ''; } }