import { TreeTableComponent, TreeNode } from '@farris/ui-treetable'; import { FavoriteAction, FAVORITE_FIELD_NAME } from '../lookup-displaytype'; import { LookupGridResult } from '../lookup-grid-options'; import { cloneDeep } from 'lodash-es'; import { Observable, of, Subject } from 'rxjs'; import { LookupGridComponent } from '../lookup-grid.component'; import { take, tap, catchError } from 'rxjs/operators'; import { LookupSelectionService } from './lookup-selection.service'; export class TreeTableEventManager { private lookupSelectionSer: LookupSelectionService; /** * 标识当前数据是否查询结果 */ private IS_SEARCH_RESULT = false; constructor(private ins: LookupGridComponent) { this.lookupSelectionSer = this.ins.lookupSelectionSer; } searchTreeData(search: { field: string; value: string }) { const tt = this.ins.componentRef.instance as TreeTableComponent; // 判断uri 发出search.emit(); if (this.ins.searching) { return; } else { this.ins.searching = true; } return this.ins.httpMgr.getData({ search, sortName: tt.sortName, sortOrder: tt.sortOrder }, 'search').pipe( tap(() => { this.ins.searching = false; }), catchError((err) => { this.ins.searching = false; return of(err); }) ).subscribe(resData => { if (resData) { this.ins.items = resData.items; this.ins.closeLoading(); tt.clearAll(); const treeInfo = this.ins.gridOptions.treeInfo; if (!treeInfo['treeDataIsInit']) { if (treeInfo.layerType.toLowerCase() === 'pathcode') { this.ins.items = this.ins.lookupUtils.makeTree(this.ins.items, treeInfo); } else { this.ins.items = this.ins.lookupUtils.makeTreeWithParentID( this.ins.items, '', `${treeInfo.dataField}.${treeInfo.parentField}`, this.ins.idField ); } } const checkNodes = (nodes): TreeNode[] => { if (nodes && nodes.length) { return nodes.map((node: TreeNode) => { if (node.hasOwnProperty('addtional')) { node.selectable = !node['addtional']; } if (node.children && node.children.length) { checkNodes(node.children); } return node; }); } return nodes; }; const expandFirstNode = (nodes): TreeNode[] => { if (nodes && nodes.length) { return nodes.map((node: TreeNode) => { if (node.children && node.children.length) { node.expanded = true; expandFirstNode(node.children); } return node; }); } return nodes; }; const _nodes = checkNodes(this.ins.items); if (_nodes && _nodes.length && _nodes[0].children && _nodes[0].children.length) { _nodes[0].expanded = true; _nodes[0].children = expandFirstNode(_nodes[0].children); } this.ins.items = _nodes; // 加载收藏数据 if (this.ins.useFavorite) { // 更新数据的收藏状态 this.ins.favHelper.updateFavoritesStatus(this.ins.items); } tt.loadData(this.ins.items); // // 展开查询结果。 // if (search.value && this.ins.items.length) { // tt.toggleExpand(this.ins.items[0], true); // } tt.resize(); this.IS_SEARCH_RESULT = true; this.ins.selectionMgr.selectCurrentValue(); } this.ins.search.emit(search); }); } private getChildren( parentPath, parentLayer, searchData: { searchField: string; searchValue: string } ): Observable { const uri = this.ins.gridOptions.uri; const search = { parentLayer, category: 'children', ...searchData }; if (this.ins.treeInfo.layerType === 'parentId') { search['parentId'] = parentPath; } else { search['parentPath'] = parentPath; } const param = { searchValue: JSON.stringify(search), customData: this.ins.customData, enableFullTree: this.ins.enableFullTree, loadTreeDataType: this.ins.loadTreeDataType }; if (this.IS_SEARCH_RESULT) { param.enableFullTree = false; param.loadTreeDataType = 'layerload'; search.searchValue = ''; search.searchField = '*'; param.searchValue = JSON.stringify(search); } if (this.ins.helpId) { param['helpId'] = this.ins.helpId; } const tt = this.ins.componentRef.instance as TreeTableComponent; if (tt && tt.sortName) { Object.assign(param, { sortName: tt.sortName, sortOrder: tt.sortOrder }); } return this.ins.http.getData(uri, param); } bindTreetableEvent() { const tt = this.ins.componentRef.instance as TreeTableComponent; const _searchTreeData = (searchparam: any) => { if (this.ins.remoteSearch) { this.ins._searchState = searchparam; this.searchTreeData(searchparam); } else { // TODO: 全部加载,前端搜索 需要完善 // if (this.ins.loadTreeDataType === 'loadall' && tt.searchHandle) { // tt.searchHandle.search(searchparam.field, searchparam.value, 'client'); // } this.ins.search.emit(searchparam); } }; const isLoadAllTreeData = () => { if (this.ins.loadTreeDataType === 'default') { return tt.loadDataType === 'all'; } else { return this.ins.loadTreeDataType === 'loadall'; } }; const isAsyncLoadTreeData = () => { if (this.ins.loadTreeDataType === 'default') { return tt.loadDataType === 'async'; } else { return this.ins.loadTreeDataType === 'layerload'; } }; tt.dblClickExpand = !this.ins.singleSelect; // 禁用双击展开节点 tt.allColumnsTitle = this.ins.allColumnsTitle; // this.displayInfo.allColumns; tt.idField = this.ins.idField; tt.columns = this.ins.columns; tt.searchFields = this.ins.gridOptions.searchFields; if (this.ins.treeInfo) { tt.loadDataType = this.ins.treeInfo.loadDataType; tt.virtualized = true; this.ins.allData = cloneDeep(this.ins.items); } if (this.ins.gridOptions.treeInfo) { tt.onlySelectLeaf = this.ins.gridOptions.treeInfo.onlySelectLeaf; tt.loadDataType = this.ins.gridOptions.treeInfo.loadDataType; } if (!tt.singleSelect) { tt.checkOnSelect = true; tt.selectOnCheck = true; tt.showCheckbox = true; tt.showCheckAll = this.ins.showCheckAll; // 启用多选后,同时启用级联选择 if (this.ins.enableCascade) { this.cascadeValueChanged(this.ins.cascadeStatus); } else { tt.cascadeCheck = false; tt.cascadeDown = false; tt.cascadeUp = false; } } tt.enableFindText = this.ins.enableFindText; // tt.findField = this.textField; tt.nodeSelected.subscribe((e: any) => { // if (this.ins.favoritesComponentRef && this.ins.singleSelect) { // const ftt = this.ins.favoritesComponentRef.instance as TreeTableComponent; // ftt.clearSelections(); // } if (this.ins.singleSelect) { this.lookupSelectionSer.clearSelections(); } this.lookupSelectionSer.updateSelections([e.node.data]); this.ins.checkedChange.emit({ data: [e.node.data] , isCheck: true }); }); tt.nodeChecked.subscribe( e => { if (!this.ins.singleSelect) { let data = null; if (e.nodes && e.nodes.length) { data = e.nodes.map(n => n.data); // this.ins.multiSelMgr.updateSelections(e.nodes.map(n => n.data)); } else { if (Array.isArray(e.node)) { data = e.node.map(n => n.data); // this.ins.multiSelMgr.updateSelections(e.node.map(n => n.data)); } else { data = [e.node.data]; // this.ins.multiSelMgr.updateSelections([e.node.data]); } } this.ins.multiSelMgr.updateSelections(data); this.ins.checkedChange.emit({ data, isCheck: true }); } }); tt.nodeUnChecked.subscribe((e: any) => { if (e && e.node) { if (!this.ins.singleSelect) { this.ins.multiSelMgr.remove(e.node.id); // 分层加载,多选,包含下级时,取消勾选 需要将当前节点的所有子级数据也要取消选择,即从已选记录中移除 if (tt.loadDataType !== 'all' && this.ins.treeInfo.layerType === 'pathcode' && (this.ins.cascadeStatus === 'enable' || this.ins.cascadeStatus === 'down')) { if (e.node.children && e.node.children.length) { const nodes = e.node.children.map(n => n.data); this.ins.lookupSelectionSer.updateSelections(nodes, false); } else { const pathcode = e.node.data[this.ins.treeInfo.dataField][this.ins.treeInfo.pathField]; this.ins.lookupSelectionSer.unselectByPathcode(pathcode); } } if (e.nodes && e.nodes.length) { this.ins.multiSelMgr.remove(e.nodes.map(n => n.id)); this.ins.checkedChange.emit({ data: e.nodes.map(n => n.data), isCheck: false }); } else { this.ins.checkedChange.emit({ data: [e.node.data], isCheck: false }); } } else { const ftt = this.ins.favoritesComponentRef && (this.ins.favoritesComponentRef.instance as TreeTableComponent); if (ftt && ftt.findRowNode(e.node.id)) { ftt.unSelectNode(e.node.id); } this.ins.checkedChange.emit({ data: [e.node.data], isCheck: false }); } } }); tt.checkAll.subscribe( e => { const data = e.instance.checkeds.map(n => n.data); this.ins.multiSelMgr.updateSelections(data); this.ins.checkedChange.emit({ data, isCheck: true }); }); tt.unCheckAll.subscribe(() => { this.ins.multiSelMgr.clear(); this.ins.checkedChange.emit({ data: [], isCheck: false }); }); tt.search.subscribe(searchparam => { if (searchparam.field !== '*' && !searchparam.value) { this.ins.messagerService.warning(this.ins.mustWriteSomething); } else { _searchTreeData(searchparam); } }); tt.cellClick.subscribe((e: any) => { if (e.col.field === FAVORITE_FIELD_NAME) { e.event.stopPropagation(); // tslint:disable-next-line: no-string-literal const classList = e.event.target['classList']; if (classList.contains('f-lookup-favorite')) { // classList.toggle('f-icon-star'); // classList.toggle('f-icon-star-outline'); const _this = this.ins; (function each(arr) { if (arr) { arr.forEach(item => { const id = _this.utils.getValue(_this.idField, item.data); if (id === e.node.id) { item.data[FAVORITE_FIELD_NAME] = !item.data[FAVORITE_FIELD_NAME]; return true; } else if (item.children && item.children.length > 0) { return each(item.children); } else { return false; } }); } })(this.ins.items); tt.loadData(this.ins.items); // 更新收藏数据 this.lookupSelectionSer.updateFavoriteData( e.node.data, e.node.data[FAVORITE_FIELD_NAME] ? FavoriteAction.add : FavoriteAction.delete); } } }); tt.dblClick.subscribe((treeNode: TreeNode) => { if (this.ins.gridOptions.singleSelect && treeNode.selectable) { if (this.ins.okButton) { // this.lookupSelectionSer.select(treeNode.data); // this.ins.okButton.nativeElement.click(); this.ins.selectItem(treeNode.data); } } }); const loadAllData = isLoadAllTreeData(); tt.columnSorted.subscribe((sort: { sortName: string; sortOrder: any; }) => { if (isLoadAllTreeData()) { tt.clientSort(); } else { const { sortName, sortOrder } = { ...sort }; const col = this.ins.columns.find(n => n.field === sortName); const _sortName = col ? col.fieldPath ? col.fieldPath : col.field : sortName; const param = Object.assign({ sortName: _sortName, sortOrder }, { search: this.ins._searchState }); this.ins.httpMgr.getData(param, 'search').subscribe(d => { this.ins.items = d.items; this.ins.closeLoading(); tt.clearAll(); const items = this.ins.checkNodeCanBeSelect(d.items, false); tt.loadData(items); tt.resize(); }); } }); tt.clearSearchValue.subscribe(() => { this.ins._searchState = null; this.IS_SEARCH_RESULT = false; const _items = this.ins.checkNodeCanBeSelect(this.ins.allData, loadAllData); if (!_items || !_items.length) { _searchTreeData({ field: '*', value: '' }); } else { this.ins.items = _items; this.ins.favHelper.updateFavoritesStatus(this.ins.items); tt.loadData(_items); } }); tt.expand.subscribe((tn: TreeNode) => { if (tn.leaf) { return; } if (!tn.children || !tn.children.length) { const treeInfo = this.ins.gridOptions.treeInfo; if (isAsyncLoadTreeData() || this.IS_SEARCH_RESULT ) { let parentPath = ''; let parentLayer = -1; const { field = '*', value = '' } = { ...tt.searchData }; tn['showLoading'] = true; tt.detectChanges(); if (treeInfo.layerType === 'parentId') { // 父ID加载方式 parentPath = tn['id']; } else { const treeInfoField = treeInfo.dataField; if (treeInfoField) { parentPath = tn.data[treeInfoField][treeInfo.pathField]; parentLayer = tn.data[treeInfoField][treeInfo.layerField]; } else { console.log('未找到分级信息。'); } } if (!this.ins.uri) { this.ins.expandTreeNode.emit({ instance: tt, node: tn, parentIdOrPath: parentPath, parentLayer, search: { value, field } }); return; } this.getChildren(parentPath, parentLayer, { searchField: field === '*' ? '*' : field, searchValue: value }).subscribe(data => { if (tt) { if (tn && data.items && data.items.length) { if (this.ins.useFavorite) { // 更新子节点收藏状态 this.ins.favHelper.updateFavoritesStatus(data.items); } const nodes = this.ins.checkNodeCanBeSelect(data.items, false); tt.appendChildren(nodes, tn); if (tt.loadDataType !== 'all' && !this.ins.singleSelect && this.ins.isGetAllChidlNodes && (this.ins.cascadeStatus === 'enable' || this.ins.cascadeStatus === 'down') ) { const rn = tt.findRowNode(tn.id); tt.propagateSelectionDown(rn, rn.isChecked); // 更新选中记录缓存 const _items = data.items.filter(n => !n.addtional); if (_items && _items.length) { this.ins.lookupSelectionSer.updateSelections(_items.map(n => n.data), rn.isChecked); } } } tn['showLoading'] = false; tt.detectChanges(); this.ins.selectionMgr.selectCurrentValue(); } }); } } else { if (!this.ins.singleSelect && this.ins.enableCascade && (this.ins.cascadeStatus === 'enable' || this.ins.cascadeStatus === 'down') && this.ins.isGetAllChidlNodes ) { const rn = tt.findRowNode(tn.id); tt.propagateSelectionDown(rn, rn.isChecked); const selectItems = tn.children.filter(n => n.selectable).map(n => n.data); this.ins.lookupSelectionSer.updateSelections(selectItems, rn.isChecked); } // this.ins.selectionMgr.selectCurrentValue(); } }); if (loadAllData && this.ins.items) { this.ins.treeNodeHelper.updateTreeNodeExpanded(this.ins.items); } return loadAllData; } cascadeValueChanged($event) { const val = $event; // const tt = this.ins.componentRef.instance as TreeTableComponent; const instanceTyp = this.ins.activeTab === 'datalist' ? 'treetable' : 'fav'; const tt = this.ins.lookupCmpMgr.getComponentInstance(instanceTyp) as TreeTableComponent; if (!tt) { return; } switch (val) { case 'enable': tt.cascadeCheck = true; tt.cascadeDown = true; tt.cascadeUp = true; break; case 'disable': tt.cascadeCheck = false; tt.cascadeDown = false; tt.cascadeUp = false; break; case 'up': tt.cascadeCheck = true; tt.cascadeUp = true; tt.cascadeDown = false; break; case 'down': tt.cascadeCheck = true; tt.cascadeDown = true; tt.cascadeUp = false; break; default: tt.cascadeCheck = true; tt.cascadeDown = true; tt.cascadeUp = true; break; } this.ins.cascadeStatus = val || 'enable'; } }