Commit 82cf1269 authored by Remon Huijts's avatar Remon Huijts
Browse files

Reduce code complexity by creating just one handler for all target cells per interaction

parent 5d99346a
......@@ -123100,6 +123100,16 @@ var interactions = {
debug("field 'action' not specified");
return;
}
if (!Array.isArray(rule.targets) || !rule.targets.length) {
debug('Warning: No target specified for rule');
return;
}
var cells = interactions.getTargetCells(graph, rule.targets);
if (!Array.isArray(cells) || !cells.length) {
debug("No valid cells were specified by field 'targets'.");
return;
}
switch (rule.action) {
case 'hide':
// fall through
......@@ -123108,45 +123118,31 @@ var interactions = {
// FALSE before and after the event. For 'hide' it is the
// other way around.
var visibleDuringEvent = rule.action === 'popup';
if (Array.isArray(rule.targets)) {
rule.targets.forEach(function(target) {
var cells = interactions.getTargetCells(graph, target);
// Reverse the visibility before the event:
// Reverse the visibility before the event:
interactions.setCellVisibility(graph, cells, !visibleDuringEvent);
handlers.push(function() {
// Apply the visibility when the event starts:
interactions.setCellVisibility(graph, cells, visibleDuringEvent);
return function() {
// Reverse the visibility again when the event ends:
interactions.setCellVisibility(graph, cells, !visibleDuringEvent);
handlers.push(function() {
// Apply the visibility when the event starts:
interactions.setCellVisibility(graph, cells, visibleDuringEvent);
return function() {
// Reverse the visibility again when the event ends:
interactions.setCellVisibility(graph, cells, !visibleDuringEvent);
};
});
});
} else {
debug('Warning: No target specified for rule');
}
};
});
break;
case 'highlight':
if (typeof rule.colour == 'undefined') {
rule.colour = '#0000FF';
}
if (Array.isArray(rule.targets)) {
rule.targets.forEach(function(target) {
var cells = interactions.getTargetCells(graph, target);
handlers.push(function() {
// Add the highlights when the event starts.
var h = interactions.highlightCells(graph, cells, rule.colour);
return function() {
// Remove the highlights when the event ends.
for (var i = 0; i < h.length; i++) {
h[i].hide();
}
};
});
});
} else {
debug('Warning: No target specified for rule');
}
handlers.push(function() {
// Add the highlights when the event starts.
var h = interactions.highlightCells(graph, cells, rule.colour);
return function() {
// Remove the highlights when the event ends.
for (var i = 0; i < h.length; i++) {
h[i].hide();
}
};
});
break;
default:
debug('action \"' + rule.action + '\" not found');
......@@ -123158,12 +123154,12 @@ var interactions = {
/**
* Function: getTargetCells
*
* Get an array of cells from a target specification.
* Get an array of cells from an array of target specifications.
*
* Parameters:
*
* graph - graph object to operate on
* target - object containing cell specification; currently this should be
* targets - array of cell specification objects; currently they should have
* a "cell_id" property containing the numeric id of the cell.
*
* Returns:
......@@ -123171,24 +123167,36 @@ var interactions = {
* An array with the cell object. If the cell is a group, it returns an
* array of all the cells in the group.
*/
getTargetCells: function(graph, target) {
if (typeof target.cell_id == 'undefined') {
debug('could not obtain target from specification:');
debug(target);
return [];
}
var cell = graph.getModel().getCell(target.cell_id);
if (typeof cell == 'undefined') {
debug('Target cell is undefined.');
return [];
}
var children = graph.getModel().getChildren(cell) || [];
if (children.length > 0) {
// Assume this is a group and return the children (not the parent)
return children;
getTargetCells: function(graph, targets) {
var cells = [];
var cell = null;
for (var i = 0; i < targets.length; i++) {
if (typeof targets[i].cell_id === 'undefined') {
debug('Could not obtain target from specification:');
debug(targets[i]);
continue;
}
cell = graph.getModel().getCell(targets[i].cell_id);
if (typeof cell === 'undefined') {
debug('Could not obtain a cell with this ID:');
debug(targets[i].cell_id);
continue;
}
// Test whether the cell is a group with child cells:
var children = graph.getModel().getChildren(cell) || [];
if (children.length > 0) {
// Assume this is a group and add the children (not the parent)
cells = cells.concat(children);
} else {
// No children, so assume not a group and add the single cell
cells.push(cell);
}
}
// Not a group, so just return the single cell in an array:
return [cell];
return cells;
},
 
/**
......@@ -128,6 +128,16 @@ var interactions = {
debug("field 'action' not specified");
return;
}
if (!Array.isArray(rule.targets) || !rule.targets.length) {
debug('Warning: No target specified for rule');
return;
}
var cells = interactions.getTargetCells(graph, rule.targets);
if (!Array.isArray(cells) || !cells.length) {
debug("No valid cells were specified by field 'targets'.");
return;
}
switch (rule.action) {
case 'hide':
// fall through
......@@ -136,45 +146,31 @@ var interactions = {
// FALSE before and after the event. For 'hide' it is the
// other way around.
var visibleDuringEvent = rule.action === 'popup';
if (Array.isArray(rule.targets)) {
rule.targets.forEach(function(target) {
var cells = interactions.getTargetCells(graph, target);
// Reverse the visibility before the event:
// Reverse the visibility before the event:
interactions.setCellVisibility(graph, cells, !visibleDuringEvent);
handlers.push(function() {
// Apply the visibility when the event starts:
interactions.setCellVisibility(graph, cells, visibleDuringEvent);
return function() {
// Reverse the visibility again when the event ends:
interactions.setCellVisibility(graph, cells, !visibleDuringEvent);
handlers.push(function() {
// Apply the visibility when the event starts:
interactions.setCellVisibility(graph, cells, visibleDuringEvent);
return function() {
// Reverse the visibility again when the event ends:
interactions.setCellVisibility(graph, cells, !visibleDuringEvent);
};
});
});
} else {
debug('Warning: No target specified for rule');
}
};
});
break;
case 'highlight':
if (typeof rule.colour == 'undefined') {
rule.colour = '#0000FF';
}
if (Array.isArray(rule.targets)) {
rule.targets.forEach(function(target) {
var cells = interactions.getTargetCells(graph, target);
handlers.push(function() {
// Add the highlights when the event starts.
var h = interactions.highlightCells(graph, cells, rule.colour);
return function() {
// Remove the highlights when the event ends.
for (var i = 0; i < h.length; i++) {
h[i].hide();
}
};
});
});
} else {
debug('Warning: No target specified for rule');
}
handlers.push(function() {
// Add the highlights when the event starts.
var h = interactions.highlightCells(graph, cells, rule.colour);
return function() {
// Remove the highlights when the event ends.
for (var i = 0; i < h.length; i++) {
h[i].hide();
}
};
});
break;
default:
debug('action \"' + rule.action + '\" not found');
......@@ -186,12 +182,12 @@ var interactions = {
/**
* Function: getTargetCells
*
* Get an array of cells from a target specification.
* Get an array of cells from an array of target specifications.
*
* Parameters:
*
* graph - graph object to operate on
* target - object containing cell specification; currently this should be
* targets - array of cell specification objects; currently they should have
* a "cell_id" property containing the numeric id of the cell.
*
* Returns:
......@@ -199,24 +195,36 @@ var interactions = {
* An array with the cell object. If the cell is a group, it returns an
* array of all the cells in the group.
*/
getTargetCells: function(graph, target) {
if (typeof target.cell_id == 'undefined') {
debug('could not obtain target from specification:');
debug(target);
return [];
}
var cell = graph.getModel().getCell(target.cell_id);
if (typeof cell == 'undefined') {
debug('Target cell is undefined.');
return [];
}
var children = graph.getModel().getChildren(cell) || [];
if (children.length > 0) {
// Assume this is a group and return the children (not the parent)
return children;
getTargetCells: function(graph, targets) {
var cells = [];
var cell = null;
for (var i = 0; i < targets.length; i++) {
if (typeof targets[i].cell_id === 'undefined') {
debug('Could not obtain target from specification:');
debug(targets[i]);
continue;
}
cell = graph.getModel().getCell(targets[i].cell_id);
if (typeof cell === 'undefined') {
debug('Could not obtain a cell with this ID:');
debug(targets[i].cell_id);
continue;
}
// Test whether the cell is a group with child cells:
var children = graph.getModel().getChildren(cell) || [];
if (children.length > 0) {
// Assume this is a group and add the children (not the parent)
cells = cells.concat(children);
} else {
// No children, so assume not a group and add the single cell
cells.push(cell);
}
}
// Not a group, so just return the single cell in an array:
return [cell];
return cells;
},
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment