Skip to content

Commit

Permalink
Consolidated parent selection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Dec 22, 2020
1 parent 5433c8f commit 3e84a93
Showing 1 changed file with 11 additions and 38 deletions.
49 changes: 11 additions & 38 deletions modules/modes/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ export function modeSelect(context, selectedIDs) {
}


// find the common parent ways for nextVertex, previousVertex
function allParents() {
// find the parent ways for nextVertex, previousVertex, and selectParent
function multipleParents(onlyCommonParents) {
var graph = context.graph();
var allParents = [];
var parents = [];

for (var i = 0; i < selectedIDs.length; i++) {
var entity = context.hasEntity(selectedIDs[i]);
Expand All @@ -97,50 +97,23 @@ export function modeSelect(context, selectedIDs) {
}

var currParents = graph.parentWays(entity).map(function(w) { return w.id; });
if (!allParents.length) {
allParents = currParents;
if (!parents.length) {
parents = currParents;
continue;
}

allParents = utilArrayUnion(allParents, currParents);
if (!allParents.length) {
parents = (onlyCommonParents ? utilArrayIntersection : utilArrayUnion)(parents, currParents);
if (!parents.length) {
return [];
}
}

return allParents;
}


// find the common parent ways for nextVertex, previousVertex
function commonParents() {
var graph = context.graph();
var commonParents = [];

for (var i = 0; i < selectedIDs.length; i++) {
var entity = context.hasEntity(selectedIDs[i]);
if (!entity || entity.geometry(graph) !== 'vertex') {
return []; // selection includes some not vertices
}

var currParents = graph.parentWays(entity).map(function(w) { return w.id; });
if (!commonParents.length) {
commonParents = currParents;
continue;
}

commonParents = utilArrayIntersection(commonParents, currParents);
if (!commonParents.length) {
return [];
}
}

return commonParents;
return parents;
}


function singularParent() {
var parents = commonParents();
var parents = multipleParents(true);
if (!parents || parents.length === 0) {
_relatedParent = null;
return null;
Expand Down Expand Up @@ -570,7 +543,7 @@ export function modeSelect(context, selectedIDs) {

function nextParent(d3_event) {
d3_event.preventDefault();
var parents = commonParents();
var parents = multipleParents(true);
if (!parents || parents.length < 2) return;

var index = parents.indexOf(_relatedParent);
Expand All @@ -592,7 +565,7 @@ export function modeSelect(context, selectedIDs) {

function selectParent(d3_event) {
d3_event.preventDefault();
var parents = _relatedParent ? [_relatedParent] : allParents();
var parents = _relatedParent ? [_relatedParent] : multipleParents(false);
if (!parents) return;

context.enter(
Expand Down

0 comments on commit 3e84a93

Please sign in to comment.