Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waiting for Swagger DOM elements to appear rather than using timeout. #411

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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