From 22d7b40393a3204a9fdf4d20797f126c9184be5f Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Fri, 20 Oct 2023 11:32:06 +0530 Subject: [PATCH 1/2] JS: Remove all references to global URL constants Instead they are passed in wherever needed. Cleaner code, and path towards everything being more testable --- binderhub/static/js/index.js | 24 +++++++++++++++++++++++- binderhub/static/js/src/constants.js | 24 ------------------------ binderhub/static/js/src/repo.js | 8 ++++---- 3 files changed, 27 insertions(+), 29 deletions(-) delete mode 100644 binderhub/static/js/src/constants.js diff --git a/binderhub/static/js/index.js b/binderhub/static/js/index.js index 7630b0d33..8b7ae9942 100644 --- a/binderhub/static/js/index.js +++ b/binderhub/static/js/index.js @@ -18,10 +18,32 @@ import "bootstrap/dist/css/bootstrap-theme.min.css"; import "../index.css"; import { setUpLog } from "./src/log"; import { updateUrls } from "./src/urls"; -import { BASE_URL, BADGE_BASE_URL } from "./src/constants"; import { getBuildFormValues } from "./src/form"; import { updateRepoText } from "./src/repo"; +/** + * @type {URL} + * Base URL of this binderhub installation. + * + * Guaranteed to have a leading & trailing slash by the binderhub python configuration. + */ +const BASE_URL = new URL( + document.getElementById("base-url").dataset.url, + document.location.origin, +); + +const badge_base_url = document.getElementById("badge-base-url").dataset.url; +/** + * @type {URL} + * Base URL to use for both badge images as well as launch links. + * + * If not explicitly set, will default to BASE_URL. Primarily set up different than BASE_URL + * when used as part of a federation + */ +const BADGE_BASE_URL = badge_base_url + ? new URL(badge_base_url, document.location.origin) + : BASE_URL; + async function build(providerSpec, log, fitAddon, path, pathType) { updateFavicon(new URL("favicon_building.ico", BASE_URL)); // split provider prefix off of providerSpec diff --git a/binderhub/static/js/src/constants.js b/binderhub/static/js/src/constants.js deleted file mode 100644 index 3057e1681..000000000 --- a/binderhub/static/js/src/constants.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @type {URL} - * Base URL of this binderhub installation. - * - * Guaranteed to have a leading & trailing slash by the binderhub python configuration. - */ -export const BASE_URL = new URL( - document.getElementById("base-url").dataset.url, - document.location.origin, -); - -const badge_base_url = document.getElementById("badge-base-url").dataset.url; -/** - * @type {URL} - * Base URL to use for both badge images as well as launch links. - * - * If not explicitly set, will default to BASE_URL. Primarily set up different than BASE_URL - * when used as part of a federation. - * - * Guaranteed to have a trailing slash by the binderhub python configuration. - */ -export const BADGE_BASE_URL = badge_base_url - ? new URL(badge_base_url, document.location.origin) - : BASE_URL; diff --git a/binderhub/static/js/src/repo.js b/binderhub/static/js/src/repo.js index 4cbcf1b1a..e848b49ed 100644 --- a/binderhub/static/js/src/repo.js +++ b/binderhub/static/js/src/repo.js @@ -1,5 +1,3 @@ -import { BASE_URL } from "./constants"; - /** * Dict holding cached values of API request to _config endpoint */ @@ -21,10 +19,12 @@ function setLabels() { /** * Update labels for various inputboxes based on user selection of repo provider + * + * @param {URL} baseUrl Base URL to use for constructing path to _config endpoint */ -export function updateRepoText() { +export function updateRepoText(baseUrl) { if (Object.keys(configDict).length === 0) { - const configUrl = new URL("_config", BASE_URL); + const configUrl = new URL("_config", baseUrl); fetch(configUrl).then((resp) => { resp.json().then((data) => { configDict = data; From e8896593d73d25a000a0ee68e99575a6ce1f478c Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Sun, 22 Oct 2023 23:02:33 +0200 Subject: [PATCH 2/2] JS: adjustment to new function signature --- binderhub/static/js/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/binderhub/static/js/index.js b/binderhub/static/js/index.js index 8b7ae9942..536a4e278 100644 --- a/binderhub/static/js/index.js +++ b/binderhub/static/js/index.js @@ -150,7 +150,7 @@ function indexMain() { $("#provider_prefix-selected").text($(this).text()); $("#provider_prefix").val($(this).attr("value")); - updateRepoText(); + updateRepoText(BASE_URL); updateUrls(BADGE_BASE_URL); }); @@ -164,7 +164,7 @@ function indexMain() { updateUrls(BADGE_BASE_URL); }); updatePathText(); - updateRepoText(); + updateRepoText(BASE_URL); $("#repository").on("keyup paste change", function () { updateUrls(BADGE_BASE_URL);