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

106 lines
3.6 KiB
JavaScript
Raw Normal View History

2023-10-05 12:11:17 +00:00
ResourceDisplays.CorpusDisplay = class CorpusDisplay extends ResourceDisplays.BaseDisplay {
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) => {
2023-10-04 12:07:39 +00:00
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
}
}
}
}
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.data.users[this.userId].corpora[this.corpusId].max_num_tokens}`
2021-12-01 13:15:20 +00:00
);
}
setDescription(description) {
2021-12-01 13:15:20 +00:00
this.setElements(this.displayElement.querySelectorAll('.corpus-description'), description);
}
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"]');
for (let element of elements) {
if (['UNPREPARED', 'FAILED'].includes(status) && Object.values(app.data.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');
2022-09-02 11:07:30 +00:00
for (let element of elements) {
element.dataset.status = status;
}
elements = this.displayElement.querySelectorAll('.corpus-status-spinner');
2022-09-02 11:07:30 +00:00
for (let 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")
);
}
2023-10-05 14:08:04 +00:00
};