Skip to content

Commit

Permalink
fix: refactor recursive function
Browse files Browse the repository at this point in the history
  • Loading branch information
marpesia committed Nov 22, 2022
1 parent 0c0a258 commit ba70db0
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions views/js/qtiCreator/helper/findParentElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ define(['lodash'], function (_) {
'use strict';

/**
* Check all figures and replace figcaption element by markup in body in one Element
* @param {Object} element
*
* @param {Object} parentElement
* @param {string} serial
* @returns {Object}
*/
function checkFigureInElement(element = {}, serial) {
let parent = null;
_.forEach(element['elements'], childElement => {
const searchRecurse = (parentElement, serial) => {
if (!parentElement) {
return null;
}
if (parentElement.serial === serial) {
return parentElement;
}
let found = null;
_.some(parentElement['elements'], childElement => {
if (childElement.serial === serial) {
parent = element;
} else {
checkFigureInElement(childElement, serial);
found = parentElement;
} else if (parentElement['elements']) {
found = searchRecurse(childElement);
}
if (found) {
return true;
}
});
if (parent) {
return parent;
}
if (element.body) {
return checkFigureInElement(element.body, serial);
}
if (element.prompt) {
return checkFigureInElement(element.prompt, serial);
}
return;
}
return found;
};

/**
* Check all figures and replace figcaption elements by markup in body
Expand All @@ -54,7 +54,7 @@ define(['lodash'], function (_) {
*/
return function checkFigureInItemData(itemData = {}, serial) {
if (itemData.bdy) {
return checkFigureInElement(itemData.bdy, serial);
return searchRecurse(itemData.bdy, serial);
}
return;
};
Expand Down

0 comments on commit ba70db0

Please sign in to comment.