2018-03-29 05:20:00 +00:00
|
|
|
const frappe = require('frappejs');
|
|
|
|
const octicons = require('octicons');
|
|
|
|
const utils = require('frappejs/client/ui/utils');
|
|
|
|
|
|
|
|
class Tree {
|
2018-03-29 07:49:08 +00:00
|
|
|
constructor({parent, label, iconSet, withSkeleton, method}) {
|
2018-03-29 05:20:00 +00:00
|
|
|
Object.assign(this, arguments[0]);
|
|
|
|
this.nodes = {};
|
|
|
|
if(!iconSet) {
|
|
|
|
this.iconSet = {
|
2018-03-29 12:05:02 +00:00
|
|
|
open: octicons["triangle-down"].toSVG({ "width": 10, "class": "node-parent"}),
|
|
|
|
closed: octicons["triangle-right"].toSVG({ "width": 5, "class": "node-parent"}),
|
|
|
|
leaf: octicons["primitive-dot"].toSVG({ "width": 7, "class": "node-leaf"})
|
2018-03-29 05:20:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
this.make();
|
|
|
|
}
|
|
|
|
|
|
|
|
make() {
|
|
|
|
this.tree = frappe.ui.create('div', {
|
|
|
|
inside: this.parent,
|
2018-03-29 07:49:08 +00:00
|
|
|
className: 'tree ' + (this.withSkeleton ? 'with-skeleton' : '')
|
2018-03-29 05:20:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.rootNode = this.makeNode(this.label, this.label, true, null, this.tree);
|
2018-03-29 12:05:02 +00:00
|
|
|
this.expandNode(this.rootNode);
|
2018-03-29 05:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
// this.selectedNode.parentNode &&
|
|
|
|
// this.loadChildren(this.selectedNode.parentNode, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadChildren(node, deep=false) {
|
|
|
|
if(!deep) {
|
|
|
|
this.renderNodeChildren(node, this.method(node.value));
|
|
|
|
} else {
|
|
|
|
this.renderChildrenDeep(node, this.getAllNodes(node.value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderChildrenDeep(dataList) {
|
|
|
|
dataList.map(d => { this.renderNodeChildren(this.nodes[d.parent], d.data); });
|
|
|
|
}
|
|
|
|
|
2018-03-29 12:05:02 +00:00
|
|
|
renderNodeChildren(node, dataSet=[]) {
|
|
|
|
frappe.ui.empty(node.childrenList);
|
2018-03-29 05:20:00 +00:00
|
|
|
|
|
|
|
dataSet.forEach(data => {
|
|
|
|
let parentNode = this.nodes[node.value];
|
|
|
|
let childNode = this.makeNode(data.label || data.value, data.value,
|
|
|
|
data.expandable, parentNode);
|
2018-03-29 12:05:02 +00:00
|
|
|
childNode.treeLink.dataset.nodeData = data;
|
2018-03-29 05:20:00 +00:00
|
|
|
});
|
|
|
|
node.expanded = false;
|
|
|
|
|
|
|
|
// As children loaded
|
|
|
|
node.loaded = true;
|
2018-03-29 12:05:02 +00:00
|
|
|
this.onNodeClick(node, false);
|
2018-03-29 05:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getAllNodes() { }
|
|
|
|
|
|
|
|
makeNode(label, value, expandable, parentNode, parentEl) {
|
|
|
|
let node = {
|
|
|
|
parent: parent,
|
|
|
|
label: label,
|
|
|
|
value: value,
|
|
|
|
loaded: 0,
|
|
|
|
expanded: 0,
|
|
|
|
expandable: expandable,
|
|
|
|
};
|
|
|
|
|
|
|
|
if(parentNode){
|
|
|
|
node.parentNode = parentNode;
|
2018-03-29 12:05:02 +00:00
|
|
|
node.parent = parentNode.childrenList;
|
2018-03-29 05:20:00 +00:00
|
|
|
node.isRoot = 0;
|
|
|
|
} else {
|
|
|
|
node.isRoot = 1;
|
|
|
|
node.parent = parentEl;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.nodes[value] = node;
|
|
|
|
this.buildNodeElement(node);
|
|
|
|
this.onRender && this.onRender(node);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildNodeElement(node) {
|
2018-03-29 07:49:08 +00:00
|
|
|
node.parentLi = frappe.ui.create('li', {
|
2018-03-29 05:20:00 +00:00
|
|
|
inside: node.parent,
|
|
|
|
className: 'tree-node'
|
|
|
|
});
|
|
|
|
|
|
|
|
let iconHtml = '';
|
|
|
|
if(this.iconSet) {
|
|
|
|
iconHtml = node.expandable ? this.iconSet.closed : this.iconSet.leaf;
|
|
|
|
}
|
2018-03-29 12:05:02 +00:00
|
|
|
let labelEl = `<a class="tree-label"> ${node.label}</a>`;
|
2018-03-29 05:20:00 +00:00
|
|
|
|
2018-03-29 12:05:02 +00:00
|
|
|
node.treeLink = frappe.ui.create('span', {
|
2018-03-29 07:49:08 +00:00
|
|
|
inside: node.parentLi,
|
2018-03-29 05:20:00 +00:00
|
|
|
className: 'tree-link',
|
|
|
|
'data-label': node.label,
|
2018-03-29 12:05:02 +00:00
|
|
|
innerHTML: iconHtml + labelEl
|
2018-03-29 05:20:00 +00:00
|
|
|
});
|
2018-03-29 12:05:02 +00:00
|
|
|
node.treeLink.dataset.node = node;
|
|
|
|
node.treeLink.addEventListener('click', () => {
|
2018-03-29 05:20:00 +00:00
|
|
|
this.onNodeClick(node);
|
|
|
|
});
|
|
|
|
|
2018-03-29 12:05:02 +00:00
|
|
|
node.childrenList = frappe.ui.create('ul', {
|
2018-03-29 07:49:08 +00:00
|
|
|
inside: node.parentLi,
|
2018-03-29 05:20:00 +00:00
|
|
|
className: 'tree-children hide'
|
|
|
|
});
|
|
|
|
|
|
|
|
// if(this.toolbar) {
|
2018-03-29 12:05:02 +00:00
|
|
|
// node.toolbar = this.getToolbar(node).insertAfter(node.treeLink);
|
2018-03-29 05:20:00 +00:00
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
onNodeClick(node, click = true) {
|
|
|
|
this.setSelectedNode(node);
|
|
|
|
if(click) {
|
|
|
|
this.onClick && this.onClick(node);
|
|
|
|
}
|
|
|
|
this.expandNode(node);
|
|
|
|
// select link
|
2018-03-29 12:05:02 +00:00
|
|
|
utils.activate(this.tree, node.treeLink, 'tree-link', 'active');
|
|
|
|
if(node.toolbar) this.showToolbar(node);
|
2018-03-29 05:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expandNode(node) {
|
|
|
|
if(node.expandable) {
|
|
|
|
this.toggleNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
node.expanded = !node.expanded;
|
2018-03-29 07:49:08 +00:00
|
|
|
// node.parent.classList.toggle('opened', node.expanded);
|
|
|
|
node.parent.classList.add('opened');
|
|
|
|
node.parentLi.classList.add('opened');
|
2018-03-29 05:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleNode(node) {
|
2018-03-29 12:05:02 +00:00
|
|
|
if(!node.loaded) this.loadChildren(node);
|
2018-03-29 05:20:00 +00:00
|
|
|
|
|
|
|
// expand children
|
2018-03-29 12:05:02 +00:00
|
|
|
if(node.childrenList) {
|
|
|
|
if(node.childrenList.innerHTML.length) {
|
|
|
|
node.childrenList.classList.toggle('hide', !node.expanded);
|
2018-03-29 05:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open close icon
|
|
|
|
if(this.iconSet) {
|
2018-03-29 12:05:02 +00:00
|
|
|
const oldIcon = node.treeLink.querySelector('svg');
|
2018-03-29 05:20:00 +00:00
|
|
|
const newIconKey = !node.expanded ? 'closed' : 'open';
|
2018-03-29 07:49:08 +00:00
|
|
|
const newIcon = frappe.ui.create(this.iconSet[newIconKey]);
|
2018-03-29 12:05:02 +00:00
|
|
|
node.treeLink.replaceChild(newIcon, oldIcon);
|
2018-03-29 05:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelectedNode() { return this.selectedNode; }
|
|
|
|
|
|
|
|
setSelectedNode(node) { this.selectedNode = node; }
|
|
|
|
|
|
|
|
showToolbar() { }
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Tree;
|