summaryrefslogtreecommitdiff
path: root/src/js/handler/mxConstraintHandler.js
blob: 39b3ab6c095e1dcfb693fa6ca4a846b10b2e1e5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
 * $Id: mxConstraintHandler.js,v 1.15 2012-11-01 16:13:41 gaudenz Exp $
 * Copyright (c) 2006-2010, JGraph Ltd
 */
/**
 * Class: mxConstraintHandler
 *
 * Handles constraints on connection targets. This class is in charge of
 * showing fixed points when the mouse is over a vertex and handles constraints
 * to establish new connections.
 *
 * Constructor: mxConstraintHandler
 *
 * Constructs an new constraint handler.
 * 
 * Parameters:
 * 
 * graph - Reference to the enclosing <mxGraph>.
 * factoryMethod - Optional function to create the edge. The function takes
 * the source and target <mxCell> as the first and second argument and
 * returns the <mxCell> that represents the new edge.
 */
function mxConstraintHandler(graph)
{
	this.graph = graph;
};

/**
 * Variable: pointImage
 * 
 * <mxImage> to be used as the image for fixed connection points.
 */
mxConstraintHandler.prototype.pointImage = new mxImage(mxClient.imageBasePath + '/point.gif', 5, 5);

/**
 * Variable: graph
 * 
 * Reference to the enclosing <mxGraph>.
 */
mxConstraintHandler.prototype.graph = null;

/**
 * Variable: enabled
 * 
 * Specifies if events are handled. Default is true.
 */
mxConstraintHandler.prototype.enabled = true;

/**
 * Variable: highlightColor
 * 
 * Specifies the color for the highlight. Default is <mxConstants.DEFAULT_VALID_COLOR>.
 */
mxConstraintHandler.prototype.highlightColor = mxConstants.DEFAULT_VALID_COLOR;

/**
 * Function: isEnabled
 * 
 * Returns true if events are handled. This implementation
 * returns <enabled>.
 */
mxConstraintHandler.prototype.isEnabled = function()
{
	return this.enabled;
};
	
/**
 * Function: setEnabled
 * 
 * Enables or disables event handling. This implementation
 * updates <enabled>.
 * 
 * Parameters:
 * 
 * enabled - Boolean that specifies the new enabled state.
 */
mxConstraintHandler.prototype.setEnabled = function(enabled)
{
	this.enabled = enabled;
};

/**
 * Function: reset
 * 
 * Resets the state of this handler.
 */
mxConstraintHandler.prototype.reset = function()
{
	if (this.focusIcons != null)
	{
		for (var i = 0; i < this.focusIcons.length; i++)
		{
			this.focusIcons[i].destroy();
		}
		
		this.focusIcons = null;
	}
	
	if (this.focusHighlight != null)
	{
		this.focusHighlight.destroy();
		this.focusHighlight = null;
	}
	
	this.currentConstraint = null;
	this.currentFocusArea = null;
	this.currentPoint = null;
	this.currentFocus = null;
	this.focusPoints = null;
};

/**
 * Function: getTolerance
 * 
 * Returns the tolerance to be used for intersecting connection points.
 */
mxConstraintHandler.prototype.getTolerance = function()
{
	return this.graph.getTolerance();
};

/**
 * Function: getImageForConstraint
 * 
 * Returns the tolerance to be used for intersecting connection points.
 */
mxConstraintHandler.prototype.getImageForConstraint = function(state, constraint, point)
{
	return this.pointImage;
};

/**
 * Function: isEventIgnored
 * 
 * Returns true if the given <mxMouseEvent> should be ignored in <update>. This
 * implementation always returns false.
 */
mxConstraintHandler.prototype.isEventIgnored = function(me, source)
{
	return false;
};

/**
 * Function: update
 * 
 * Updates the state of this handler based on the given <mxMouseEvent>.
 * Source is a boolean indicating if the cell is a source or target.
 */
