import { Injectable, NgZone } from '@angular/core'; import { CommonUtils, RuntimeStateService } from '@farris/ui-common'; import { FilterRelation, FilterCondition, Compare, EntityFilter } from '@farris/ui-common/types'; import { cloneDeep } from 'lodash-es'; import { TreeInfo } from './lookup-grid-options'; @Injectable() export class LookupUtils { constructor(private utils: CommonUtils, private rts: RuntimeStateService, private ngZone: NgZone) { } setActiveLookupInstance(lookupIns: any) { if (this.rts) { this.rts.setLookupInstance(lookupIns); } } destroy() { this.rts.destroy(); } pendingStart() { if (this.rts) { this.rts.updateFormState({ lookup: { pending: true } }); // 禁用页面的所有鼠标事件 document.body.style['pointer-events'] = 'none'; } } pendingEnd() { if (this.rts) { this.rts.updateFormState({ lookup: { pending: false } }); // 激活鼠标事件 document.body.style['pointer-events'] = ''; } } private createFilterCondition(field: string, value: string) { return { filterField: field, value, lbracket: '', rbracket: '', relation: FilterRelation.Or, compare: Compare.Like }; } mergeCondition(condition: EntityFilter, fields: string[], searchData: { field: string, value: string }) { if (!condition) { condition = { pagination: { pageIndex: 1, pageSize: 20 }, filterConditions: [], sortConditions: [] }; } else { condition = cloneDeep(condition); } const { field = '*', value = '' } = { ...searchData }; if (value) { if (field === '*') { if (fields && fields.length) { const searchConditions: FilterCondition[] = fields.map((f: string) => { return this.createFilterCondition(f, value); }); if (searchConditions.length) { searchConditions[0].lbracket = '('; const lastSearchConditions = searchConditions[searchConditions.length - 1]; lastSearchConditions.rbracket = ')'; lastSearchConditions.relation = FilterRelation.Empty; } if (condition.filterConditions && condition.filterConditions.length) { condition.filterConditions[condition.filterConditions.length - 1].relation = FilterRelation.And; condition.filterConditions = condition.filterConditions.concat(searchConditions); } else { condition.filterConditions = searchConditions; } } } else { const searchCondition = this.createFilterCondition(field, value); searchCondition.relation = FilterRelation.Empty; if (condition.filterConditions && condition.filterConditions.length) { condition.filterConditions[condition.filterConditions.length - 1].relation = FilterRelation.And; condition.filterConditions.push(searchCondition); } else { condition.filterConditions = [searchCondition]; } } } return condition; } private canSelectable(n: any) { if (n.hasOwnProperty('farris_selectable')) { return !!n['farris_selectable']; } return true; } /** 将数据转树形结构 */ makeTreeWithParentID(data: any[], parentId, parentIdField = 'parentId', idField = 'id') { const nodes = new Map(); const result = []; const unattached = []; data.forEach(t => { const node = { data: t, children: [], selectable: this.canSelectable(t), parent: null, parents: [] }; const id = t[idField]; nodes.set(id, node); const PID = this.utils.getValue(parentIdField, t); if (PID === parentId) { result.push(node); } else { const parent = nodes.get(PID); if (parent) { node.parent = PID; node.parents = [...parent.parents, PID]; parent.children.push(node); } else { unattached.push(node); } } }); unattached.forEach(n => { const pid = this.utils.getValue(parentIdField, n.data); const parent = nodes.get(pid); if (parent) { n.parent = pid; n.parents = [...parent.parents, pid]; parent.children.push(n); } }); return result; } makeTree(data, treeInfo: TreeInfo) { const treeInfoField = treeInfo.dataField; const layerField = treeInfo.layerField; const pathField = treeInfo.pathField; const nodes = new Map(); const result = []; const unattached = []; data.forEach(t => { const node = { data: t, children: [], selectable: this.canSelectable(t) }; const pathCode = t[treeInfoField][pathField]; nodes.set(pathCode, node); if (t[treeInfoField][layerField] === 1) { result.push(node); } else { const parentPathCode = pathCode.substr(0, pathCode.length - 4); const parent = nodes.get(parentPathCode); if (parent) { parent.children.push(node); } else { unattached.push(node); } } }); unattached.forEach(n => { const pathCode = n.data[treeInfoField][pathField]; const parent = nodes.get(pathCode.substr(0, pathCode.length - 4)); if (parent) { parent.children.push(n); } }); return result; } }