Skip to content

Commit

Permalink
Merge pull request #411 from fyvon/fix/swagger_dom_update
Browse files Browse the repository at this point in the history
Waiting for Swagger DOM elements to appear rather than using timeout.
  • Loading branch information
fyvon authored Feb 5, 2025
2 parents 333a6e2 + 3e8d426 commit 905bd8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
29 changes: 29 additions & 0 deletions catalog/static/catalog/pgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1422,3 +1422,32 @@ class PGSPieChartTiny extends PGSPieChart {
.text(label);
}
}


/**
* This function returns a Promise which resolves when an element matching the given
* selector is found in the given parent node.
* @param selector A selector for the future wanted element.
* @param [parent=document.body] Only monitor within this node and children, or the whole document if not defined.
* @returns {Promise<Element>} A Promise which resolves when the element is found.
*/
function wait_for_element(selector, parent = document.body) {
return new Promise(resolve => {
if (parent.querySelector(selector)) {
// It's already present, just return it.
return resolve(parent.querySelector(selector));
}

const observer = new MutationObserver(mutations => {
if (parent.querySelector(selector)) {
observer.disconnect();
resolve(parent.querySelector(selector));
}
});

observer.observe(parent, {
childList: true,
subtree: true
});
});
}
7 changes: 3 additions & 4 deletions rest_api/templates/rest_api/rest_doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@
var topbar = document.getElementsByClassName("topbar")[0];
topbar.parentNode.removeChild(topbar);

// Remove the list of servers
setTimeout(function(){
wait_for_element('hgroup', document.getElementById('swagger-ui')).then(function(hgroup){

// Add Swagger logo
var hgroup = document.getElementsByTagName("hgroup")[0];
// Create 'div' class and add it ti the hgroup
var title_div = document.createElement("div");
title_div.classList.add('clearfix');
Expand Down Expand Up @@ -165,7 +163,8 @@
});

$('[data-toggle="tooltip"]').tooltip();
}, 150);
});

}
</script>
</head>
Expand Down

0 comments on commit 905bd8e

Please sign in to comment.