mxConstraintHandler.prototype.update = function(me, source)
{
	if (this.isEnabled() && !this.isEventIgnored(me))
	{
		var tol = this.getTolerance();
		var mouse = new mxRectangle(me.getGraphX() - tol, me.getGraphY() - tol, 2 * tol, 2 * tol);
		var connectable = (me.getCell() != null) ? this.graph.isCellConnectable(me.getCell()) : false;

		if ((this.currentFocusArea == null || (!mxUtils.intersects(this.currentFocusArea, mouse) ||
			(me.getState() != null && this.currentFocus != null && connectable))))
		{
			this.currentFocusArea = null;
	
			if (me.getState() != this.currentFocus)
			{
				this.currentFocus = null;
				this.constraints = (me.getState() != null && connectable) ?
					this.graph.getAllConnectionConstraints(me.getState(), source) : null;
				
				// Only uses cells which have constraints
				if (this.constraints != null)
				{
					this.currentFocus = me.getState();
					this.currentFocusArea = new mxRectangle(me.getState().x, me.getState().y, me.getState().width, me.getState().height);
					
					if (this.focusIcons != null)
					{
						for (var i = 0; i < this.focusIcons.length; i++)
						{
							this.focusIcons[i].destroy();
						}
						
						this.focusIcons = null;
						this.focusPoints = null;
					}
					
					this.focusIcons = [];
					this.focusPoints = [];
					
					for (var i = 0; i < this.constraints.length; i++)
					{
						var cp = this.graph.getConnectionPoint(me.getState(), this.constraints[i]);
						var img = this.getImageForConstraint(me.getState(), this.constraints[i], cp);
	
						var src = img.src;
						var bounds = new mxRectangle(cp.x - img.width / 2,
							cp.y - img.height / 2, img.width, img.height);
						var icon = new mxImageShape(bounds, src);
						icon.dialect = (this.graph.dialect == mxConstants.DIALECT_SVG) ?
							mxConstants.DIALECT_SVG :
							mxConstants.DIALECT_VML;
						icon.init(this.graph.getView().getOverlayPane());
						
						// Move the icon behind all other overlays
						if (icon.node.previousSibling != null)
						{
							icon.node.parentNode.insertBefore(icon.node, icon.node.parentNode.firstChild);
						}
	
						var getState = mxUtils.bind(this, function()
						{
							return (this.currentFocus != null) ? this.currentFocus : me.getState();
						});
						
						icon.redraw();
	
						mxEvent.redirectMouseEvents(icon.node, this.graph, getState);
						this.currentFocusArea.add(icon.bounds);
						this.focusIcons.push(icon);
						this.focusPoints.push(cp);
					}
					
					this.currentFocusArea.grow(tol);
				}
				else if (this.focusIcons != null)
				{
					if (this.focusHighlight != null)
					{
						this.focusHighlight.destroy();
						this.focusHighlight = null;
					}
					
					for (var i = 0; i < this.focusIcons.length; i++)
					{
						this.focusIcons[i].destroy();
					}
					
					this.focusIcons = null;
					this.focusPoints = null;
				}
			}
		}
		
		this.currentConstraint = null;
		this.currentPoint = null;
		
		if (this.focusIcons != null && this.constraints != null &&
			(me.getState() == null || this.currentFocus == me.getState()))
		{
			for (var i = 0; i < this.focusIcons.length; i++)
			{
				if (mxUtils.intersects(this.focusIcons[i].bounds, mouse))
				{
					this.currentConstraint = this.constraints[i];
					this.currentPoint = this.focusPoints[i];
					
					var tmp = this.focusIcons[i].bounds.clone();
					tmp.grow((mxClient.IS_IE) ? 3 : 2);
					
					if (mxClient.IS_IE)
					{
						tmp.width -= 1;
						tmp.height -= 1;
					}
					
					if (this.focusHighlight == null)
					{
						var hl = new mxRectangleShape(tmp, null, this.highlightColor, 3);
						hl.dialect = (this.graph.dialect == mxConstants.DIALECT_SVG) ?
									mxConstants.DIALECT_SVG :
									mxConstants.DIALECT_VML;
						hl.init(this.graph.getView().getOverlayPane());
						this.focusHighlight = hl;
						
						var getState = mxUtils.bind(this, function()
						{
							return (this.currentFocus != null) ? this.currentFocus : me.getState();
						});
	
						mxEvent.redirectMouseEvents(hl.node, this.graph, getState/*, mouseDown*/);
					}
					else
					{
						this.focusHighlight.bounds = tmp;
						this.focusHighlight.redraw();
					}
					
					break;
				}
			}
		}
		
		if (this.currentConstraint == null &&
			this.focusHighlight != null)
		{
			this.focusHighlight.destroy();
			this.focusHighlight = null;
		}
	}
};

/**
 * Function: destroy
 * 
 * Destroy this handler.
 */
mxConstraintHandler.prototype.destroy = function()
{
	this.reset();
};