close search toolbar on escape

This commit is contained in:
Steve-Mcl 2022-03-08 16:09:05 +00:00
parent ad96c6f838
commit 97d2b5df15
1 changed files with 30 additions and 13 deletions

View File

@ -429,9 +429,8 @@ RED.search = (function() {
} }
searchHistory.unshift(searchVal); searchHistory.unshift(searchVal);
$("#red-ui-view-searchtools-search").data("term", searchVal); $("#red-ui-view-searchtools-search").data("term", searchVal);
hide();
activeResults = Object.assign([], currentResults); activeResults = Object.assign([], currentResults);
updateSearchToolbar(); hide(null, activeResults.length > 0);
RED.view.reveal(node.id); RED.view.reveal(node.id);
} }
@ -452,6 +451,7 @@ RED.search = (function() {
const n = activeResults[currentIndex]; const n = activeResults[currentIndex];
if (n && n.node && n.node.id) { if (n && n.node && n.node.id) {
RED.view.reveal(n.node.id); RED.view.reveal(n.node.id);
$("#red-ui-view-searchtools-prev").trigger("focus");
} }
updateSearchToolbar(); updateSearchToolbar();
} }
@ -472,6 +472,7 @@ RED.search = (function() {
const n = activeResults[currentIndex]; const n = activeResults[currentIndex];
if (n && n.node && n.node.id) { if (n && n.node && n.node.id) {
RED.view.reveal(n.node.id); RED.view.reveal(n.node.id);
$("#red-ui-view-searchtools-next").trigger("focus");
} }
updateSearchToolbar(); updateSearchToolbar();
} }
@ -505,7 +506,7 @@ RED.search = (function() {
searchInput.trigger("focus"); searchInput.trigger("focus");
} }
function hide() { function hide(el, keepSearchToolbar) {
if (visible) { if (visible) {
visible = false; visible = false;
$("#red-ui-header-shade").hide(); $("#red-ui-header-shade").hide();
@ -519,27 +520,34 @@ RED.search = (function() {
}); });
} }
RED.events.emit("search:close"); RED.events.emit("search:close");
if (previousActiveElement) { if (previousActiveElement && (!keepSearchToolbar || !activeResults.length)) {
$(previousActiveElement).trigger("focus"); $(previousActiveElement).trigger("focus");
previousActiveElement = null;
} }
previousActiveElement = null;
}
if(!keepSearchToolbar) {
clearActiveSearch();
} }
updateSearchToolbar(); updateSearchToolbar();
if(keepSearchToolbar && activeResults.length) {
$("#red-ui-view-searchtools-next").trigger("focus");
}
} }
function updateSearchToolbar() { function updateSearchToolbar() {
if(!disabled && currentIndex >= 0 && activeResults && activeResults.length ) { if (!disabled && currentIndex >= 0 && activeResults && activeResults.length) {
let term = $("#red-ui-view-searchtools-search").data("term") || ""; let term = $("#red-ui-view-searchtools-search").data("term") || "";
if(term.length > 16) { if (term.length > 16) {
term = term.substring(0,12) + "..." term = term.substring(0, 12) + "..."
} }
const i18nSearchCounterData = { const i18nSearchCounterData = {
term : term, term: term,
result: (currentIndex+1), result: (currentIndex + 1),
count: activeResults.length count: activeResults.length
} }
$("#red-ui-view-searchtools-counter").text(RED._('actions.search-counter', i18nSearchCounterData)); $("#red-ui-view-searchtools-counter").text(RED._('actions.search-counter', i18nSearchCounterData));
$("#view-search-tools > :not(:first-child)").show(); //show other tools $("#view-search-tools > :not(:first-child)").show(); //show other tools
} else { } else {
clearActiveSearch();
$("#view-search-tools > :not(:first-child)").hide(); //hide all but search button $("#view-search-tools > :not(:first-child)").hide(); //hide all but search button
} }
} }
@ -564,6 +572,11 @@ RED.search = (function() {
addItemToIndex(item); addItemToIndex(item);
} }
function clearActiveSearch() {
activeResults = [];
currentIndex = 0;
$("#red-ui-view-searchtools-search").data("term", "");
}
function init() { function init() {
RED.actions.add("core:search",show); RED.actions.add("core:search",show);
@ -579,14 +592,18 @@ RED.search = (function() {
RED.keyboard.add("red-ui-search","escape",hide); RED.keyboard.add("red-ui-search","escape",hide);
RED.keyboard.add("view-search-tools","escape",function() {
clearActiveSearch();
updateSearchToolbar();
});
$("#red-ui-header-shade").on('mousedown',hide); $("#red-ui-header-shade").on('mousedown',hide);
$("#red-ui-editor-shade").on('mousedown',hide); $("#red-ui-editor-shade").on('mousedown',hide);
$("#red-ui-palette-shade").on('mousedown',hide); $("#red-ui-palette-shade").on('mousedown',hide);
$("#red-ui-sidebar-shade").on('mousedown',hide); $("#red-ui-sidebar-shade").on('mousedown',hide);
$("#red-ui-view-searchtools-close").on("click", function close() { $("#red-ui-view-searchtools-close").on("click", function close() {
activeResults = []; clearActiveSearch();
$("#red-ui-view-searchtools-search").data("term", "");
updateSearchToolbar(); updateSearchToolbar();
}); });
$("#red-ui-view-searchtools-close").trigger("click"); $("#red-ui-view-searchtools-close").trigger("click");