Files
ingglish-hyperdope-ci/patches/0001-feat-calc-ingglish-priority-toggle.patch
T

142 lines
6.4 KiB
Diff

diff --git a/calc/calc.js b/calc/calc.js
index f58e9e8..37c1a2c 100644
--- a/calc/calc.js
+++ b/calc/calc.js
@@ -60,6 +60,7 @@ var optHltSameCipher = false;
var alphaHlt = 0.15; // opacity for values that do not match - change value here and in conf_SOM()
var optAllowPhraseComments = true; // allow phrase comments, text inside [...] is not evaluated
+var optPrioritizeIngglish = (typeof CALC_CONFIG !== 'undefined' && CALC_CONFIG.prioritizeIngglishMatches) ? true : false; // sort [Ingglish: ...] matches to top of query results
var liveDatabaseMode = true; // live database mode
var dbPageItems = (typeof CALC_CONFIG !== 'undefined' && CALC_CONFIG.resultsPerPage) ? CALC_CONFIG.resultsPerPage : 12;
@@ -103,6 +104,7 @@ var calcOptionsArr = [
"optNewPhrasesGoFirst",
"optShowExtraCiphers",
"optAllowPhraseComments",
+ "optPrioritizeIngglish",
"liveDatabaseMode",
"optLetterWordCount",
"optWordBreakdown",
@@ -546,6 +548,8 @@ function createOptionsMenu() {
if (optShowExtraCiphers) SECstate = "checked"; // Show Extra Ciphers
if (optAllowPhraseComments) APCstate = "checked"; // Allow Phrase Comments
+ var PIMstate = "";
+ if (optPrioritizeIngglish) PIMstate = "checked"; // Prioritize Ingglish [...]
if (liveDatabaseMode) LDMstate = "checked"; // Live Database Mode
if (optNewPhrasesGoFirst) NPGFstate = "checked"; // New Phrases Go First
@@ -586,6 +590,10 @@ function createOptionsMenu() {
'<div class="optionElement"><label class="chkLabel ciphCheckboxLabel2">Ignore Comments [...]<input type="checkbox" id="chkbox_APC" onclick="conf_APC()" ' +
APCstate +
'><span class="custChkBox"></span></label></div>';
+ o +=
+ '<div class="optionElement"><label class="chkLabel ciphCheckboxLabel2">Prioritize Ingglish [...]<input type="checkbox" id="chkbox_PIM" onclick="conf_PIM()" ' +
+ PIMstate +
+ '><span class="custChkBox"></span></label></div>';
o +=
'<div class="optionElement"><label class="chkLabel ciphCheckboxLabel2">Live Database Mode<input type="checkbox" id="chkbox_LDM" onclick="conf_LDM()" ' +
LDMstate +
@@ -709,6 +717,16 @@ function conf_APC() {
updateTables();
}
+function conf_PIM() {
+ // Prioritize Ingglish [...] Matches
+ optPrioritizeIngglish = !optPrioritizeIngglish;
+ var chk = document.getElementById("chkbox_PIM");
+ if (chk !== null) chk.checked = optPrioritizeIngglish;
+ var btn = document.getElementById("queryIngPriorityBtn");
+ if (btn !== null) btn.classList.toggle("ingPriorityOn", optPrioritizeIngglish);
+ refreshOpenQuery();
+}
+
function conf_LWC() {
// Letter/Word Count
optLetterWordCount = !optLetterWordCount;
diff --git a/calc/database.js b/calc/database.js
index 31704c8..df9f3a5 100644
--- a/calc/database.js
+++ b/calc/database.js
@@ -109,6 +109,31 @@ function clearDatabaseQueryTable() {
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
@@ -145,14 +170,10 @@ function searchDBcrossCipher() { // populate "queryResult" array with matching p
}
if (tmpArr[0] > 0) queryResult.push(tmpArr) // if total score is more than zero, add phrase to array
}
- // Sort by score descending, but pin exact phrase match to top
- var searchPhrase = sVal().toLowerCase()
+ // Sort by score descending, pinning exact match and (when enabled) [Ingglish: ...] matches to top
+ var searchPhrase = sVal().toLowerCase().trim()
queryResult.sort(function(a, b) {
- // Exact phrase match always first
- var aExact = a[1].toLowerCase() === searchPhrase ? 1 : 0
- var bExact = b[1].toLowerCase() === searchPhrase ? 1 : 0
- if (aExact !== bExact) return bExact - aExact
- return b[0] - a[0]; // then by score descending
+ return compareQueryMatches(a, b, searchPhrase)
});
}
@@ -185,13 +206,10 @@ function searchDBsameCipher() { // populate "queryResult" array with matching ph
}
if (tmpArr[0] > 0) queryResult.push(tmpArr) // if total score is more than zero, add phrase to array
}
- // Sort by score descending, but pin exact phrase match to top
- var searchPhrase = sVal().toLowerCase()
+ // Sort by score descending, pinning exact match and (when enabled) [Ingglish: ...] matches to top
+ var searchPhrase = sVal().toLowerCase().trim()
queryResult.sort(function(a, b) {
- var aExact = a[1].toLowerCase() === searchPhrase ? 1 : 0
- var bExact = b[1].toLowerCase() === searchPhrase ? 1 : 0
- if (aExact !== bExact) return bExact - aExact
- return b[0] - a[0];
+ return compareQueryMatches(a, b, searchPhrase)
});
}
@@ -267,9 +285,11 @@ function updateDatabaseQueryTable(stPos = 0, dItems, scrollBarEvent = false) { /
if (firstPhrase) { // open row on the first phrase
firstPhrase = false
+ var ingBtnClass = (typeof optPrioritizeIngglish !== "undefined" && optPrioritizeIngglish) ? "queryPageBtn ingPriorityBtn ingPriorityOn" : "queryPageBtn ingPriorityBtn"
ms += '<tr class="cH"><td class="mPQ">'
ms += queryResult.length.toLocaleString('en')+' matches'
- ms += '<br><br>'
+ ms += '<br><input class="'+ingBtnClass+'" id="queryIngPriorityBtn" type="button" value="ING &#8593;" title="Toggle Ingglish [...] priority sort to top" onclick="event.stopPropagation();conf_PIM()">'
+ ms += '<br>'
ms += '<div style="display: inline-flex;">'
ms += '<input class="queryPageBtn" id="queryPrevPageBtn" type="button" value="<" onclick="queryShowPrevPage()">'
ms += '<input id="queryPosInput" type="tel" autocomplete="off" value="'+curPos+'">'