Removed redundant range functions and create one class method for its purpose.

This commit is contained in:
Stephan Porada 2020-07-14 10:59:02 +02:00
parent 82c26a02bb
commit e9eaa49826

View File

@ -402,6 +402,26 @@ class ResultsList extends List {
this.addToSubResultsIdsToShow = new Set(); // If check button is pressed its corresponding data_index is saved in this set. The set is shown to the user. this.addToSubResultsIdsToShow = new Set(); // If check button is pressed its corresponding data_index is saved in this set. The set is shown to the user.
} }
helperCreateCpos(cpos_ranges, cpos_values) {
console.log(cpos_values);
let lc;
let c;
let rc;
if (cpos_ranges) {
// 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(cpos_values.lc[0], cpos_values.lc[1], 1)
c = range(cpos_values.c[0], cpos_values.c[1], 1)
rc = range(cpos_values.rc[0], cpos_values.rc[1], 1)
} else {
lc = cpos_values.lc;
c = cpos_values.c;
rc = cpos_values.rc;
}
return {lc: lc, c: c, rc: rc};
}
// handels interactionElements during a pagination navigation // handels interactionElements during a pagination navigation
// loops over interactionElements and executes callback functions accordingly // loops over interactionElements and executes callback functions accordingly
pageChangeEventInteractionHandler(interactionElements) { pageChangeEventInteractionHandler(interactionElements) {
@ -564,9 +584,6 @@ class ResultsList extends List {
// results to be able to inspect matches. // results to be able to inspect matches.
createFakeResponse() { createFakeResponse() {
contextModal.open(); contextModal.open();
let lc;
let c;
let rc;
let cpos_lookup; let cpos_lookup;
let fake_response = {}; let fake_response = {};
let contextResultsElement; let contextResultsElement;
@ -577,24 +594,8 @@ class ResultsList extends List {
fake_response.payload["matches"] = [results.data.matches[dataIndex]]; fake_response.payload["matches"] = [results.data.matches[dataIndex]];
contextResultsElement = document.getElementById("context-results"); contextResultsElement = document.getElementById("context-results");
contextResultsElement.innerHTML = ""; contextResultsElement.innerHTML = "";
if (results.data.cpos_ranges) { let {lc, c, rc} = this.helperCreateCpos(results.data.cpos_ranges,
// python range like function from MDN fake_response.payload.matches[0]);
// 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(fake_response.payload.matches[0].lc[0],
fake_response.payload.matches[0].lc[1],
1);
c = range(fake_response.payload.matches[0].c[0],
fake_response.payload.matches[0].c[1],
1)
rc = range(fake_response.payload.matches[0].rc[0],
fake_response.payload.matches[0].rc[1],
1);
} else {
lc = fake_response.payload.matches[0].lc;
c = fake_response.payload.matches[0].c;
rc = fake_response.payload.matches[0].rc;
}
cpos_lookup = {}; cpos_lookup = {};
for (let cpos of lc) { for (let cpos of lc) {
cpos_lookup[cpos] = results.data.cpos_lookup[cpos]; cpos_lookup[cpos] = results.data.cpos_lookup[cpos];
@ -662,18 +663,15 @@ class ResultsList extends List {
// has been used. // has been used.
showMatchContext(response) { showMatchContext(response) {
this.contextData; this.contextData;
let c;
let contextModalLoading; let contextModalLoading;
let contextModalReady; let contextModalReady;
let contextResultsElement; let contextResultsElement;
let highlightSentencesSwitchElement; let highlightSentencesSwitchElement;
let htmlTokenStr; let htmlTokenStr;
let lc;
let modalExpertModeSwitchElement; let modalExpertModeSwitchElement;
let modalTokenElements; let modalTokenElements;
let nrOfContextSentences; let nrOfContextSentences;
let partElement; let partElement;
let rc;
let token; let token;
let tokenHTMLArray; let tokenHTMLArray;
let tokenHTMlElement; let tokenHTMlElement;
@ -694,19 +692,8 @@ class ResultsList extends List {
nrOfContextSentences = document.getElementById("context-sentences"); nrOfContextSentences = document.getElementById("context-sentences");
uniqueS = new Set(); uniqueS = new Set();
uniqueContextS = new Set(); uniqueContextS = new Set();
// check if cpos ranges are used or not let {lc, c, rc} = this.helperCreateCpos(this.contextData.cpos_ranges,
if (this.contextData.cpos_ranges == true) { this.contextData.matches[0])
// 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 // create sentence strings as tokens
tokenHTMLArray = []; tokenHTMLArray = [];
for (let cpos of lc) { for (let cpos of lc) {
@ -994,16 +981,13 @@ class ResultsList extends List {
createResultRowElement(item, chunk, imported=false) { createResultRowElement(item, chunk, imported=false) {
let aCellElement; let aCellElement;
let addToSubResultsBtn; let addToSubResultsBtn;
let c;
let cCellElement; let cCellElement;
let cpos; let cpos;
let fakeResponse; // used if imported results are being created; let fakeResponse; // used if imported results are being created;
let inspectBtn let inspectBtn
let lc;
let lcCellElement; let lcCellElement;
let matchNrElement; let matchNrElement;
let matchRowElement; let matchRowElement;
let rc;
let rcCellElement; let rcCellElement;
let textTitles; let textTitles;
let textTitlesCellElement; let textTitlesCellElement;
@ -1011,18 +995,20 @@ class ResultsList extends List {
let values; let values;
// gather values from item // gather values from item
values = item.values(); values = item.values();
if (chunk.cpos_ranges == true) { let {lc, c, rc} = this.helperCreateCpos(chunk.cpos_ranges,
// python range like function from MDN values)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range) // if (chunk.cpos_ranges == true) {
const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step)); // // python range like function from MDN
lc = range(values.lc[0], values.lc[1], 1) // // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range)
c = range(values.c[0], values.c[1], 1) // const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
rc = range(values.rc[0], values.rc[1], 1) // lc = range(values.lc[0], values.lc[1], 1)
} else { // c = range(values.c[0], values.c[1], 1)
lc = values.lc; // rc = range(values.rc[0], values.rc[1], 1)
c = values.c; // } else {
rc = values.rc; // lc = values.lc;
} // c = values.c;
// rc = values.rc;
// }
// get infos for full match row // get infos for full match row
matchRowElement = document.createElement("tr"); matchRowElement = document.createElement("tr");
matchRowElement.setAttribute("data-index", values.index) matchRowElement.setAttribute("data-index", values.index)