/
jsPlumb-renderers-vml-1.4.0-RC1.js
445 lines (393 loc) · 15.4 KB
/
jsPlumb-renderers-vml-1.4.0-RC1.js
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* jsPlumb
*
* Title:jsPlumb 1.4.0
*
* Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
* elements, or VML.
*
* This file contains the VML renderers.
*
* Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
*
* http://jsplumb.org
* http://github.com/sporritt/jsplumb
* http://code.google.com/p/jsplumb
*
* Dual licensed under the MIT and GPL2 licenses.
*/
;(function() {
// http://ajaxian.com/archives/the-vml-changes-in-ie-8
// http://www.nczonline.net/blog/2010/01/19/internet-explorer-8-document-and-browser-modes/
// http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
var vmlAttributeMap = {
"stroke-linejoin":"joinstyle",
"joinstyle":"joinstyle",
"endcap":"endcap",
"miterlimit":"miterlimit"
},
jsPlumbStylesheet = null;
if (document.createStyleSheet && document.namespaces) {
var ruleClasses = [
".jsplumb_vml", "jsplumb\\:textbox", "jsplumb\\:oval", "jsplumb\\:rect",
"jsplumb\\:stroke", "jsplumb\\:shape", "jsplumb\\:group"
],
rule = "behavior:url(#default#VML);position:absolute;";
jsPlumbStylesheet = document.createStyleSheet();
for (var i = 0; i < ruleClasses.length; i++)
jsPlumbStylesheet.addRule(ruleClasses[i], rule);
// in this page it is also mentioned that IE requires the extra arg to the namespace
// http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
// but someone commented saying they didn't need it, and it seems jsPlumb doesnt need it either.
// var iev = document.documentMode;
//if (!iev || iev < 8)
document.namespaces.add("jsplumb", "urn:schemas-microsoft-com:vml");
//else
// document.namespaces.add("jsplumb", "urn:schemas-microsoft-com:vml", "#default#VML");
}
jsPlumb.vml = {};
var scale = 1000,
_groupMap = {},
_getGroup = function(container, connectorClass) {
var id = jsPlumb.getId(container),
g = _groupMap[id];
if(!g) {
g = _node("group", [0,0,scale, scale], {"class":connectorClass});
//g.style.position=absolute;
//g["coordsize"] = "1000,1000";
g.style.backgroundColor="red";
_groupMap[id] = g;
jsPlumb.appendElement(g, container); // todo if this gets reinstated, remember to use the current jsplumb instance.
//document.body.appendChild(g);
}
return g;
},
_atts = function(o, atts) {
for (var i in atts) {
// IE8 fix: setattribute does not work after an element has been added to the dom!
// http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
//o.setAttribute(i, atts[i]);
/*There is an additional problem when accessing VML elements by using get/setAttribute. The simple solution is following:
if (document.documentMode==8) {
ele.opacity=1;
} else {
ele.setAttribute(‘opacity’,1);
}
*/
o[i] = atts[i];
}
},
_node = function(name, d, atts, parent, _jsPlumb) {
atts = atts || {};
var o = document.createElement("jsplumb:" + name);
_jsPlumb.appendElement(o, parent);
o.className = (atts["class"] ? atts["class"] + " " : "") + "jsplumb_vml";
_pos(o, d);
_atts(o, atts);
return o;
},
_pos = function(o,d) {
o.style.left = d[0] + "px";
o.style.top = d[1] + "px";
o.style.width= d[2] + "px";
o.style.height= d[3] + "px";
o.style.position = "absolute";
},
_conv = jsPlumb.vml.convertValue = function(v) {
return Math.floor(v * scale);
},
// tests if the given style is "transparent" and then sets the appropriate opacity node to 0 if so,
// or 1 if not. TODO in the future, support variable opacity.
_maybeSetOpacity = function(styleToWrite, styleToCheck, type, component) {
if ("transparent" === styleToCheck)
component.setOpacity(type, "0.0");
else
component.setOpacity(type, "1.0");
},
_applyStyles = function(node, style, component, _jsPlumb) {
var styleToWrite = {};
if (style.strokeStyle) {
styleToWrite["stroked"] = "true";
var strokeColor = jsPlumbUtil.convertStyle(style.strokeStyle, true);
styleToWrite["strokecolor"] = strokeColor;
_maybeSetOpacity(styleToWrite, strokeColor, "stroke", component);
styleToWrite["strokeweight"] = style.lineWidth + "px";
}
else styleToWrite["stroked"] = "false";
if (style.fillStyle) {
styleToWrite["filled"] = "true";
var fillColor = jsPlumbUtil.convertStyle(style.fillStyle, true);
styleToWrite["fillcolor"] = fillColor;
_maybeSetOpacity(styleToWrite, fillColor, "fill", component);
}
else styleToWrite["filled"] = "false";
if(style["dashstyle"]) {
if (component.strokeNode == null) {
component.strokeNode = _node("stroke", [0,0,0,0], { dashstyle:style["dashstyle"] }, node, _jsPlumb);
}
else
component.strokeNode.dashstyle = style["dashstyle"];
}
else if (style["stroke-dasharray"] && style["lineWidth"]) {
var sep = style["stroke-dasharray"].indexOf(",") == -1 ? " " : ",",
parts = style["stroke-dasharray"].split(sep),
styleToUse = "";
for(var i = 0; i < parts.length; i++) {
styleToUse += (Math.floor(parts[i] / style.lineWidth) + sep);
}
if (component.strokeNode == null) {
component.strokeNode = _node("stroke", [0,0,0,0], { dashstyle:styleToUse }, node, _jsPlumb);
//node.appendChild(component.strokeNode);
}
else
component.strokeNode.dashstyle = styleToUse;
}
_atts(node, styleToWrite);
},
/*
* Base class for Vml endpoints and connectors. Extends jsPlumbUIComponent.
*/
VmlComponent = function() {
var self = this;
jsPlumb.jsPlumbUIComponent.apply(this, arguments);
this.opacityNodes = {
"stroke":null,
"fill":null
};
this.initOpacityNodes = function(vml) {
self.opacityNodes["stroke"] = _node("stroke", [0,0,1,1], {opacity:"0.0"}, vml, self._jsPlumb);
self.opacityNodes["fill"] = _node("fill", [0,0,1,1], {opacity:"0.0"}, vml, self._jsPlumb);
};
this.setOpacity = function(type, value) {
var node = self.opacityNodes[type];
if (node) node["opacity"] = "" + value;
};
var displayElements = [ ];
this.getDisplayElements = function() {
return displayElements;
};
this.appendDisplayElement = function(el, doNotAppendToCanvas) {
if (!doNotAppendToCanvas) self.canvas.parentNode.appendChild(el);
displayElements.push(el);
};
},
/*
* Base class for Vml connectors. extends VmlComponent.
*/
VmlConnector = jsPlumb.VmlConnector = function(params) {
var self = this;
self.strokeNode = null;
self.canvas = null;
VmlComponent.apply(this, arguments);
var clazz = self._jsPlumb.connectorClass + (params.cssClass ? (" " + params.cssClass) : "");
this.paint = function(d, style, anchor) {
if (style != null) {
var path = self.getPath(d), p = { "path":path };
//*
if (style.outlineColor) {
var outlineWidth = style.outlineWidth || 1,
outlineStrokeWidth = style.lineWidth + (2 * outlineWidth),
outlineStyle = {
strokeStyle : jsPlumbUtil.convertStyle(style.outlineColor),
lineWidth : outlineStrokeWidth
};
for (var aa in vmlAttributeMap) outlineStyle[aa] = style[aa];
if (self.bgCanvas == null) {
p["class"] = clazz;
p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
self.bgCanvas = _node("shape", d, p, params.parent, self._jsPlumb);
_pos(self.bgCanvas, d);
self.appendDisplayElement(self.bgCanvas, true);
self.attachListeners(self.bgCanvas, self);
self.initOpacityNodes(self.bgCanvas, ["stroke"]);
}
else {
p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
_pos(self.bgCanvas, d);
_atts(self.bgCanvas, p);
}
_applyStyles(self.bgCanvas, outlineStyle, self);
}
//*/
if (self.canvas == null) {
p["class"] = clazz;
p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
if (self.tooltip) p["label"] = self.tooltip;
self.canvas = _node("shape", d, p, params.parent, self._jsPlumb);
//var group = _getGroup(params.parent); // test of append everything to a group
//group.appendChild(self.canvas); // sort of works but not exactly;
//params["_jsPlumb"].appendElement(self.canvas, params.parent); //before introduction of groups
self.appendDisplayElement(self.canvas, true);
self.attachListeners(self.canvas, self);
self.initOpacityNodes(self.canvas, ["stroke"]);
}
else {
p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
_pos(self.canvas, d);
_atts(self.canvas, p);
}
_applyStyles(self.canvas, style, self, self._jsPlumb);
}
};
//self.appendDisplayElement(self.canvas);
this.reattachListeners = function() {
if (self.canvas) self.reattachListenersForElement(self.canvas, self);
};
},
/*
*
* Base class for Vml Endpoints. extends VmlComponent.
*
*/
VmlEndpoint = window.VmlEndpoint = function(params) {
VmlComponent.apply(this, arguments);
var vml = null, self = this, opacityStrokeNode = null, opacityFillNode = null;
self.canvas = document.createElement("div");
self.canvas.style["position"] = "absolute";
var clazz = self._jsPlumb.endpointClass + (params.cssClass ? (" " + params.cssClass) : "");
//var group = _getGroup(params.parent);
//group.appendChild(self.canvas);
params["_jsPlumb"].appendElement(self.canvas, params.parent);
if (self.tooltip) self.canvas.setAttribute("label", self.tooltip);
this.paint = function(d, style, anchor) {
var p = { };
jsPlumb.sizeCanvas(self.canvas, d[0], d[1], d[2], d[3]);
if (vml == null) {
p["class"] = clazz;
vml = self.getVml([0,0, d[2], d[3]], p, anchor, self.canvas, self._jsPlumb);
self.attachListeners(vml, self);
self.appendDisplayElement(vml, true);
self.appendDisplayElement(self.canvas, true);
self.initOpacityNodes(vml, ["fill"]);
}
else {
_pos(vml, [0,0, d[2], d[3]]);
_atts(vml, p);
}
_applyStyles(vml, style, self);
};
this.reattachListeners = function() {
if (vml) self.reattachListenersForElement(vml, self);
};
};
jsPlumb.Connectors.vml.Bezier = function() {
jsPlumb.Connectors.Bezier.apply(this, arguments);
VmlConnector.apply(this, arguments);
this.getPath = function(d) {
return "m" + _conv(d[4]) + "," + _conv(d[5]) +
" c" + _conv(d[8]) + "," + _conv(d[9]) + "," + _conv(d[10]) + "," + _conv(d[11]) + "," + _conv(d[6]) + "," + _conv(d[7]) + " e";
};
};
jsPlumb.Connectors.vml.Straight = function() {
jsPlumb.Connectors.Straight.apply(this, arguments);
VmlConnector.apply(this, arguments);
this.getPath = function(d) {
return "m" + _conv(d[4]) + "," + _conv(d[5]) + " l" + _conv(d[6]) + "," + _conv(d[7]) + " e";
};
};
jsPlumb.Connectors.vml.Flowchart = function() {
jsPlumb.Connectors.Flowchart.apply(this, arguments);
VmlConnector.apply(this, arguments);
this.getPath = function(dimensions) {
var p = "m " + _conv(dimensions[4]) + "," + _conv(dimensions[5]) + " l";
// loop through extra points
for (var i = 0; i < dimensions[8]; i++) {
p = p + " " + _conv(dimensions[9 + (i*2)]) + "," + _conv(dimensions[10 + (i*2)]);
}
// finally draw a line to the end
p = p + " " + _conv(dimensions[6]) + "," + _conv(dimensions[7]) + " e";
return p;
};
};
jsPlumb.Endpoints.vml.Dot = function() {
jsPlumb.Endpoints.Dot.apply(this, arguments);
VmlEndpoint.apply(this, arguments);
this.getVml = function(d, atts, anchor, parent, _jsPlumb) { return _node("oval", d, atts, parent, _jsPlumb); };
};
jsPlumb.Endpoints.vml.Rectangle = function() {
jsPlumb.Endpoints.Rectangle.apply(this, arguments);
VmlEndpoint.apply(this, arguments);
this.getVml = function(d, atts, anchor, parent, _jsPlumb) { return _node("rect", d, atts, parent, _jsPlumb); };
};
/*
* VML Image Endpoint is the same as the default image endpoint.
*/
jsPlumb.Endpoints.vml.Image = jsPlumb.Endpoints.Image;
/**
* placeholder for Blank endpoint in vml renderer.
*/
jsPlumb.Endpoints.vml.Blank = jsPlumb.Endpoints.Blank;
/**
* VML Label renderer. uses the default label renderer (which adds an element to the DOM)
*/
jsPlumb.Overlays.vml.Label = jsPlumb.Overlays.Label;
var AbstractVmlArrowOverlay = function(superclass, originalArgs) {
superclass.apply(this, originalArgs);
VmlComponent.apply(this, originalArgs);
var self = this, path = null;
self.canvas = null;
self.isAppendedAtTopLevel = true;
var getPath = function(d, connectorDimensions) {
return "m " + _conv(d.hxy.x) + "," + _conv(d.hxy.y) +
" l " + _conv(d.tail[0].x) + "," + _conv(d.tail[0].y) +
" " + _conv(d.cxy.x) + "," + _conv(d.cxy.y) +
" " + _conv(d.tail[1].x) + "," + _conv(d.tail[1].y) +
" x e";
};
this.paint = function(connector, d, lineWidth, strokeStyle, fillStyle, connectorDimensions) {
var p = {};
if (strokeStyle) {
p["stroked"] = "true";
p["strokecolor"] = jsPlumbUtil.convertStyle(strokeStyle, true);
}
if (lineWidth) p["strokeweight"] = lineWidth + "px";
if (fillStyle) {
p["filled"] = "true";
p["fillcolor"] = fillStyle;
}
var xmin = Math.min(d.hxy.x, d.tail[0].x, d.tail[1].x, d.cxy.x),
ymin = Math.min(d.hxy.y, d.tail[0].y, d.tail[1].y, d.cxy.y),
xmax = Math.max(d.hxy.x, d.tail[0].x, d.tail[1].x, d.cxy.x),
ymax = Math.max(d.hxy.y, d.tail[0].y, d.tail[1].y, d.cxy.y),
w = Math.abs(xmax - xmin),
h = Math.abs(ymax - ymin),
dim = [xmin, ymin, w, h];
// for VML, we create overlays using shapes that have the same dimensions and
// coordsize as their connector - overlays calculate themselves relative to the
// connector (it's how it's been done since the original canvas implementation, because
// for canvas that makes sense).
p["path"] = getPath(d, connectorDimensions);
p["coordsize"] = (connectorDimensions[2] * scale) + "," + (connectorDimensions[3] * scale);
dim[0] = connectorDimensions[0];
dim[1] = connectorDimensions[1];
dim[2] = connectorDimensions[2];
dim[3] = connectorDimensions[3];
if (self.canvas == null) {
//p["class"] = jsPlumb.overlayClass; // TODO currentInstance?
self.canvas = _node("shape", dim, p, connector.canvas.parentNode, connector._jsPlumb);
connector.appendDisplayElement(self.canvas, true);
self.attachListeners(self.canvas, connector);
self.attachListeners(self.canvas, self);
}
else {
_pos(self.canvas, dim);
_atts(self.canvas, p);
}
};
this.reattachListeners = function() {
if (self.canvas) self.reattachListenersForElement(self.canvas, self);
};
this.cleanup = function() {
if (self.canvas != null) jsPlumb.CurrentLibrary.removeElement(self.canvas);
};
};
jsPlumb.Overlays.vml.Arrow = function() {
AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.Arrow, arguments]);
};
jsPlumb.Overlays.vml.PlainArrow = function() {
AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.PlainArrow, arguments]);
};
jsPlumb.Overlays.vml.Diamond = function() {
AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.Diamond, arguments]);
};
})();