diff --git a/canvas_editor/editor/templates/editor/editor.html b/canvas_editor/editor/templates/editor/editor.html index fc2bda92..85aa4f09 100644 --- a/canvas_editor/editor/templates/editor/editor.html +++ b/canvas_editor/editor/templates/editor/editor.html @@ -38,7 +38,7 @@ "inspectorClass": "{% static 'js/editor/inspector.mjs' %}", "previewHandler": "{% static 'js/editor/previewHandler.mjs' %}", "projectSettingsManager": "{% static 'js/editor/projectSettingsManager.mjs' %}", - "overview": "{% static 'js/editor/overview.mjs' %}", + "projectOverviewManager": "{% static 'js/editor/projectOverviewManager.mjs' %}", "createCommands": "{% static 'js/commands/createCommands.mjs' %}", "navBar": "{% static 'js/editor/navbar.mjs' %}", "jobInterface": "{% static 'js/editor/jobInterface.mjs' %}", diff --git a/canvas_editor/static/js/editor/jobInterface.mjs b/canvas_editor/static/js/editor/jobInterface.mjs index 322bb8d7..d616bbe0 100644 --- a/canvas_editor/static/js/editor/jobInterface.mjs +++ b/canvas_editor/static/js/editor/jobInterface.mjs @@ -1,4 +1,4 @@ -import { SaveAndLoadHandler } from "saveAndLoadHandler"; +import { getCookie } from "../utils/cookieUtils.mjs"; let apiUrl = window.location.origin; @@ -58,7 +58,7 @@ export class JobInterface { method: "POST", headers: { "Content-Type": "application/json", - "X-CSRFToken": SaveAndLoadHandler.getCookie("csrftoken"), + "X-CSRFToken": getCookie("csrftoken"), }, }) .then((res) => res.json()) @@ -92,7 +92,7 @@ export class JobInterface { method: "DELETE", headers: { "Content-Type": "application/json", - "X-CSRFToken": SaveAndLoadHandler.getCookie("csrftoken"), + "X-CSRFToken": getCookie("csrftoken"), }, }).catch((error) => { console.error("Error deleting job:", error); diff --git a/canvas_editor/static/js/editor/previewHandler.mjs b/canvas_editor/static/js/editor/previewHandler.mjs index 7a5cd8fe..dbc089e5 100644 --- a/canvas_editor/static/js/editor/previewHandler.mjs +++ b/canvas_editor/static/js/editor/previewHandler.mjs @@ -1,6 +1,6 @@ import * as THREE from "three"; -import { SaveAndLoadHandler } from "saveAndLoadHandler"; import { errorUploadingFile } from "message_dict"; +import { getCookie } from "../utils/cookieUtils.mjs"; /** * Handles the generation of project previews of the editor page @@ -65,7 +65,7 @@ export class PreviewHandler { method: "POST", body: formData, headers: { - "X-CSRFToken": SaveAndLoadHandler.getCookie("csrftoken"), + "X-CSRFToken": getCookie("csrftoken"), }, }).catch((error) => { console.error(errorUploadingFile, error); diff --git a/canvas_editor/static/js/editor/saveAndLoadHandler.mjs b/canvas_editor/static/js/editor/saveAndLoadHandler.mjs index 770eb213..59a3517a 100644 --- a/canvas_editor/static/js/editor/saveAndLoadHandler.mjs +++ b/canvas_editor/static/js/editor/saveAndLoadHandler.mjs @@ -2,6 +2,7 @@ import { Heliostat } from "heliostat"; import { LightSource } from "lightSource"; import { projectIdRequiredError } from "message_dict"; import { Receiver } from "receiver"; +import { getCookie } from "../utils/cookieUtils.mjs"; /** * Provides a wrapper for the API @@ -262,30 +263,6 @@ export class SaveAndLoadHandler { return this.#makeApiCall(url, "PUT", body); } - /** - * Utility function that gets the cookie specified by the name - * @param {string} name The name of the cookie you want to get. - * @returns {string|null} the cookie or null if it couldn't be found. - */ - static getCookie(name) { - if (!document.cookie) { - return null; - } - - // document.cookie is a key=value list separated by ';' - const xsrfCookies = document.cookie - .split(";") - .map((c) => c.trim()) - //filter the right cookie name - .filter((c) => c.startsWith(name + "=")); - - if (xsrfCookies.length === 0) { - return null; - } - // return the decoded value of the first cookie found - return decodeURIComponent(xsrfCookies[0].split("=")[1]); - } - /** * Wrapper function for an standard api call * @param {string} endpoint The endpoint to make the api call to @@ -298,7 +275,7 @@ export class SaveAndLoadHandler { method: method, headers: { "Content-Type": "application/json", - "X-CSRFToken": SaveAndLoadHandler.getCookie("csrftoken"), + "X-CSRFToken": getCookie("csrftoken"), }, body: JSON.stringify(body), }) diff --git a/canvas_editor/static/js/projectOverviewManager.mjs b/canvas_editor/static/js/projectOverviewManager.mjs index ed4c392e..ef937963 100644 --- a/canvas_editor/static/js/projectOverviewManager.mjs +++ b/canvas_editor/static/js/projectOverviewManager.mjs @@ -1,3 +1,4 @@ +import { getCookie } from "./utils/cookieUtils.mjs"; /** * Handles the project overview page */ @@ -43,7 +44,7 @@ export class ProjectOverviewManager { method: "POST", headers: { "Content-Type": "application/json", - "X-CSRFToken": this.#getCookie("csrftoken"), + "X-CSRFToken": getCookie("csrftoken"), }, }); } @@ -81,28 +82,4 @@ export class ProjectOverviewManager { }); }); } - - /** - * Gets the cookie specified by the name - * @param {string} name The name of the cookie you want to get. - * @returns {string|null} the cookie or null if it couldn't be found. - */ - #getCookie(name) { - if (!document.cookie) { - return null; - } - - // document.cookie is a key=value list separated by ';' - const xsrfCookies = document.cookie - .split(";") - .map((c) => c.trim()) - //filter the right cookie name - .filter((c) => c.startsWith(name + "=")); - - if (xsrfCookies.length === 0) { - return null; - } - // return the decoded value of the first cookie found - return decodeURIComponent(xsrfCookies[0].split("=")[1]); - } } diff --git a/canvas_editor/static/js/utils/cookieUtils.mjs b/canvas_editor/static/js/utils/cookieUtils.mjs new file mode 100644 index 00000000..9ad07a88 --- /dev/null +++ b/canvas_editor/static/js/utils/cookieUtils.mjs @@ -0,0 +1,24 @@ +/** + * Utility function that gets the cookie specified by the name + * @param {string} name The name of the cookie you want to get. + * @returns {string|null} The cookie or null if it couldn't be found. + */ +export function getCookie(name) { + if (!document.cookie) { + return null; + } + + // document.cookie is a key=value list separated by ';' + const cookies = document.cookie + .split(";") + .map((c) => c.trim()) + //filter the right cookie name + .filter((c) => c.startsWith(name + "=")); + + if (cookies.length === 0) { + return null; + } + + // return the decoded value of the first cookie found + return decodeURIComponent(cookies[0].split("=")[1]); +}