Add searching for inputs

This commit is contained in:
j433866 2019-04-30 14:15:05 +01:00
parent b4208ede3b
commit 13ee79a06b
2 changed files with 132 additions and 0 deletions

View File

@ -247,6 +247,9 @@ class InputWaiter {
case "bake":
this.app.bake(false);
break;
case "displayTabSearchResults":
this.displayTabSearchResults(r.data);
break;
default:
log.error(`Unknown action ${r.action}.`);
}
@ -1080,6 +1083,78 @@ class InputWaiter {
const tabNum = parseInt(window.prompt("Enter tab number:", this.getActiveTab().toString()), 10);
this.changeTab(tabNum, this.app.options.syncTabs);
}
/**
* Handler for find tab button clicked
*/
findTab() {
this.filterTabSearch();
$("#input-tab-modal").modal();
}
/**
* Sends a message to the inputWorker to search the inputs
*/
filterTabSearch() {
const showPending = document.getElementById("input-show-pending").checked;
const showLoading = document.getElementById("input-show-loading").checked;
const showLoaded = document.getElementById("input-show-loaded").checked;
const fileNameFilter = document.getElementById("input-filename-filter").value;
const contentFilter = document.getElementById("input-content-filter").value;
const numResults = parseInt(document.getElementById("input-num-results").value, 10);
this.inputWorker.postMessage({
action: "filterTabs",
data: {
showPending: showPending,
showLoading: showLoading,
showLoaded: showLoaded,
fileNameFilter: fileNameFilter,
contentFilter: contentFilter,
numResults: numResults
}
});
}
/**
* Displays the results of a tab search in the find tab box
*
* @param {object[]} results
*
*/
displayTabSearchResults(results) {
const resultsList = document.getElementById("input-search-results");
for (let i = resultsList.children.length - 1; i >= 0; i--) {
resultsList.children.item(i).remove();
}
for (let i = 0; i < results.length; i++) {
const newListItem = document.createElement("li");
newListItem.classList.add("input-filter-result");
newListItem.setAttribute("inputNum", results[i].inputNum);
newListItem.innerText = `${results[i].inputNum}: ${results[i].textDisplay}`;
resultsList.appendChild(newListItem);
}
}
/**
* Handler for clicking on a filter result
*
* @param {event} e
*/
filterItemClick(e) {
if (!e.target) return;
const inputNum = parseInt(e.target.getAttribute("inputNum"), 10);
if (inputNum <= 0) return;
$("#input-tab-modal").modal("hide");
this.changeTab(inputNum, this.app.options.syncTabs);
}
}
export default InputWaiter;

View File

@ -6,6 +6,7 @@
* @license Apache-2.0
*/
import Utils from "../core/Utils";
self.maxWorkers = 4;
self.maxTabs = 1;
@ -79,6 +80,9 @@ self.addEventListener("message", function(e) {
case "autobake":
self.autoBake(r.data);
break;
case "filterTabs":
self.filterTabs(r.data);
break;
default:
log.error(`Unknown action '${r.action}'.`);
}
@ -641,3 +645,56 @@ self.changeTabLeft = function(inputNum, tabNums) {
self.refreshTabs(newInput, "left");
}
};
self.filterTabs = function(searchData) {
const showPending = searchData.showPending;
const showLoading = searchData.showLoading;
const showLoaded = searchData.showLoaded;
const fileNameFilter = searchData.fileNameFilter;
const contentFilter = searchData.contentFilter;
const numResults = searchData.numResults;
const inputs = [];
const inputNums = Object.keys(self.inputs);
for (let i = 0; i < inputNums.length; i++) {
const iNum = inputNums[i];
let textDisplay = "";
let addInput = false;
if (self.inputs[iNum].status === "pending" && showPending ||
self.inputs[iNum].status === "loading" && showLoading ||
self.inputs[iNum].status === "loaded" && showLoaded) {
if (typeof self.inputs[iNum].data === "string") {
if (self.inputs[iNum].data.slice(0, 4096).toLowerCase().includes(contentFilter)) {
textDisplay = self.inputs[iNum].data.slice(0, 4096);
addInput = true;
}
} else {
if (self.inputs[iNum].data.name.toLowerCase().includes(fileNameFilter) &&
Utils.arrayBufferToStr(self.inputs[iNum].data.fileBuffer.slice(0, 4096)).toLowerCase().includes(contentFilter)) {
textDisplay = self.inputs[iNum].data.name;
addInput = true;
}
}
}
if (addInput) {
if (textDisplay === "" || textDisplay === undefined) {
textDisplay = "New Tab";
}
const inputItem = {
inputNum: iNum,
textDisplay: textDisplay
};
inputs.push(inputItem);
}
if (inputs.length >= numResults) {
break;
}
}
self.postMessage({
action: "displayTabSearchResults",
data: inputs
});
}