Skip to content

Commit

Permalink
Merge pull request #27 from fishtown-analytics/support-at-selector
Browse files Browse the repository at this point in the history
add @ selector support
  • Loading branch information
drewbanin authored May 9, 2019
2 parents 60eff58 + 433b266 commit ec5c9a4
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/app/services/node_selection_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const $ = require('jquery');
const _ = require('underscore');

var SELECTOR_AT = '@'
var SELECTOR_PARENTS = '+'
var SELECTOR_CHILDREN = '+'
var SELECTOR_GLOB = '*'
Expand Down Expand Up @@ -120,6 +121,19 @@ angular
return service.selection.clean;
}

// Returns all parents of all children of the node
function select_at(dag, node) {
var selected = [node];
var children = _.union([node], descendents(dag, node));

_.each(children, function(child) {
var ancestor_nodes = ancestors(dag, child);
selected = _.union(selected, ancestor_nodes, [child]);
});

return selected;
}

function ancestors(dag, node, max_hops, hop_index) {
if (!hop_index) hop_index = 1;

Expand Down Expand Up @@ -152,12 +166,17 @@ angular
}

function parse_spec(node_spec) {
var select_at = false;
var select_children = false;
var select_parents = false;
var index_start = 0;
var index_end = node_spec.length;

if (node_spec.startsWith(SELECTOR_PARENTS)) {
// @+ is not a valid selector - one or the other is required
if (node_spec.startsWith(SELECTOR_AT)) {
select_at = true;
index_start = 1;
} else if (node_spec.startsWith(SELECTOR_PARENTS)) {
select_parents = true;
index_start = 1;
}
Expand All @@ -183,6 +202,7 @@ angular
}

return {
select_at: select_at,
select_parents: select_parents,
select_children: select_children,
selector_type: selector_type,
Expand Down Expand Up @@ -312,6 +332,10 @@ angular

var upstream = [];
var downstream = [];
var both = []
if (selector.select_at) {
both = _.union(select_at(dag, selected_node));
}

if (selector.select_parents) {
upstream = ancestors(dag, selected_node, hops);
Expand All @@ -321,7 +345,7 @@ angular
downstream = descendents(dag, selected_node, hops)
}

selected_nodes = _.union([selected_node], selected_nodes, downstream, upstream);
selected_nodes = _.union([selected_node], selected_nodes, downstream, upstream, both);
});

return {
Expand Down

0 comments on commit ec5c9a4

Please sign in to comment.