From 6bb4594937619307e66bacf65d0e560547af9964 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Wed, 8 Mar 2023 11:42:53 +0100
Subject: [PATCH] Restructure javascript
---
app/static/js/Requests/Contributions.js | 51 +++++++++++++++++++
app/static/js/Requests/Requests.js | 37 ++++++++++++++
.../CorpusDisplay.js | 9 +---
.../JobDisplay.js | 2 +-
.../ResourceDisplay.js} | 2 +-
.../SpacyNLPPipelineModelList.js | 4 +-
.../TesseractOCRPipelineModelList.js | 4 +-
app/templates/_scripts.html.j2 | 44 ++++++++++++----
8 files changed, 131 insertions(+), 22 deletions(-)
create mode 100644 app/static/js/Requests/Contributions.js
create mode 100644 app/static/js/Requests/Requests.js
rename app/static/js/{RessourceDisplays => ResourceDisplays}/CorpusDisplay.js (91%)
rename app/static/js/{RessourceDisplays => ResourceDisplays}/JobDisplay.js (99%)
rename app/static/js/{RessourceDisplays/RessourceDisplay.js => ResourceDisplays/ResourceDisplay.js} (97%)
diff --git a/app/static/js/Requests/Contributions.js b/app/static/js/Requests/Contributions.js
new file mode 100644
index 00000000..30605135
--- /dev/null
+++ b/app/static/js/Requests/Contributions.js
@@ -0,0 +1,51 @@
+/*****************************************************************************
+* Contributions *
+* Fetch requests for /contributions routes *
+*****************************************************************************/
+Requests.contributions = {};
+
+Requests.contributions.spacy_nlp_pipeline_models = {};
+
+Requests.contributions.spacy_nlp_pipeline_models.ent = {};
+
+Requests.contributions.spacy_nlp_pipeline_models.ent.delete = (spacyNlpPipelineModelId) => {
+ let input = `/contributions/spacy-nlp-pipeline-models/${spacyNlpPipelineModelId}`;
+ let init = {
+ method: 'DELETE'
+ };
+ return Requests.JSONfetch(input, init);
+};
+
+Requests.contributions.spacy_nlp_pipeline_models.ent.isPublic = {};
+
+Requests.contributions.spacy_nlp_pipeline_models.ent.isPublic.update = (spacyNlpPipelineModelId, value) => {
+ let input = `/contributions/spacy-nlp-pipeline-models/${spacyNlpPipelineModelId}/is_public`;
+ let init = {
+ method: 'PUT',
+ body: JSON.stringify(value)
+ };
+ return Requests.JSONfetch(input, init);
+};
+
+Requests.contributions.tesseract_ocr_pipeline_models = {};
+
+Requests.contributions.tesseract_ocr_pipeline_models.ent = {};
+
+Requests.contributions.tesseract_ocr_pipeline_models.ent.delete = (tesseractOcrPipelineModelId) => {
+ let input = `/contributions/tesseract-ocr-pipeline-models/${tesseractOcrPipelineModelId}`;
+ let init = {
+ method: 'DELETE'
+ };
+ return Requests.JSONfetch(input, init);
+};
+
+Requests.contributions.tesseract_ocr_pipeline_models.ent.isPublic = {};
+
+Requests.contributions.tesseract_ocr_pipeline_models.ent.isPublic.update = (tesseractOcrPipelineModelId, value) => {
+ let input = `/contributions/tesseract-ocr-pipeline-models/${tesseractOcrPipelineModelId}/is_public`;
+ let init = {
+ method: 'PUT',
+ body: JSON.stringify(value)
+ };
+ return Requests.JSONfetch(input, init);
+};
diff --git a/app/static/js/Requests/Requests.js b/app/static/js/Requests/Requests.js
new file mode 100644
index 00000000..41f05a7f
--- /dev/null
+++ b/app/static/js/Requests/Requests.js
@@ -0,0 +1,37 @@
+Requests = {};
+
+Requests.JSONfetch = (input, init={}) => {
+ return new Promise((resolve, reject) => {
+ let fixedInit = {};
+ fixedInit.headers = {};
+ fixedInit.headers['Accept'] = 'application/json';
+ if (init.hasOwnProperty('body')) {
+ fixedInit.headers['Content-Type'] = 'application/json';
+ }
+ fetch(input, Utils.mergeObjectsDeep(init, fixedInit))
+ .then(
+ (response) => {
+ response.json()
+ .then(
+ (json) => {
+ let message = json.message || json;
+ let category = json.category || 'message';
+ app.flash(message, category);
+ },
+ (error) => {
+ app.flash(`[${response.status}]: ${response.statusText}`, 'error');
+ }
+ );
+ if (response.ok) {
+ resolve(response);
+ } else {
+ reject(response);
+ }
+ },
+ (response) => {
+ app.flash('Something went wrong', 'error');
+ reject(response);
+ }
+ );
+ });
+};
diff --git a/app/static/js/RessourceDisplays/CorpusDisplay.js b/app/static/js/ResourceDisplays/CorpusDisplay.js
similarity index 91%
rename from app/static/js/RessourceDisplays/CorpusDisplay.js
rename to app/static/js/ResourceDisplays/CorpusDisplay.js
index ab462e9b..07f0a9be 100644
--- a/app/static/js/RessourceDisplays/CorpusDisplay.js
+++ b/app/static/js/ResourceDisplays/CorpusDisplay.js
@@ -1,16 +1,11 @@
-class CorpusDisplay extends RessourceDisplay {
+class CorpusDisplay extends ResourceDisplay {
constructor(displayElement) {
super(displayElement);
this.corpusId = displayElement.dataset.corpusId;
this.displayElement
.querySelector('.action-button[data-action="build-request"]')
.addEventListener('click', (event) => {
- Utils.buildCorpusRequest(this.userId, this.corpusId);
- });
- this.displayElement
- .querySelector('.action-button[data-action="delete-request"]')
- .addEventListener('click', (event) => {
- Utils.deleteCorpusRequest(this.userId, this.corpusId);
+ Requests.corpora.corpus.build(this.corpusId);
});
}
diff --git a/app/static/js/RessourceDisplays/JobDisplay.js b/app/static/js/ResourceDisplays/JobDisplay.js
similarity index 99%
rename from app/static/js/RessourceDisplays/JobDisplay.js
rename to app/static/js/ResourceDisplays/JobDisplay.js
index 03c5601b..8b94e49b 100644
--- a/app/static/js/RessourceDisplays/JobDisplay.js
+++ b/app/static/js/ResourceDisplays/JobDisplay.js
@@ -1,4 +1,4 @@
-class JobDisplay extends RessourceDisplay {
+class JobDisplay extends ResourceDisplay {
constructor(displayElement) {
super(displayElement);
this.jobId = this.displayElement.dataset.jobId;
diff --git a/app/static/js/RessourceDisplays/RessourceDisplay.js b/app/static/js/ResourceDisplays/ResourceDisplay.js
similarity index 97%
rename from app/static/js/RessourceDisplays/RessourceDisplay.js
rename to app/static/js/ResourceDisplays/ResourceDisplay.js
index a07c2163..24a5dec3 100644
--- a/app/static/js/RessourceDisplays/RessourceDisplay.js
+++ b/app/static/js/ResourceDisplays/ResourceDisplay.js
@@ -1,4 +1,4 @@
-class RessourceDisplay {
+class ResourceDisplay {
constructor(displayElement) {
this.displayElement = displayElement;
this.userId = this.displayElement.dataset.userId;
diff --git a/app/static/js/ResourceLists/SpacyNLPPipelineModelList.js b/app/static/js/ResourceLists/SpacyNLPPipelineModelList.js
index 405c29d6..5997fb0c 100644
--- a/app/static/js/ResourceLists/SpacyNLPPipelineModelList.js
+++ b/app/static/js/ResourceLists/SpacyNLPPipelineModelList.js
@@ -120,7 +120,7 @@ class SpaCyNLPPipelineModelList extends ResourceList {
switch (listAction) {
case 'toggle-is-public': {
let newIsPublicValue = listActionElement.checked;
- Utils.updateSpaCyNLPPipelineModelIsPublicRequest(itemId, newIsPublicValue)
+ Requests.contributions.spacy_nlp_pipeline_models.ent.isPublic.update(itemId, newIsPublicValue)
.catch((response) => {
listActionElement.checked = !newIsPublicValue;
});
@@ -141,7 +141,7 @@ class SpaCyNLPPipelineModelList extends ResourceList {
let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction;
switch (listAction) {
case 'delete-request': {
- Utils.deleteSpaCyNLPPipelineModelRequest(this.userId, itemId);
+ Requests.contributions.spacy_nlp_pipeline_models.ent.delete(itemId);
break;
}
case 'view': {
diff --git a/app/static/js/ResourceLists/TesseractOCRPipelineModelList.js b/app/static/js/ResourceLists/TesseractOCRPipelineModelList.js
index 8d9ce515..29d48dbb 100644
--- a/app/static/js/ResourceLists/TesseractOCRPipelineModelList.js
+++ b/app/static/js/ResourceLists/TesseractOCRPipelineModelList.js
@@ -129,7 +129,7 @@ class TesseractOCRPipelineModelList extends ResourceList {
switch (listAction) {
case 'toggle-is-public': {
let newIsPublicValue = listActionElement.checked;
- Utils.updateTesseractOCRPipelineModelIsPublicRequest(itemId, newIsPublicValue)
+ Requests.contributions.tesseract_ocr_pipeline_models.ent.isPublic.update(itemId, newIsPublicValue)
.catch((response) => {
listActionElement.checked = !newIsPublicValue;
});
@@ -155,7 +155,7 @@ class TesseractOCRPipelineModelList extends ResourceList {
let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction;
switch (listAction) {
case 'delete-request': {
- Utils.deleteTesseractOCRPipelineModelRequest(this.userId, itemId);
+ Requests.contributions.tesseract_ocr_pipeline_models.ent.delete(itemId);
break;
}
case 'view': {
diff --git a/app/templates/_scripts.html.j2 b/app/templates/_scripts.html.j2
index 18cde5b8..a97f1d97 100644
--- a/app/templates/_scripts.html.j2
+++ b/app/templates/_scripts.html.j2
@@ -6,18 +6,37 @@
output='gen/app.%(version)s.js',
'js/App.js',
'js/Utils.js',
- 'js/Forms/Form.js',
- 'js/Forms/CreateCorpusFileForm.js',
- 'js/Forms/CreateJobForm.js',
- 'js/Forms/CreateContributionForm.js',
'js/CorpusAnalysis/CQiClient.js',
'js/CorpusAnalysis/CorpusAnalysisApp.js',
'js/CorpusAnalysis/CorpusAnalysisConcordance.js',
'js/CorpusAnalysis/CorpusAnalysisReader.js',
'js/CorpusAnalysis/QueryBuilder.js',
- 'js/RessourceDisplays/RessourceDisplay.js',
- 'js/RessourceDisplays/CorpusDisplay.js',
- 'js/RessourceDisplays/JobDisplay.js',
+ 'js/XMLtoObject.js'
+%}
+
+{%- endassets %}
+{%- assets
+ filters='rjsmin',
+ output='gen/Forms.%(version)s.js',
+ 'js/Forms/Form.js',
+ 'js/Forms/CreateCorpusFileForm.js',
+ 'js/Forms/CreateJobForm.js',
+ 'js/Forms/CreateContributionForm.js'
+%}
+
+{%- endassets %}
+{%- assets
+ filters='rjsmin',
+ output='gen/ResourceDisplays.%(version)s.js',
+ 'js/ResourceDisplays/ResourceDisplay.js',
+ 'js/ResourceDisplays/CorpusDisplay.js',
+ 'js/ResourceDisplays/JobDisplay.js'
+%}
+
+{%- endassets %}
+{%- assets
+ filters='rjsmin',
+ output='gen/ResourceLists.%(version)s.js',
'js/ResourceLists/ResourceList.js',
'js/ResourceLists/CorpusFileList.js',
'js/ResourceLists/PublicCorpusFileList.js',
@@ -31,8 +50,15 @@
'js/ResourceLists/TesseractOCRPipelineModelList.js',
'js/ResourceLists/UserList.js',
'js/ResourceLists/AdminUserList.js',
- 'js/ResourceLists/CorpusFollowerList.js',
- 'js/XMLtoObject.js'
+ 'js/ResourceLists/CorpusFollowerList.js'
+%}
+
+{%- endassets %}
+{%- assets
+ filters='rjsmin',
+ output='gen/Requests.%(version)s.js',
+ 'js/Requests/Requests.js',
+ 'js/Requests/Contributions.js'
%}
{%- endassets %}