Some Codestyle enhancements

This commit is contained in:
Patrick Jentsch 2023-07-13 15:27:49 +02:00
parent 4ae4b88a44
commit d898cd8516
2 changed files with 41 additions and 36 deletions

View File

@ -27,34 +27,32 @@ class CorpusAnalysisApp {
this.elements.m.initModal.open(); this.elements.m.initModal.open();
// Setup CQi over SocketIO connection and gather data from the CQPServer // Setup CQi over SocketIO connection and gather data from the CQPServer
let cqiClient;
let cqiCorpus;
try { try {
cqiClient = new cqi.CQiClient('/cqi_over_sio'); const cqiClient = new cqi.CQiClient('/cqi_over_sio');
let response = await cqiClient.api.socket.emitWithAck('init', this.corpusId); const response = await cqiClient.api.socket.emitWithAck('init', this.corpusId);
if (response.code !== 200) {throw new Error();} if (response.code !== 200) {throw new Error();}
await cqiClient.connect('anonymous', ''); await cqiClient.connect('anonymous', '');
cqiCorpus = await cqiClient.corpora.get(`NOPAQUE-${this.corpusId.toUpperCase()}`); const cqiCorpus = await cqiClient.corpora.get(`NOPAQUE-${this.corpusId.toUpperCase()}`);
// TODO: Don't do this hgere // TODO: Don't do this hgere
await cqiCorpus.updateDb(); await cqiCorpus.updateDb();
this.data.cqiClient = cqiClient;
this.data.cqiCorpus = cqiCorpus;
this.data.corpus = {o: cqiCorpus}; // legacy
} catch (error) { } catch (error) {
// TODO: Currently we can only handle CQiErrors here, // TODO: Currently we can only handle CQiErrors here,
// but we should also handle other errors. // but we should also handle other errors.
let errorString = `${error.code}: ${error.constructor.name}`; const errorString = `${error.code}: ${error.constructor.name}`;
let errorsElement = this.elements.initModal.querySelector('.errors'); const errorsElement = this.elements.initModal.querySelector('.errors');
let progressElement = this.elements.initModal.querySelector('.progress'); const progressElement = this.elements.initModal.querySelector('.progress');
errorsElement.innerText = errorString; errorsElement.innerText = errorString;
errorsElement.classList.remove('hide'); errorsElement.classList.remove('hide');
progressElement.classList.add('hide'); progressElement.classList.add('hide');
return; return;
} }
this.data.cqiClient = cqiClient;
this.data.cqiCorpus = cqiCorpus;
this.data.corpus = {o: cqiCorpus}; // legacy
// Initialize extensions // Initialize extensions
for (let extension of Object.values(this.extensions)) {extension.init();} for (const extension of Object.values(this.extensions)) {extension.init();}
for (let extensionSelectorElement of this.elements.extensionCards.querySelectorAll('.extension-selector')) { for (const extensionSelectorElement of this.elements.extensionCards.querySelectorAll('.extension-selector')) {
extensionSelectorElement.addEventListener('click', () => { extensionSelectorElement.addEventListener('click', () => {
this.elements.m.extensionTabs.select(extensionSelectorElement.dataset.target); this.elements.m.extensionTabs.select(extensionSelectorElement.dataset.target);
}); });
@ -65,34 +63,37 @@ class CorpusAnalysisApp {
} }
registerExtension(extension) { registerExtension(extension) {
if (extension.name in this.extensions) { if (extension.name in this.extensions) {return;}
console.error(`Can't register extension ${extension.name}: Already registered`);
return;
}
this.extensions[extension.name] = extension; this.extensions[extension.name] = extension;
} }
disableActionElements() { disableActionElements() {
let actionElements = this.elements.container.querySelectorAll('.corpus-analysis-action'); const actionElements = this.elements.container.querySelectorAll('.corpus-analysis-action');
for (let actionElement of actionElements) { for (const actionElement of actionElements) {
if (actionElement.nodeName === 'INPUT') { switch(actionElement.nodeName) {
case 'INPUT':
actionElement.disabled = true; actionElement.disabled = true;
} else if (actionElement.nodeName === 'SELECT') { break;
case 'SELECT':
actionElement.parentNode.querySelector('input.select-dropdown').disabled = true; actionElement.parentNode.querySelector('input.select-dropdown').disabled = true;
} else { break;
default:
actionElement.classList.add('disabled'); actionElement.classList.add('disabled');
} }
} }
} }
enableActionElements() { enableActionElements() {
let actionElements = this.elements.container.querySelectorAll('.corpus-analysis-action'); const actionElements = this.elements.container.querySelectorAll('.corpus-analysis-action');
for (let actionElement of actionElements) { for (const actionElement of actionElements) {
if (actionElement.nodeName === 'INPUT') { switch(actionElement.nodeName) {
case 'INPUT':
actionElement.disabled = false; actionElement.disabled = false;
} else if (actionElement.nodeName === 'SELECT') { break;
case 'SELECT':
actionElement.parentNode.querySelector('input.select-dropdown').disabled = false; actionElement.parentNode.querySelector('input.select-dropdown').disabled = false;
} else { break;
default:
actionElement.classList.remove('disabled'); actionElement.classList.remove('disabled');
} }
} }

View File

@ -19,7 +19,11 @@ class CorpusAnalysisReader {
this.settings = { this.settings = {
perPage: parseInt(this.elements.form['per-page'].value), perPage: parseInt(this.elements.form['per-page'].value),
textStyle: parseInt(this.elements.form['text-style'].value), textStyle: parseInt(this.elements.form['text-style'].value),
tokenRepresentation: this.elements.form['token-representation'].value tokenRepresentation: this.elements.form['token-representation'].value,
pagination: {
innerWindow: 5,
outerWindow: 1
}
} }
this.app.registerExtension(this); this.app.registerExtension(this);
@ -142,7 +146,7 @@ class CorpusAnalysisReader {
} }
// render page buttons (5 before and 5 after current page) // render page buttons (5 before and 5 after current page)
for (let i = this.data.corpus.p.page -5; i <= this.data.corpus.p.page; i++) { for (let i = this.data.corpus.p.page - this.settings.pagination.innerWindow; i <= this.data.corpus.p.page; i++) {
if (i <= 0) {continue;} if (i <= 0) {continue;}
pageElement = Utils.HTMLToElement( pageElement = Utils.HTMLToElement(
` `
@ -153,7 +157,7 @@ class CorpusAnalysisReader {
); );
this.elements.corpusPagination.appendChild(pageElement); this.elements.corpusPagination.appendChild(pageElement);
}; };
for (let i = this.data.corpus.p.page +1; i <= this.data.corpus.p.page +5; i++) { for (let i = this.data.corpus.p.page +1; i <= this.data.corpus.p.page + this.settings.pagination.innerWindow; i++) {
if (i > this.data.corpus.p.pages) {break;} if (i > this.data.corpus.p.pages) {break;}
pageElement = Utils.HTMLToElement( pageElement = Utils.HTMLToElement(
` `