From d481f20342a87007f93cd2298d82f862dd96b886 Mon Sep 17 00:00:00 2001 From: Florent Yvon Date: Wed, 5 Feb 2025 14:01:01 +0000 Subject: [PATCH 1/3] Waiting for Swagger DOM elements to appear rather than using timeout. --- catalog/static/catalog/pgs.js | 29 +++++++++++++++++++++++ rest_api/templates/rest_api/rest_doc.html | 9 +++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/catalog/static/catalog/pgs.js b/catalog/static/catalog/pgs.js index 5af99f4e..4cf4c47c 100644 --- a/catalog/static/catalog/pgs.js +++ b/catalog/static/catalog/pgs.js @@ -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} 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 + }); + }); +} \ No newline at end of file diff --git a/rest_api/templates/rest_api/rest_doc.html b/rest_api/templates/rest_api/rest_doc.html index 180b293d..7628b29c 100644 --- a/rest_api/templates/rest_api/rest_doc.html +++ b/rest_api/templates/rest_api/rest_doc.html @@ -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'); @@ -165,7 +163,10 @@ }); $('[data-toggle="tooltip"]').tooltip(); - }, 150); + } + ) + + } From aeadc7d4138dd5a0dc3be7aef6fe0fce5d074568 Mon Sep 17 00:00:00 2001 From: Florent Yvon Date: Wed, 5 Feb 2025 14:10:40 +0000 Subject: [PATCH 2/3] Minor reformatting --- rest_api/templates/rest_api/rest_doc.html | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/rest_api/templates/rest_api/rest_doc.html b/rest_api/templates/rest_api/rest_doc.html index 7628b29c..ecc8d4f3 100644 --- a/rest_api/templates/rest_api/rest_doc.html +++ b/rest_api/templates/rest_api/rest_doc.html @@ -163,9 +163,7 @@ }); $('[data-toggle="tooltip"]').tooltip(); - } - ) - + }) } From 3e8d426a7c2f074e2168b19f20df5b6a28974c03 Mon Sep 17 00:00:00 2001 From: Florent Yvon Date: Wed, 5 Feb 2025 14:11:20 +0000 Subject: [PATCH 3/3] Minor reformatting --- rest_api/templates/rest_api/rest_doc.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_api/templates/rest_api/rest_doc.html b/rest_api/templates/rest_api/rest_doc.html index ecc8d4f3..ad26a104 100644 --- a/rest_api/templates/rest_api/rest_doc.html +++ b/rest_api/templates/rest_api/rest_doc.html @@ -163,7 +163,7 @@ }); $('[data-toggle="tooltip"]').tooltip(); - }) + }); }