1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #3262 from node-red/search-history

Add search history to main search box
This commit is contained in:
Nick O'Leary 2022-01-12 17:51:46 +00:00 committed by GitHub
commit 6a82d683a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 3 deletions

View File

@ -894,6 +894,8 @@
"addTitle": "add an item" "addTitle": "add an item"
}, },
"search": { "search": {
"history": "Search history",
"clear": "clear all",
"empty": "No matches found", "empty": "No matches found",
"addNode": "add a node..." "addNode": "add a node..."
}, },

View File

@ -22,6 +22,7 @@ RED.search = (function() {
var selected = -1; var selected = -1;
var visible = false; var visible = false;
var searchHistory = [];
var index = {}; var index = {};
var currentResults = []; var currentResults = [];
var previousActiveElement; var previousActiveElement;
@ -205,6 +206,20 @@ RED.search = (function() {
} }
} }
function populateSearchHistory() {
if (searchHistory.length > 0) {
searchResults.editableList('addItem',{
historyHeader: true
});
searchHistory.forEach(function(entry) {
searchResults.editableList('addItem',{
history: true,
value: entry
});
})
}
}
function createDialog() { function createDialog() {
dialog = $("<div>",{id:"red-ui-search",class:"red-ui-search"}).appendTo("#red-ui-main-container"); dialog = $("<div>",{id:"red-ui-search",class:"red-ui-search"}).appendTo("#red-ui-main-container");
var searchDiv = $("<div>",{class:"red-ui-search-container"}).appendTo(dialog); var searchDiv = $("<div>",{class:"red-ui-search-container"}).appendTo(dialog);
@ -213,7 +228,12 @@ RED.search = (function() {
change: function() { change: function() {
searchResults.editableList('empty'); searchResults.editableList('empty');
selected = -1; selected = -1;
currentResults = search($(this).val()); var value = $(this).val();
if (value === "") {
populateSearchHistory();
return;
}
currentResults = search(value);
if (currentResults.length > 0) { if (currentResults.length > 0) {
for (i=0;i<Math.min(currentResults.length,25);i++) { for (i=0;i<Math.min(currentResults.length,25);i++) {
searchResults.editableList('addItem',currentResults[i]) searchResults.editableList('addItem',currentResults[i])
@ -285,7 +305,12 @@ RED.search = (function() {
}) })
} }
} }
} else { } if ($(children[selected]).hasClass("red-ui-search-history")) {
var object = $(children[selected]).find(".red-ui-editableList-item-content").data('data');
if (object) {
searchInput.searchBox('value',object.value)
}
} else if (!$(children[selected]).hasClass("red-ui-search-historyHeader")) {
if (currentResults.length > 0) { if (currentResults.length > 0) {
reveal(currentResults[Math.max(0,selected)].node); reveal(currentResults[Math.max(0,selected)].node);
} }
@ -301,7 +326,32 @@ RED.search = (function() {
addItem: function(container,i,object) { addItem: function(container,i,object) {
var node = object.node; var node = object.node;
var div; var div;
if (object.more) { if (object.historyHeader) {
container.parent().addClass("red-ui-search-historyHeader")
$('<div>',{class:"red-ui-search-empty"}).text(RED._("search.history")).appendTo(container);
$('<button type="button" class="red-ui-button red-ui-button-small"></button>').text(RED._("search.clear")).appendTo(container).on("click", function(evt) {
evt.preventDefault();
searchHistory = [];
searchResults.editableList('empty');
});
} else if (object.history) {
container.parent().addClass("red-ui-search-history")
div = $('<a>',{href:'#',class:"red-ui-search-result"}).appendTo(container);
div.text(object.value);
div.on("click", function(evt) {
evt.preventDefault();
searchInput.searchBox('value',object.value)
searchInput.focus();
})
$('<button type="button" class="red-ui-button red-ui-button-small"><i class="fa fa-remove"></i></button>').appendTo(container).on("click", function(evt) {
evt.preventDefault();
var index = searchHistory.indexOf(object.value);
searchHistory.splice(index,1);
searchResults.editableList('removeItem', object);
});
} else if (object.more) {
container.parent().addClass("red-ui-search-more") container.parent().addClass("red-ui-search-more")
div = $('<a>',{href:'#',class:"red-ui-search-result red-ui-search-empty"}).appendTo(container); div = $('<a>',{href:'#',class:"red-ui-search-result red-ui-search-empty"}).appendTo(container);
div.text(RED._("palette.editor.more",{count:object.more.results.length-object.more.start})); div.text(RED._("palette.editor.more",{count:object.more.results.length-object.more.start}));
@ -356,6 +406,12 @@ RED.search = (function() {
} }
function reveal(node) { function reveal(node) {
var searchVal = searchInput.val();
var existingIndex = searchHistory.indexOf(searchVal);
if (existingIndex > -1) {
searchHistory.splice(existingIndex,1);
}
searchHistory.unshift(searchInput.val());
hide(); hide();
RED.view.reveal(node.id); RED.view.reveal(node.id);
} }
@ -374,9 +430,14 @@ RED.search = (function() {
if (dialog === null) { if (dialog === null) {
createDialog(); createDialog();
} else {
searchResults.editableList('empty');
} }
dialog.slideDown(300); dialog.slideDown(300);
searchInput.searchBox('value',v) searchInput.searchBox('value',v)
if (!v || v === "") {
populateSearchHistory();
}
RED.events.emit("search:open"); RED.events.emit("search:open");
visible = true; visible = true;
} }

View File

@ -204,6 +204,28 @@
font-style: italic; font-style: italic;
color: $form-placeholder-color; color: $form-placeholder-color;
} }
.red-ui-search-history {
button {
display: none;
position: absolute;
top: 8px;
right: 7px;
}
&:hover button {
display: inline;
}
}
.red-ui-search-historyHeader {
button {
position: absolute;
top: 10px;
right: 7px;
}
}
.red-ui-search-history-result {
}
.red-ui-search-result-action { .red-ui-search-result-action {
color: $primary-text-color; color: $primary-text-color;