fix(Hierarchy Chart): check if company is set before loading children (#38985)

* fix(Org Chart): check if company is set before loading children

* refactor: avoid assigning undefined values, use empty strings, null instead

* fix: returning to org chart view from employee master does not render chart

- the check for company field is truthy from the employee doctype, so it does not render company field again. Explicitly check for company field on the same page

* fix(Org Chart Mobile): check if company is set before loading children
This commit is contained in:
Rucha Mahabal
2023-12-28 12:54:31 +05:30
committed by GitHub
parent 7ad42ec957
commit e4d6df39ff
2 changed files with 30 additions and 22 deletions

View File

@@ -68,7 +68,7 @@ erpnext.HierarchyChart = class {
show() { show() {
this.setup_actions(); this.setup_actions();
if ($(`[data-fieldname="company"]`).length) return; if (this.page.main.find('[data-fieldname="company"]').length) return;
let me = this; let me = this;
let company = this.page.add_field({ let company = this.page.add_field({
@@ -80,7 +80,7 @@ erpnext.HierarchyChart = class {
only_select: true, only_select: true,
reqd: 1, reqd: 1,
change: () => { change: () => {
me.company = undefined; me.company = '';
$('#hierarchy-chart-wrapper').remove(); $('#hierarchy-chart-wrapper').remove();
if (company.get_value()) { if (company.get_value()) {
@@ -219,8 +219,8 @@ erpnext.HierarchyChart = class {
} }
}).then(r => { }).then(r => {
if (r.message.length) { if (r.message.length) {
let expand_node = undefined; let expand_node;
let node = undefined; let node;
$.each(r.message, (_i, data) => { $.each(r.message, (_i, data) => {
if ($(`[id="${data.id}"]`).length) if ($(`[id="${data.id}"]`).length)
@@ -229,7 +229,7 @@ erpnext.HierarchyChart = class {
node = new me.Node({ node = new me.Node({
id: data.id, id: data.id,
parent: $('<li class="child-node"></li>').appendTo(me.$hierarchy.find('.node-children')), parent: $('<li class="child-node"></li>').appendTo(me.$hierarchy.find('.node-children')),
parent_id: undefined, parent_id: '',
image: data.image, image: data.image,
name: data.name, name: data.name,
title: data.title, title: data.title,
@@ -286,6 +286,10 @@ erpnext.HierarchyChart = class {
} }
load_children(node, deep=false) { load_children(node, deep=false) {
if (!this.company) {
frappe.throw(__('Please select a company first.'));
}
if (!deep) { if (!deep) {
frappe.run_serially([ frappe.run_serially([
() => this.get_child_nodes(node.id), () => this.get_child_nodes(node.id),
@@ -367,8 +371,8 @@ erpnext.HierarchyChart = class {
} }
render_children_of_all_nodes(data_list) { render_children_of_all_nodes(data_list) {
let entry = undefined; let entry;
let node = undefined; let node;
while (data_list.length) { while (data_list.length) {
// to avoid overlapping connectors // to avoid overlapping connectors
@@ -423,7 +427,7 @@ erpnext.HierarchyChart = class {
title: data.title, title: data.title,
expandable: data.expandable, expandable: data.expandable,
connections: data.connections, connections: data.connections,
children: undefined children: null,
}); });
} }
@@ -519,7 +523,7 @@ erpnext.HierarchyChart = class {
collapse_previous_level_nodes(node) { collapse_previous_level_nodes(node) {
let node_parent = $(`[id="${node.parent_id}"]`); let node_parent = $(`[id="${node.parent_id}"]`);
let previous_level_nodes = node_parent.parent().parent().children('li'); let previous_level_nodes = node_parent.parent().parent().children('li');
let node_card = undefined; let node_card;
previous_level_nodes.each(function() { previous_level_nodes.each(function() {
node_card = $(this).find('.node-card'); node_card = $(this).find('.node-card');
@@ -582,12 +586,12 @@ erpnext.HierarchyChart = class {
level.nextAll('li').remove(); level.nextAll('li').remove();
let nodes = level.find('.node-card'); let nodes = level.find('.node-card');
let node_object = undefined; let node_object;
$.each(nodes, (_i, element) => { $.each(nodes, (_i, element) => {
node_object = this.nodes[element.id]; node_object = this.nodes[element.id];
node_object.expanded = 0; node_object.expanded = 0;
node_object.$children = undefined; node_object.$children = null;
}); });
nodes.removeClass('collapsed active-path'); nodes.removeClass('collapsed active-path');

View File

@@ -59,8 +59,8 @@ erpnext.HierarchyChartMobile = class {
} }
show() { show() {
if (this.page.main.find('[data-fieldname="company"]').length) return;
let me = this; let me = this;
if ($(`[data-fieldname="company"]`).length) return;
let company = this.page.add_field({ let company = this.page.add_field({
fieldtype: 'Link', fieldtype: 'Link',
@@ -71,7 +71,7 @@ erpnext.HierarchyChartMobile = class {
only_select: true, only_select: true,
reqd: 1, reqd: 1,
change: () => { change: () => {
me.company = undefined; me.company = '';
if (company.get_value() && me.company != company.get_value()) { if (company.get_value() && me.company != company.get_value()) {
me.company = company.get_value(); me.company = company.get_value();
@@ -154,7 +154,7 @@ erpnext.HierarchyChartMobile = class {
return new me.Node({ return new me.Node({
id: data.id, id: data.id,
parent: root_level, parent: root_level,
parent_id: undefined, parent_id: '',
image: data.image, image: data.image,
name: data.name, name: data.name,
title: data.title, title: data.title,
@@ -174,7 +174,7 @@ erpnext.HierarchyChartMobile = class {
if (this.$sibling_group) { if (this.$sibling_group) {
const sibling_parent = this.$sibling_group.find('.node-group').attr('data-parent'); const sibling_parent = this.$sibling_group.find('.node-group').attr('data-parent');
if (node.parent_id !== undefined && node.parent_id != sibling_parent) if (node.parent_id != '' && node.parent_id != sibling_parent)
this.$sibling_group.empty(); this.$sibling_group.empty();
} }
@@ -225,6 +225,10 @@ erpnext.HierarchyChartMobile = class {
} }
load_children(node) { load_children(node) {
if (!this.company) {
frappe.throw(__('Please select a company first'));
}
frappe.run_serially([ frappe.run_serially([
() => this.get_child_nodes(node.id), () => this.get_child_nodes(node.id),
(child_nodes) => this.render_child_nodes(node, child_nodes) (child_nodes) => this.render_child_nodes(node, child_nodes)
@@ -281,7 +285,7 @@ erpnext.HierarchyChartMobile = class {
title: data.title, title: data.title,
expandable: data.expandable, expandable: data.expandable,
connections: data.connections, connections: data.connections,
children: undefined children: null
}); });
} }
@@ -291,7 +295,7 @@ erpnext.HierarchyChartMobile = class {
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
let connector = undefined; let connector = null;
if ($(`[id="${parent_id}"]`).hasClass('active')) { if ($(`[id="${parent_id}"]`).hasClass('active')) {
connector = this.get_connector_for_active_node(parent_node, child_node); connector = this.get_connector_for_active_node(parent_node, child_node);
@@ -377,7 +381,7 @@ erpnext.HierarchyChartMobile = class {
let node_element = $(`[id="${node.id}"]`); let node_element = $(`[id="${node.id}"]`);
node_element.click(function() { node_element.click(function() {
let el = undefined; let el = null;
if (node.is_root) { if (node.is_root) {
el = $(this).detach(); el = $(this).detach();
@@ -411,7 +415,7 @@ erpnext.HierarchyChartMobile = class {
$('.node-group').on('click', function() { $('.node-group').on('click', function() {
let parent = $(this).attr('data-parent'); let parent = $(this).attr('data-parent');
if (parent === 'undefined') { if (parent == '') {
me.setup_hierarchy(); me.setup_hierarchy();
me.render_root_nodes(); me.render_root_nodes();
} else { } else {
@@ -427,7 +431,7 @@ erpnext.HierarchyChartMobile = class {
let node_object = this.nodes[node.id]; let node_object = this.nodes[node.id];
node_object.expanded = 0; node_object.expanded = 0;
node_object.$children = undefined; node_object.$children = null;
this.nodes[node.id] = node_object; this.nodes[node.id] = node_object;
} }
@@ -484,7 +488,7 @@ erpnext.HierarchyChartMobile = class {
node.removeClass('active-child active-path'); node.removeClass('active-child active-path');
node_object.expanded = 0; node_object.expanded = 0;
node_object.$children = undefined; node_object.$children = null;
this.nodes[node.id] = node_object; this.nodes[node.id] = node_object;
// show parent's siblings and expand parent node // show parent's siblings and expand parent node
@@ -523,7 +527,7 @@ erpnext.HierarchyChartMobile = class {
current_node.removeClass('active-child active-path'); current_node.removeClass('active-child active-path');
node_object.expanded = 0; node_object.expanded = 0;
node_object.$children = undefined; node_object.$children = null;
level.empty().append(current_node); level.empty().append(current_node);
} }