Skip to content

Commit

Permalink
front: Add script name prefix for running website in non-root
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Aug 27, 2024
1 parent 7136fb1 commit 941cc7a
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 45 deletions.
3 changes: 2 additions & 1 deletion front/src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios";
import { SCRIPT_NAME } from '../scriptname.js';

axios.defaults.baseURL = "/api";
axios.defaults.baseURL = SCRIPT_NAME + '/api'
axios.defaults.xsrfCookieName = "csrftoken";
axios.defaults.xsrfHeaderName = "X-CSRFToken";
axios.defaults.withCredentials = true;
Expand Down
3 changes: 2 additions & 1 deletion front/src/documentstasks/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios";
import { SCRIPT_NAME } from '../scriptname.js';

axios.defaults.baseURL = "/api";
axios.defaults.baseURL = SCRIPT_NAME + '/api'
axios.defaults.xsrfCookieName = "csrftoken";
axios.defaults.xsrfHeaderName = "X-CSRFToken";
axios.defaults.withCredentials = true;
Expand Down
3 changes: 2 additions & 1 deletion front/src/editor/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios";
import { SCRIPT_NAME } from '../scriptname.js';

axios.defaults.baseURL = "/api";
axios.defaults.baseURL = SCRIPT_NAME + '/api'
axios.defaults.xsrfCookieName = "csrftoken";
axios.defaults.xsrfHeaderName = "X-CSRFToken";
axios.defaults.withCredentials = true;
Expand Down
10 changes: 6 additions & 4 deletions front/src/image_cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Dropzone.autoDiscover = false;
var g_dragged = null; // Note: chrome doesn't understand dataTransfer very well
var lastSelected = null;

import { SCRIPT_NAME } from './scriptname.js';

