Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add @ selector support #27

Merged
merged 1 commit into from
May 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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