Skip to content

Commit

Permalink
fix: Avoid recursive endless loop when a role references itself (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM authored Aug 21, 2022
1 parent 75f26a9 commit bd59661
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions ansibleplaybookgrapher/data/highlight-hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,57 @@ let currentSelectedElement = null;
/**
* Highlight the linked nodes of the given root element
* @param {Element} parentElement
* @param {string[]} visitedElements
*/
function highlightLinkedNodes(parentElement) {
const parentElementId = $(parentElement).attr('id')
function highlightLinkedNodes(parentElement, visitedElements = []) {
$(parentElement).find('link').each(function (index, element) {
const target = $(element).attr('target');
const edge = $(element).attr('edge');
const linkedElementId = $(element).attr('target');
const edgeId = $(element).attr('edge');

const currentElement = $(`#${target}`);
const currentElement = $(`#${linkedElementId}`);
currentElement.addClass(HIGHLIGHT_CLASS);

// Highlight the edge point to the target
$(`#${edge}`).addClass(HIGHLIGHT_CLASS);
$(`#${edgeId}`).addClass(HIGHLIGHT_CLASS);

// Recursively highlight
highlightLinkedNodes(currentElement);
if (!visitedElements.includes(linkedElementId)) {
visitedElements.push(linkedElementId);
// Recursively highlight
highlightLinkedNodes(currentElement, visitedElements);
}
})
}


/**
* Unhighlight the linked nodes of the given root element
* @param {Element} parentElement
* @param {string[]} visitedElements
* @param {boolean} isHover True when we are coming from a mouseleave event. In that case, we should not unhighlight if
* the parentElement is the current selected element
*/
function unHighlightLinkedNodes(parentElement, isHover) {
function unHighlightLinkedNodes(parentElement, visitedElements = [], isHover) {
const currentSelectedElementId = $(currentSelectedElement).attr('id');
const parentElementId = $(parentElement).attr('id')
// Do not unhighlight the current current selected element
// Do not unhighlight the current selected element
if ($(parentElement).attr('id') !== currentSelectedElementId || !isHover) {

$(parentElement).find('link').each(function (index, element) {
const linkedElementId = $(element).attr('target');
const edge = $(element).attr('edge');
const edgeId = $(element).attr('edge');

const linkedElement = $('#' + linkedElementId);
const linkedElement = $(`#${linkedElementId}`);

if (linkedElement.attr('id') !== currentSelectedElementId) {
linkedElement.removeClass(HIGHLIGHT_CLASS);

// Unhighlight the edge point to the target
$(`#${edge}`).removeClass(HIGHLIGHT_CLASS);
$(`#${edgeId}`).removeClass(HIGHLIGHT_CLASS);

// Recursively unhighlight
unHighlightLinkedNodes(linkedElement, isHover);
if (!visitedElements.includes(linkedElementId)) {
visitedElements.push(linkedElementId);
// Recursively unhighlight
unHighlightLinkedNodes(linkedElement, visitedElements, isHover);
}
}

})
Expand All @@ -75,15 +81,15 @@ function unHighlightLinkedNodes(parentElement, isHover) {
* @param {Event} event
*/
function hoverMouseEnter(event) {
highlightLinkedNodes(event.currentTarget);
highlightLinkedNodes(event.currentTarget, []);
}

/**
* Hover handler for mouseleave event
* @param {Event} event
*/
function hoverMouseLeave(event) {
unHighlightLinkedNodes(event.currentTarget, true);
unHighlightLinkedNodes(event.currentTarget, [], true);
}

/**
Expand All @@ -97,18 +103,18 @@ function clickOnElement(event) {

if (newClickedElement.attr('id') === $(currentSelectedElement).attr('id')) { // clicking again on the same element
newClickedElement.removeClass(HIGHLIGHT_CLASS);
unHighlightLinkedNodes(currentSelectedElement, false);
unHighlightLinkedNodes(currentSelectedElement, [], false);
currentSelectedElement = null;
} else { // clicking on a different node

// Remove highlight from all the nodes linked to the current selected node
unHighlightLinkedNodes(currentSelectedElement, false);
unHighlightLinkedNodes(currentSelectedElement, [], false);
if (currentSelectedElement) {
currentSelectedElement.removeClass(HIGHLIGHT_CLASS);
}

newClickedElement.addClass(HIGHLIGHT_CLASS);
highlightLinkedNodes(newClickedElement);
highlightLinkedNodes(newClickedElement, []);
currentSelectedElement = newClickedElement;
}
}
Expand All @@ -122,8 +128,7 @@ function dblClickElement(event) {
const links = $(newElementDlbClicked).find("a[xlink\\:href]");

if (links.length > 0) {
const targetLink = $(links[0]).attr("xlink:href");
document.location = targetLink;
document.location = $(links[0]).attr("xlink:href");
} else {
console.log("No links found on this element");
}
Expand Down

0 comments on commit bd59661

Please sign in to comment.