Restructure javascript

This commit is contained in:
Patrick Jentsch 2023-03-08 11:42:53 +01:00
parent 0d7fca9b0b
commit 6bb4594937
8 changed files with 131 additions and 22 deletions

View File

@ -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);
};

View File

@ -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);
}
);
});
};

View File

@ -1,16 +1,11 @@
class CorpusDisplay extends RessourceDisplay { class CorpusDisplay extends ResourceDisplay {
constructor(displayElement) { constructor(displayElement) {
super(displayElement); super(displayElement);
this.corpusId = displayElement.dataset.corpusId; this.corpusId = displayElement.dataset.corpusId;
this.displayElement this.displayElement
.querySelector('.action-button[data-action="build-request"]') .querySelector('.action-button[data-action="build-request"]')
.addEventListener('click', (event) => { .addEventListener('click', (event) => {
Utils.buildCorpusRequest(this.userId, this.corpusId); Requests.corpora.corpus.build(this.corpusId);
});
this.displayElement
.querySelector('.action-button[data-action="delete-request"]')
.addEventListener('click', (event) => {
Utils.deleteCorpusRequest(this.userId, this.corpusId);
}); });
} }

View File

@ -1,4 +1,4 @@
class JobDisplay extends RessourceDisplay { class JobDisplay extends ResourceDisplay {
constructor(displayElement) { constructor(displayElement) {
super(displayElement); super(displayElement);
this.jobId = this.displayElement.dataset.jobId; this.jobId = this.displayElement.dataset.jobId;

View File

@ -1,4 +1,4 @@
class RessourceDisplay { class ResourceDisplay {
constructor(displayElement) { constructor(displayElement) {
this.displayElement = displayElement; this.displayElement = displayElement;
this.userId = this.displayElement.dataset.userId; this.userId = this.displayElement.dataset.userId;

View File

@ -120,7 +120,7 @@ class SpaCyNLPPipelineModelList extends ResourceList {
switch (listAction) { switch (listAction) {
case 'toggle-is-public': { case 'toggle-is-public': {
let newIsPublicValue = listActionElement.checked; let newIsPublicValue = listActionElement.checked;
Utils.updateSpaCyNLPPipelineModelIsPublicRequest(itemId, newIsPublicValue) Requests.contributions.spacy_nlp_pipeline_models.ent.isPublic.update(itemId, newIsPublicValue)
.catch((response) => { .catch((response) => {
listActionElement.checked = !newIsPublicValue; listActionElement.checked = !newIsPublicValue;
}); });
@ -141,7 +141,7 @@ class SpaCyNLPPipelineModelList extends ResourceList {
let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction; let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction;
switch (listAction) { switch (listAction) {
case 'delete-request': { case 'delete-request': {
Utils.deleteSpaCyNLPPipelineModelRequest(this.userId, itemId); Requests.contributions.spacy_nlp_pipeline_models.ent.delete(itemId);
break; break;
} }
case 'view': { case 'view': {

View File

@ -129,7 +129,7 @@ class TesseractOCRPipelineModelList extends ResourceList {
switch (listAction) { switch (listAction) {
case 'toggle-is-public': { case 'toggle-is-public': {
let newIsPublicValue = listActionElement.checked; let newIsPublicValue = listActionElement.checked;
Utils.updateTesseractOCRPipelineModelIsPublicRequest(itemId, newIsPublicValue) Requests.contributions.tesseract_ocr_pipeline_models.ent.isPublic.update(itemId, newIsPublicValue)
.catch((response) => { .catch((response) => {
listActionElement.checked = !newIsPublicValue; listActionElement.checked = !newIsPublicValue;
}); });
@ -155,7 +155,7 @@ class TesseractOCRPipelineModelList extends ResourceList {
let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction; let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction;
switch (listAction) { switch (listAction) {
case 'delete-request': { case 'delete-request': {
Utils.deleteTesseractOCRPipelineModelRequest(this.userId, itemId); Requests.contributions.tesseract_ocr_pipeline_models.ent.delete(itemId);
break; break;
} }
case 'view': { case 'view': {

View File

@ -6,18 +6,37 @@
output='gen/app.%(version)s.js', output='gen/app.%(version)s.js',
'js/App.js', 'js/App.js',
'js/Utils.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/CQiClient.js',
'js/CorpusAnalysis/CorpusAnalysisApp.js', 'js/CorpusAnalysis/CorpusAnalysisApp.js',
'js/CorpusAnalysis/CorpusAnalysisConcordance.js', 'js/CorpusAnalysis/CorpusAnalysisConcordance.js',
'js/CorpusAnalysis/CorpusAnalysisReader.js', 'js/CorpusAnalysis/CorpusAnalysisReader.js',
'js/CorpusAnalysis/QueryBuilder.js', 'js/CorpusAnalysis/QueryBuilder.js',
'js/RessourceDisplays/RessourceDisplay.js', 'js/XMLtoObject.js'
'js/RessourceDisplays/CorpusDisplay.js', %}
'js/RessourceDisplays/JobDisplay.js', <script src="{{ ASSET_URL }}"></script>
{%- 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'
%}
<script src="{{ ASSET_URL }}"></script>
{%- endassets %}
{%- assets
filters='rjsmin',
output='gen/ResourceDisplays.%(version)s.js',
'js/ResourceDisplays/ResourceDisplay.js',
'js/ResourceDisplays/CorpusDisplay.js',
'js/ResourceDisplays/JobDisplay.js'
%}
<script src="{{ ASSET_URL }}"></script>
{%- endassets %}
{%- assets
filters='rjsmin',
output='gen/ResourceLists.%(version)s.js',
'js/ResourceLists/ResourceList.js', 'js/ResourceLists/ResourceList.js',
'js/ResourceLists/CorpusFileList.js', 'js/ResourceLists/CorpusFileList.js',
'js/ResourceLists/PublicCorpusFileList.js', 'js/ResourceLists/PublicCorpusFileList.js',
@ -31,8 +50,15 @@
'js/ResourceLists/TesseractOCRPipelineModelList.js', 'js/ResourceLists/TesseractOCRPipelineModelList.js',
'js/ResourceLists/UserList.js', 'js/ResourceLists/UserList.js',
'js/ResourceLists/AdminUserList.js', 'js/ResourceLists/AdminUserList.js',
'js/ResourceLists/CorpusFollowerList.js', 'js/ResourceLists/CorpusFollowerList.js'
'js/XMLtoObject.js' %}
<script src="{{ ASSET_URL }}"></script>
{%- endassets %}
{%- assets
filters='rjsmin',
output='gen/Requests.%(version)s.js',
'js/Requests/Requests.js',
'js/Requests/Contributions.js'
%} %}
<script src="{{ ASSET_URL }}"></script> <script src="{{ ASSET_URL }}"></script>
{%- endassets %} {%- endassets %}