class RessourceList extends List { constructor(idOrElement, subscriberList, type, options={}) { if (!['corpus', 'job'].includes(type)) { console.error("Unknown Type!"); return; } super(idOrElement, {...RessourceList.options['common'], ...RessourceList.options[type], ...options}); this.type = type; subscriberList.push(this); } _init(ressources) { this.clear(); this.addRessources(Object.values(ressources)); this.sort("creation_date", {order: "desc"}); } _update(patch) { let item, pathArray; for (let operation of patch) { /* "/{ressourceName}/{ressourceId}/..." -> ["{ressourceId}", "..."] */ pathArray = operation.path.split("/").slice(2); switch(operation.op) { case "add": if (pathArray.includes("results")) {break;} this.addRessources([operation.value]); break; case "remove": this.remove("id", pathArray[0]); break; case "replace": item = this.get("id", pathArray[0])[0]; switch(pathArray[1]) { case "status": item.values({status: operation.value, "analyse-link": ["analysing", "prepared", "start analysis"].includes(operation.value) ? `/corpora/${pathArray[0]}/analyse` : ""}); break; default: break; } default: break; } } } addRessources(ressources) { this.add(ressources.map(x => RessourceList.dataMapper[this.type](x))); } } RessourceList.dataMapper = { corpus: corpus => ({creation_date: corpus.creation_date, description: corpus.description, id: corpus.id, "analyse-link": ["analysing", "prepared", "start analysis"].includes(corpus.status) ? `/corpora/${corpus.id}/analyse` : "", "edit-link": `/corpora/${corpus.id}`, status: corpus.status, title: corpus.title}), job: job => ({creation_date: job.creation_date, description: job.description, id: job.id, link: `/jobs/${job.id}`, service: job.service, status: job.status, title: job.title}) }; RessourceList.options = { common: {page: 4, pagination: {innerWindow: 8, outerWindow: 1}}, corpus: {item: ` book
edit Analysesearch `, valueNames: ["creation_date", "description", "title", {data: ["id"]}, {name: "analyse-link", attr: "href"}, {name: "edit-link", attr: "href"}, {name: "status", attr: "data-status"}]}, job: {item: `
Viewsend `, valueNames: ["creation_date", "description", "title", {data: ["id"]}, {name: "link", attr: "href"}, {name: "service", attr: "data-service"}, {name: "status", attr: "data-status"}]} }; class ResultsList extends List { constructor(idOrElement, options={}) { super(idOrElement, options); this.eventTokens = {}; // all span tokens which are holdeing events if expert // mode is on. Collected here to delete later on this.currentExpertTokenElements = {}; // all token elements which have added // classes like chip and hoverable for expert view. Collected //here to delete later on this.addToSubResultsStatus = {}; this.addToSubResultsIdsToShow = new Set(); } // handels interactionElements during a pagination navigation pageChangeEventInteractionHandler(interactionElements) { // get elements to check thier status for (let interaction of interactionElements) { if (interaction.checkStatus) { let element = interaction.getElement(); if (element.checked) { let f_on = interaction.bindThisToCallback("on"); let args_on = interaction.callbacks.on.args; f_on(...args_on); console.log("ON TRIGGERED"); } else { let f_off = interaction.bindThisToCallback("off"); let args_off = interaction.callbacks.off.args; f_off(...args_off); console.log("OFF TRIGGERED"); } } else { let f = interaction.bindThisToCallback("noCheck"); let args = interaction.callbacks.noCheck.args; f(...args); console.log("noCheck activated"); } } } // get display options from display options form element static getDisplayOptions(displayOptionsFormElement) { // gets display options parameters let displayOptionsFormData let displayOptionsData; displayOptionsFormData = new FormData(displayOptionsFormElement); displayOptionsData = { "resultsPerPage": displayOptionsFormData.get("display-options-form-results_per_page"), "resultsContex": displayOptionsFormData.get("display-options-form-result_context"), "expertMode": displayOptionsFormData.get("display-options-form-expert_mode") }; return displayOptionsData } // ###### Functions to add one match to a sub-results ###### // activate add button activateAddToSubResults() { subResultsIdListElement.classList.remove("hide"); subResultsCreateElement.classList.remove("hide"); let addToSubResultsBtnElements = document.getElementsByClassName("add"); for (let addToSubResultsBtn of addToSubResultsBtnElements) { addToSubResultsBtn.classList.remove("hide"); } } // deactivate add button deactivateAddToSubResults() { subResultsIdListElement.classList.add("hide"); subResultsCreateElement.classList.add("hide"); let addToSubResultsBtnElements = document.getElementsByClassName("add"); for (let addToSubResultsBtn of addToSubResultsBtnElements) { addToSubResultsBtn.classList.add("hide"); } } // add match id on click to a List of marked matches for SubSubcorpora // remove match id on click from same list addToSubResults(dataIndex) { let textarea = subResultsIdListElement.getElementsByTagName("textarea")[0] if (!this.addToSubResultsStatus[dataIndex] || this.addToSubResultsStatus === undefined) { // add button is activated nopaque.flash(`[Match ${dataIndex + 1}] Marked for Sub-Results!`); event.target.classList.remove("grey"); event.target.classList.add("green"); event.target.innerText = "check"; this.addToSubResultsStatus[dataIndex] = true; this.addToSubResultsIdsToShow.add(dataIndex + 1); textarea.innerText = [...this.addToSubResultsIdsToShow].sort(function(a, b){return a-b}).join(", "); M.textareaAutoResize(textarea); } else if (this.addToSubResultsStatus[dataIndex]) { // add button is deactivated nopaque.flash(`[Match ${dataIndex + 1}] Unmarked for Sub-results!`); event.target.classList.remove("green"); event.target.classList.add("grey"); event.target.innerText = "add"; this.addToSubResultsStatus[dataIndex] = false; this.addToSubResultsIdsToShow.delete(dataIndex + 1); textarea.innerText = [...this.addToSubResultsIdsToShow].sort(function(a, b){return a-b}).join(", "); M.textareaAutoResize(textarea); } if ([...this.addToSubResultsIdsToShow].length > 0) { subResultsCreateElement.firstElementChild.classList.remove("disabled"); } else if ([...this.addToSubResultsIdsToShow].length === 0) { subResultsCreateElement.firstElementChild.classList.add("disabled"); } subResultsExportElement.classList.add("disabled"); } // triggers emit to get full match context from server for a number of // matches identified by their data_index getMatchWithContext(dataIndexes, type) { let tmp_first_cpos = []; let tmp_last_cpos = []; for (let dataIndex of dataIndexes) { tmp_first_cpos.push(results.data.matches[dataIndex].c[0]); tmp_last_cpos.push(results.data.matches[dataIndex].c[1]); } nopaque.socket.emit("corpus_analysis_inspect_match", { type: type, data_indexes: dataIndexes, first_cpos: tmp_first_cpos, last_cpos: tmp_last_cpos, } ); } // ###### Functions to inspect one match, to show more details ###### // activate inspect buttons if progress is 100 activateInspect() { if (progress === 100) { let inspectBtnElements; inspectBtnElements = document.getElementsByClassName("inspect"); for (let inspectBtn of inspectBtnElements) { inspectBtn.classList.remove("disabled"); } } else { return } } // gets result cpos infos for one dataIndex (list of length 1) to send back to // the server inspect(dataIndex, type) { this.contextId = dataIndex[0]; let contextResultsElement; contextResultsElement = document.getElementById("context-results"); contextResultsElement.innerHTML = ""; // clear it from old inspects this.getMatchWithContext(dataIndex, type); contextModal.open(); let css = `margin-right: 10px;` let classes = `btn-floating btn waves-effect` + `waves-light grey right` addToSubResultsFromInspectElement = document.createElement("a"); addToSubResultsFromInspectElement.setAttribute("class", classes + `add`); addToSubResultsFromInspectElement.innerHTML = 'add'; addToSubResultsFromInspectElement.onclick= (event) => {this.addToSubResults(dataIndex)}; } // create Element from HTML String helper function HTMLTStrToElement(htmlStr) { // https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro/35385518#35385518 let template = document.createElement("template"); htmlStr = htmlStr.trim(); template.innerHTML = htmlStr; return template.content.firstChild; } showMatchContext(response) { this.contextData; let c; let contextModalLoading; let contextModalReady; let contextResultsElement; let highlightSentencesSwitchElement; let htmlTokenStr; let lc; let modalExpertModeSwitchElement; let modalTokenElements; let nrOfContextSentences; let partElement; let rc; let token; let tokenHTMLArray; let tokenHTMlElement; let uniqueContextS; let uniqueS; this.contextData = response.payload; this.contextData["cpos_ranges"] = response.payload.cpos_ranges; this.contextData["query"] = results.data.query; this.contextData["context_id"] = this.contextId; this.contextData["match_count"] = this.contextData.matches.length this.contextData["corpus_type"] = "inspect-result" Object.assign(this.contextData, results.metaData); console.log(this.contextData); contextResultsElement = document.getElementById("context-results"); modalExpertModeSwitchElement = document.getElementById("inspect-display-options-form-expert_mode_inspect"); highlightSentencesSwitchElement = document.getElementById("inspect-display-options-form-highlight_sentences"); nrOfContextSentences = document.getElementById("context-sentences"); uniqueS = new Set(); uniqueContextS = new Set(); // check if cpos ranges are used or not if (this.contextData.cpos_ranges == true) { // python range like function from MDN // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range) const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step)); lc = range(this.contextData.matches[0].lc[0], this.contextData.matches[0].lc[1], 1) c = range(this.contextData.matches[0].c[0], this.contextData.matches[0].c[1], 1) rc = range(this.contextData.matches[0].rc[0], this.contextData.matches[0].rc[1], 1) } else { lc = this.contextData.matches[0].lc; c = this.contextData.matches[0].c; rc = this.contextData.matches[0].rc; } // create sentence strings as tokens tokenHTMLArray = []; for (let cpos of lc) { token = this.contextData.cpos_lookup[cpos]; uniqueS.add(token.s) htmlTokenStr = `` + `${token.word}` + ``; tokenHTMlElement = this.HTMLTStrToElement(htmlTokenStr) tokenHTMLArray.push(tokenHTMlElement); } for (let cpos of c) { token = this.contextData.cpos_lookup[cpos]; uniqueContextS.add(token.s); uniqueS.add(token.s); htmlTokenStr = `` + `${token.word}` + ``; tokenHTMlElement = this.HTMLTStrToElement(htmlTokenStr) tokenHTMLArray.push(tokenHTMlElement); } this.contextData["context_s_ids"] = Array.from(uniqueContextS); for (let cpos of rc) { token = this.contextData.cpos_lookup[cpos]; uniqueS.add(token.s) htmlTokenStr = `` + `${token.word}` + ``; tokenHTMlElement = this.HTMLTStrToElement(htmlTokenStr) tokenHTMLArray.push(tokenHTMlElement); } // console.log(tokenHTMLArray); // console.log(uniqueS); for (let sId of uniqueS) { let htmlSentence = ``; let sentenceElement = this.HTMLTStrToElement(htmlSentence); for (let tokenElement of tokenHTMLArray) { if (tokenElement.dataset.sid == sId) { sentenceElement.appendChild(tokenElement); sentenceElement.insertAdjacentHTML("beforeend", ` `); } else { continue; } } contextResultsElement.appendChild(sentenceElement); } // add inspect display options events modalExpertModeSwitchElement.onchange = (event) => { if (event.target.checked) { this.expertModeOn("context-results"); } else { this.expertModeOff("context-results") } }; highlightSentencesSwitchElement.onchange = (event) => { if (event.target.checked) { this.higlightContextSentences(); } else { this.unhighlightContextSentences(); } }; nrOfContextSentences.onchange = (event) => { // console.log(event.target.value); this.changeSentenceContext(event.target.value); } // checks on new modal opening if switches are checked // if switches are checked functions are executed if (modalExpertModeSwitchElement.checked) { this.expertModeOn("context-results"); } if (highlightSentencesSwitchElement.checked) { this.higlightContextSentences(); } // checks the value of the number of sentences to show on modal opening // sets context sentences accordingly this.changeSentenceContext(nrOfContextSentences.value) } higlightContextSentences() { let sentences; sentences = document.getElementById("context-results").getElementsByClassName("sentence"); for (let s of sentences) { s.insertAdjacentHTML("beforeend", `

`) } } unhighlightContextSentences() { let sentences; let br; sentences = document.getElementById("context-results").getElementsByClassName("sentence"); for (let s of sentences) { br = s.lastChild; br.remove(); } } changeSentenceContext(sValue, maxSValue=10) { let array; let sentences; let toHideArray; let toShowArray; sValue = maxSValue - sValue; // console.log(sValue); sentences = document.getElementById("context-results").getElementsByClassName("sentence"); array = Array.from(sentences); if (sValue != 0) { toHideArray = array.slice(0, sValue).concat(array.slice(-(sValue))); toShowArray = array.slice(sValue, 9).concat(array.slice(9, -(sValue))) } else { toHideArray = []; toShowArray = array; } // console.log(array); // console.log("#######"); // console.log(toHideArray); for (let s of toHideArray) { s.classList.add("hide"); } for (let s of toShowArray) { s.classList.remove("hide"); } } // ###### Display options changing live how the matches are being displayed ###### // Event function that changes the shown hits per page. // Just alters the resultsList.page property changeHitsPerPage(event) { try { // console.log(this); this.page = event.target.value; this.update(); this.activateInspect(); if (expertModeSwitchElement.checked) { this.expertModeOn("query-display"); // page holds new result rows, so add new tooltips } nopaque.flash("Updated matches per page.", "corpus") } catch (e) { // console.log(e); // console.log("resultsList has no results right now."); } } // Event function triggered on context select change // also if pagination is clicked changeContext(event) { let array; let lc; let newContextValue; let rc; try { if (event.type === "change") { nopaque.flash("Updated context per match!", "corpus"); } } catch (e) { } finally { newContextValue = document.getElementById("display-options-form-result_context").value; lc = document.getElementsByClassName("left-context"); rc = document.getElementsByClassName("right-context"); for (let element of lc) { array = Array.from(element.childNodes); for (let element of array.reverse().slice(newContextValue)) { element.classList.add("hide"); } for (let element of array.slice(0, newContextValue)) { element.classList.remove("hide"); } } for (let element of rc) { array = Array.from(element.childNodes); for (let element of array.slice(newContextValue)) { element.classList.add("hide"); } for (let element of array.slice(0, newContextValue)) { element.classList.remove("hide"); } } } } // ###### Expert view event functions ###### // function to create a tooltip for the current hovered token tooltipEventCreate(event) { // console.log("Create Tooltip on mouseover."); let token; token = results.data.cpos_lookup[event.target.dataset.cpos]; if (!token) { token = this.contextData.cpos_lookup[event.target.dataset.cpos]; } this.addToolTipToTokenElement(event.target, token); } // Function to destroy the current Tooltip for the current hovered tooltip // on mouse leave tooltipEventDestroy(event) { // console.log("Tooltip destroy on leave."); this.currentTooltipElement.destroy(); } expertModeOn(htmlId) { // turn the expert mode on for all tokens in the DOM element identified by its htmlID console.log(this); if (!Array.isArray(this.currentExpertTokenElements[htmlId])) { this.currentExpertTokenElements[htmlId] = []; } this.currentExpertTokenElements[htmlId].push( ...document.getElementById(htmlId).getElementsByClassName("token")); this.tooltipEventCreateBind = this.tooltipEventCreate.bind(this); this.tooltipEventDestroyBind = this.tooltipEventDestroy.bind(this); this.eventTokens[htmlId] = []; for (let tokenElement of this.currentExpertTokenElements[htmlId]) { tokenElement.classList.add("chip", "hoverable", "expert-view"); tokenElement.onmouseover = this.tooltipEventCreateBind; tokenElement.onmouseout = this.tooltipEventDestroyBind; this.eventTokens[htmlId].push(tokenElement); } } // fuction that creates Tooltip for one token and extracts the corresponding // infos from the result JSON addToolTipToTokenElement(tokenElement, token) { this.currentTooltipElement; this.currentTooltipElement = M.Tooltip.init(tokenElement, {"html": `
Token information Source information
Word: ${token.word}
Lemma: ${token.lemma}
POS: ${token.pos}
Simple POS: ${token.simple_pos}
NER: ${token.ner}
Title: ${results.data.text_lookup[token.text].title}
Author: ${results.data.text_lookup[token.text].author}
Publishing year: ${results.data.text_lookup[token.text].publishing_year}
`} ); } // function to remove extra informations and animations from tokens expertModeOff(htmlId) { // console.log("Expert mode is off."); console.log(this); if (!Array.isArray(this.currentExpertTokenElements[htmlId])) { this.currentExpertTokenElements[htmlId] = []; } if (!Array.isArray(this.eventTokens[htmlId])) { this.eventTokens[htmlId] = []; } for (let tokenElement of this.currentExpertTokenElements[htmlId]) { tokenElement.classList.remove("chip", "hoverable", "expert-view"); } this.currentExpertTokenElements[htmlId] = []; for (let eventToken of this.eventTokens[htmlId]) { eventToken.onmouseover = ""; eventToken.onmouseout = ""; } this.eventTokens[htmlId] = []; } createResultRowElement(item, chunk) { let addToSubResultsBtn; let c; let cCellElement; let cpos; let inspectBtn let lc; let lcCellElement; let matchNrElement; let matchRowElement; let rc; let rcCellElement; let textTitles; let textTitlesCellElement; let token; let values; // gather values from item values = item.values(); if (chunk.cpos_ranges == true) { // python range like function from MDN // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range) const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step)); lc = range(values.lc[0], values.lc[1], 1) c = range(values.c[0], values.c[1], 1) rc = range(values.rc[0], values.rc[1], 1) } else { lc = values.lc; c = values.c; rc = values.rc; } // get infos for full match row matchRowElement = document.createElement("tr"); matchRowElement.setAttribute("data-index", values.index) lcCellElement = document.createElement("td"); lcCellElement.classList.add("left-context"); matchRowElement.appendChild(lcCellElement); for (cpos of lc) { token = chunk.cpos_lookup[cpos]; lcCellElement.insertAdjacentHTML("beforeend", `${token.word} `); } // get infos for hit of match textTitles = new Set(); cCellElement = document.createElement("td"); cCellElement.classList.add("match-hit"); textTitlesCellElement = document.createElement("td"); textTitlesCellElement.classList.add("titles"); matchNrElement = document.createElement("td"); matchNrElement.classList.add("match-nr"); matchRowElement.appendChild(cCellElement); for (cpos of c) { token = chunk.cpos_lookup[cpos]; cCellElement.insertAdjacentHTML("beforeend", `${token.word} `); // get text titles of every hit cpos token textTitles.add(chunk.text_lookup[token.text].title); } // add some interaction buttons // # some btn css rules and classes let css = `margin-right: 10px;` let classes = `btn-floating btn waves-effect` + `waves-light grey right` // # add button to trigger more context to every match td inspectBtn = document.createElement("a"); inspectBtn.setAttribute("class", classes + ` disabled inspect` ); inspectBtn.setAttribute("style", css) inspectBtn.innerHTML = 'search'; inspectBtn.onclick = () => {this.inspect([values.index], "inspect")}; // # add btn to add matches to sub-results. hidden per default addToSubResultsBtn = document.createElement("a"); addToSubResultsBtn.setAttribute("class", classes + ` hide add` ); addToSubResultsBtn.setAttribute("style", css) addToSubResultsBtn.innerHTML = 'add'; addToSubResultsBtn.onclick= (event) => {this.addToSubResults(values.index)} cCellElement.appendChild(inspectBtn); cCellElement.appendChild(addToSubResultsBtn); // add text titles at front as first td of one row textTitlesCellElement.innerText = [...textTitles].join(", "); matchRowElement.insertAdjacentHTML("afterbegin", textTitlesCellElement.outerHTML); matchNrElement.innerText = values.index + 1; matchRowElement.insertAdjacentHTML("afterbegin", matchNrElement.outerHTML); // get infos for right context of match rcCellElement = document.createElement("td"); rcCellElement.classList.add("right-context"); matchRowElement.appendChild(rcCellElement); for (cpos of rc) { token = chunk.cpos_lookup[cpos]; rcCellElement.insertAdjacentHTML("beforeend", `${token.word} `); } return matchRowElement } }