diff --git a/web/app/static/js/modules/nopaque.CorpusAnalysisClient.js b/web/app/static/js/modules/nopaque.CorpusAnalysisClient.js index 9c172608..a2659b76 100644 --- a/web/app/static/js/modules/nopaque.CorpusAnalysisClient.js +++ b/web/app/static/js/modules/nopaque.CorpusAnalysisClient.js @@ -1,121 +1,169 @@ +/** + * This class is used to create an CorpusAnalysisClient object. + * The client handels the client server communication. + * It requests data (e.g. the analysis session or query results) from the + * the server and recieves them. + */ class CorpusAnalysisClient { - constructor(corpusId, socket) { + constructor(corpusId, socket, logging=false) { this.corpusId = corpusId; - this.socket = socket; this.displays = {}; + this.logging = logging; + this.socket = socket; + this.socketEventListeners = {}; } - setDisplay(type, display) { - this.displays[type] = display; + // Registers one or more SocketEventListeners to the CorpusAnalysisClient. + setSocketEventListeners(socketEventListeners) { + for (let listener of socketEventListeners) { + this.socketEventListeners[listener.type] = listener.listenerFunction; + } } - async initSession() { - let request = await this.requestSession(); - let recvieveSession = await this.recvieveSession(); - console.log("corpus_analysis_init: Waiting for response"); // this happens inbetween the two functions above + loadListeners() { + for (let [type, listener] of Object.entries(this.socketEventListeners)) { + listener(this); + } } - requestSession() { // should be private with ES2020 - console.log("corpus_analysis_init: Emitting to server"); + // Registers a CorpusAnalysisDisplay object to the CorpusAnalysisClient. + setDisplay(type, corpusAnalysisDisplay) { + this.displays[type] = corpusAnalysisDisplay; + } + + // /** + // * Initializes the interactive corpus analysis session via socket.io. + // * This function uses helper functions. + // */ + // initSession() { + // let request = this.requestSession(); + // let recvieveSession = this.recvieveSession(); + // console.info('corpus_analysis_init: Client waiting for response'); // this happens inbetween the two functions above + // } + + /** + * Requests a corpus analysis session via socket.io. + * Opens a loading modal at the start of the request + * Should be a private method if ES2020 is finalized (Maybe?) + */ + requestSession() { + console.info('corpus_analysis_init: Client requesting session via', + 'socket.emit'); if (this.displays.init != undefined) { this.displays.init.element.M_Modal.open(); - this.displays.init.setVisibilityByStatus("waiting"); + this.displays.init.setVisibilityByStatus('waiting'); } - return new Promise((resolve, reject) => { - this.socket.emit("corpus_analysis_init", this.corpusId); - resolve(); - }) + this.socket.emit('corpus_analysis_init', this.corpusId); } - recvieveSession() { // should be private with ES2020 - this.socket.on("corpus_analysis_init", (response) => { - console.log("corpus_analysis_init: Recieving response from server"); - if (response.code === 200) { - console.log("corpus_analysis_init: Initialization succeeded"); - if (this.displays.init != undefined) { - this.displays.init.element.M_Modal.close(); - this.displays.init.setVisibilityByStatus("success"); - } - } else { - let errorText = `Error ${response.code} - ${response.msg}`; - if (this.displays.init.errorContainer != undefined) { - this.displays.init.errorContainer.innerHTML = `
` + - `error ${errorText}
`; - } - if (this.displays.init != undefined) { - this.displays.init.setVisibilityByStatus("error"); - } - console.error(`corpus_analysis_init: ${errorText}`); - } - }); + // /** + // * Sends a query to the server and handles the response to that query. + // * This function uses helper functions. + // */ + // query(queryStr) { + // let requestQueryData = this.requestQueryData(queryStr); + // let recieveQueryProcessStatus = this.recieveQueryProcessStatus(); + // let recieveQueryData = this.recieveQueryData(); + // console.info('corpus_analysis_query: Client waiting for query data'); // this happens inbetween the two functions above + // } + + /** + * Sends the query string to the server. + * Should be a private method if ES2020 is finalized (Maybe?) + */ + requestQueryData(queryStr) { + console.info('corpus_analysis_query: Client requesting query data via', + 'socket.emit for the query', queryStr); + // TODO: Display stuff + this.socket.emit('corpus_analysis_query', queryStr); } } +/** + * This class is used to create an CorpusAnalysisDisplay object. + * Input is one HTMLElement that can then be hidden or shown depending on + * its CSS classes. + */ class CorpusAnalysisDisplay { constructor(element) { + // with this function initalized modals can also be handeld this.element = (() => {if (element instanceof HTMLElement) { return element; } else { - element = element["$el"][0]; + element = element['$el'][0]; return element; } - })(); // with this function modals can also be handeld - this.errorContainer = element.querySelector(".error-container"); - this.showOnError = element.querySelectorAll(".show-on-error"); - this.showOnSuccess = element.querySelectorAll(".show-on-success"); - this.showWhileWaiting = element.querySelectorAll(".show-while-waiting"); - this.hideOnComplete = element.querySelectorAll(".hide-on-complete") + })(); + this.errorContainer = element.querySelector('.error-container'); + this.showOnError = element.querySelectorAll('.show-on-error'); + this.showOnSuccess = element.querySelectorAll('.show-on-success'); + this.showWhileWaiting = element.querySelectorAll('.show-while-waiting'); + this.hideOnComplete = element.querySelectorAll('.hide-on-complete') } + // Changes the visibility of its own setVisibilityByStatus(status) { switch (status) { - case "error": + case 'error': for (let element of this.showOnError) { - element.classList.remove("hide"); + element.classList.remove('hide'); } for (let element of this.showOnSuccess) { - element.classList.add("hide"); + element.classList.add('hide'); } for (let element of this.showWhileWaiting) { - element.classList.add("hide"); + element.classList.add('hide'); } break; - case "success": + case 'success': for (let element of this.showOnError) { - element.classList.add("hide"); + element.classList.add('hide'); } for (let element of this.showOnSuccess) { - element.classList.remove("hide"); + element.classList.remove('hide'); } for (let element of this.showWhileWaiting) { - element.classList.add("hide"); + element.classList.add('hide'); } break; - case "waiting": + case 'waiting': for (let element of this.showOnError) { - element.classList.add("hide"); + element.classList.add('hide'); } for (let element of this.showOnSuccess) { - element.classList.add("hide"); + element.classList.add('hide'); } for (let element of this.showWhileWaiting) { - element.classList.remove("hide"); + element.classList.remove('hide'); } break; default: // Hide all for (let element of this.showOnError) { - element.classList.add("hide"); + element.classList.add('hide'); } for (let element of this.showOnSuccess) { - element.classList.add("hide"); + element.classList.add('hide'); } for (let element of this.showWhileWaiting) { - element.classList.add("hide"); + element.classList.add('hide'); } } } } -export {CorpusAnalysisClient, CorpusAnalysisDisplay}; \ No newline at end of file +/** + * This class is used to create an CorpusAnalysisDisplay object. + * Input is one HTMLElement that can then be hidden or shown depending on + * its CSS classes. + */ +class SocketEventListener { + constructor(type, listenerFunction) { + this.listenerFunction = listenerFunction; + this.type = type; + } +} + +// export both Classes from this module +export {CorpusAnalysisClient, CorpusAnalysisDisplay, SocketEventListener}; \ No newline at end of file diff --git a/web/app/static/js/modules/nopaque.listenerFunctions.js b/web/app/static/js/modules/nopaque.listenerFunctions.js new file mode 100644 index 00000000..ce94ca91 --- /dev/null +++ b/web/app/static/js/modules/nopaque.listenerFunctions.js @@ -0,0 +1,60 @@ +/** + * Recieves a corpus analysis session via socket.io. + * Closes the loading modal that has been opend with requestSession at the + * start of the request. + */ +function recieveSession(client) { + client.socket.on('corpus_analysis_init', (response) => { + console.group('recieve session') + console.info('corpus_analysis_init: Client recieving session/or error', + 'codes via socket.on'); + if (response.code === 200) { + console.info('corpus_analysis_init: Initialization succeeded'); + console.info(response); + console.groupEnd(); + if (client.displays.init != undefined) { + client.displays.init.element.M_Modal.close(); + client.displays.init.setVisibilityByStatus('success'); + } + } else { + let errorText = `Error ${response.code} - ${response.msg}`; + if (client.displays.init.errorContainer != undefined) { + client.displays.init.errorContainer.innerHTML = `` + + `error${errorText}
`; + } + if (client.displays.init != undefined) { + client.displays.init.setVisibilityByStatus('error'); + } + console.error(`corpus_analysis_init: ${errorText}`); + } + }); +} + +/** + * Recieves the query process status before any actual results are being + * transmitted. So it recieves error codes if a query failed or + * was invalid etc. + */ +function recieveQueryStatus(client) { + client.socket.on('corpus_analysis_query', (response) => { + console.group('corpus_analysis_query: Client recieving query process', + 'status via socket.on'); + console.info(response); + console.groupEnd(); + }); +} + +/** + * Recieves the query data from the request and handles it. + */ +function recieveQueryData(client) { + client.socket.on('corpus_analysis_query_results', (response) => { + console.group('corpus_analysis_query_results: Client recieving query', + 'data via socket.on'); + console.info(response); + console.groupEnd(); + }); +} + +// export listeners from this module +export {recieveSession, recieveQueryStatus, recieveQueryData}; \ No newline at end of file diff --git a/web/app/templates/corpora/analyse_corpus.html.j2 b/web/app/templates/corpora/analyse_corpus.html.j2 index 420ab29f..d96bdbea 100644 --- a/web/app/templates/corpora/analyse_corpus.html.j2 +++ b/web/app/templates/corpora/analyse_corpus.html.j2 @@ -67,11 +67,20 @@ {% endblock %}