|
|
@ -1,12 +1,11 @@ |
|
|
|
var Node = require("./components/Node"); |
|
|
|
var util = require('../../util'); |
|
|
|
let Node = require("./components/Node"); |
|
|
|
let util = require('../../util'); |
|
|
|
|
|
|
|
class SelectionHandler { |
|
|
|
constructor(body, canvas) { |
|
|
|
this.body = body; |
|
|
|
this.canvas = canvas; |
|
|
|
this.selectionObj = {nodes: [], edges: []}; |
|
|
|
this.forceSelectEdges = false; |
|
|
|
|
|
|
|
this.options = {}; |
|
|
|
this.defaultOptions = { |
|
|
@ -35,10 +34,10 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
selectOnPoint(pointer) { |
|
|
|
var selected = false; |
|
|
|
let selected = false; |
|
|
|
if (this.options.select === true) { |
|
|
|
this.unselectAll(); |
|
|
|
var obj = this.getNodeAt(pointer) || this.getEdgeAt(pointer);; |
|
|
|
let obj = this.getNodeAt(pointer) || this.getEdgeAt(pointer);; |
|
|
|
if (obj !== undefined) { |
|
|
|
selected = this.selectObject(obj); |
|
|
|
} |
|
|
@ -48,9 +47,9 @@ class SelectionHandler { |
|
|
|
} |
|
|
|
|
|
|
|
selectAdditionalOnPoint(pointer) { |
|
|
|
var selectionChanged = false; |
|
|
|
let selectionChanged = false; |
|
|
|
if (this.options.select === true) { |
|
|
|
var obj = this.getNodeAt(pointer) || this.getEdgeAt(pointer);; |
|
|
|
let obj = this.getNodeAt(pointer) || this.getEdgeAt(pointer);; |
|
|
|
|
|
|
|
if (obj !== undefined) { |
|
|
|
selectionChanged = true; |
|
|
@ -67,19 +66,23 @@ class SelectionHandler { |
|
|
|
return selectionChanged; |
|
|
|
} |
|
|
|
|
|
|
|
_generateClickEvent(eventType,pointer) { |
|
|
|
var properties = this.getSelection(); |
|
|
|
_generateClickEvent(eventType, pointer, oldSelection) { |
|
|
|
let properties = this.getSelection(); |
|
|
|
properties['pointer'] = { |
|
|
|
DOM: {x: pointer.x, y: pointer.y}, |
|
|
|
canvas: this.canvas.DOMtoCanvas(pointer) |
|
|
|
} |
|
|
|
|
|
|
|
if (oldSelection !== undefined) { |
|
|
|
properties['previousSelection'] = oldSelection; |
|
|
|
} |
|
|
|
this.body.emitter.emit(eventType, properties); |
|
|
|
} |
|
|
|
|
|
|
|
selectObject(obj) { |
|
|
|
selectObject(obj, highlightEdges = this.options.selectConnectedEdges) { |
|
|
|
if (obj !== undefined) { |
|
|
|
if (obj instanceof Node) { |
|
|
|
if (this.options.selectConnectedEdges === true || this.forceSelectEdges === true) { |
|
|
|
if (highlightEdges === true) { |
|
|
|
this._selectConnectedEdges(obj); |
|
|
|
} |
|
|
|
} |
|
|
@ -106,8 +109,8 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_getAllNodesOverlappingWith(object) { |
|
|
|
var overlappingNodes = []; |
|
|
|
var nodes = this.body.nodes; |
|
|
|
let overlappingNodes = []; |
|
|
|
let nodes = this.body.nodes; |
|
|
|
for (let i = 0; i < this.body.nodeIndices.length; i++) { |
|
|
|
let nodeId = this.body.nodeIndices[i]; |
|
|
|
if (nodes[nodeId].isOverlappingWith(object)) { |
|
|
@ -126,7 +129,7 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_pointerToPositionObject(pointer) { |
|
|
|
var canvasPos = this.canvas.DOMtoCanvas(pointer); |
|
|
|
let canvasPos = this.canvas.DOMtoCanvas(pointer); |
|
|
|
return { |
|
|
|
left: canvasPos.x - 1, |
|
|
|
top: canvasPos.y + 1, |
|
|
@ -145,8 +148,8 @@ class SelectionHandler { |
|
|
|
*/ |
|
|
|
getNodeAt(pointer) { |
|
|
|
// we first check if this is an navigation controls element
|
|
|
|
var positionObject = this._pointerToPositionObject(pointer); |
|
|
|
var overlappingNodes = this._getAllNodesOverlappingWith(positionObject); |
|
|
|
let positionObject = this._pointerToPositionObject(pointer); |
|
|
|
let overlappingNodes = this._getAllNodesOverlappingWith(positionObject); |
|
|
|
|
|
|
|
// if there are overlapping nodes, select the last one, this is the
|
|
|
|
// one which is drawn on top of the others
|
|
|
@ -166,7 +169,7 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_getEdgesOverlappingWith(object, overlappingEdges) { |
|
|
|
var edges = this.body.edges; |
|
|
|
let edges = this.body.edges; |
|
|
|
for (let i = 0; i < this.body.edgeIndices.length; i++) { |
|
|
|
let edgeId = this.body.edgeIndices[i]; |
|
|
|
if (edges[edgeId].isOverlappingWith(object)) { |
|
|
@ -183,7 +186,7 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_getAllEdgesOverlappingWith(object) { |
|
|
|
var overlappingEdges = []; |
|
|
|
let overlappingEdges = []; |
|
|
|
this._getEdgesOverlappingWith(object,overlappingEdges); |
|
|
|
return overlappingEdges; |
|
|
|
} |
|
|
@ -198,8 +201,8 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
getEdgeAt(pointer) { |
|
|
|
var positionObject = this._pointerToPositionObject(pointer); |
|
|
|
var overlappingEdges = this._getAllEdgesOverlappingWith(positionObject); |
|
|
|
let positionObject = this._pointerToPositionObject(pointer); |
|
|
|
let overlappingEdges = this._getAllEdgesOverlappingWith(positionObject); |
|
|
|
|
|
|
|
if (overlappingEdges.length > 0) { |
|
|
|
return this.body.edges[overlappingEdges[overlappingEdges.length - 1]]; |
|
|
@ -262,12 +265,12 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
unselectAll() { |
|
|
|
for(var nodeId in this.selectionObj.nodes) { |
|
|
|
for(let nodeId in this.selectionObj.nodes) { |
|
|
|
if(this.selectionObj.nodes.hasOwnProperty(nodeId)) { |
|
|
|
this.selectionObj.nodes[nodeId].unselect(); |
|
|
|
} |
|
|
|
} |
|
|
|
for(var edgeId in this.selectionObj.edges) { |
|
|
|
for(let edgeId in this.selectionObj.edges) { |
|
|
|
if(this.selectionObj.edges.hasOwnProperty(edgeId)) { |
|
|
|
this.selectionObj.edges[edgeId].unselect(); |
|
|
|
} |
|
|
@ -284,8 +287,8 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_getSelectedNodeCount() { |
|
|
|
var count = 0; |
|
|
|
for (var nodeId in this.selectionObj.nodes) { |
|
|
|
let count = 0; |
|
|
|
for (let nodeId in this.selectionObj.nodes) { |
|
|
|
if (this.selectionObj.nodes.hasOwnProperty(nodeId)) { |
|
|
|
count += 1; |
|
|
|
} |
|
|
@ -300,7 +303,7 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_getSelectedNode() { |
|
|
|
for (var nodeId in this.selectionObj.nodes) { |
|
|
|
for (let nodeId in this.selectionObj.nodes) { |
|
|
|
if (this.selectionObj.nodes.hasOwnProperty(nodeId)) { |
|
|
|
return this.selectionObj.nodes[nodeId]; |
|
|
|
} |
|
|
@ -315,7 +318,7 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_getSelectedEdge() { |
|
|
|
for (var edgeId in this.selectionObj.edges) { |
|
|
|
for (let edgeId in this.selectionObj.edges) { |
|
|
|
if (this.selectionObj.edges.hasOwnProperty(edgeId)) { |
|
|
|
return this.selectionObj.edges[edgeId]; |
|
|
|
} |
|
|
@ -331,8 +334,8 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_getSelectedEdgeCount() { |
|
|
|
var count = 0; |
|
|
|
for (var edgeId in this.selectionObj.edges) { |
|
|
|
let count = 0; |
|
|
|
for (let edgeId in this.selectionObj.edges) { |
|
|
|
if (this.selectionObj.edges.hasOwnProperty(edgeId)) { |
|
|
|
count += 1; |
|
|
|
} |
|
|
@ -348,13 +351,13 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_getSelectedObjectCount() { |
|
|
|
var count = 0; |
|
|
|
for(var nodeId in this.selectionObj.nodes) { |
|
|
|
let count = 0; |
|
|
|
for(let nodeId in this.selectionObj.nodes) { |
|
|
|
if(this.selectionObj.nodes.hasOwnProperty(nodeId)) { |
|
|
|
count += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
for(var edgeId in this.selectionObj.edges) { |
|
|
|
for(let edgeId in this.selectionObj.edges) { |
|
|
|
if(this.selectionObj.edges.hasOwnProperty(edgeId)) { |
|
|
|
count += 1; |
|
|
|
} |
|
|
@ -369,12 +372,12 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_selectionIsEmpty() { |
|
|
|
for(var nodeId in this.selectionObj.nodes) { |
|
|
|
for(let nodeId in this.selectionObj.nodes) { |
|
|
|
if(this.selectionObj.nodes.hasOwnProperty(nodeId)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
for(var edgeId in this.selectionObj.edges) { |
|
|
|
for(let edgeId in this.selectionObj.edges) { |
|
|
|
if(this.selectionObj.edges.hasOwnProperty(edgeId)) { |
|
|
|
return false; |
|
|
|
} |
|
|
@ -390,7 +393,7 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_clusterInSelection() { |
|
|
|
for(var nodeId in this.selectionObj.nodes) { |
|
|
|
for(let nodeId in this.selectionObj.nodes) { |
|
|
|
if(this.selectionObj.nodes.hasOwnProperty(nodeId)) { |
|
|
|
if (this.selectionObj.nodes[nodeId].clusterSize > 1) { |
|
|
|
return true; |
|
|
@ -407,8 +410,8 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_selectConnectedEdges(node) { |
|
|
|
for (var i = 0; i < node.edges.length; i++) { |
|
|
|
var edge = node.edges[i]; |
|
|
|
for (let i = 0; i < node.edges.length; i++) { |
|
|
|
let edge = node.edges[i]; |
|
|
|
edge.select(); |
|
|
|
this._addToSelection(edge); |
|
|
|
} |
|
|
@ -421,8 +424,8 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_hoverConnectedEdges(node) { |
|
|
|
for (var i = 0; i < node.edges.length; i++) { |
|
|
|
var edge = node.edges[i]; |
|
|
|
for (let i = 0; i < node.edges.length; i++) { |
|
|
|
let edge = node.edges[i]; |
|
|
|
edge.hover = true; |
|
|
|
this._addToHover(edge); |
|
|
|
} |
|
|
@ -436,8 +439,8 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
_unselectConnectedEdges(node) { |
|
|
|
for (var i = 0; i < node.edges.length; i++) { |
|
|
|
var edge = node.edges[i]; |
|
|
|
for (let i = 0; i < node.edges.length; i++) { |
|
|
|
let edge = node.edges[i]; |
|
|
|
edge.unselect(); |
|
|
|
this._removeFromSelection(edge); |
|
|
|
} |
|
|
@ -491,8 +494,8 @@ class SelectionHandler { |
|
|
|
* @return {{nodes: Array.<String>, edges: Array.<String>}} selection |
|
|
|
*/ |
|
|
|
getSelection() { |
|
|
|
var nodeIds = this.getSelectedNodes(); |
|
|
|
var edgeIds = this.getSelectedEdges(); |
|
|
|
let nodeIds = this.getSelectedNodes(); |
|
|
|
let edgeIds = this.getSelectedEdges(); |
|
|
|
return {nodes:nodeIds, edges:edgeIds}; |
|
|
|
} |
|
|
|
|
|
|
@ -503,9 +506,9 @@ class SelectionHandler { |
|
|
|
* selected nodes. |
|
|
|
*/ |
|
|
|
getSelectedNodes() { |
|
|
|
var idArray = []; |
|
|
|
let idArray = []; |
|
|
|
if (this.options.select === true) { |
|
|
|
for (var nodeId in this.selectionObj.nodes) { |
|
|
|
for (let nodeId in this.selectionObj.nodes) { |
|
|
|
if (this.selectionObj.nodes.hasOwnProperty(nodeId)) { |
|
|
|
idArray.push(nodeId); |
|
|
|
} |
|
|
@ -521,9 +524,9 @@ class SelectionHandler { |
|
|
|
* selected nodes. |
|
|
|
*/ |
|
|
|
getSelectedEdges() { |
|
|
|
var idArray = []; |
|
|
|
let idArray = []; |
|
|
|
if (this.options.select === true) { |
|
|
|
for (var edgeId in this.selectionObj.edges) { |
|
|
|
for (let edgeId in this.selectionObj.edges) { |
|
|
|
if (this.selectionObj.edges.hasOwnProperty(edgeId)) { |
|
|
|
idArray.push(edgeId); |
|
|
|
} |
|
|
@ -539,25 +542,25 @@ class SelectionHandler { |
|
|
|
* selected nodes. |
|
|
|
* @param {boolean} [highlightEdges] |
|
|
|
*/ |
|
|
|
selectNodes(selection, highlightEdges) { |
|
|
|
var i, iMax, id; |
|
|
|
selectNodes(selection, highlightEdges = true) { |
|
|
|
let i, id; |
|
|
|
|
|
|
|
if (!selection || (selection.length === undefined)) |
|
|
|
throw 'Selection must be an array with ids'; |
|
|
|
|
|
|
|
// first unselect any selected node
|
|
|
|
this.unselectAll(true); |
|
|
|
this.unselectAll(); |
|
|
|
|
|
|
|
for (i = 0, iMax = selection.length; i < iMax; i++) { |
|
|
|
for (i = 0; i < selection.length; i++) { |
|
|
|
id = selection[i]; |
|
|
|
|
|
|
|
var node = this.body.nodes[id]; |
|
|
|
let node = this.body.nodes[id]; |
|
|
|
if (!node) { |
|
|
|
throw new RangeError('Node with id "' + id + '" not found'); |
|
|
|
} |
|
|
|
this._selectObject(node,true,true,highlightEdges,true); |
|
|
|
this.selectObject(node,highlightEdges); |
|
|
|
} |
|
|
|
this.redraw(); |
|
|
|
this.body.emitter.emit('_requestRedraw'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -567,24 +570,24 @@ class SelectionHandler { |
|
|
|
* selected nodes. |
|
|
|
*/ |
|
|
|
selectEdges(selection) { |
|
|
|
var i, iMax, id; |
|
|
|
let i, id; |
|
|
|
|
|
|
|
if (!selection || (selection.length === undefined)) |
|
|
|
throw 'Selection must be an array with ids'; |
|
|
|
|
|
|
|
// first unselect any selected node
|
|
|
|
this.unselectAll(true); |
|
|
|
// first unselect any selected objects
|
|
|
|
this.unselectAll(); |
|
|
|
|
|
|
|
for (i = 0, iMax = selection.length; i < iMax; i++) { |
|
|
|
for (i = 0; i < selection.length; i++) { |
|
|
|
id = selection[i]; |
|
|
|
|
|
|
|
var edge = this.body.edges[id]; |
|
|
|
let edge = this.body.edges[id]; |
|
|
|
if (!edge) { |
|
|
|
throw new RangeError('Edge with id "' + id + '" not found'); |
|
|
|
} |
|
|
|
this._selectObject(edge,true,true,false,true); |
|
|
|
this.selectObject(edge); |
|
|
|
} |
|
|
|
this.redraw(); |
|
|
|
this.body.emitter.emit('_requestRedraw'); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -592,14 +595,14 @@ class SelectionHandler { |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
updateSelection() { |
|
|
|
for (var nodeId in this.selectionObj.nodes) { |
|
|
|
for (let nodeId in this.selectionObj.nodes) { |
|
|
|
if (this.selectionObj.nodes.hasOwnProperty(nodeId)) { |
|
|
|
if (!this.body.nodes.hasOwnProperty(nodeId)) { |
|
|
|
delete this.selectionObj.nodes[nodeId]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
for (var edgeId in this.selectionObj.edges) { |
|
|
|
for (let edgeId in this.selectionObj.edges) { |
|
|
|
if (this.selectionObj.edges.hasOwnProperty(edgeId)) { |
|
|
|
if (!this.body.edges.hasOwnProperty(edgeId)) { |
|
|
|
delete this.selectionObj.edges[edgeId]; |
|
|
|