nopaque/app/static/js/resource-displays/corpus-display.js

103 lines
3.5 KiB
JavaScript
Raw Normal View History

nopaque.resource_displays.CorpusDisplay = class CorpusDisplay extends nopaque.resource_displays.ResourceDisplay {
static htmlClass = 'corpus-display';
constructor(displayElement) {
super(displayElement);
this.corpusId = displayElement.dataset.corpusId;
2022-09-02 11:07:30 +00:00
this.displayElement
.querySelector('.action-button[data-action="build-request"]')
.addEventListener('click', (event) => {
nopaque.requests.corpora.entity.build(this.corpusId);
2022-09-02 11:07:30 +00:00
});
}
2021-11-30 15:22:16 +00:00
init(user) {
2022-09-02 11:07:30 +00:00
let corpus = user.corpora[this.corpusId];
2021-11-30 15:22:16 +00:00
this.setCreationDate(corpus.creation_date);
this.setDescription(corpus.description);
this.setStatus(corpus.status);
this.setTitle(corpus.title);
2021-12-01 13:15:20 +00:00
this.setNumTokens(corpus.num_tokens);
}
2022-09-02 11:07:30 +00:00
onPatch(patch) {
let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}`);
let filteredPatch = patch.filter(operation => re.test(operation.path));
for (let operation of filteredPatch) {
switch(operation.op) {
2022-09-02 11:07:30 +00:00
case 'remove': {
let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}$`);
if (re.test(operation.path)) {
window.location.href = '/dashboard#corpora';
}
break;
}
case 'replace': {
2021-12-01 13:15:20 +00:00
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;
2022-09-02 11:07:30 +00:00
}
default: {
break;
2022-09-02 11:07:30 +00:00
}
}
}
}
2024-12-05 14:07:13 +00:00
async setTitle(title) {
const corpusTitleElements = this.displayElement.querySelectorAll('.corpus-title');
this.setElements(corpusTitleElements, title);
}
2021-12-01 13:15:20 +00:00
setNumTokens(numTokens) {
2024-12-05 14:07:13 +00:00
const corpusTokenRatioElements = this.displayElement.querySelectorAll('.corpus-token-ratio');
const maxNumTokens = 2147483647;
this.setElements(corpusTokenRatioElements, `${numTokens}/${maxNumTokens}`);
}
setDescription(description) {
2021-12-01 13:15:20 +00:00
this.setElements(this.displayElement.querySelectorAll('.corpus-description'), description);
}
2024-12-05 14:07:13 +00:00
async setStatus(status) {
let elements = this.displayElement.querySelectorAll('.action-button[data-action="analyze"]');
2022-09-02 11:07:30 +00:00
for (let 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');
}
}
2022-09-02 11:07:30 +00:00
elements = this.displayElement.querySelectorAll('.action-button[data-action="build-request"]');
2024-12-05 14:07:13 +00:00
const user = await app.userHub.get(this.userId);
const corpusFiles = user.corpora[this.corpusId].files;
2022-09-02 11:07:30 +00:00
for (let element of elements) {
2024-12-05 14:07:13 +00:00
if (['UNPREPARED', 'FAILED'].includes(status) && Object.values(corpusFiles.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');
2022-09-02 11:07:30 +00:00
for (let element of elements) {
2024-05-21 08:29:12 +00:00
element.dataset.corpusStatus = status;
}
}
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")
);
}
2023-10-05 14:08:04 +00:00
};