nopaque/app/static/js/corpus-analysis/app.js

120 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

2023-11-13 11:59:36 +00:00
nopaque.corpus_analysis.App = class App {
2021-11-16 14:23:57 +00:00
constructor(corpusId) {
2023-07-13 10:42:47 +00:00
this.corpusId = corpusId;
this.data = {};
2021-11-16 14:23:57 +00:00
// HTML elements
this.elements = {
2023-08-08 14:00:05 +00:00
container: document.querySelector('#corpus-analysis-container'),
extensionCards: document.querySelector('#corpus-analysis-extension-cards'),
extensionTabs: document.querySelector('#corpus-analysis-extension-tabs'),
initModal: document.querySelector('#corpus-analysis-init-modal')
2021-11-16 14:23:57 +00:00
};
// Materialize elements
this.elements.m = {
extensionTabs: M.Tabs.init(this.elements.extensionTabs),
initModal: M.Modal.init(this.elements.initModal, {dismissible: false})
};
this.extensions = {};
2023-07-13 10:42:47 +00:00
this.settings = {};
2021-11-16 14:23:57 +00:00
}
2023-07-13 10:42:47 +00:00
async init() {
2021-11-16 14:23:57 +00:00
this.disableActionElements();
this.elements.m.initModal.open();
2023-10-30 10:36:28 +00:00
2023-07-13 10:42:47 +00:00
try {
// Setup CQi over SocketIO connection and gather data from the CQPServer
const statusTextElement = this.elements.initModal.querySelector('.status-text');
statusTextElement.innerText = 'Creating CQi over SocketIO client...';
const cqiClient = new nopaque.corpus_analysis.cqi.Client('/cqi_over_sio');
statusTextElement.innerText += ' Done';
statusTextElement.innerHTML = 'Waiting for the CQP server...';
2023-07-13 13:27:49 +00:00
const response = await cqiClient.api.socket.emitWithAck('init', this.corpusId);
2023-07-13 10:42:47 +00:00
if (response.code !== 200) {throw new Error();}
statusTextElement.innerText += ' Done';
statusTextElement.innerHTML = 'Connecting to the CQP server...';
2023-07-13 10:42:47 +00:00
await cqiClient.connect('anonymous', '');
statusTextElement.innerText += ' Done';
statusTextElement.innerHTML = 'Building and receiving corpus data cache from the server (This may take a while)...';
2023-07-13 13:27:49 +00:00
const cqiCorpus = await cqiClient.corpora.get(`NOPAQUE-${this.corpusId.toUpperCase()}`);
statusTextElement.innerText += ' Done';
2023-07-13 10:42:47 +00:00
// TODO: Don't do this hgere
await cqiCorpus.updateDb();
2023-07-13 13:27:49 +00:00
this.data.cqiClient = cqiClient;
this.data.cqiCorpus = cqiCorpus;
this.data.corpus = {o: cqiCorpus}; // legacy
// Initialize extensions
for (const extension of Object.values(this.extensions)) {
statusTextElement.innerHTML = `Initializing ${extension.name} extension...`;
await extension.init();
statusTextElement.innerText += ' Done'
}
2023-07-13 10:42:47 +00:00
} catch (error) {
let errorString = '';
if ('code' in error && error.code !== undefined && error.code !== null) {
errorString += `[${error.code}] `;
}
errorString += `${error.constructor.name}`;
if ('description' in error && error.description !== undefined && error.description !== null) {
errorString += `: ${error.description}`;
}
2023-07-13 13:27:49 +00:00
const errorsElement = this.elements.initModal.querySelector('.errors');
const progressElement = this.elements.initModal.querySelector('.progress');
2023-07-13 10:42:47 +00:00
errorsElement.innerText = errorString;
errorsElement.classList.remove('hide');
progressElement.classList.add('hide');
return;
}
2023-07-13 13:27:49 +00:00
for (const extensionSelectorElement of this.elements.extensionCards.querySelectorAll('.extension-selector')) {
2021-11-16 14:23:57 +00:00
extensionSelectorElement.addEventListener('click', () => {
this.elements.m.extensionTabs.select(extensionSelectorElement.dataset.target);
});
}
2023-07-13 10:42:47 +00:00
this.enableActionElements();
this.elements.m.initModal.close();
2021-11-16 14:23:57 +00:00
}
registerExtension(extension) {
2023-07-13 13:27:49 +00:00
if (extension.name in this.extensions) {return;}
2021-11-16 14:23:57 +00:00
this.extensions[extension.name] = extension;
}
disableActionElements() {
2023-07-13 13:27:49 +00:00
const actionElements = this.elements.container.querySelectorAll('.corpus-analysis-action');
for (const actionElement of actionElements) {
switch(actionElement.nodeName) {
case 'INPUT':
actionElement.disabled = true;
break;
case 'SELECT':
actionElement.parentNode.querySelector('input.select-dropdown').disabled = true;
break;
default:
actionElement.classList.add('disabled');
2021-11-16 14:23:57 +00:00
}
}
}
enableActionElements() {
2023-07-13 13:27:49 +00:00
const actionElements = this.elements.container.querySelectorAll('.corpus-analysis-action');
for (const actionElement of actionElements) {
switch(actionElement.nodeName) {
case 'INPUT':
actionElement.disabled = false;
break;
case 'SELECT':
actionElement.parentNode.querySelector('input.select-dropdown').disabled = false;
break;
default:
actionElement.classList.remove('disabled');
2021-11-16 14:23:57 +00:00
}
}
}
}