2021-07-25 14:58:01 +00:00
|
|
|
import html2canvas from 'html2canvas';
|
2021-06-29 13:45:08 +00:00
|
|
|
erpnext.HierarchyChart = class {
|
2021-06-29 15:56:47 +00:00
|
|
|
/* Options:
|
2021-06-29 16:21:21 +00:00
|
|
|
- doctype
|
2021-06-29 15:56:47 +00:00
|
|
|
- wrapper: wrapper for the hierarchy view
|
|
|
|
- method:
|
|
|
|
- to get the data for each node
|
|
|
|
- this method should return id, name, title, image, and connections for each node
|
|
|
|
*/
|
2021-06-29 16:21:21 +00:00
|
|
|
constructor(doctype, wrapper, method) {
|
2021-06-29 13:45:08 +00:00
|
|
|
this.page = wrapper.page;
|
2021-06-29 15:56:47 +00:00
|
|
|
this.method = method;
|
2021-06-29 16:21:21 +00:00
|
|
|
this.doctype = doctype;
|
2021-06-29 13:45:08 +00:00
|
|
|
|
2021-07-25 14:58:01 +00:00
|
|
|
this.setup_page_style();
|
|
|
|
this.page.main.addClass('frappe-card');
|
|
|
|
|
|
|
|
this.nodes = {};
|
|
|
|
this.setup_node_class();
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_page_style() {
|
2021-06-29 13:45:08 +00:00
|
|
|
this.page.main.css({
|
|
|
|
'min-height': '300px',
|
|
|
|
'max-height': '600px',
|
|
|
|
'overflow': 'auto',
|
|
|
|
'position': 'relative'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_node_class() {
|
|
|
|
let me = this;
|
|
|
|
this.Node = class {
|
|
|
|
constructor({
|
|
|
|
id, parent, parent_id, image, name, title, expandable, connections, is_root // eslint-disable-line
|
|
|
|
}) {
|
|
|
|
// to setup values passed via constructor
|
|
|
|
$.extend(this, arguments[0]);
|
|
|
|
|
|
|
|
this.expanded = 0;
|
|
|
|
|
|
|
|
me.nodes[this.id] = this;
|
|
|
|
me.make_node_element(this);
|
2021-07-21 17:49:47 +00:00
|
|
|
|
|
|
|
if (!me.all_nodes_expanded) {
|
|
|
|
me.setup_node_click_action(this);
|
|
|
|
}
|
|
|
|
|
2021-06-29 16:21:21 +00:00
|
|
|
me.setup_edit_node_action(this);
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
2021-07-02 12:45:18 +00:00
|
|
|
};
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
make_node_element(node) {
|
|
|
|
let node_card = frappe.render_template('node_card', {
|
|
|
|
id: node.id,
|
|
|
|
name: node.name,
|
|
|
|
title: node.title,
|
|
|
|
image: node.image,
|
|
|
|
parent: node.parent_id,
|
2021-07-14 18:20:54 +00:00
|
|
|
connections: node.connections,
|
|
|
|
is_mobile: false
|
2021-06-29 13:45:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
node.parent.append(node_card);
|
|
|
|
node.$link = $(`#${node.id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
|
|
|
frappe.breadcrumbs.add('HR');
|
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
this.setup_actions();
|
2021-07-08 04:23:31 +00:00
|
|
|
if ($(`[data-fieldname="company"]`).length) return;
|
2021-07-21 17:49:47 +00:00
|
|
|
let me = this;
|
2021-07-08 04:23:31 +00:00
|
|
|
|
2021-06-29 13:45:08 +00:00
|
|
|
let company = this.page.add_field({
|
|
|
|
fieldtype: 'Link',
|
|
|
|
options: 'Company',
|
|
|
|
fieldname: 'company',
|
|
|
|
placeholder: __('Select Company'),
|
|
|
|
default: frappe.defaults.get_default('company'),
|
|
|
|
only_select: true,
|
|
|
|
reqd: 1,
|
|
|
|
change: () => {
|
|
|
|
me.company = undefined;
|
|
|
|
|
|
|
|
if (company.get_value() && me.company != company.get_value()) {
|
|
|
|
me.company = company.get_value();
|
|
|
|
|
|
|
|
// svg for connectors
|
2021-07-02 12:45:18 +00:00
|
|
|
me.make_svg_markers();
|
2021-07-25 14:58:01 +00:00
|
|
|
me.setup_hierarchy();
|
2021-07-07 06:35:50 +00:00
|
|
|
me.render_root_nodes();
|
2021-07-21 17:49:47 +00:00
|
|
|
me.all_nodes_expanded = false;
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
company.refresh();
|
|
|
|
$(`[data-fieldname="company"]`).trigger('change');
|
2021-08-15 16:02:39 +00:00
|
|
|
$(`[data-fieldname="company"] .link-field`).css('z-index', 2);
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
setup_actions() {
|
|
|
|
let me = this;
|
2021-08-16 04:49:48 +00:00
|
|
|
this.page.clear_inner_toolbar();
|
2021-07-25 14:58:01 +00:00
|
|
|
this.page.add_inner_button(__('Export'), function() {
|
|
|
|
me.export_chart();
|
|
|
|
});
|
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
this.page.add_inner_button(__('Expand All'), function() {
|
|
|
|
me.load_children(me.root_node, true);
|
|
|
|
me.all_nodes_expanded = true;
|
|
|
|
|
|
|
|
me.page.remove_inner_button(__('Expand All'));
|
|
|
|
me.page.add_inner_button(__('Collapse All'), function() {
|
|
|
|
me.setup_hierarchy();
|
|
|
|
me.render_root_nodes();
|
|
|
|
me.all_nodes_expanded = false;
|
|
|
|
|
|
|
|
me.page.remove_inner_button(__('Collapse All'));
|
|
|
|
me.setup_actions();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-25 14:58:01 +00:00
|
|
|
export_chart() {
|
2021-08-16 04:49:48 +00:00
|
|
|
frappe.dom.freeze(__('Exporting...'));
|
2021-07-25 14:58:01 +00:00
|
|
|
this.page.main.css({
|
|
|
|
'min-height': '',
|
|
|
|
'max-height': '',
|
|
|
|
'overflow': 'visible',
|
|
|
|
'position': 'fixed',
|
|
|
|
'left': '0',
|
|
|
|
'top': '0'
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.node-card').addClass('exported');
|
|
|
|
|
|
|
|
html2canvas(document.querySelector('#hierarchy-chart-wrapper'), {
|
|
|
|
scrollY: -window.scrollY,
|
|
|
|
scrollX: 0
|
|
|
|
}).then(function(canvas) {
|
|
|
|
// Export the canvas to its data URI representation
|
|
|
|
let dataURL = canvas.toDataURL('image/png');
|
|
|
|
|
|
|
|
// download the image
|
|
|
|
let a = document.createElement('a');
|
|
|
|
a.href = dataURL;
|
|
|
|
a.download = 'hierarchy_chart';
|
|
|
|
a.click();
|
2021-08-16 04:49:48 +00:00
|
|
|
}).finally(() => {
|
|
|
|
frappe.dom.unfreeze();
|
2021-07-25 14:58:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.setup_page_style();
|
|
|
|
$('.node-card').removeClass('exported');
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
setup_hierarchy() {
|
|
|
|
if (this.$hierarchy)
|
|
|
|
this.$hierarchy.remove();
|
|
|
|
|
|
|
|
$(`#connectors`).empty();
|
|
|
|
|
|
|
|
// setup hierarchy
|
|
|
|
this.$hierarchy = $(
|
|
|
|
`<ul class="hierarchy">
|
|
|
|
<li class="root-level level">
|
|
|
|
<ul class="node-children"></ul>
|
|
|
|
</li>
|
|
|
|
</ul>`);
|
|
|
|
|
2021-07-25 14:58:01 +00:00
|
|
|
this.page.main
|
|
|
|
.find('#hierarchy-chart-wrapper')
|
|
|
|
.append(this.$hierarchy);
|
2021-08-16 04:49:48 +00:00
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
this.nodes = {};
|
2021-08-16 04:49:48 +00:00
|
|
|
this.all_nodes_expanded = false;
|
2021-07-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:45:08 +00:00
|
|
|
make_svg_markers() {
|
2021-07-25 17:41:18 +00:00
|
|
|
$('#hierarchy-chart-wrapper').remove();
|
2021-06-29 13:45:08 +00:00
|
|
|
|
2021-07-25 14:58:01 +00:00
|
|
|
this.page.main.append(`
|
|
|
|
<div id="hierarchy-chart-wrapper">
|
|
|
|
<svg id="arrows" width="100%" height="100%">
|
|
|
|
<defs>
|
|
|
|
<marker id="arrowhead-active" viewBox="0 0 10 10" refX="3" refY="5" markerWidth="6" markerHeight="6" orient="auto" fill="var(--blue-500)">
|
|
|
|
<path d="M 0 0 L 10 5 L 0 10 z"></path>
|
|
|
|
</marker>
|
|
|
|
<marker id="arrowhead-collapsed" viewBox="0 0 10 10" refX="3" refY="5" markerWidth="6" markerHeight="6" orient="auto" fill="var(--blue-300)">
|
|
|
|
<path d="M 0 0 L 10 5 L 0 10 z"></path>
|
|
|
|
</marker>
|
|
|
|
|
|
|
|
<marker id="arrowstart-active" viewBox="0 0 10 10" refX="3" refY="5" markerWidth="8" markerHeight="8" orient="auto" fill="var(--blue-500)">
|
|
|
|
<circle cx="4" cy="4" r="3.5" fill="white" stroke="var(--blue-500)"/>
|
|
|
|
</marker>
|
|
|
|
<marker id="arrowstart-collapsed" viewBox="0 0 10 10" refX="3" refY="5" markerWidth="8" markerHeight="8" orient="auto" fill="var(--blue-300)">
|
|
|
|
<circle cx="4" cy="4" r="3.5" fill="white" stroke="var(--blue-300)"/>
|
|
|
|
</marker>
|
|
|
|
</defs>
|
|
|
|
<g id="connectors" fill="none">
|
|
|
|
</g>
|
|
|
|
</svg>
|
|
|
|
</div>`);
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
render_root_nodes(expanded_view=false) {
|
2021-06-29 13:45:08 +00:00
|
|
|
let me = this;
|
|
|
|
|
2021-08-16 04:49:48 +00:00
|
|
|
return frappe.call({
|
2021-06-29 13:45:08 +00:00
|
|
|
method: me.method,
|
|
|
|
args: {
|
|
|
|
company: me.company
|
2021-07-08 04:23:31 +00:00
|
|
|
}
|
|
|
|
}).then(r => {
|
|
|
|
if (r.message.length) {
|
2021-07-08 05:53:50 +00:00
|
|
|
let expand_node = undefined;
|
2021-07-08 04:23:31 +00:00
|
|
|
let node = undefined;
|
|
|
|
|
|
|
|
$.each(r.message, (i, data) => {
|
|
|
|
node = new me.Node({
|
|
|
|
id: data.id,
|
|
|
|
parent: $('<li class="child-node"></li>').appendTo(me.$hierarchy.find('.node-children')),
|
|
|
|
parent_id: undefined,
|
|
|
|
image: data.image,
|
|
|
|
name: data.name,
|
|
|
|
title: data.title,
|
|
|
|
expandable: true,
|
|
|
|
connections: data.connections,
|
|
|
|
is_root: true
|
2021-06-29 13:45:08 +00:00
|
|
|
});
|
|
|
|
|
2021-07-08 05:53:50 +00:00
|
|
|
if (!expand_node && data.connections)
|
|
|
|
expand_node = node;
|
2021-07-08 04:23:31 +00:00
|
|
|
});
|
|
|
|
|
2021-08-16 04:49:48 +00:00
|
|
|
me.root_node = expand_node;
|
2021-07-21 17:49:47 +00:00
|
|
|
if (!expanded_view) {
|
|
|
|
me.expand_node(expand_node);
|
|
|
|
}
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
2021-07-02 12:45:18 +00:00
|
|
|
});
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expand_node(node) {
|
2021-07-07 04:13:28 +00:00
|
|
|
const is_sibling = this.selected_node && this.selected_node.parent_id === node.parent_id;
|
2021-06-29 13:45:08 +00:00
|
|
|
this.set_selected_node(node);
|
|
|
|
this.show_active_path(node);
|
|
|
|
this.collapse_previous_level_nodes(node);
|
|
|
|
|
|
|
|
// since the previous node collapses, all connections to that node need to be rebuilt
|
|
|
|
// if a sibling node is clicked, connections don't need to be rebuilt
|
|
|
|
if (!is_sibling) {
|
|
|
|
// rebuild outgoing connections
|
|
|
|
this.refresh_connectors(node.parent_id);
|
|
|
|
|
|
|
|
// rebuild incoming connections
|
|
|
|
let grandparent = $(`#${node.parent_id}`).attr('data-parent');
|
2021-07-02 12:45:18 +00:00
|
|
|
this.refresh_connectors(grandparent);
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node.expandable && !node.expanded) {
|
|
|
|
return this.load_children(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collapse_node() {
|
|
|
|
if (this.selected_node.expandable) {
|
2021-07-06 12:46:49 +00:00
|
|
|
this.selected_node.$children.hide();
|
|
|
|
$(`path[data-parent="${this.selected_node.id}"]`).hide();
|
2021-06-29 13:45:08 +00:00
|
|
|
this.selected_node.expanded = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
show_active_path(node) {
|
|
|
|
// mark node parent on active path
|
|
|
|
$(`#${node.parent_id}`).addClass('active-path');
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
load_children(node, deep=false) {
|
|
|
|
if (!deep) {
|
|
|
|
frappe.run_serially([
|
|
|
|
() => this.get_child_nodes(node.id),
|
|
|
|
(child_nodes) => this.render_child_nodes(node, child_nodes)
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
frappe.run_serially([
|
2021-08-16 04:49:48 +00:00
|
|
|
() => frappe.dom.freeze(),
|
2021-07-21 17:49:47 +00:00
|
|
|
() => this.setup_hierarchy(),
|
|
|
|
() => this.render_root_nodes(true),
|
|
|
|
() => this.get_all_nodes(node.id, node.name),
|
2021-08-16 04:49:48 +00:00
|
|
|
(data_list) => this.render_children_of_all_nodes(data_list),
|
|
|
|
() => frappe.dom.unfreeze()
|
2021-07-21 17:49:47 +00:00
|
|
|
]);
|
|
|
|
}
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get_child_nodes(node_id) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
frappe.call({
|
|
|
|
method: this.method,
|
|
|
|
args: {
|
|
|
|
parent: node_id,
|
2021-07-08 04:23:31 +00:00
|
|
|
company: this.company
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
2021-07-08 04:23:31 +00:00
|
|
|
}).then(r => resolve(r.message));
|
2021-06-29 13:45:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render_child_nodes(node, child_nodes) {
|
|
|
|
const last_level = this.$hierarchy.find('.level:last').index();
|
|
|
|
const current_level = $(`#${node.id}`).parent().parent().parent().index();
|
|
|
|
|
|
|
|
if (last_level === current_level) {
|
|
|
|
this.$hierarchy.append(`
|
|
|
|
<li class="level"></li>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node.$children) {
|
|
|
|
node.$children = $('<ul class="node-children"></ul>')
|
2021-07-02 12:45:18 +00:00
|
|
|
.hide()
|
|
|
|
.appendTo(this.$hierarchy.find('.level:last'));
|
2021-06-29 13:45:08 +00:00
|
|
|
|
|
|
|
node.$children.empty();
|
|
|
|
|
|
|
|
if (child_nodes) {
|
|
|
|
$.each(child_nodes, (_i, data) => {
|
|
|
|
this.add_node(node, data);
|
|
|
|
setTimeout(() => {
|
2021-06-29 15:56:47 +00:00
|
|
|
this.add_connector(node.id, data.id);
|
2021-06-29 13:45:08 +00:00
|
|
|
}, 250);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 12:46:49 +00:00
|
|
|
node.$children.show();
|
|
|
|
$(`path[data-parent="${node.id}"]`).show();
|
2021-06-29 13:45:08 +00:00
|
|
|
node.expanded = true;
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
get_all_nodes(node_id, node_name) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
frappe.call({
|
|
|
|
method: 'erpnext.utilities.hierarchy_chart.get_all_nodes',
|
|
|
|
args: {
|
|
|
|
method: this.method,
|
|
|
|
company: this.company,
|
|
|
|
parent: node_id,
|
|
|
|
parent_name: node_name
|
|
|
|
},
|
|
|
|
callback: (r) => {
|
|
|
|
resolve(r.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render_children_of_all_nodes(data_list) {
|
|
|
|
let entry = undefined;
|
|
|
|
let node = undefined;
|
|
|
|
|
2021-07-25 14:58:01 +00:00
|
|
|
while (data_list.length) {
|
2021-07-21 17:49:47 +00:00
|
|
|
// to avoid overlapping connectors
|
|
|
|
entry = data_list.shift();
|
|
|
|
node = this.nodes[entry.parent];
|
|
|
|
if (node) {
|
|
|
|
this.render_child_nodes_for_expanded_view(node, entry.data);
|
2021-08-16 04:49:48 +00:00
|
|
|
} else if (data_list.length) {
|
2021-07-21 17:49:47 +00:00
|
|
|
data_list.push(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render_child_nodes_for_expanded_view(node, child_nodes) {
|
2021-07-25 14:58:01 +00:00
|
|
|
node.$children = $('<ul class="node-children"></ul>');
|
2021-07-21 17:49:47 +00:00
|
|
|
|
|
|
|
const last_level = this.$hierarchy.find('.level:last').index();
|
|
|
|
const node_level = $(`#${node.id}`).parent().parent().parent().index();
|
|
|
|
|
|
|
|
if (last_level === node_level) {
|
|
|
|
this.$hierarchy.append(`
|
|
|
|
<li class="level"></li>
|
|
|
|
`);
|
|
|
|
node.$children.appendTo(this.$hierarchy.find('.level:last'));
|
|
|
|
} else {
|
|
|
|
node.$children.appendTo(this.$hierarchy.find('.level:eq(' + (node_level + 1) + ')'));
|
|
|
|
}
|
|
|
|
|
|
|
|
node.$children.hide().empty();
|
|
|
|
|
|
|
|
if (child_nodes) {
|
|
|
|
$.each(child_nodes, (_i, data) => {
|
|
|
|
this.add_node(node, data);
|
|
|
|
setTimeout(() => {
|
|
|
|
this.add_connector(node.id, data.id);
|
|
|
|
}, 250);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
node.$children.show();
|
|
|
|
$(`path[data-parent="${node.id}"]`).show();
|
|
|
|
node.expanded = true;
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:45:08 +00:00
|
|
|
add_node(node, data) {
|
|
|
|
return new this.Node({
|
2021-06-29 15:56:47 +00:00
|
|
|
id: data.id,
|
2021-07-07 04:13:28 +00:00
|
|
|
parent: $('<li class="child-node"></li>').appendTo(node.$children),
|
2021-06-29 13:45:08 +00:00
|
|
|
parent_id: node.id,
|
|
|
|
image: data.image,
|
2021-06-29 15:56:47 +00:00
|
|
|
name: data.name,
|
|
|
|
title: data.title,
|
2021-06-29 13:45:08 +00:00
|
|
|
expandable: data.expandable,
|
|
|
|
connections: data.connections,
|
|
|
|
children: undefined
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
add_connector(parent_id, child_id) {
|
2021-07-08 04:23:31 +00:00
|
|
|
// using pure javascript for better performance
|
2021-07-07 04:13:28 +00:00
|
|
|
const parent_node = document.querySelector(`#${parent_id}`);
|
|
|
|
const child_node = document.querySelector(`#${child_id}`);
|
2021-06-29 13:45:08 +00:00
|
|
|
|
2021-07-06 12:46:49 +00:00
|
|
|
let path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
2021-06-29 13:45:08 +00:00
|
|
|
|
|
|
|
// we need to connect right side of the parent to the left side of the child node
|
2021-07-08 04:23:31 +00:00
|
|
|
const pos_parent_right = {
|
2021-06-29 13:45:08 +00:00
|
|
|
x: parent_node.offsetLeft + parent_node.offsetWidth,
|
|
|
|
y: parent_node.offsetTop + parent_node.offsetHeight / 2
|
|
|
|
};
|
2021-07-08 04:23:31 +00:00
|
|
|
const pos_child_left = {
|
2021-06-29 13:45:08 +00:00
|
|
|
x: child_node.offsetLeft - 5,
|
|
|
|
y: child_node.offsetTop + child_node.offsetHeight / 2
|
|
|
|
};
|
|
|
|
|
2021-07-08 04:23:31 +00:00
|
|
|
const connector = this.get_connector(pos_parent_right, pos_child_left);
|
2021-06-29 20:16:10 +00:00
|
|
|
|
2021-07-07 04:13:28 +00:00
|
|
|
path.setAttribute('d', connector);
|
2021-06-29 20:16:10 +00:00
|
|
|
this.set_path_attributes(path, parent_id, child_id);
|
|
|
|
|
2021-07-08 04:23:31 +00:00
|
|
|
document.getElementById('connectors').appendChild(path);
|
2021-06-29 20:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get_connector(pos_parent_right, pos_child_left) {
|
|
|
|
if (pos_parent_right.y === pos_child_left.y) {
|
|
|
|
// don't add arcs if it's a straight line
|
|
|
|
return "M" +
|
2021-06-29 13:45:08 +00:00
|
|
|
(pos_parent_right.x) + "," + (pos_parent_right.y) + " " +
|
2021-06-29 20:16:10 +00:00
|
|
|
"L"+
|
2021-06-29 13:45:08 +00:00
|
|
|
(pos_child_left.x) + "," + (pos_child_left.y);
|
2021-06-29 20:16:10 +00:00
|
|
|
} else {
|
|
|
|
let arc_1 = "";
|
|
|
|
let arc_2 = "";
|
|
|
|
let offset = 0;
|
|
|
|
|
|
|
|
if (pos_parent_right.y > pos_child_left.y) {
|
|
|
|
// if child is above parent on Y axis 1st arc is anticlocwise
|
|
|
|
// second arc is clockwise
|
|
|
|
arc_1 = "a10,10 1 0 0 10,-10 ";
|
|
|
|
arc_2 = "a10,10 0 0 1 10,-10 ";
|
|
|
|
offset = 10;
|
|
|
|
} else {
|
|
|
|
// if child is below parent on Y axis 1st arc is clockwise
|
|
|
|
// second arc is anticlockwise
|
|
|
|
arc_1 = "a10,10 0 0 1 10,10 ";
|
|
|
|
arc_2 = "a10,10 1 0 0 10,10 ";
|
|
|
|
offset = -10;
|
|
|
|
}
|
2021-06-29 13:45:08 +00:00
|
|
|
|
2021-06-29 20:16:10 +00:00
|
|
|
return "M" + (pos_parent_right.x) + "," + (pos_parent_right.y) + " " +
|
|
|
|
"L" +
|
|
|
|
(pos_parent_right.x + 40) + "," + (pos_parent_right.y) + " " +
|
|
|
|
arc_1 +
|
|
|
|
"L" +
|
|
|
|
(pos_parent_right.x + 50) + "," + (pos_child_left.y + offset) + " " +
|
|
|
|
arc_2 +
|
|
|
|
"L"+
|
|
|
|
(pos_child_left.x) + "," + (pos_child_left.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_path_attributes(path, parent_id, child_id) {
|
2021-06-29 13:45:08 +00:00
|
|
|
path.setAttribute("data-parent", parent_id);
|
|
|
|
path.setAttribute("data-child", child_id);
|
2021-07-08 04:23:31 +00:00
|
|
|
const parent = $(`#${parent_id}`);
|
2021-06-29 13:45:08 +00:00
|
|
|
|
2021-07-08 04:23:31 +00:00
|
|
|
if (parent.hasClass('active')) {
|
2021-06-29 13:45:08 +00:00
|
|
|
path.setAttribute("class", "active-connector");
|
|
|
|
path.setAttribute("marker-start", "url(#arrowstart-active)");
|
|
|
|
path.setAttribute("marker-end", "url(#arrowhead-active)");
|
2021-07-21 17:49:47 +00:00
|
|
|
} else {
|
2021-06-29 13:45:08 +00:00
|
|
|
path.setAttribute("class", "collapsed-connector");
|
|
|
|
path.setAttribute("marker-start", "url(#arrowstart-collapsed)");
|
|
|
|
path.setAttribute("marker-end", "url(#arrowhead-collapsed)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_selected_node(node) {
|
2021-07-08 04:23:31 +00:00
|
|
|
// remove active class from the current node
|
|
|
|
if (this.selected_node)
|
|
|
|
this.selected_node.$link.removeClass('active');
|
2021-06-29 13:45:08 +00:00
|
|
|
|
|
|
|
// add active class to the newly selected node
|
|
|
|
this.selected_node = node;
|
|
|
|
node.$link.addClass('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
collapse_previous_level_nodes(node) {
|
|
|
|
let node_parent = $(`#${node.parent_id}`);
|
|
|
|
let previous_level_nodes = node_parent.parent().parent().children('li');
|
|
|
|
let node_card = undefined;
|
|
|
|
|
|
|
|
previous_level_nodes.each(function() {
|
|
|
|
node_card = $(this).find('.node-card');
|
|
|
|
|
|
|
|
if (!node_card.hasClass('active-path')) {
|
|
|
|
node_card.addClass('collapsed');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh_connectors(node_parent) {
|
|
|
|
if (!node_parent) return;
|
|
|
|
|
|
|
|
$(`path[data-parent="${node_parent}"]`).remove();
|
|
|
|
|
|
|
|
frappe.run_serially([
|
|
|
|
() => this.get_child_nodes(node_parent),
|
|
|
|
(child_nodes) => {
|
|
|
|
if (child_nodes) {
|
|
|
|
$.each(child_nodes, (_i, data) => {
|
2021-06-29 15:56:47 +00:00
|
|
|
this.add_connector(node_parent, data.id);
|
2021-06-29 13:45:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_node_click_action(node) {
|
|
|
|
let me = this;
|
|
|
|
let node_element = $(`#${node.id}`);
|
|
|
|
|
|
|
|
node_element.click(function() {
|
2021-07-07 04:13:28 +00:00
|
|
|
const is_sibling = me.selected_node.parent_id === node.parent_id;
|
2021-06-29 13:45:08 +00:00
|
|
|
|
|
|
|
if (is_sibling) {
|
|
|
|
me.collapse_node();
|
|
|
|
} else if (node_element.is(':visible')
|
|
|
|
&& (node_element.hasClass('collapsed') || node_element.hasClass('active-path'))) {
|
|
|
|
me.remove_levels_after_node(node);
|
|
|
|
me.remove_orphaned_connectors();
|
|
|
|
}
|
|
|
|
|
|
|
|
me.expand_node(node);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-29 16:21:21 +00:00
|
|
|
setup_edit_node_action(node) {
|
|
|
|
let node_element = $(`#${node.id}`);
|
|
|
|
let me = this;
|
|
|
|
|
|
|
|
node_element.find('.btn-edit-node').click(function() {
|
|
|
|
frappe.set_route('Form', me.doctype, node.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:45:08 +00:00
|
|
|
remove_levels_after_node(node) {
|
2021-07-08 04:23:31 +00:00
|
|
|
let level = $(`#${node.id}`).parent().parent().parent().index();
|
2021-06-29 13:45:08 +00:00
|
|
|
|
2021-07-08 04:23:31 +00:00
|
|
|
level = $('.hierarchy > li:eq('+ level + ')');
|
2021-06-29 13:45:08 +00:00
|
|
|
level.nextAll('li').remove();
|
|
|
|
|
|
|
|
let nodes = level.find('.node-card');
|
|
|
|
let node_object = undefined;
|
|
|
|
|
|
|
|
$.each(nodes, (_i, element) => {
|
|
|
|
node_object = this.nodes[element.id];
|
|
|
|
node_object.expanded = 0;
|
|
|
|
node_object.$children = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
nodes.removeClass('collapsed active-path');
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_orphaned_connectors() {
|
|
|
|
let paths = $('#connectors > path');
|
|
|
|
$.each(paths, (_i, path) => {
|
2021-07-08 04:23:31 +00:00
|
|
|
const parent = $(path).data('parent');
|
|
|
|
const child = $(path).data('child');
|
2021-06-29 13:45:08 +00:00
|
|
|
|
2021-07-06 12:46:49 +00:00
|
|
|
if ($(`#${parent}`).length && $(`#${child}`).length)
|
2021-06-29 13:45:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
$(path).remove();
|
2021-07-02 12:45:18 +00:00
|
|
|
});
|
2021-06-29 13:45:08 +00:00
|
|
|
}
|
2021-07-02 12:45:18 +00:00
|
|
|
};
|