class CorpusDisplay extends RessourceDisplay { 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); }); this.displayElement .querySelector('.action-switch[data-action="toggle-is-public"]') .addEventListener('click', (event) => { if (event.target.tagName !== 'INPUT') {return;} if (event.target.checked) { Utils.enableCorpusIsPublicRequest(this.userId, this.corpusId); } else { Utils.disableCorpusIsPublicRequest(this.userId, this.corpusId); } }); } init(user) { let corpus = user.corpora[this.corpusId]; this.setCreationDate(corpus.creation_date); this.setDescription(corpus.description); this.setIsPublic(corpus.is_public); this.setStatus(corpus.status); this.setTitle(corpus.title); this.setNumTokens(corpus.num_tokens); } 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) { 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': { re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/num_tokens`); if (re.test(operation.path)) { this.setNumTokens(operation.value); break; } re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/status$`); if (re.test(operation.path)) { this.setStatus(operation.value); break; } break; } default: { break; } } } } setTitle(title) { this.setElements(this.displayElement.querySelectorAll('.corpus-title'), title); } setNumTokens(numTokens) { this.setElements( this.displayElement.querySelectorAll('.corpus-token-ratio'), `${numTokens}/${app.data.users[this.userId].corpora[this.corpusId].max_num_tokens}` ); } setDescription(description) { this.setElements(this.displayElement.querySelectorAll('.corpus-description'), description); } setStatus(status) { let elements = this.displayElement.querySelectorAll('.corpus-analyse-trigger') for (let element of elements) { if (['BUILT', 'STARTING_ANALYSIS_SESSION', 'RUNNING_ANALYSIS_SESSION', 'CANCELING_ANALYSIS_SESSION'].includes(status)) { element.classList.remove('disabled'); } else { element.classList.add('disabled'); } } 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) { element.classList.remove('disabled'); } else { element.classList.add('disabled'); } } elements = this.displayElement.querySelectorAll('.corpus-status'); for (let element of elements) { element.dataset.status = status; } elements = this.displayElement.querySelectorAll('.corpus-status-spinner'); 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'); } } } setCreationDate(creationDate) { this.setElements( this.displayElement.querySelectorAll('.corpus-creation-date'), new Date(creationDate).toLocaleString("en-US") ); } }