diff options
| author | Alon Levy <alon@pobox.com> | 2015-03-09 20:09:48 +0200 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2015-03-09 20:12:31 +0200 |
| commit | dd2c16e22b76ca299c4edfcd8f62d22e6bf39baa (patch) | |
| tree | 072390f028ac6bf80ff3caec7f27188aa8dd1d5c /src/client/view | |
| parent | 05e7b8021d17772f3fb0db4667b7901b1c34a474 (diff) | |
client: search in nodes and attributes
Fixes #358
Implementation limitations:
- uses hasOwnProperty for search, and further filters on strings. So
technically could be oversearching - i.e. string properties that are
not attributes. We should keep an attribute key on Node and Link
- there is no selected link, just selected nodes. So we select both
source and destination. Opening issue #367 for this.
Diffstat (limited to 'src/client/view')
| -rw-r--r-- | src/client/view/search.js | 19 | ||||
| -rw-r--r-- | src/client/view/selection.js | 23 |
2 files changed, 38 insertions, 4 deletions
diff --git a/src/client/view/search.js b/src/client/view/search.js index 29e702d1..09950687 100644 --- a/src/client/view/search.js +++ b/src/client/view/search.js @@ -28,9 +28,24 @@ function init() { return true; }); + function attribute_match(obj, regexp) { + var v, k; + + for (k in obj) { + if (obj.hasOwnProperty(k)) { + v = obj[k]; + if ("string" === typeof(v) && v.match(regexp)) { + return true; + } + } + } + return false; + } + function search_on_submit() { var text = search[0].value.trim(), - r; + r, + selector = function (obj) { return attribute_match(obj, r); }; try { r = new RegExp(text.replace(/ /, '|'), 'i'); @@ -38,7 +53,7 @@ function init() { return; // don't clear selection either } if (text.length > 0) { - selection.byVisitors(function (n) { return n.name.match(r); }); + selection.byVisitors(selector, selector); } else { selection.clear(); } diff --git a/src/client/view/selection.js b/src/client/view/selection.js index 6ac675aa..7a99dd03 100644 --- a/src/client/view/selection.js +++ b/src/client/view/selection.js @@ -117,10 +117,29 @@ function updateSelectedNodesBus(new_selected_nodes) selectionChangedBus.push(new_selection(selected_nodes, root_nodes)); } +/* add nodes in nodes_b to a copy of nodes_a in order, skipping duplicates */ +function sum_nodes(nodes_a, nodes_b) +{ + var set_a_id = _.object(nodes_a.map(function (n) { return [n.id, 1]; })), + ret = nodes_a.slice(0); + + for (var k in nodes_b) { + if (set_a_id[nodes_b[k].id] === undefined) { + ret.push(nodes_b[k]); + } + } + return ret; +} + +function links_to_nodes(links) +{ + return _.flatten(_.map(links, function (link) { return [link.__src, link.__dst]; })); +} + function byVisitors(node_selector, link_selector) { - var new_selected_nodes = get_main_graph().find__by_visitors(node_selector, link_selector); + var new_selection = get_main_graph().find__by_visitors(node_selector, link_selector); - inner_update(new_selected_nodes); + inner_update(sum_nodes(new_selection.nodes, links_to_nodes(new_selection.links))); } function connectedComponent(nodes) { |
