nopaque/app/static/js/RessourceDisplays/CorpusDisplay.js

114 lines
3.6 KiB
JavaScript
Raw Normal View History

class CorpusDisplay extends RessourceDisplay {
constructor(displayElement) {
super(displayElement);
this.corpusId = displayElement.dataset.corpusId;
}
2021-11-30 15:22:16 +00:00
init(user) {
let corpus;
2021-12-01 13:15:20 +00:00
2021-11-30 15:22:16 +00:00
corpus = user.corpora[this.corpusId];
this.setCreationDate(corpus.creation_date);
this.setDescription(corpus.description);
this.setLastEditedDate(corpus.last_edited_date);
this.setStatus(corpus.status);
this.setTitle(corpus.title);
2021-12-01 13:15:20 +00:00
this.setNumTokens(corpus.num_tokens);
}
2021-12-03 15:10:54 +00:00
usersPatchHandler(patch) {
2021-12-01 13:15:20 +00:00
let filteredPatch;
let operation;
let re;
2021-11-30 15:22:16 +00:00
re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}`);
filteredPatch = patch.filter(operation => re.test(operation.path));
2021-12-01 13:15:20 +00:00
for (operation of filteredPatch) {
switch(operation.op) {
case 'replace':
2021-11-30 15:22:16 +00:00
re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/last_edited_date$`);
2021-12-01 13:15:20 +00:00
if (re.test(operation.path)) {
this.setLastEditedDate(operation.value);
break;
}
re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/num_tokens`);
if (re.test(operation.path)) {
2021-12-03 15:10:54 +00:00
this.setNumTokens(operation.value);
2021-12-01 13:15:20 +00:00
break;
}
2021-11-30 15:22:16 +00:00
re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/status$`);
2021-12-01 13:15:20 +00:00
if (re.test(operation.path)) {
2021-12-03 15:10:54 +00:00
this.setStatus(operation.value);
2021-12-01 13:15:20 +00:00
break;
}
break;
default:
break;
}
}
}
setTitle(title) {
2021-12-01 13:15:20 +00:00
this.setElements(this.displayElement.querySelectorAll('.corpus-title'), title);
}
2021-12-01 13:15:20 +00:00
setNumTokens(numTokens) {
this.setElements(
this.displayElement.querySelectorAll('.corpus-token-ratio'),
`${numTokens}/${app.users[this.userId].corpora[this.corpusId].max_num_tokens}`
);
}
setDescription(description) {
2021-12-01 13:15:20 +00:00
this.setElements(this.displayElement.querySelectorAll('.corpus-description'), description);
}
setStatus(status) {
2021-12-01 13:15:20 +00:00
let element;
let elements;
elements = this.displayElement.querySelectorAll('.corpus-analyse-trigger')
2021-12-01 13:15:20 +00:00
for (element of elements) {
if (['BUILT', 'STARTING_ANALYSIS_SESSION', 'RUNNING_ANALYSIS_SESSION', 'CANCELING_ANALYSIS_SESSION'].includes(status)) {
2021-01-11 14:43:15 +00:00
element.classList.remove('disabled');
} else {
2021-01-11 14:43:15 +00:00
element.classList.add('disabled');
}
}
elements = this.displayElement.querySelectorAll('.corpus-build-trigger');
2021-12-01 13:15:20 +00:00
for (element of elements) {
2022-04-29 11:04:03 +00:00
if (['UNPREPARED', 'FAILED'].includes(status) && Object.values(app.users[this.userId].corpora[this.corpusId].files).length > 0) {
2021-01-11 14:43:15 +00:00
element.classList.remove('disabled');
} else {
element.classList.add('disabled');
}
}
elements = this.displayElement.querySelectorAll('.corpus-status');
2021-12-01 13:15:20 +00:00
for (element of elements) {
element.dataset.corpusStatus = status;
}
elements = this.displayElement.querySelectorAll('.corpus-status-spinner');
2021-12-01 13:15:20 +00:00
for (element of elements) {
if (['SUBMITTED', 'QUEUED', 'BUILDING', 'STARTING_ANALYSIS_SESSION', 'CANCELING_ANALYSIS_SESSION'].includes(status)) {
element.classList.remove('hide');
} else {
element.classList.add('hide');
}
}
}
2021-11-30 15:22:16 +00:00
setCreationDate(creationDate) {
2021-12-01 13:15:20 +00:00
this.setElements(
this.displayElement.querySelectorAll('.corpus-creation-date'),
new Date(creationDate).toLocaleString("en-US")
);
}
2021-11-30 15:22:16 +00:00
setLastEditedDate(lastEditedDate) {
2021-12-01 13:15:20 +00:00
this.setElements(
this.displayElement.querySelectorAll('.corpus-end-date'),
new Date(lastEditedDate).toLocaleString("en-US")
);
}
}