UNPKG

armisa-models

Version:
417 lines (416 loc) 15.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StructrulCodeTreeView = void 0; const ElementsOfFormFactory_1 = require("../../Page/ElementsOfFormFactory"); const SelfCheckedIDs_1 = require("../../SelfModels/SelfCheckedIDs"); const StructrulCodeTreeViewCode_1 = require("./StructrulCodeTreeViewCode"); class StructrulCodeTreeView { get any() { return this; } constructor(mainStateFactory, selfStructuralCode, onlyLastLevel, closeModal) { this.mainStateFactory = mainStateFactory; this.selfStructuralCode = selfStructuralCode; this.onlyLastLevel = onlyLastLevel; this.closeModal = closeModal; this.forceUpdate = () => { }; this.code = ''; this._nodes = []; this.waitForLoad = true; this.currentNodeWaitForLoading = false; this.waitForEndLoading = () => { return new Promise((resolve) => { if (this.waitForLoad || this.currentNodeWaitForLoading) { this.endingWaitForEndLoading = () => { resolve(); }; } else { resolve(); } }); }; this.endingWaitForEndLoading = () => { }; this.selectedRow = null; this.getFlatten = (nodes) => { if (nodes === null) { return []; } return nodes.reduce((result, item) => { return [...result, item, ...this.getFlatten(item.children)]; }, []); }; this.loadData = () => { this.waitForLoad = true; this.selfStructuralCode.apiAxios.get(this.selfStructuralCode.urls.tree) .then(response => { const result = response.data; this.checkStates = SelfCheckedIDs_1.SelfCheckedIDs.deserialize(result).checkStates; this.loadTreeView(); }) .catch(e => { console.error(e); this.waitForLoad = false; this.endingWaitForEndLoading(); //this.events.trigger('form.wasErrored', e); }); }; this.loadTreeView = () => { const _findPropriateNodeByThisCodeForSelect = (code) => { if (!code && this.flatten && this.flatten.length) { this.selectedRow = this.flatten[0]; return; } let lastFind = null; const _set = (item) => { this.selectedRow = item; if (item.childCount && item.children && item.children.length) { item.isExpanded = true; } }; this.flatten.forEach((item) => { if (code === item.code) { _set(item); return; } else if (code.length > item.code.length && code.substring(0, item.code.length) === item.code) { _set(item); lastFind = item; } }); if (lastFind) { this._expandParent(lastFind); } }; this.waitForLoad = true; this.selfStructuralCode.apiAxios.get(this.selfStructuralCode.urls.search, { params: { code: this.code } }) .then((response) => { const nodes = response.data.map((item) => this.deserializeRow(item)); this._nodes = nodes; this.checkStates = []; this._updateFlatten(); _findPropriateNodeByThisCodeForSelect(this.code); this.waitForLoad = false; this.endingWaitForEndLoading(); this.forceUpdate(); }) .catch((error) => { this.waitForLoad = false; this.endingWaitForEndLoading(); this.forceUpdate(); console.error(error); }); }; this.nodeCheckChange = (node) => { if (!this.checkStates) { this.checkStates = []; } if (this.flatten.find((i) => i.id === node.id && node.check === 1)) { this.removeCheckToNode(node); } else { this.addCheckToNode(node); } this.forceUpdate(); }; this.expandCurrentNode = () => { const currentNode = this.currentNode(); if (currentNode) { this.expandThisNode(currentNode); } }; this.expandThisNode = (node) => { if (node.childCount && node.children && node.children.length) { node.isExpanded = true; this.selectThisNodeFirstChild(node); } else if (node.childCount && !(node.children && node.children.length)) { node.isExpanded = true; node.isLoading = true; this.forceUpdate(); this.getTreeViewChildren(node); } }; this.getTreeViewChildren = (node) => { node.isLoading = true; node.isExpanded = true; this.currentNodeWaitForLoading = true; this.forceUpdate(); this.selfStructuralCode.apiAxios .get(`${this.selfStructuralCode.urls.treeChild}/${node.id}`) .then((response) => { node.children = response.data.map((item) => this.deserializeRow(item)); node.isLoading = false; this.currentNodeWaitForLoading = false; this._updateFlatten(); this.selectThisNodeFirstChild(node); this.forceUpdate(); }) .catch((error) => { this.currentNodeWaitForLoading = false; node.isLoading = false; console.error(error); this.forceUpdate(); }); }; this.collapseCurrentNode = () => { const currentNode = this.currentNode(); if (currentNode) { this.collapseThisNode(currentNode); } }; this.collapseThisNode = (node) => { if (node.isExpanded) { node.isExpanded = false; this.forceUpdate(); } else { if (node.parentId) { const parentNode = this.flatten.find((i) => i.id === node.parentId); if (parentNode) { this.selectThisNode(parentNode); } } } }; this.toggleThisNode = (node) => { if (node.isExpanded) { this.collapseThisNode(node); } else { this.expandThisNode(node); } }; this._updateFlatten = () => { this._flatten = this.getFlatten(this.nodes); }; this.currentNode = () => { return this.flatten.find((i) => i === this.selectedRow); }; this.selectThisNode = (node) => { this.selectedRow = node; this.forceUpdate(); }; this._expandParent = (node) => { if (!node.parentId) { return; } const parentNode = this.flatten.find((i) => i.id === node.parentId); if (parentNode) { parentNode.isExpanded = true; this._expandParent(parentNode); } }; this.deserializeRow = (json) => { if (this.checkStates) { if (this.checkStates.find((i) => i.id === json.id && !i.isFromChild)) { json.check = 1; } else if (this.checkStates.find((i) => i.id === json.id && i.isFromChild)) { json.check = 4; } else if (this.checkStates.find((i) => i.id === json.parentId && !i.isFromChild)) { this.checkStates?.push({ id: json.id, isFromChild: false, isFromParent: true, code: json }); json.check = 4; } } const result = StructrulCodeTreeViewCode_1.StructrulCodeTreeViewCode.deserialize(json, json.children || []); return result; }; this.acceptCurrentRow = () => { if (this.selectedRow) { this.closeModal(); this.selfStructuralCode.focusToElement(); this.selfStructuralCode.onChangeHandlerCode(this.selectedRow.code); this.selfStructuralCode.getCodeData(); } }; this.cancelForm = () => { this.closeModal(); this.selfStructuralCode.focusToElement(); }; this.mainStateManager = this.mainStateFactory.mainStateManager; this.elementsOfForm = new ElementsOfFormFactory_1.ElementsOfFormFactory(this.mainStateFactory); this.code = selfStructuralCode.code; } get nodes() { return this._nodes; } get flatten() { const flatten = this._flatten; if (flatten) { return flatten; } else { return this.getFlatten(this.nodes); } } selectPreviousNode() { const currentNode = this.currentNode(); if (currentNode) { const siblingNodes = this.flatten.filter((i) => i.parentId === currentNode.parentId); if (siblingNodes && siblingNodes.length > 1) { const currentNodeIndex = siblingNodes.indexOf(currentNode); if (currentNodeIndex > 0) { this.selectThisNode(siblingNodes[currentNodeIndex - 1]); } else if (currentNodeIndex === 0) { this.selectThisNode(siblingNodes[siblingNodes.length - 1]); } } } else { this.selectThisNode(this.flatten[0]); } } selectNextNode() { const currentNode = this.currentNode(); if (currentNode) { const siblingNodes = this.flatten.filter((i) => i.parentId === currentNode.parentId); if (siblingNodes && siblingNodes.length > 1) { const currentNodeIndex = siblingNodes.indexOf(currentNode); if (currentNodeIndex > -1 && currentNodeIndex < siblingNodes.length - 1) { this.selectThisNode(siblingNodes[currentNodeIndex + 1]); } else if (currentNodeIndex === siblingNodes.length - 1) { this.selectThisNode(siblingNodes[0]); } } } else { this.selectThisNode(this.flatten[0]); } } removeCheckToNode(node) { if (node.parentId) { this.removeCheckToParent(node); } this.removeCheckToChild(node); this.checkStates = this.checkStates?.filter((i) => i.id !== node.id); node.check = 0; } removeCheckToParent(node) { if (node.parentId) { const child = this.flatten.filter((i) => i.parentId === node.parentId && i.check > 0); if (child.length === 1) { const parent = this.flatten.find((i) => i.id === node.parentId); this.removeCheckToParent(parent); this.checkStates = this.checkStates?.filter((i) => i.id !== parent.id); parent.check = 0; } } } removeCheckToChild(node) { const children = this.flatten.filter((i) => i.parentId === node.id); if (children && children.length) { for (const child of children) { this.removeCheckToChild(child); } } this.checkStates = this.checkStates?.filter((i) => i.id !== node.id); node.check = 0; } addCheckToNode(node) { if (this.onlyLastLevel && node.childCount > 0) { return; } if (node.parentId) { this.addCheckType4ToParent(this.flatten.find((i) => i.id === node.parentId)); } this.addCheckType4ToChild(node); this.checkStates?.push({ id: node.id, isFromChild: false, isFromParent: false, code: node, }); node.check = 1; } addCheckType4ToParent(node) { if (node.parentId) { this.addCheckType4ToParent(this.flatten.find((i) => i.id === node.parentId)); } this.checkStates?.push({ id: node.id, isFromChild: true, isFromParent: false, code: node, }); node.check = 4; } addCheckType4ToChild(node) { const children = this.flatten.filter((i) => i.parentId === node.id); if (children && children.length) { for (const child of children) { this.addCheckType4ToChild(child); } } this.checkStates?.push({ id: node.id, isFromChild: true, isFromParent: false, code: node, }); node.check = 4; } _setUnCheckNodeCheckType(mainNode) { mainNode.check = 0; if (mainNode.children && mainNode.children.length) { mainNode.children.map((i) => this._setUnCheckNodeCheckType(i)); } } _setParentCheckNodeCheckType(mainNode, check) { mainNode.check = check; if (mainNode.children && mainNode.children.length) { mainNode.children.map((i) => this._setParentCheckNodeCheckType(i, 4)); } } _setDoubleCheckNodeCheckType(mainNode) { mainNode.check = 2; if (mainNode.children && mainNode.children.length) { mainNode.children.map((i) => this._setDoubleCheckNodeCheckType(i)); } } _setSingleCheckNodeCheckType(mainNode) { mainNode.check = 3; if (mainNode.children && mainNode.children.length) { mainNode.children.map((i) => this._setSingleCheckNodeCheckType(i)); } } selectFirstNode() { if (this.nodes.length > 0) { this.selectThisNode(this.nodes[0]); } } selectLastNode() { if (this.nodes.length > 0) { this.selectThisNode(this.nodes[this.nodes.length - 1]); } } get canCloseItsModal() { const currentNode = this.currentNode(); if (currentNode) { if (currentNode.parentId) { const parentNode = this.flatten.find((i) => i.id === currentNode.parentId); if (parentNode && parentNode.isExpanded) { return false; } } } return true; } selectThisNodeFirstChild(node) { if (node.children && node.children.length) { const firstChild = (node.children)[0]; this.selectThisNode(firstChild); } } } exports.StructrulCodeTreeView = StructrulCodeTreeView;