mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
[search] Refactor search to use editor events to generate index
This commit is contained in:
parent
a5b33d11fc
commit
55a5917282
@ -23,8 +23,7 @@ RED.search = (function() {
|
||||
var visible = false;
|
||||
|
||||
var index = {};
|
||||
var keys = [];
|
||||
var results = [];
|
||||
var currentResults = [];
|
||||
var previousActiveElement;
|
||||
|
||||
|
||||
@ -66,23 +65,9 @@ RED.search = (function() {
|
||||
}
|
||||
}
|
||||
|
||||
function indexWorkspace() {
|
||||
index = {};
|
||||
RED.nodes.eachWorkspace(indexNode);
|
||||
RED.nodes.eachSubflow(indexNode);
|
||||
RED.nodes.eachConfig(indexNode);
|
||||
RED.nodes.eachNode(indexNode);
|
||||
keys = Object.keys(index);
|
||||
keys.sort();
|
||||
keys.forEach(function(key) {
|
||||
index[key] = Object.keys(index[key]).map(function(id) {
|
||||
return index[key][id];
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function search(val) {
|
||||
searchResults.editableList('empty');
|
||||
var results = [];
|
||||
var keys = Object.keys(index);
|
||||
var typeFilter;
|
||||
var m = /(?:^| )type:([^ ]+)/.exec(val);
|
||||
if (m) {
|
||||
@ -92,8 +77,7 @@ RED.search = (function() {
|
||||
|
||||
val = val.trim();
|
||||
|
||||
selected = -1;
|
||||
results = [];
|
||||
|
||||
if (val.length > 0 || typeFilter) {
|
||||
val = val.toLowerCase();
|
||||
var i;
|
||||
@ -104,10 +88,14 @@ RED.search = (function() {
|
||||
var key = keys[i];
|
||||
var kpos = keys[i].indexOf(val);
|
||||
if (kpos > -1) {
|
||||
for (j=0;j<index[key].length;j++) {
|
||||
var node = index[key][j];
|
||||
var ids = Object.keys(index[key]);
|
||||
for (j=0;j<ids.length;j++) {
|
||||
var node = index[key][ids[j]];
|
||||
if (!typeFilter || node.node.type === typeFilter) {
|
||||
nodes[node.node.id] = nodes[node.node.id] = node;
|
||||
nodes[node.node.id] = nodes[node.node.id] = {
|
||||
node: node.node,
|
||||
label: node.label
|
||||
};
|
||||
nodes[node.node.id].index = Math.min(nodes[node.node.id].index||Infinity,kpos);
|
||||
}
|
||||
}
|
||||
@ -121,22 +109,8 @@ RED.search = (function() {
|
||||
for (i=0;i<list.length;i++) {
|
||||
results.push(nodes[list[i]]);
|
||||
}
|
||||
if (results.length > 0) {
|
||||
for (i=0;i<Math.min(results.length,25);i++) {
|
||||
searchResults.editableList('addItem',results[i])
|
||||
}
|
||||
if (results.length > 25) {
|
||||
searchResults.editableList('addItem', {
|
||||
more: {
|
||||
results: results,
|
||||
start: 25
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
searchResults.editableList('addItem',{});
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
function ensureSelectedIsVisible() {
|
||||
@ -161,13 +135,32 @@ RED.search = (function() {
|
||||
searchInput = $('<input type="text" data-i18n="[placeholder]menu.label.searchInput">').appendTo(searchDiv).searchBox({
|
||||
delay: 200,
|
||||
change: function() {
|
||||
search($(this).val());
|
||||
searchResults.editableList('empty');
|
||||
selected = -1;
|
||||
currentResults = search($(this).val());
|
||||
if (currentResults.length > 0) {
|
||||
for (i=0;i<Math.min(currentResults.length,25);i++) {
|
||||
searchResults.editableList('addItem',currentResults[i])
|
||||
}
|
||||
if (currentResults.length > 25) {
|
||||
searchResults.editableList('addItem', {
|
||||
more: {
|
||||
results: currentResults,
|
||||
start: 25
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
searchResults.editableList('addItem',{});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
searchInput.on('keydown',function(evt) {
|
||||
var children;
|
||||
if (results.length > 0) {
|
||||
if (currentResults.length > 0) {
|
||||
if (evt.keyCode === 40) {
|
||||
// Down
|
||||
children = searchResults.children();
|
||||
@ -199,21 +192,21 @@ RED.search = (function() {
|
||||
var object = $(children[selected]).find(".red-ui-editableList-item-content").data('data');
|
||||
if (object) {
|
||||
searchResults.editableList('removeItem',object);
|
||||
for (i=object.more.start;i<Math.min(results.length,object.more.start+25);i++) {
|
||||
searchResults.editableList('addItem',results[i])
|
||||
for (i=object.more.start;i<Math.min(currentResults.length,object.more.start+25);i++) {
|
||||
searchResults.editableList('addItem',currentResults[i])
|
||||
}
|
||||
if (results.length > object.more.start+25) {
|
||||
if (currentResults.length > object.more.start+25) {
|
||||
searchResults.editableList('addItem', {
|
||||
more: {
|
||||
results: results,
|
||||
results: currentResults,
|
||||
start: object.more.start+25
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (results.length > 0) {
|
||||
reveal(results[Math.max(0,selected)].node);
|
||||
if (currentResults.length > 0) {
|
||||
reveal(currentResults[Math.max(0,selected)].node);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -234,13 +227,13 @@ RED.search = (function() {
|
||||
div.on("click", function(evt) {
|
||||
evt.preventDefault();
|
||||
searchResults.editableList('removeItem',object);
|
||||
for (i=object.more.start;i<Math.min(results.length,object.more.start+25);i++) {
|
||||
searchResults.editableList('addItem',results[i])
|
||||
for (i=object.more.start;i<Math.min(currentResults.length,object.more.start+25);i++) {
|
||||
searchResults.editableList('addItem',currentResults[i])
|
||||
}
|
||||
if (results.length > object.more.start+25) {
|
||||
if (currentResults.length > object.more.start+25) {
|
||||
searchResults.editableList('addItem', {
|
||||
more: {
|
||||
results: results,
|
||||
results: currentResults,
|
||||
start: object.more.start+25
|
||||
}
|
||||
})
|
||||
@ -308,7 +301,7 @@ RED.search = (function() {
|
||||
$("#red-ui-palette-shade").show();
|
||||
$("#red-ui-sidebar-shade").show();
|
||||
$("#red-ui-sidebar-separator").hide();
|
||||
indexWorkspace();
|
||||
|
||||
if (dialog === null) {
|
||||
createDialog();
|
||||
}
|
||||
@ -342,6 +335,28 @@ RED.search = (function() {
|
||||
}
|
||||
}
|
||||
|
||||
function clearIndex() {
|
||||
index = {};
|
||||
}
|
||||
|
||||
function addItemToIndex(item) {
|
||||
indexNode(item);
|
||||
}
|
||||
function removeItemFromIndex(item) {
|
||||
var keys = Object.keys(index);
|
||||
for (var i=0,l=keys.length;i<l;i++) {
|
||||
delete index[keys[i]][item.id];
|
||||
if (Object.keys(index[keys[i]]).length === 0) {
|
||||
delete index[keys[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
function updateItemOnIndex(item) {
|
||||
removeItemFromIndex(item);
|
||||
addItemToIndex(item);
|
||||
}
|
||||
|
||||
|
||||
function init() {
|
||||
RED.actions.add("core:search",show);
|
||||
|
||||
@ -358,12 +373,33 @@ RED.search = (function() {
|
||||
$("#red-ui-editor-shade").on('mousedown',hide);
|
||||
$("#red-ui-palette-shade").on('mousedown',hide);
|
||||
$("#red-ui-sidebar-shade").on('mousedown',hide);
|
||||
|
||||
|
||||
RED.events.on("workspace:clear", clearIndex)
|
||||
|
||||
RED.events.on("flows:add", addItemToIndex)
|
||||
RED.events.on("flows:remove", removeItemFromIndex)
|
||||
RED.events.on("flows:change", updateItemOnIndex)
|
||||
|
||||
RED.events.on("subflows:add", addItemToIndex)
|
||||
RED.events.on("subflows:remove", removeItemFromIndex)
|
||||
RED.events.on("subflows:change", updateItemOnIndex)
|
||||
|
||||
RED.events.on("nodes:add",addItemToIndex);
|
||||
RED.events.on("nodes:remove",removeItemFromIndex);
|
||||
RED.events.on("nodes:change",updateItemOnIndex);
|
||||
|
||||
RED.events.on("groups:add",addItemToIndex);
|
||||
RED.events.on("groups:remove",removeItemFromIndex);
|
||||
RED.events.on("groups:change",updateItemOnIndex);
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
init: init,
|
||||
show: show,
|
||||
hide: hide
|
||||
hide: hide,
|
||||
search: search
|
||||
};
|
||||
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user