// ============================ Database ============================ function queryDatabase() { var cVal if(sVal() == "") return // empty input lastQueryPhrase = sVal() // remember what this box is showing, so a later cipher toggle can refresh it if (liveDatabaseMode == true) calcLiveDatabase(userDBlive) // calculate gematria for live database for enabled ciphers $("#calcMain").addClass("splitInterface") // split screen if (document.getElementById("queryArea") == null) { // create div if it doesn't exist var o = '
' $(o).appendTo('body'); } // Position modal below the enabled cipher table (the Ordinal/Reduction values box) var ciphTable = document.getElementById("enabledCiphTable"); if (ciphTable && ciphTable.getBoundingClientRect().height > 0) { var ciphBottom = ciphTable.getBoundingClientRect().bottom + window.scrollY; document.getElementById("queryArea").style.top = (ciphBottom + 4) + "px"; } else { var inputEl = document.getElementById("inputArea"); if (inputEl) { var inputBottom = inputEl.getBoundingClientRect().bottom + window.scrollY; document.getElementById("queryArea").style.top = (inputBottom + 4) + "px"; } } mArr = [] // array with matching phrases from database gemArr = [] // gematria of current phrase for enabled ciphers gemArrCiph = [] // indices of enabled ciphers numericalMode = NumberArray() // boolean var num_index = 0 // numerical array for (i = 0; i < cipherList.length; i++) { if (cipherList[i].enabled) { if (!numericalMode) { val = cipherList[i].calcGematria(sVal()) // if (val == 0) val = "n/a" gemArr.push(val) // value for current phrase in each enabled cipher } else { // numerical mode if (num_index < pArr.length) { gemArr.push(pArr[num_index]); num_index++ } // assign first number to first enabled cipher else { gemArr.push('-') } // if more ciphers than numbers push zero } gemArrCiph.push(i) // corresponding cipher index } } // console.log(gemArr) // console.log(gemArrCiph) if (optFiltCrossCipherMatch) { searchDBcrossCipher() } else if (optFiltSameCipherMatch) { searchDBsameCipher() } queryResultInitial = [...queryResult] // save initial matches // console.log(queryResult) // var longestPhr = 0; var tmp // for (i = 0; i < queryResult.length; i++) { // tmp = queryResult[i][1].length // if (tmp > longestPhr) longestPhr = tmp // } // var tWidth = longestPhr*11 + 58*gemArrCiph.length // 2x1px outer borders + phrase cell and amount of ciphers var tWidth = 202 + 58*gemArrCiph.length // 2x1px outer borders + phrase cell and amount of ciphers $("#queryArea").css("min-width", tWidth) // set initial/minimal width for the table $("#queryArea").css("width", tWidth) // set initial/minimal width for the table /*var o = 'min-width:'+tWidth+';width:'+tWidth+';' $("#queryArea").attr("style", o) // set minimal/initial width for the table*/ updateDatabaseQueryTable(0, dbPageItems) // Progressive loading: surface the spinner ONLY when this query is waiting on "…" cells // (a non-lite cipher enabled before the full db finished streaming); hide it otherwise. var _ind = document.getElementById("dbLoadIndicator") if (_ind) { if (!fullDBLoaded && queryHadUnloadedCipher) { _ind.title = "loading all cyphers…"; _ind.style.display = "inline" } else { _ind.style.display = "none" } } } var _refreshingQuery = false // reentrancy guard: updateTables -> refreshOpenQuery -> queryDatabase must not recurse function refreshOpenQuery() { // Called after any cipher-set change (updateTables). If a matches box is open and maximized, // re-run the SAME query so a newly enabled cipher adds its column (and re-ranks) with no // resubmit — and a newly disabled cipher drops its column. No-op when no box is open. if (_refreshingQuery) return var qa = document.getElementById("queryArea") if (qa === null || $("#queryArea").hasClass("minimizeQuery")) return // only when open + not minimized if (lastQueryPhrase === "") return var box = document.getElementById("phraseBox") var savedInput = box.value // preserve whatever the user may be typing now var savedFilter = searchBarValue // preserve the find-in-results filter _refreshingQuery = true try { box.value = lastQueryPhrase // queryDatabase reads sVal(); point it at the open box's phrase queryDatabase() // rebuilds the table for the current enabled-cipher set } finally { box.value = savedInput // restore silently (direct assignment fires no input event) _refreshingQuery = false } if (savedFilter) searchBarDBQuery(savedFilter) // re-apply the filter over the fresh matches } function clearDatabaseQueryTable() { $("#calcMain").removeClass("splitInterface") $("#queryArea").remove() // remove element from page searchBarValue = '' // clear search bar queryResult = [] // clear queryResultInitial = [] // clear } function isIngglishEntry(phr) { return typeof phr === "string" && phr.indexOf("[Ingglish:") !== -1 } function compareQueryMatches(a, b, searchPhrase) { // 1. Exact phrase match (full string or stripped of [...] comment) always first var aRaw = a[1].toLowerCase() var bRaw = b[1].toLowerCase() var aStripped = aRaw.replace(/\[.+\]/g, '').trim() var bStripped = bRaw.replace(/\[.+\]/g, '').trim() var aExact = (aRaw === searchPhrase || aStripped === searchPhrase) ? 1 : 0 var bExact = (bRaw === searchPhrase || bStripped === searchPhrase) ? 1 : 0 if (aExact !== bExact) return bExact - aExact // 2. When optPrioritizeIngglish is enabled, sort [Ingglish: ...] matches above non-Ingglish matches if (typeof optPrioritizeIngglish !== "undefined" && optPrioritizeIngglish) { var aIng = isIngglishEntry(a[1]) ? 1 : 0 var bIng = isIngglishEntry(b[1]) ? 1 : 0 if (aIng !== bIng) return bIng - aIng } // 3. Then sort by match score descending return b[0] - a[0] } function searchDBcrossCipher() { // populate "queryResult" array with matching phrases var p, m, n queryResult = [] // reset matching phrases var tmpArr = [] // one phrase with score and gematria var tmpVal = 0 // current phrase value var gemArrCiphUsed = [...gemArrCiph] if (cipherOrder.length > 0 && !liveDatabaseMode) { // pre-computed mode gemArrCiphUsed = getPrecomputedCipherPositions() } else if (liveDatabaseMode == true) { // live mode: 0,1,2... len = gemArrCiph.length gemArrCiphUsed = [] for (n = 0; n < len; n++) { gemArrCiphUsed.push(n) } } // take phrase, take cipher, all values match in cipher add score, build string, next phrase, then sort by score for (p = 0; p < userDB.length; p++) { // for each phrase in database tmpArr = [0, userDB[p][0]] // reset, set score[0], phrase[1] for (m = 0; m < gemArrCiphUsed.length; m++) { // for each enabled cipher index if (gemArrCiphUsed[m] === -1) { tmpArr.push(null); continue } // cipher not in current db (not yet loaded) — show "—", no score tmpVal = Number(userDB[p][gemArrCiphUsed[m]+1]) // value for that phrase (+1 because [0] contains phrase), string to number tmpArr.push(tmpVal) // first add values irrelevant of match validity for (n = 0; n < gemArr.length; n++) { // for each gematria value (cross cipher) if (tmpVal == gemArr[m]) { // tmpArr[0] += gemArr[n] * 10 // add score for a matching value, same cipher tmpArr[0] += 10 + 0.00001*tmpVal // add score for a matching value, same cipher n = gemArr.length // exit innermost loop (score is added once for same cross cipher values) } else if (tmpVal == gemArr[n]) { // tmpArr[0] += gemArr[n] // cross cipher tmpArr[0] += 1 + 0.00001*tmpVal // cross cipher n = gemArr.length // exit innermost loop } } } if (tmpArr[0] > 0) queryResult.push(tmpArr) // if total score is more than zero, add phrase to array } // Sort by score descending, pinning exact match and (when enabled) [Ingglish: ...] matches to top var searchPhrase = sVal().toLowerCase().trim() queryResult.sort(function(a, b) { return compareQueryMatches(a, b, searchPhrase) }); } function searchDBsameCipher() { // populate "queryResult" array with matching phrases var p, m, n queryResult = [] // reset matching phrases var tmpArr = [] // one phrase with score and gematria var tmpVal = 0 // current phrase value var gemArrCiphUsed = [...gemArrCiph] if (cipherOrder.length > 0 && !liveDatabaseMode) { // pre-computed mode gemArrCiphUsed = getPrecomputedCipherPositions() } else if (liveDatabaseMode == true) { // live mode: 0,1,2... len = gemArrCiph.length gemArrCiphUsed = [] for (n = 0; n < len; n++) { gemArrCiphUsed.push(n) } } // take phrase, take cipher, all values match in cipher add score, build string, next phrase, then sort by score for (p = 0; p < userDB.length; p++) { // for each phrase in database tmpArr = [0, userDB[p][0]] // reset, set score[0], phrase[1] for (m = 0; m < gemArrCiphUsed.length; m++) { // for each enabled cipher index if (gemArrCiphUsed[m] === -1) { tmpArr.push(null); continue } // cipher not in current db (not yet loaded) — show "—", no score tmpVal = Number(userDB[p][gemArrCiphUsed[m]+1]) // value for that phrase (+1 because [0] contains phrase), string to number tmpArr.push(tmpVal) // first add values irrelevant of match validity if (tmpVal == gemArr[m]) { // tmpArr[0] += gemArr[m] // same cipher, add score tmpArr[0] += 1 + 0.00001*gemArr[m] // same cipher, add score } } if (tmpArr[0] > 0) queryResult.push(tmpArr) // if total score is more than zero, add phrase to array } // Sort by score descending, pinning exact match and (when enabled) [Ingglish: ...] matches to top var searchPhrase = sVal().toLowerCase().trim() queryResult.sort(function(a, b) { return compareQueryMatches(a, b, searchPhrase) }); } function searchBarDBQuery(str) { // search bar searchBarValue = str // save input if (str == "") { queryResult = [...queryResultInitial] // restore inital matches updateDatabaseQueryTable(0, dbPageItems) document.getElementById('querySearchInput').focus() return } var i; var tmpPhr = ''; var tmpPhr2 = ''; queryResult = [] // clear current matches for (i = 0; i < queryResultInitial.length; i++) { tmpPhr = queryResultInitial[i] if (encodingMenuOpened) { // encoding mode if (encodingMode == 0 && optGemSubstitutionMode) { // syllables, substitution mode tmpPhr2 = tmpPhr.replace(/\[.+\]/g, '').trim() // exclude comments when searching (anagram link) if (tmpPhr2.match(new RegExp(str)) !== null) { queryResult.push(tmpPhr) } } else if (tmpPhr.match(new RegExp(str)) !== null) { queryResult.push(tmpPhr) } } else { // database match if (tmpPhr[1].match(new RegExp(str)) !== null) queryResult.push(tmpPhr) } } updateDatabaseQueryTable(0, dbPageItems) } function updateDatabaseQueryTable(stPos = 0, dItems, scrollBarEvent = false) { // starting position, total displayed items var ms, x, y, mCross, mSame, curCiph, curCiphCol // stPos - starting position // dbPageItems - number of phrases in one section var nextBlock = stPos+dbPageItems // position for next section if (nextBlock > queryResult.length) nextBlock = queryResult.length // out of bounds var valPos = 2 // used to retrive gematria from "gemArr" (no recalculation, [0] - score, [1] - phrase) var firstPhrase = true // display cipher names before the first phrase var tmpComment, commentMatch, dispPhrase // temporary variables to display comments [...] $("#queryArea").removeClass("minimizeQuery") // unhide query area var curPos = stPos // current position begins with starting index var endPos = stPos + dItems // ending position if (curPos < 0) curPos = 0 // out of bounds if (!scrollBarEvent) { if (endPos > queryResult.length) endPos = queryResult.length sliderMax = Math.floor(queryResult.length/dbPageItems) if (queryResult.length % dbPageItems == 0) sliderMax -= 1 // if total is divisible, no pagination for last element curSliderPos = Math.round(curPos/dbPageItems) ms = '| '
ms += queryResult.length.toLocaleString('en')+' matches'
ms += ' ' ms += ' ' ms += ' '
ms += ''
ms += ''
ms += ''
ms += ' '
ms += '-' ms += ' '+nextBlock ms += ' | '
for (z = 0; z < gemArrCiph.length; z++) { // use compact layout
curCiph = cipherList[ gemArrCiph[z] ]
curCiphCol = (optColoredCiphers) ? 'hsl('+curCiph.H+' '+curCiph.S+'% '+curCiph.L+'% / 1)' : ''
// ms += ''+curCiph.cipherName.replace(/ /g, " ")+' | ' // color of cipher displayed in the table
ms += '' ms += escHtml(curCiph.cipherName)+' | ' // color of cipher displayed in the table } ms += "
| ' + dispPhrase + ' | ' // phrase at index 1 (title + body both escaped) valPos = 2 // reset position for new phrase for (y = 0; y < gemArrCiph.length; y++) { // gemArrCiph contains indices of ciphers used for query curCiph = cipherList[ gemArrCiph[y] ] curCiphCol = (optColoredCiphers) ? 'hsl('+curCiph.H+' '+curCiph.S+'% '+curCiph.L+'% / 1)' : '' if (liveDatabaseMode == true) { gemVal = (encodingMenuOpened) ? curCiph.calcGematria(queryResult[x]) : curCiph.calcGematria(queryResult[x][1]) // recalculate displayed value } else { gemVal = queryResult[x][valPos] // value only } // if (gemVal == 0) gemVal = "-" valPos++ // increment value position if (gemVal === null) { // cipher not in the current db yet (precompute still streaming) — placeholder ms += '… | ' continue } // mSame - same cipher match, mCross - cross cipher match if ( gemVal == gemArr[y] ) {mSame = true} else {mSame = false} // if value is at the same index (meaning same cipher) if (gemArr.indexOf(gemVal) > -1) {mCross = true} else {mCross = false} ms += '' // use instead of .tC to disable highlighting/blinking/hide effects on click if (mSame) { ms += ' '+gemVal+' | ' // number properties are available } ms += '