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);
|
2022-04-13 07:05:01 -05:00
|
|
|
this.treeToOurTreeTranslator = {};
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2018-05-14 07:26:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
addNewNode(id, data, domNode, path) {
|
2022-04-13 07:05:01 -05:00
|
|
|
this.treeToOurTreeTranslator[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('/');
|
2022-04-13 07:05:01 -05:00
|
|
|
this.treeToOurTreeTranslator[id] = path != '' ? path + '/' + id : id;
|
2021-09-27 06:14:26 -05:00
|
|
|
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);
|
2022-04-13 07:05:01 -05:00
|
|
|
this.treeToOurTreeTranslator[child.id] = this.treeToOurTreeTranslator[parent.id] + '/' + child.id;
|
2018-05-14 07:26:04 -05:00
|
|
|
parent.children.push(child);
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:05:01 -05:00
|
|
|
hasParent(treeNode) {
|
|
|
|
let parents = this.translateTreeNodeIdFromReactTree(treeNode).split('/');
|
2021-09-27 06:14:26 -05:00
|
|
|
return parents.length > 1;
|
2018-05-14 07:26:04 -05:00
|
|
|
}
|
|
|
|
|
2022-04-13 07:05:01 -05:00
|
|
|
parent(treeNode) {
|
|
|
|
if (this.hasParent(treeNode)) {
|
|
|
|
let path = this.translateTreeNodeIdFromReactTree(treeNode);
|
2021-09-27 06:14:26 -05:00
|
|
|
return [{id: this.findNode('/browser/' + path).parent().id}];
|
2018-05-14 07:26:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:05:01 -05:00
|
|
|
translateTreeNodeIdFromReactTree(treeNode) {
|
|
|
|
if (treeNode === undefined || treeNode[0] === undefined) {
|
2018-05-14 07:26:04 -05:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-13 07:05:01 -05:00
|
|
|
return this.treeToOurTreeTranslator[treeNode[0].id];
|
2018-05-14 07:26:04 -05:00
|
|
|
}
|
|
|
|
|
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 = {};
|
2022-01-18 03:19:54 -06:00
|
|
|
let item = null;
|
|
|
|
if (TreeNode.prototype.isPrototypeOf(identifier)) {
|
|
|
|
item = identifier;
|
|
|
|
} else {
|
|
|
|
item = identifier.path ? this.findNode(identifier.path) : this.findNodeByDomElement(identifier);
|
|
|
|
}
|
2021-09-27 06:14:26 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:05:01 -05:00
|
|
|
itemData(treeNode) {
|
|
|
|
let node = this.findNodeByDomElement(treeNode);
|
2018-05-14 07:26:04 -05:00
|
|
|
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;
|
|
|
|
}
|