UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

683 lines (682 loc) • 20.8 kB
import * as i0 from "@angular/core"; import { Injectable, InjectionToken } from "@angular/core"; import { BehaviorSubject } from "rxjs"; var NzTreeNode = class NzTreeNode { _title = ""; key; level = 0; origin; parentNode = null; _icon = ""; _children = []; _isLeaf = false; _isChecked = false; _isSelectable = false; _isDisabled = false; _isDisableCheckbox = false; _isExpanded = false; _isHalfChecked = false; _isSelected = false; _isLoading = false; canHide = false; isMatched = false; service = null; component; isStart; isEnd; get treeService() { return this.service || this.parentNode && this.parentNode.treeService; } constructor(option, parent = null, service = null) { if (option instanceof NzTreeNode) return option; this.service = service || null; this.origin = option; this.key = option.key; this.parentNode = parent; this._title = option.title || "---"; this._icon = option.icon || ""; this._isLeaf = option.isLeaf || false; this._children = []; this._isChecked = option.checked || false; this._isSelectable = option.disabled || option.selectable !== false; this._isDisabled = option.disabled || false; this._isDisableCheckbox = option.disableCheckbox || false; this._isExpanded = option.isLeaf ? false : option.expanded || false; this._isHalfChecked = false; this._isSelected = !option.disabled && option.selected || false; this._isLoading = false; this.isMatched = false; if (parent) this.level = parent.level + 1; else this.level = 0; const s = this.treeService; s?.treeNodePostProcessor?.(this); if (typeof option.children !== "undefined" && option.children !== null) option.children.forEach((nodeOptions) => { if (s && !s.isCheckStrictly && option.checked && !option.disabled && !nodeOptions.disabled && !nodeOptions.disableCheckbox) nodeOptions.checked = option.checked; this._children.push(new NzTreeNode(nodeOptions, this)); }); } get title() { return this._title; } set title(value) { this._title = value; this.update(); } get icon() { return this._icon; } set icon(value) { this._icon = value; this.update(); } get children() { return this._children; } set children(value) { this._children = value; this.update(); } get isLeaf() { return this._isLeaf; } set isLeaf(value) { this._isLeaf = value; this.update(); } get isChecked() { return this._isChecked; } set isChecked(value) { this._isChecked = value; this.origin.checked = value; this.afterValueChange("isChecked"); } get isHalfChecked() { return this._isHalfChecked; } set isHalfChecked(value) { this._isHalfChecked = value; this.afterValueChange("isHalfChecked"); } get isSelectable() { return this._isSelectable; } set isSelectable(value) { this._isSelectable = value; this.update(); } get isDisabled() { return this._isDisabled; } set isDisabled(value) { this._isDisabled = value; this.update(); } get isDisableCheckbox() { return this._isDisableCheckbox; } set isDisableCheckbox(value) { this._isDisableCheckbox = value; this.update(); } get isExpanded() { return this._isExpanded; } set isExpanded(value) { this._isExpanded = value; this.origin.expanded = value; this.afterValueChange("isExpanded"); this.afterValueChange("reRender"); } get isSelected() { return this._isSelected; } set isSelected(value) { this._isSelected = value; this.origin.selected = value; this.afterValueChange("isSelected"); } get isLoading() { return this._isLoading; } set isLoading(value) { this._isLoading = value; this.update(); } setSyncChecked(checked = false, halfChecked = false) { this.setChecked(checked, halfChecked); if (this.treeService && !this.treeService.isCheckStrictly) this.treeService.conduct(this); } setChecked(checked = false, halfChecked = false) { this.origin.checked = checked; this.isChecked = checked; this.isHalfChecked = halfChecked; } setExpanded(value) { this._isExpanded = value; this.origin.expanded = value; this.afterValueChange("isExpanded"); } getParentNode() { return this.parentNode; } getChildren() { return this.children; } addChildren(children, childPos = -1) { if (!this.isLeaf) { children.forEach((node) => { const refreshLevel = (n) => { n.getChildren().forEach((c) => { c.level = c.getParentNode().level + 1; c.origin.level = c.level; refreshLevel(c); }); }; let child = node; if (child instanceof NzTreeNode) child.parentNode = this; else child = new NzTreeNode(node, this); child.level = this.level + 1; child.origin.level = child.level; refreshLevel(child); try { childPos === -1 ? this.children.push(child) : this.children.splice(childPos, 0, child); } catch {} }); this.origin.children = this.getChildren().map((v) => v.origin); this.isLoading = false; } this.afterValueChange("addChildren"); this.afterValueChange("reRender"); } clearChildren() { this.afterValueChange("clearChildren"); this.children = []; this.origin.children = []; this.afterValueChange("reRender"); } remove() { const parentNode = this.getParentNode(); if (parentNode) { parentNode.children = parentNode.getChildren().filter((v) => v.key !== this.key); parentNode.origin.children = parentNode.origin.children.filter((v) => v.key !== this.key); this.afterValueChange("remove"); this.afterValueChange("reRender"); } } afterValueChange(key) { if (this.treeService) switch (key) { case "isChecked": this.treeService.setCheckedNodeList(this); break; case "isHalfChecked": this.treeService.setHalfCheckedNodeList(this); break; case "isExpanded": this.treeService.setExpandedNodeList(this); break; case "isSelected": this.treeService.setNodeActive(this); break; case "clearChildren": this.treeService.afterRemove(this.getChildren()); break; case "remove": this.treeService.afterRemove([this]); break; case "reRender": this.treeService.flattenTreeData(this.treeService.rootNodes, this.treeService.getExpandedNodeList().map((v) => v.key)); } this.update(); } update() { if (this.component) this.component.markForCheck(); } }; function isCheckDisabled(node) { const { isDisabled, isDisableCheckbox } = node; return !!(isDisabled || isDisableCheckbox); } function isInArray(needle, haystack) { return haystack.length > 0 && haystack.indexOf(needle) > -1; } function getPosition(level, index) { return `${level}-${index}`; } function getKey(key, pos) { if (key !== null && key !== void 0) return key; return pos; } function flattenTreeData(treeNodeList = [], expandedKeys = []) { const expandedKeySet = new Set(expandedKeys === true ? [] : expandedKeys); const flattenList = []; function dig(list, parent = null) { return list.map((treeNode, index) => { const pos = getPosition(parent ? parent.pos : "0", index); const mergedKey = getKey(treeNode.key, pos); treeNode.isStart = [...parent ? parent.isStart : [], index === 0]; treeNode.isEnd = [...parent ? parent.isEnd : [], index === list.length - 1]; const flattenNode = { parent, pos, children: [], data: treeNode, isStart: [...parent ? parent.isStart : [], index === 0], isEnd: [...parent ? parent.isEnd : [], index === list.length - 1] }; flattenList.push(flattenNode); if (expandedKeys === true || expandedKeySet.has(mergedKey) || treeNode.isExpanded) flattenNode.children = dig(treeNode.children || [], flattenNode); else flattenNode.children = []; return flattenNode; }); } dig(treeNodeList); return flattenList; } var NzTreeBaseService = class NzTreeBaseService { DRAG_SIDE_RANGE = .25; DRAG_MIN_GAP = 2; isCheckStrictly = false; isMultiple = false; selectedNode; rootNodes = []; flattenNodes$ = new BehaviorSubject([]); selectedNodeList = []; expandedNodeList = []; checkedNodeList = []; halfCheckedNodeList = []; matchedNodeList = []; treeNodePostProcessor; initTree(nzNodes) { this.rootNodes = nzNodes; this.expandedNodeList = []; this.selectedNodeList = []; this.halfCheckedNodeList = []; this.checkedNodeList = []; this.matchedNodeList = []; } flattenTreeData(nzNodes, expandedKeys = []) { this.flattenNodes$.next(flattenTreeData(nzNodes, expandedKeys).map((item) => item.data)); } getSelectedNode() { return this.selectedNode; } getSelectedNodeList() { return this.conductNodeState("select"); } getCheckedNodeKeys() { const keys = []; const checkedNodes = this.getCheckedNodeList(); const calc = (nodes) => { nodes.forEach((node) => { keys.push(node.key); if (node.children.length < 1) return; calc(node.children); }); }; calc(checkedNodes); return keys; } getCheckedNodeList() { return this.conductNodeState("check"); } getHalfCheckedNodeList() { return this.conductNodeState("halfCheck"); } getExpandedNodeList() { return this.conductNodeState("expand"); } getMatchedNodeList() { return this.conductNodeState("match"); } isArrayOfNzTreeNode(value) { return value.every((item) => item instanceof NzTreeNode); } setSelectedNode(node) { this.selectedNode = node; } setNodeActive(node) { if (!this.isMultiple && node.isSelected) { this.selectedNodeList.forEach((n) => { if (node.key !== n.key) n.isSelected = false; }); this.selectedNodeList = []; } this.setSelectedNodeList(node, this.isMultiple); } setSelectedNodeList(node, isMultiple = false) { const index = this.getIndexOfArray(this.selectedNodeList, node.key); if (isMultiple) { if (node.isSelected && index === -1) this.selectedNodeList.push(node); } else if (node.isSelected && index === -1) this.selectedNodeList = [node]; if (!node.isSelected) this.selectedNodeList = this.selectedNodeList.filter((n) => n.key !== node.key); } setHalfCheckedNodeList(node) { const index = this.getIndexOfArray(this.halfCheckedNodeList, node.key); if (node.isHalfChecked && index === -1) this.halfCheckedNodeList.push(node); else if (!node.isHalfChecked && index > -1) this.halfCheckedNodeList = this.halfCheckedNodeList.filter((n) => node.key !== n.key); } setCheckedNodeList(node) { const index = this.getIndexOfArray(this.checkedNodeList, node.key); if (node.isChecked && index === -1) this.checkedNodeList.push(node); else if (!node.isChecked && index > -1) this.checkedNodeList = this.checkedNodeList.filter((n) => node.key !== n.key); } conductNodeState(type = "check") { let resultNodesList = []; switch (type) { case "select": resultNodesList = this.selectedNodeList; break; case "expand": resultNodesList = this.expandedNodeList; break; case "match": resultNodesList = this.matchedNodeList; break; case "check": { resultNodesList = this.checkedNodeList; const isIgnore = (node) => { const parentNode = node.getParentNode(); if (parentNode) { if (this.checkedNodeList.findIndex((n) => n.key === parentNode.key) > -1) return true; else return isIgnore(parentNode); } return false; }; if (!this.isCheckStrictly) resultNodesList = this.checkedNodeList.filter((n) => !isIgnore(n)); break; } case "halfCheck": if (!this.isCheckStrictly) resultNodesList = this.halfCheckedNodeList; } return resultNodesList; } setExpandedNodeList(node) { if (node.isLeaf) return; const index = this.getIndexOfArray(this.expandedNodeList, node.key); if (node.isExpanded && index === -1) this.expandedNodeList.push(node); else if (!node.isExpanded && index > -1) this.expandedNodeList.splice(index, 1); } setMatchedNodeList(node) { const index = this.getIndexOfArray(this.matchedNodeList, node.key); if (node.isMatched && index === -1) this.matchedNodeList.push(node); else if (!node.isMatched && index > -1) this.matchedNodeList.splice(index, 1); } refreshCheckState(isCheckStrictly = false) { if (isCheckStrictly) return; this.checkedNodeList.forEach((node) => { this.conduct(node, isCheckStrictly); }); } conduct(node, isCheckStrictly = false) { const isChecked = node.isChecked; if (node && !isCheckStrictly) { this.conductUp(node); this.conductDown(node, isChecked); } } conductUp(node) { const parentNode = node.getParentNode(); if (parentNode) { if (!isCheckDisabled(parentNode)) { if (parentNode.children.every((child) => isCheckDisabled(child) || !child.isHalfChecked && child.isChecked)) { parentNode.isChecked = true; parentNode.isHalfChecked = false; } else if (parentNode.children.some((child) => child.isHalfChecked || child.isChecked)) { parentNode.isChecked = false; parentNode.isHalfChecked = true; } else { parentNode.isChecked = false; parentNode.isHalfChecked = false; } } this.setCheckedNodeList(parentNode); this.setHalfCheckedNodeList(parentNode); this.conductUp(parentNode); } } conductDown(node, value) { if (!isCheckDisabled(node)) { node.isChecked = value; node.isHalfChecked = false; this.setCheckedNodeList(node); this.setHalfCheckedNodeList(node); node.children.forEach((n) => { this.conductDown(n, value); }); } } afterRemove(nodes) { const loopNode = (node) => { this.selectedNodeList = this.selectedNodeList.filter((n) => n.key !== node.key); this.expandedNodeList = this.expandedNodeList.filter((n) => n.key !== node.key); this.checkedNodeList = this.checkedNodeList.filter((n) => n.key !== node.key); if (node.children) node.children.forEach((child) => { loopNode(child); }); }; nodes.forEach((n) => { loopNode(n); }); this.refreshCheckState(this.isCheckStrictly); } refreshDragNode(node) { if (node.children.length === 0) this.conductUp(node); else node.children.forEach((child) => { this.refreshDragNode(child); }); } resetNodeLevel(node) { const parentNode = node.getParentNode(); if (parentNode) node.level = parentNode.level + 1; else node.level = 0; for (const child of node.children) this.resetNodeLevel(child); } calcDropPosition(event) { const { clientY } = event; const { top, bottom, height } = event.target.getBoundingClientRect(); const des = Math.max(height * this.DRAG_SIDE_RANGE, this.DRAG_MIN_GAP); if (clientY <= top + des) return -1; else if (clientY >= bottom - des) return 1; return 0; } dropAndApply(targetNode, dragPos = -1) { if (!targetNode || dragPos > 1) return; const treeService = targetNode.treeService; const targetParent = targetNode.getParentNode(); const isSelectedRootNode = this.selectedNode.getParentNode(); if (isSelectedRootNode) isSelectedRootNode.children = isSelectedRootNode.children.filter((n) => n.key !== this.selectedNode.key); else this.rootNodes = this.rootNodes.filter((n) => n.key !== this.selectedNode.key); switch (dragPos) { case 0: targetNode.addChildren([this.selectedNode]); this.resetNodeLevel(targetNode); break; case -1: case 1: { const tIndex = dragPos === 1 ? 1 : 0; if (targetParent) { targetParent.addChildren([this.selectedNode], targetParent.children.indexOf(targetNode) + tIndex); const parentNode = this.selectedNode.getParentNode(); if (parentNode) this.resetNodeLevel(parentNode); } else { const targetIndex = this.rootNodes.indexOf(targetNode) + tIndex; this.rootNodes.splice(targetIndex, 0, this.selectedNode); this.rootNodes[targetIndex].parentNode = null; this.resetNodeLevel(this.rootNodes[targetIndex]); } break; } } this.rootNodes.forEach((child) => { if (!child.treeService) child.service = treeService; this.refreshDragNode(child); }); } formatEvent(eventName, node, event) { const emitStructure = { eventName, node, event }; switch (eventName) { case "dragstart": case "dragenter": case "dragover": case "dragleave": case "drop": case "dragend": Object.assign(emitStructure, { dragNode: this.getSelectedNode() }); break; case "click": case "dblclick": Object.assign(emitStructure, { selectedKeys: this.selectedNodeList }); Object.assign(emitStructure, { nodes: this.selectedNodeList }); Object.assign(emitStructure, { keys: this.selectedNodeList.map((n) => n.key) }); break; case "check": { const checkedNodeList = this.getCheckedNodeList(); Object.assign(emitStructure, { checkedKeys: checkedNodeList }); Object.assign(emitStructure, { nodes: checkedNodeList }); Object.assign(emitStructure, { keys: checkedNodeList.map((n) => n.key) }); break; } case "search": Object.assign(emitStructure, { matchedKeys: this.getMatchedNodeList() }); Object.assign(emitStructure, { nodes: this.getMatchedNodeList() }); Object.assign(emitStructure, { keys: this.getMatchedNodeList().map((n) => n.key) }); break; case "expand": Object.assign(emitStructure, { nodes: this.expandedNodeList }); Object.assign(emitStructure, { keys: this.expandedNodeList.map((n) => n.key) }); } return emitStructure; } getIndexOfArray(list, key) { return list.findIndex((v) => v.key === key); } conductCheck(keys, checkStrictly) { this.checkedNodeList = []; this.halfCheckedNodeList = []; const calc = (nodes) => { nodes.forEach((node) => { if (keys === null) node.isChecked = !!node.origin.checked; else if (isInArray(node.key, keys || [])) { node.isChecked = true; node.isHalfChecked = false; } else { node.isChecked = false; node.isHalfChecked = false; } if (node.children.length > 0) calc(node.children); }); }; calc(this.rootNodes); this.refreshCheckState(checkStrictly); } conductExpandedKeys(keys = []) { const expandedKeySet = new Set(keys === true ? [] : keys); this.expandedNodeList = []; const calc = (nodes) => { nodes.forEach((node) => { node.setExpanded(keys === true || expandedKeySet.has(node.key) || node.isExpanded === true); if (node.isExpanded) this.setExpandedNodeList(node); if (node.children.length > 0) calc(node.children); }); }; calc(this.rootNodes); } conductSelectedKeys(keys, isMulti) { this.selectedNodeList.forEach((node) => node.isSelected = false); this.selectedNodeList = []; const calc = (nodes) => nodes.every((node) => { if (isInArray(node.key, keys)) { node.isSelected = true; this.setSelectedNodeList(node); if (!isMulti) return false; } else node.isSelected = false; if (node.children.length > 0) return calc(node.children); return true; }); calc(this.rootNodes); } expandNodeAllParentBySearch(node) { const calc = (n) => { if (n) { n.canHide = false; n.setExpanded(true); this.setExpandedNodeList(n); if (n.getParentNode()) return calc(n.getParentNode()); } }; calc(node.getParentNode()); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzTreeBaseService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzTreeBaseService }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzTreeBaseService, decorators: [{ type: Injectable }] }); const NzTreeHigherOrderServiceToken = new InjectionToken(typeof ngDevMode !== "undefined" && ngDevMode ? "nz-tree-higher-order" : ""); var NzTreeBase = class { nzTreeService; constructor(nzTreeService) { this.nzTreeService = nzTreeService; } coerceTreeNodes(value) { let nodes = []; if (!this.nzTreeService.isArrayOfNzTreeNode(value)) nodes = value.map((item) => new NzTreeNode(item, null, this.nzTreeService)); else nodes = value.map((item) => { item.service = this.nzTreeService; return item; }); return nodes; } getTreeNodes() { return this.nzTreeService.rootNodes; } getTreeNodeByKey(key) { const nodes = []; const getNode = (node) => { nodes.push(node); node.getChildren().forEach((n) => { getNode(n); }); }; this.getTreeNodes().forEach((n) => { getNode(n); }); return nodes.find((n) => n.key === key) || null; } getCheckedNodeList() { return this.nzTreeService.getCheckedNodeList(); } getSelectedNodeList() { return this.nzTreeService.getSelectedNodeList(); } getHalfCheckedNodeList() { return this.nzTreeService.getHalfCheckedNodeList(); } getExpandedNodeList() { return this.nzTreeService.getExpandedNodeList(); } getMatchedNodeList() { return this.nzTreeService.getMatchedNodeList(); } }; export { NzTreeBase, NzTreeBaseService, NzTreeHigherOrderServiceToken, NzTreeNode, flattenTreeData, getKey, getPosition, isCheckDisabled, isInArray }; //# sourceMappingURL=ng-zorro-antd-core-tree.mjs.map