Skip to content
Open
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
2 changes: 1 addition & 1 deletion canvas_editor/editor/templates/editor/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}",
Expand Down
6 changes: 3 additions & 3 deletions canvas_editor/static/js/editor/jobInterface.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SaveAndLoadHandler } from "saveAndLoadHandler";
import { getCookie } from "../utils/cookieUtils.mjs";

let apiUrl = window.location.origin;

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions canvas_editor/static/js/editor/previewHandler.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 2 additions & 25 deletions canvas_editor/static/js/editor/saveAndLoadHandler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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),
})
Expand Down
27 changes: 2 additions & 25 deletions canvas_editor/static/js/projectOverviewManager.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getCookie } from "./utils/cookieUtils.mjs";
/**
* Handles the project overview page
*/
Expand Down Expand Up @@ -43,7 +44,7 @@ export class ProjectOverviewManager {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": this.#getCookie("csrftoken"),
"X-CSRFToken": getCookie("csrftoken"),
},
});
}
Expand Down Expand Up @@ -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]);
}
}
24 changes: 24 additions & 0 deletions canvas_editor/static/js/utils/cookieUtils.mjs
Original file line number Diff line number Diff line change
@@ -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]);
}