nopaque/app/static/js/nopaque.lists.js

513 lines
18 KiB
JavaScript
Raw Normal View History

2020-01-31 13:14:08 +00:00
class RessourceList extends List {
2020-02-12 11:19:54 +00:00
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;
2020-01-29 09:50:31 +00:00
subscriberList.push(this);
}
2020-01-31 13:14:08 +00:00
_init(ressources) {
2020-02-07 15:00:48 +00:00
this.addRessources(Object.values(ressources));
2020-02-07 14:21:59 +00:00
this.sort("creation_date", {order: "desc"});
2020-01-29 09:50:31 +00:00
}
_update(patch) {
2020-01-31 13:14:08 +00:00
let item, pathArray;
2020-01-29 09:50:31 +00:00
2020-01-31 13:14:08 +00:00
for (let operation of patch) {
/* "/ressourceId/valueName" -> ["ressourceId", "valueName"] */
2020-01-29 09:50:31 +00:00
pathArray = operation.path.split("/").slice(1);
switch(operation.op) {
case "add":
2020-02-03 15:04:47 +00:00
if (pathArray.includes("results")) {break;}
2020-02-07 15:00:48 +00:00
this.addRessources([operation.value]);
2020-01-29 09:50:31 +00:00
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});
break;
default:
break;
}
default:
break;
}
}
}
2020-02-07 15:00:48 +00:00
addRessources(ressources) {
2020-02-12 11:19:54 +00:00
this.add(ressources.map(x => RessourceList.dataMapper[this.type](x)));
2020-01-29 09:50:31 +00:00
}
}
2020-01-31 13:14:08 +00:00
RessourceList.dataMapper = {
2020-02-07 14:21:59 +00:00
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` : "",
2020-02-12 11:19:54 +00:00
"edit-link": `/corpora/${corpus.id}`,
2020-02-07 14:21:59 +00:00
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})
2020-01-31 13:14:08 +00:00
};
RessourceList.options = {
2020-02-12 11:19:54 +00:00
common: {page: 4, pagination: {innerWindow: 8, outerWindow: 1}},
corpus: {item: `<tr>
<td>
<a class="btn-floating disabled">
<i class="material-icons service">book</i>
</a>
</td>
<td>
<b class="title"></b><br>
<i class="description"></i>
</td>
<td>
2020-04-08 09:37:34 +00:00
<span class="badge new status" data-badge-caption="">
</span>
2020-02-12 11:19:54 +00:00
</td>
<td class="right-align">
2020-04-08 09:37:34 +00:00
<a class="btn-small edit-link waves-effect waves-light">
<i class="material-icons">edit</i>
</a>
<a class="btn-small analyse-link waves-effect waves-light">
Analyse<i class="material-icons right">search</i>
</a>
2020-02-12 11:19:54 +00:00
</td>
</tr>`,
valueNames: ["creation_date", "description", "title",
{data: ["id"]},
{name: "analyse-link", attr: "href"},
{name: "edit-link", attr: "href"},
{name: "status", attr: "data-status"}]},
job: {item: `<tr>
<td>
<a class="btn-floating disabled">
<i class="material-icons service"></i>
</a>
</td>
<td>
<b class="title"></b><br>
<i class="description"></i>
</td>
<td>
<span class="badge new status" data-badge-caption=""></span>
</td>
<td class="right-align">
2020-04-08 09:37:34 +00:00
<a class="btn-small link waves-effect waves-light">
View<i class="material-icons right">send</i>
</a>
2020-02-12 11:19:54 +00:00
</td>
</tr>`,
valueNames: ["creation_date", "description", "title",
{data: ["id"]},
{name: "link", attr: "href"},
{name: "service", attr: "data-service"},
{name: "status", attr: "data-status"}]}
};
2020-01-29 15:12:57 +00:00
2020-04-02 12:22:03 +00:00
class ResultsList extends List {
constructor(idOrElement, options={}) {
2020-04-14 09:31:57 +00:00
super(idOrElement, options);
this.eventTokens = []; // all span tokens which are holdeing events if expert mode is on
}
2020-01-29 15:12:57 +00:00
2020-04-14 09:31:57 +00:00
2020-04-07 11:13:48 +00:00
// get display options from display options form element
static getDisplayOptions(displayOptionsFormElement) {
// gets display options parameters
let displayOptionsFormData
let displayOptionsData;
displayOptionsFormData = new FormData(displayOptionsFormElement);
2020-04-08 09:37:34 +00:00
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")
};
2020-04-07 11:13:48 +00:00
return displayOptionsData
}
// ###### Functions to inspect one match, to show more details ######
2020-04-08 09:37:34 +00:00
// activate inspect buttons if progress is 100
activateInspect() {
let inspectBtnElements;
2020-04-07 11:13:48 +00:00
if (progress === 100) {
inspectBtnElements = document.getElementsByClassName("inspect");
for (let inspectBtn of inspectBtnElements) {
inspectBtn.classList.remove("disabled");
}
} else {
return
}
}
//gets result cpos infos for one dataIndex to send back to the server
inspect(dataIndex) {
2020-04-08 09:37:34 +00:00
let contextResultsElement;
contextResultsElement = document.getElementById("context-results");
2020-04-07 11:13:48 +00:00
contextResultsElement.innerHTML = ""; // clear it from old inspects
contextModal.open();
nopaque.socket.emit("corpus_analysis_inspect_match",
2020-04-09 08:17:04 +00:00
{
payload: {
2020-04-08 09:37:34 +00:00
first_cpos: results.resultsJSON.matches[dataIndex].c[0],
last_cpos: results.resultsJSON.matches[dataIndex].c[1]
}
}
);
2020-04-07 11:13:48 +00:00
}
2020-04-15 13:11:25 +00:00
HTMLTStrToElement(htmlStr) {
2020-04-15 12:55:29 +00:00
// 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;
}
2020-04-07 11:13:48 +00:00
showMatchContext(response) {
2020-04-15 12:55:29 +00:00
this.contextData;
2020-04-08 09:37:34 +00:00
let c;
2020-04-07 11:13:48 +00:00
let contextModalLoading;
let contextModalReady;
2020-04-08 09:37:34 +00:00
let contextResultsElement;
let expertModeSwitchElement;
let lc;
2020-04-15 12:55:29 +00:00
let modalTokenElements;
2020-04-08 09:37:34 +00:00
let partElement;
let rc;
2020-04-07 11:13:48 +00:00
let token;
2020-04-15 12:55:29 +00:00
let tokenHTMLArray;
let uniqueS;
let htmlTokenStr;
let tokenHTMlElement;
this.contextData = response.payload;
2020-04-07 11:13:48 +00:00
contextResultsElement = document.getElementById("context-results");
2020-04-08 09:37:34 +00:00
expertModeSwitchElement = document.getElementById("display-options-form-expert_mode");
2020-04-15 12:55:29 +00:00
uniqueS = new Set();
2020-04-08 09:37:34 +00:00
// check if cpos ranges are used or not
2020-04-15 12:55:29 +00:00
if (this.contextData.cpos_ranges == true) {
2020-04-08 09:37:34 +00:00
// python range like function from MDN
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range)
2020-04-07 11:13:48 +00:00
const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
2020-04-15 12:55:29 +00:00
lc = range(this.contextData.match.lc[0], this.contextData.match.lc[1], 1)
c = range(this.contextData.match.c[0], this.contextData.match.c[1], 1)
rc = range(this.contextData.match.rc[0], this.contextData.match.rc[1], 1)
2020-04-07 11:13:48 +00:00
} else {
2020-04-15 12:55:29 +00:00
lc = this.contextData.match.lc;
c = this.contextData.match.c;
rc = this.contextData.match.rc;
2020-04-07 11:13:48 +00:00
}
2020-04-08 09:37:34 +00:00
// create sentence strings as tokens
2020-04-15 12:55:29 +00:00
tokenHTMLArray = [];
2020-04-07 11:13:48 +00:00
for (let cpos of lc) {
2020-04-15 12:55:29 +00:00
token = this.contextData.cpos_lookup[cpos];
uniqueS.add(token.s)
htmlTokenStr = `<span class="token"` +
`data-sid="${token.s}"` +
`data-cpos="${cpos}">` +
`${token.word}` +
`</span>`;
2020-04-15 13:11:25 +00:00
tokenHTMlElement = this.HTMLTStrToElement(htmlTokenStr)
2020-04-15 12:55:29 +00:00
tokenHTMLArray.push(tokenHTMlElement);
2020-04-07 11:13:48 +00:00
}
for (let cpos of c) {
2020-04-15 12:55:29 +00:00
token = this.contextData.cpos_lookup[cpos];
uniqueS.add(token.s)
htmlTokenStr = `<span class="token bold light-green"` +
`data-sid="${token.s}"` +
`data-cpos="${cpos}"` +
`style="text-decoration-line: underline;">` +
`${token.word}` +
`</span>`;
2020-04-15 13:11:25 +00:00
tokenHTMlElement = this.HTMLTStrToElement(htmlTokenStr)
2020-04-15 12:55:29 +00:00
tokenHTMLArray.push(tokenHTMlElement);
2020-04-07 11:13:48 +00:00
}
for (let cpos of rc) {
2020-04-15 12:55:29 +00:00
token = this.contextData.cpos_lookup[cpos];
uniqueS.add(token.s)
htmlTokenStr = `<span class="token"` +
`data-sid="${token.s}"` +
`data-cpos="${cpos}">` +
`${token.word}` +
`</span>`;
2020-04-15 13:11:25 +00:00
tokenHTMlElement = this.HTMLTStrToElement(htmlTokenStr)
2020-04-15 12:55:29 +00:00
tokenHTMLArray.push(tokenHTMlElement);
}
console.log(tokenHTMLArray);
console.log(uniqueS);
2020-04-15 13:22:48 +00:00
partElement = document.createElement("div");
2020-04-15 12:55:29 +00:00
for (let sId of uniqueS) {
2020-04-15 13:11:25 +00:00
let htmlSentence = `<div class="sentence" data-sid="${sId}"></div>`;
let sentenceElement = this.HTMLTStrToElement(htmlSentence);
2020-04-15 12:55:29 +00:00
for (let tokenElement of tokenHTMLArray) {
if (tokenElement.dataset.sid == sId) {
sentenceElement.appendChild(tokenElement);
sentenceElement.insertAdjacentHTML("beforeend", `<span>&nbsp;</span>`);
} else {
continue;
}
}
partElement.appendChild(sentenceElement);
2020-04-07 11:13:48 +00:00
}
2020-04-15 12:55:29 +00:00
contextResultsElement.appendChild(partElement);
2020-04-07 11:13:48 +00:00
if (expertModeSwitchElement.checked) {
2020-04-14 09:31:57 +00:00
this.expertModeOn();
2020-04-07 11:13:48 +00:00
}
}
2020-04-15 12:55:29 +00:00
changeSentenceContext(sValue) {
}
2020-04-07 11:13:48 +00:00
// ###### 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 {
2020-04-14 09:31:57 +00:00
// console.log(this);
this.page = event.target.value;
this.update();
if (expertModeSwitchElement.checked) {
this.expertModeOn(); // page holds new result rows, so add new tooltips
}
2020-04-07 11:13:48 +00:00
nopaque.flash("Updated matches per page.")
} catch (e) {
2020-04-14 09:31:57 +00:00
// console.log(e);
// console.log("resultsList has no results right now.");
2020-04-07 11:13:48 +00:00
}
}
2020-04-08 09:37:34 +00:00
// Event function triggered on context select change
// also if pagination is clicked
2020-04-07 11:13:48 +00:00
changeContext(event) {
2020-04-08 09:37:34 +00:00
let array;
2020-04-07 11:13:48 +00:00
let lc;
2020-04-08 09:37:34 +00:00
let newContextValue;
2020-04-07 11:13:48 +00:00
let rc;
try {
if (event.type === "change") {
nopaque.flash("Updated context per match!");
}
} 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);
2020-04-08 07:46:37 +00:00
for (let element of array.reverse().slice(newContextValue)) {
2020-04-07 11:13:48 +00:00
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 ######
// Event function to check if pagination is used and then look if
// expertModeSwitchElement is checked
// if checked than expertModeOn is executed
// if unchecked expertModeOff is executed
eventHandlerCheck(event) {
if (expertModeSwitchElement.checked) {
this.expertModeOn(event.currentTarget.tokenElements, resultsJSON);
} else if (!expertModeSwitchElement.checked) {
event.preventDefault();
this.expertModeOff(event.currentTarget.tokenElements);
}
}
2020-04-14 09:31:57 +00:00
// function to create a tooltip for the current hovered token
tooltipEventCreate(event) {
// console.log("Create Tooltip on mouseover.");
2020-04-07 11:13:48 +00:00
let token;
token = results.resultsJSON.cpos_lookup[event.target.dataset.cpos];
2020-04-15 12:55:29 +00:00
if (!token) {
token = this.contextData.cpos_lookup[event.target.dataset.cpos];
}
this.addToolTipToTokenElement(event.target, token);
}
2020-04-14 09:31:57 +00:00
// 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() {
2020-04-14 09:31:57 +00:00
// console.log("Expert mode is on.");
this.currentExpertTokenElements = document.getElementsByClassName("token");
2020-04-14 09:31:57 +00:00
this.tooltipEventCreateBind = this.tooltipEventCreate.bind(this);
this.tooltipEventDestroyBind = this.tooltipEventDestroy.bind(this);
for (let tokenElement of this.currentExpertTokenElements) {
tokenElement.classList.add("chip", "hoverable", "expert-view");
2020-04-14 09:31:57 +00:00
tokenElement.onmouseover = this.tooltipEventCreateBind;
tokenElement.onmouseout = this.tooltipEventDestroyBind;
this.eventTokens.push(tokenElement);
2020-04-07 11:13:48 +00:00
}
}
// fuction that creates Tooltip for one token and extracts the corresponding
// infos from the result JSON
addToolTipToTokenElement(tokenElement, token) {
2020-04-14 09:31:57 +00:00
this.currentTooltipElement;
this.currentTooltipElement = M.Tooltip.init(tokenElement,
2020-04-08 09:37:34 +00:00
{"html": `<table>
<tr>
<th>Token information</th>
<th>Source information</th>
</tr>
<tr>
<td class="left-align">
Word: ${token.word}<br>
Lemma: ${token.lemma}<br>
POS: ${token.pos}<br>
Simple POS: ${token.simple_pos}<br>
NER: ${token.ner}
</td>
<td class="left-align">
Title: ${resultsJSON.text_lookup[token.text].title}
<br>
Author: ${resultsJSON.text_lookup[token.text].author}
<br>
Publishing year: ${resultsJSON.text_lookup[token.text].publishing_year}
</td>
</tr>
</table>`}
2020-04-14 09:31:57 +00:00
);
2020-04-07 11:13:48 +00:00
}
// function to remove extra informations and animations from tokens
expertModeOff() {
2020-04-14 09:31:57 +00:00
// console.log("Expert mode is off.");
for (let tokenElement of this.currentExpertTokenElements) {
tokenElement.classList.remove("chip", "hoverable", "expert-view");
}
for (let eventToken of this.eventTokens) {
2020-04-14 09:31:57 +00:00
eventToken.onmouseover = "";
eventToken.onmouseout = "";
2020-04-07 11:13:48 +00:00
}
2020-04-14 09:31:57 +00:00
this.eventTokens = [];
2020-04-07 11:13:48 +00:00
}
2020-01-30 13:23:19 +00:00
createResultRowElement(item, chunk) {
2020-04-08 09:37:34 +00:00
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;
2020-01-29 15:12:57 +00:00
// gather values from item
values = item.values();
2020-04-02 09:37:45 +00:00
if (chunk.cpos_ranges == true) {
2020-04-08 09:37:34 +00:00
// python range like function from MDN
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range)
2020-04-02 09:37:45 +00:00
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;
}
2020-01-29 15:12:57 +00:00
// get infos for full match row
matchRowElement = document.createElement("tr");
2020-04-09 08:17:04 +00:00
matchRowElement.setAttribute("data-index", values.index)
2020-02-03 11:58:40 +00:00
lcCellElement = document.createElement("td");
lcCellElement.classList.add("left-context");
matchRowElement.appendChild(lcCellElement);
2020-04-02 09:37:45 +00:00
for (cpos of lc) {
2020-04-08 09:37:34 +00:00
token = chunk.cpos_lookup[cpos];
lcCellElement.insertAdjacentHTML("beforeend",
`<span class="token" data-cpos="${cpos}">${token.word} </span>`);
2020-01-29 15:12:57 +00:00
}
2020-02-03 11:58:40 +00:00
// get infos for hit of match
2020-04-08 09:37:34 +00:00
textTitles = new Set();
cCellElement = document.createElement("td");
cCellElement.classList.add("match-hit");
2020-02-03 11:58:40 +00:00
textTitlesCellElement = document.createElement("td");
textTitlesCellElement.classList.add("titles");
2020-02-03 12:59:37 +00:00
matchNrElement = document.createElement("td");
matchNrElement.classList.add("match-nr");
2020-04-08 09:37:34 +00:00
matchRowElement.appendChild(cCellElement);
2020-04-02 09:37:45 +00:00
for (cpos of c) {
2020-04-08 09:37:34 +00:00
token = chunk.cpos_lookup[cpos];
cCellElement.insertAdjacentHTML("beforeend",
`<span class="token" data-cpos="${cpos}">${token.word} </span>`);
2020-02-03 11:58:40 +00:00
// get text titles of every hit cpos token
2020-04-08 09:37:34 +00:00
textTitles.add(chunk.text_lookup[token.text].title);
2020-02-03 11:58:40 +00:00
// add button to trigger more context to every match td
2020-04-08 09:37:34 +00:00
inspectBtn = document.createElement("a");
inspectBtn.setAttribute("class", `btn-floating btn-flat waves-effect` +
`waves-light grey right inspect disabled`
);
2020-02-03 11:58:40 +00:00
inspectBtn.innerHTML = '<i class="material-icons">search</i>';
2020-04-08 09:37:34 +00:00
inspectBtn.onclick = () => {this.inspect(values.index)};
2020-02-03 11:58:40 +00:00
}
// add text titles at front as first td of one row
2020-04-08 09:37:34 +00:00
cCellElement.appendChild(inspectBtn);
2020-02-03 11:58:40 +00:00
textTitlesCellElement.innerText = [...textTitles].join(", ");
matchRowElement.insertAdjacentHTML("afterbegin", textTitlesCellElement.outerHTML);
2020-04-08 09:37:34 +00:00
matchNrElement.innerText = values.index + 1;
2020-02-03 12:59:37 +00:00
matchRowElement.insertAdjacentHTML("afterbegin", matchNrElement.outerHTML);
2020-02-03 11:58:40 +00:00
// get infos for right context of match
rcCellElement = document.createElement("td");
rcCellElement.classList.add("right-context");
matchRowElement.appendChild(rcCellElement);
2020-04-02 09:37:45 +00:00
for (cpos of rc) {
2020-04-09 08:17:04 +00:00
token = chunk.cpos_lookup[cpos];
2020-04-08 09:37:34 +00:00
rcCellElement.insertAdjacentHTML("beforeend",
`<span class="token" data-cpos="${cpos}">${token.word} </span>`);
2020-02-03 11:58:40 +00:00
}
2020-01-30 13:23:19 +00:00
return matchRowElement
2020-01-29 15:12:57 +00:00
}
}