-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery.graph.js
More file actions
executable file
·337 lines (294 loc) · 8.44 KB
/
jquery.graph.js
File metadata and controls
executable file
·337 lines (294 loc) · 8.44 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Copyright (c) 2011 Jasper A. Visser, https://github.com/jasperavisser/jquery.graph/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*jslint browser: true, plusplus: true */
/*global jQuery: true */
(function ($) {
"use strict";
/**
* Get the containing block or blocks for the selected elements.
*
* @returns jQuery Containing blocks of selected elements.
*/
$.fn.containingBlock = function () {
var blocks = $();
this.each(function () {
var element = $(this).parent();
while (element) {
if (element.is("body") || ["absolute", "fixed"].indexOf(element.css("position")) !== false) {
blocks = blocks.add(element);
break;
}
element = element.parent();
}
});
return blocks;
};
/**
* Create a directed graph layer from an HTML canvas element.
*
* @param settings object Settings to use for the graph.
*
* @returns jQuery Reference to self.
*/
$.fn.graph = function (settings) {
var that, edgeRenderers;
// standard collection of edge renderers
edgeRenderers = {
/**
* Render a bezier edge between the bottom-center of the source
* element and the top-center of the target element.
*
* @param canvas jQuery Canvas element to draw on.
* @param source jQuery Source element
* @param target jQuery Target element
* @param color string Color to draw this edge in.
*
* @returns undefined
*/
bezier : function (canvas, source, target, color) {
var sourceVertex, targetVertex, vertices, context;
// get bezier vertices
sourceVertex = {
x : source.position().left + source.width() / 2,
y : source.position().top + source.height()
};
targetVertex = {
x : target.position().left + target.width() / 2,
y : target.position().top
};
vertices = [
sourceVertex,
{ x : sourceVertex.x, y : sourceVertex.y + 100 },
{ x : targetVertex.x, y : targetVertex.y - 100 },
targetVertex
];
// the context
context = canvas[0].getContext("2d");
// draw bezier
context.moveTo(vertices[0].x, vertices[0].y);
context.bezierCurveTo(
vertices[1].x,
vertices[1].y,
vertices[2].x,
vertices[2].y,
vertices[3].x,
vertices[3].y
);
context.lineWidth = 1;
context.strokeStyle = color;
context.stroke();
// draw knob
context.beginPath();
context.arc(vertices[3].x, vertices[3].y, 4, 0, Math.PI * 2, true);
context.closePath();
context.strokeStyle = color;
context.stroke();
context.fillStyle = color;
context.fill();
},
/**
* Render a straight edge between the bottom-center of the source
* element and the top-center of the target element.
*
* @param canvas jQuery Canvas element to draw on.
* @param source jQuery Source element
* @param target jQuery Target element
* @param color string Color to draw this edge in.
*
* @returns undefined
*/
straight : function (canvas, source, target, color) {
var sourceVertex, targetVertex, context;
// get vertices
sourceVertex = {
x : source.position().left + source.width() / 2,
y : source.position().top + source.height()
};
targetVertex = {
x : target.position().left + target.width() / 2,
y : target.position().top
};
// the context
context = canvas[0].getContext("2d");
// draw bezier
context.moveTo(sourceVertex.x, sourceVertex.y);
context.lineTo(targetVertex.x, targetVertex.y);
context.lineWidth = 1;
context.strokeStyle = color;
context.stroke();
}
};
/**
* Get the edge rendering function for the given input.
*
* @param renderer string or function Name of the edge renderer, or an
* edge-rendering function.
*
* @returns function Edge-rendering function.
*/
function getEdgeRenderer(renderer) {
if (typeof renderer === "function") {
return renderer;
}
switch (renderer) {
case "bezier":
return edgeRenderers.bezier;
default:
return edgeRenderers.straight;
}
}
/**
* Render edges from all source to all targets in the given group.
*
* @param i int Index of the edge group to render.
*
* @returns undefined
*/
that = this;
function renderEdgeGroup(i) {
// for each source
that.edgeGroups[i].sources.each(function () {
var source = $(this);
// for each target
that.edgeGroups[i].targets.each(function () {
// render an edge
getEdgeRenderer(that.settings.renderEdge)(that.canvas, source, $(this), that.edgeGroups[i].color);
});
});
}
/**
* Render edges for all groups.
*
* @returns undefined
*/
that = this;
function renderEdgeGroups() {
var j;
// redraw all edges
for (j = 0; j < that.edgeGroups.length; j++) {
renderEdgeGroup(j);
}
}
// default settings
this.defaults = {
draggableNodes : true,
renderEdge : "bezier",
zindex : 1
};
// given settings override default settings
this.settings = $.extend({}, this.defaults, settings);
// create canvas
this.canvas = $("<canvas>")
.css("position", "absolute")
.css("height", "100%")
.css("left", "0px")
.css("top", "0px")
.css("width", "100%")
.css("z-index", this.settings.zindex);
this.append(this.canvas);
// groups of edges between source & target nodes
this.edgeGroups = [];
// set of node elements
this.nodes = $();
/**
* Clear all edges from the graph.
*
* @returns jQuery Reference to self.
*/
this.fnClear = function () {
this.edgeGroups = [];
return this;
};
/**
* Add edges to the graph from all given sources to all given targets.
*
* @param sources array(HTMLElement) or jQuery or selector Any number of source elements.
* @param targets array(HTMLElement) or jQuery or selector Any number of target elements.
*
* @returns jQuery Reference to self.
*/
that = this;
this.fnEdge = function (sources, targets, color) {
if (color === undefined) {
color = "black";
}
return this.each(function () {
var i = that.edgeGroups.push({
sources : $(sources),
targets : $(targets),
color : color
}) - 1;
renderEdgeGroup(i);
});
};
/**
* Add the given element to the graph as a node.
*
* @param element HTMLElement or jQuery or selector Any number of elements that serve as
* nodes in the graph.
*
* @returns jQuery Reference to self.
*/
that = this;
this.fnNode = function (element) {
that.nodes = that.nodes.add(element);
return this;
};
/**
* Render the graph.
*
* @returns jQuery Reference to self.
*/
this.fnRender = function () {
// resize canvas
this.canvas[0].width = this.canvas.width();
this.canvas[0].height = this.canvas.height();
// redraw all edges
renderEdgeGroups();
return this;
};
// initialize graph
that = this;
return this.each(function () {
// if the containing block is body, re-render graph when window is resized
if ($(this).is("body")) {
$(window).bind("resize.graph", function () {
that.fnRender();
});
}
// when a node is dragged, re-render graph
if (that.settings.draggableNodes) {
$(window)
.delegate("*", "drag.graph", function () {
if (that.nodes.is(this)) {
that.fnRender();
}
})
.delegate("*", "dragstop.graph", function () {
if (that.nodes.is(this)) {
that.fnRender();
}
});
}
// render graph now
that.fnRender();
});
};
}(jQuery));