function openWizard(proc) {
var selected_num = partCard.getSelectedPks().length;

Expand Down Expand Up @@ -115,7 +117,7 @@ class partCard {
this.progressBar.css("width", this.progress + "%");
this.progressBar.text(this.progress + "%");
this.updateWorkflowIcons();
var url = "/document/" + DOCUMENT_ID + "/part/" + this.pk + "/edit/";
var url = SCRIPT_NAME + "/document/" + DOCUMENT_ID + "/part/" + this.pk + "/edit/";

// show avg confidence on the card
var avgConfidenceElement = this.progressBar;
Expand Down Expand Up @@ -486,9 +488,9 @@ export function bootImageCards(
) {
DOCUMENT_ID = documentId;
API = {
document: "/api/documents/" + DOCUMENT_ID,
parts: "/api/documents/" + DOCUMENT_ID + "/parts/",
part: "/api/documents/" + DOCUMENT_ID + "/parts/{part_pk}/",
document: SCRIPT_NAME + "/api/documents/" + DOCUMENT_ID,
parts: SCRIPT_NAME + "/api/documents/" + DOCUMENT_ID + "/parts/",
part: SCRIPT_NAME + "/api/documents/" + DOCUMENT_ID + "/parts/{part_pk}/",
};
//************* Card ordering *************
$("#cards-container").on("dragover", ".js-drop", function (ev) {
Expand Down
4 changes: 3 additions & 1 deletion front/src/messages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export var msgSocket;

import { SCRIPT_NAME } from './scriptname.js';

var alerts = {};
export class Alert {
constructor(id, message, level, links) {
Expand Down Expand Up @@ -61,7 +63,7 @@ export class Alert {
export function bootWebsocket() {
let scheme = location.protocol === "https:" ? "wss:" : "ws:";
msgSocket = new ReconnectingWebSocket(
scheme + "//" + window.location.host + "/ws/notif/",
scheme + "//" + window.location.host + SCRIPT_NAME + "/ws/notif/",
);
msgSocket.maxReconnectAttempts = 3;

Expand Down
4 changes: 3 additions & 1 deletion front/src/ontology_form.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";

import { SCRIPT_NAME } from './scriptname.js';

export function bootOntologyForm() {
/*** Region/Line types ***/
function pushType(type, name) {
let uri = "/api/types/" + type + "/";
let uri = SCRIPT_NAME + "/api/types/" + type + "/";
return fetch(uri, {
method: "post",
credentials: "same-origin",
Expand Down
7 changes: 7 additions & 0 deletions front/src/scriptname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function scriptName() {
let prefix = document.currentScript.src.split('/').slice(3, -2).join('/');
return (prefix.length > 0 ? "/" + prefix : "");
}

//export const SCRIPT_NAME = scriptName();
export const SCRIPT_NAME = '/escriptorium2';
5 changes: 3 additions & 2 deletions front/src/stories/DocumentDashboard.stories.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import { Server } from "mock-socket";
import { SCRIPT_NAME } from '../scriptname.js';
import DocumentDashboard from "../../vue/pages/Document/Document.vue";
import GlobalNavigation from "../../vue/components/GlobalNavigation/GlobalNavigation.vue";
import {
Expand Down Expand Up @@ -30,7 +31,7 @@ export default {
// mock dropzone image upload api endpoint
mockData: [
{
url: "/api/documents/1/parts",
url: SCRIPT_NAME + "/api/documents/1/parts",
method: "POST",
status: 201,
},
Expand Down Expand Up @@ -146,7 +147,7 @@ const Template = (args, { argTypes }) => ({
setup() {
// setup websocket mocks for tasks
const scheme = location.protocol === "https:" ? "wss:" : "ws:";
const mockSocketURL = `${scheme}//${window.location.host}/ws/notif/`;
const mockSocketURL = `${scheme}//${window.location.host}${SCRIPT_NAME}/ws/notif/`;
// wrap in try/catch in case reloaded with mock socket still open
try {
const mockServer = new Server(mockSocketURL);
Expand Down
34 changes: 19 additions & 15 deletions front/vue/components/GlobalNavigation/GlobalNavigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<nav class="escr-global-nav">
<div class="escr-upper-navgroup">
<a
href="/"
:href="baseUrl + '/'"
aria-label="eScriptorium"
>
<EscrLogo />
Expand All @@ -15,7 +15,7 @@
>
<button
v-if="isAuthenticated && !searchDisabled"
href="/search/"
:href="baseUrl + '/search/'"
aria-label="global search"
class="escr-global-search"
:class="{
Expand All @@ -32,12 +32,12 @@
<template #popper>
<ul class="escr-vertical-menu escr-tasks-menu">
<li>
<a href="/search/">
<a :href="baseUrl + '/search/'">
<span>Search</span>
</a>
</li>
<li>
<a href="/find-replace/">
<a :href="baseUrl + '/find-replace/'">
<span>Find and Replace</span>
</a>
</li>
Expand All @@ -46,7 +46,7 @@
</VMenu>
<a
v-if="isAuthenticated"
href="/projects/"
:href="baseUrl + '/projects/'"
aria-label="projects list"
:class="{
'escr-globalnav-icon': true,
Expand All @@ -58,7 +58,7 @@
</a>
<a
v-if="isAuthenticated"
href="/models/"
:href="baseUrl + '/models/'"
aria-label="models list"
:class="{
'escr-globalnav-icon': true,
Expand Down Expand Up @@ -90,12 +90,12 @@
<template #popper>
<ul class="escr-vertical-menu escr-tasks-menu">
<li>
<a href="/documents/tasks/">
<a :href="baseUrl + '/documents/tasks/'">
<span>Task Monitoring</span>
</a>
</li>
<li>
<a href="/quotas/">
<a :href="baseUrl + '/quotas/'">
<span>Task Usage</span>
</a>
</li>
Expand Down Expand Up @@ -126,35 +126,35 @@
<template #popper>
<ul class="escr-vertical-menu">
<li>
<a href="/profile/">
<a :href="baseUrl + '/profile/'">
<span>Profile Settings</span>
</a>
</li>
<li>
<a href="/password_change/">
<a :href="baseUrl + '/password_change/'">
<span>Change Password</span>
</a>
</li>
<li v-if="canInvite">
<a href="/invite/">
<a :href="baseUrl + '/invite/'">
<span>Invite Users</span>
</a>
</li>
<li
v-if="isStaff"
class="new-section"
>
<a href="/quotas/instance/">
<a :href="baseUrl + '/quotas/instance/'">
<span>Leaderboard</span>
</a>
</li>
<li v-if="isStaff">
<a href="/admin/">
<a :href="baseUrl + '/admin/'">
<span>Site Administration</span>
</a>
</li>
<li class="new-section">
<a href="/logout/">
<a :href="baseUrl + '/logout/'">
<span>Logout</span>
</a>
</li>
Expand All @@ -163,7 +163,7 @@
</VMenu>
<a
v-else
href="/login"
:href="baseUrl + '/login'"
aria-label="sign in"
class="escr-globalnav-icon"
>
Expand Down Expand Up @@ -199,6 +199,7 @@ import TasksIcon from "../Icons/TasksIcon/TasksIcon.vue";
import "../VerticalMenu/VerticalMenu.css";
import "./GlobalNavigation.css";
import { mapActions, mapState } from "vuex";
import { SCRIPT_NAME } from '../../../src/scriptname.js';
export default {
name: "EscrGlobalNavigation",
Expand Down Expand Up @@ -233,6 +234,9 @@ export default {
canInvite: (state) => state.user.canInvite,
isStaff: (state) => state.user.isStaff,
}),
baseUrl() {
return SCRIPT_NAME || '/';
},
location() {
// helper to access window object from within template
return window.location;
Expand Down
3 changes: 2 additions & 1 deletion front/vue/components/ImportModal/ImportImagesForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { mapActions, mapState } from "vuex";
import UploadIcon from "../Icons/UploadIcon/UploadIcon.vue";
import ImageDropzone from "vue2-dropzone";
import "vue2-dropzone/dist/vue2Dropzone.min.css";
import { SCRIPT_NAME } from '../../../src/scriptname.js';
export default {
name: "EscrImportImagesForm",
Expand Down Expand Up @@ -62,7 +63,7 @@ export default {
* Use the document ID from vuex store state to get the parts upload URL.
*/
imageUploadURL() {
return `/api/documents/${this.documentId}/parts/`;
return SCRIPT_NAME + `/api/documents/${this.documentId}/parts/`;
},
getCsrfToken() {
return document.cookie.match("(^|;)\\s*" + "csrftoken" + "\\s*=\\s*([^;]+)")?.pop()
Expand Down
2 changes: 1 addition & 1 deletion front/vue/components/ModelsPanel/ModelsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<dd>{{ model.rights }}</dd>
<dt>Sharing</dt>
<dd v-if="model.can_share">
<a :href="`/model/${model.pk}/rights/`">Share</a>
<a :href="SCRIPT_NAME + `/model/${model.pk}/rights/`">Share</a>
</dd>
<dd v-else>
-
Expand Down
11 changes: 6 additions & 5 deletions front/vue/pages/Document/Document.vue
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ import OntologyCard from "../../components/OntologyCard/OntologyCard.vue";
import PencilIcon from "../../components/Icons/PencilIcon/PencilIcon.vue";
import PeopleIcon from "../../components/Icons/PeopleIcon/PeopleIcon.vue";
import QuickActionsPanel from "../../components/QuickActionsPanel/QuickActionsPanel.vue";
import { SCRIPT_NAME } from "../../../src/scriptname.js";
import SearchIcon from "../../components/Icons/SearchIcon/SearchIcon.vue";
import SearchPanel from "../../components/SearchPanel/SearchPanel.vue";
import SegmentModal from "../../components/SegmentModal/SegmentModal.vue";
Expand Down Expand Up @@ -484,13 +485,13 @@ export default {
docBreadcrumbs = [
{
title: this.projectName,
href: `/project/${this.projectSlug}`
href: SCRIPT_NAME + `/project/${this.projectSlug}`
},
{ title: this.documentName }
];
}
return [
{ title: "My Projects", href: "/projects" },
{ title: "My Projects", href: SCRIPT_NAME + "/projects" },
...docBreadcrumbs,
];
},
Expand Down Expand Up @@ -606,7 +607,7 @@ export default {
// join document websocket room
const msg = `{"type": "join-room", "object_cls": "document", "object_pk": ${this.id}}`;
const scheme = location.protocol === "https:" ? "wss:" : "ws:";
const msgSocket = new ReconnectingWebSocket(`${scheme}//${window.location.host}/ws/notif/`);
const msgSocket = new ReconnectingWebSocket(`${scheme}//${window.location.host}${SCRIPT_NAME}/ws/notif/`);
msgSocket.maxReconnectAttempts = 3;
msgSocket.addEventListener("open", function() {
msgSocket.send(msg);
Expand Down Expand Up @@ -663,13 +664,13 @@ export default {
}),
navigateToImages() {
if (this.id) {
window.location = `/document/${this.id}/images`;
window.location = SCRIPT_NAME + `/document/${this.id}/images`;
} else {
this.addError({ message: "Error navigating to the images page." });
}
},
navigateToTasks() {
window.location = "/quotas/";
window.location = SCRIPT_NAME + "/quotas/";
},
selectTranscription(e) {
this.changeSelectedTranscription(parseInt(e.target.value, 10));
Expand Down
3 changes: 2 additions & 1 deletion front/vue/pages/Page/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { mapActions } from "vuex";
import Alerts from "../../components/Toast/ToastGroup.vue";
import EscrBreadcrumbs from "../../components/Breadcrumbs/Breadcrumbs.vue";
import EscrSidebar from "../../components/Sidebar/Sidebar.vue";
import { SCRIPT_NAME } from "../../../src/scriptname.js";
export default {
name: "EscrPage",
Expand Down Expand Up @@ -62,7 +63,7 @@ export default {
created() {
// intercept all websocket messages
const scheme = location.protocol === "https:" ? "wss:" : "ws:";
const msgSocket = new ReconnectingWebSocket(`${scheme}//${window.location.host}/ws/notif/`);
const msgSocket = new ReconnectingWebSocket(`${scheme}//${window.location.host}${SCRIPT_NAME}/ws/notif/`);
msgSocket.maxReconnectAttempts = 3;
msgSocket.addEventListener("message", this.websocketListener);
},
Expand Down
5 changes: 3 additions & 2 deletions front/vue/pages/Project/Project.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ import ImagesIcon from "../../components/Icons/ImagesIcon/ImagesIcon.vue";
import PencilIcon from "../../components/Icons/PencilIcon/PencilIcon.vue";
import PeopleIcon from "../../components/Icons/PeopleIcon/PeopleIcon.vue";
import PlusIcon from "../../components/Icons/PlusIcon/PlusIcon.vue";
import { SCRIPT_NAME } from '../../../src/scriptname.js';
import SearchIcon from "../../components/Icons/SearchIcon/SearchIcon.vue";
import SearchPanel from "../../components/SearchPanel/SearchPanel.vue";
import ShareModal from "../../components/SharePanel/ShareModal.vue";
Expand Down Expand Up @@ -257,7 +258,7 @@ export default {
*/
breadcrumbs() {
return [
{ title: "My Projects", href: "/projects" },
{ title: "My Projects", href: SCRIPT_NAME + "/projects" },
{ title: this.projectName || "Loading..." }
];
},
Expand Down Expand Up @@ -395,7 +396,7 @@ export default {
},
navigateToImages(item) {
if (item?.pk) {
window.location = `/document/${item.pk}/images`;
window.location = SCRIPT_NAME + `/document/${item.pk}/images`;
} else {
this.addError({ message: "Error navigating to the images page." });
}
Expand Down
Loading

0 comments on commit 941cc7a

Please sign in to comment.