2018-05-14 07:26:04 -05:00
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
2022-01-04 02:24:25 -06:00
|
|
|
// Copyright (C) 2013 - 2022, The pgAdmin Development Team
|
2018-05-14 07:26:04 -05:00
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
import {Tree} from '../../../pgadmin/static/js/tree/tree';
|
2021-09-27 06:14:26 -05:00
|
|
|
import {ManageTreeNodes, TreeNode} from '../../../pgadmin/static/js/tree/tree_nodes';
|
2018-05-14 07:26:04 -05:00
|
|
|
|
|
|
|
export class TreeFake extends Tree {
|
2021-09-27 06:14:26 -05:00
|
|
|
static build(structure, pgBrowser) {
|
|
|
|
let tree = new TreeFake(pgBrowser);
|
2018-06-05 05:36:19 -05:00
|
|
|
let rootNode = tree.rootNode;
|
|
|
|
|
|
|
|
if (structure.children !== undefined) {
|
|
|
|
structure.children.forEach((child) => {
|
|
|
|
TreeFake.recursivelyAddNodes(tree, child, rootNode);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
static recursivelyAddNodes(tree, newNode, parent) {
|
|
|
|
let id = newNode.id;
|
|
|
|
let data = newNode.data ? newNode.data : {};
|
|
|
|
let domNode = newNode.domNode ? newNode.domNode : [{id: id}];
|
2021-09-27 06:14:26 -05:00
|
|
|
|
|
|
|
let parentPath = tree.translateTreeNodeIdFromReactTree([parent]);
|
|
|
|
|
|
|
|
tree.addNewNode(id, data, domNode, parentPath);
|
2018-06-05 05:36:19 -05:00
|
|
|
|
|
|
|
if (newNode.children !== undefined) {
|
|
|
|
newNode.children.forEach((child) => {
|
|
|
|
TreeFake.recursivelyAddNodes(tree, child, newNode);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:14:26 -05:00
|
|
|
constructor(pgBrowser) {
|
|
|
|
let manageTree = new ManageTreeNodes();
|
|
|
|
let tree = jasmine.createSpyObj(
|
|
|
|
'tree', ['unload', 'onTreeEvents',
|
|
|
|
'getActiveFile', 'setActiveFile',
|
|
|
|
'deSelectActiveFile', 'closeDirectory']);
|
|
|
|
tree.unload.and.callFake(function(domNode, config) {
|
2020-04-06 07:03:07 -05:00
|
|
|
config.success();
|
|
|
|
});
|
2021-09-27 06:14:26 -05:00
|
|
|
|
|
|
|
super(tree, manageTree, pgBrowser);
|
|
|
|
this.aciTreeToOurTreeTranslator = {};
|
|
|
|
|
2018-05-14 07:26:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
addNewNode(id, data, domNode, path) {
|
2021-09-27 06:14:26 -05:00
|
|
|
this.aciTreeToOurTreeTranslator[id] = id;
|
2018-05-14 07:26:04 -05:00
|
|
|
if (path !== null && path !== undefined) {
|
2021-09-27 06:14:26 -05:00
|
|
|
if (typeof(path) === 'object') path = path.join('/');
|
|
|
|
this.aciTreeToOurTreeTranslator[id] = path != '' ? path + '/' + id : id;
|
|
|
|
if (path.indexOf('/browser/') != 0) path = path != '' ? '/browser/' + path : undefined;
|
2018-05-14 07:26:04 -05:00
|
|
|
}
|
|
|
|
return super.addNewNode(id, data, domNode, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
addChild(parent, child) {
|
|
|
|
child.setParent(parent);
|
2021-09-27 06:14:26 -05:00
|
|
|
this.aciTreeToOurTreeTranslator[child.id] = this.aciTreeToOurTreeTranslator[parent.id] + '/' + child.id;
|
2018-05-14 07:26:04 -05:00
|
|
|
parent.children.push(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
hasParent(aciTreeNode) {
|
2021-09-27 06:14:26 -05:00
|
|
|
let parents = this.translateTreeNodeIdFromReactTree(aciTreeNode).split('/');
|
|
|
|
return parents.length > 1;
|
2018-05-14 07:26:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
parent(aciTreeNode) {
|
|
|
|
if (this.hasParent(aciTreeNode)) {
|
2021-09-27 06:14:26 -05:00
|
|
|
let path = this.translateTreeNodeIdFromReactTree(aciTreeNode);
|
|
|
|
return [{id: this.findNode('/browser/' + path).parent().id}];
|
2018-05-14 07:26:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:14:26 -05:00
|
|
|
translateTreeNodeIdFromReactTree(aciTreeNode) {
|
2018-06-05 05:36:19 -05:00
|
|
|
if (aciTreeNode === undefined || aciTreeNode[0] === undefined) {
|
2018-05-14 07:26:04 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.aciTreeToOurTreeTranslator[aciTreeNode[0].id];
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:14:26 -05:00
|
|
|
findNodeByDomElement(domElement) {
|
|
|
|
const path = this.translateTreeNodeIdFromReactTree(domElement);
|
|
|
|
|
|
|
|
if(!path || !path[0]) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findNode('/browser/' + path);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
getTreeNodeHierarchy(identifier) {
|
|
|
|
let idx = 0;
|
|
|
|
let node_cnt = 0;
|
|
|
|
let result = {};
|
|
|
|
let item = TreeNode.prototype.isPrototypeOf(identifier) ? identifier :
|
|
|
|
(identifier.path ? this.findNode(identifier.path) : this.findNodeByDomElement(identifier));
|
|
|
|
|
|
|
|
if (item == undefined || item == null) return null;
|
|
|
|
|
|
|
|
do {
|
|
|
|
const currentNodeData = item.getData();
|
|
|
|
if (currentNodeData._type in this.Nodes && this.Nodes[currentNodeData._type].hasId) {
|
|
|
|
const nodeType = mapType(currentNodeData._type, node_cnt);
|
|
|
|
if (result[nodeType] === undefined) {
|
|
|
|
result[nodeType] = _.extend({}, currentNodeData, {
|
|
|
|
'priority': idx,
|
|
|
|
});
|
|
|
|
idx -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node_cnt += 1;
|
|
|
|
item = item.hasParent() ? item.parent() : null;
|
|
|
|
} while (item);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-14 07:26:04 -05:00
|
|
|
itemData(aciTreeNode) {
|
|
|
|
let node = this.findNodeByDomElement(aciTreeNode);
|
|
|
|
if (node === undefined || node === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return node.getData();
|
|
|
|
}
|
|
|
|
|
|
|
|
selected() {
|
|
|
|
return this.selectedNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
selectNode(selectedNode) {
|
|
|
|
this.selectedNode = selectedNode;
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 06:14:26 -05:00
|
|
|
|
|
|
|
function mapType(type, idx) {
|
|
|
|
return (type === 'partition' && idx > 0) ? 'table' : type;
|
|
|
|
}
|