jsPlumb-renderers-vml-1.3.4-RC1.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * jsPlumb
  3. *
  4. * Title:jsPlumb 1.3.5
  5. *
  6. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  7. * elements, or VML.
  8. *
  9. * This file contains the VML renderers.
  10. *
  11. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  12. *
  13. * http://jsplumb.org
  14. * http://github.com/sporritt/jsplumb
  15. * http://code.google.com/p/jsplumb
  16. *
  17. * Dual licensed under the MIT and GPL2 licenses.
  18. */
  19. ;(function() {
  20. // http://ajaxian.com/archives/the-vml-changes-in-ie-8
  21. // http://www.nczonline.net/blog/2010/01/19/internet-explorer-8-document-and-browser-modes/
  22. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  23. var vmlAttributeMap = {
  24. "stroke-linejoin":"joinstyle",
  25. "joinstyle":"joinstyle",
  26. "endcap":"endcap",
  27. "miterlimit":"miterlimit"
  28. };
  29. if (document.createStyleSheet) {
  30. // this is the style rule for IE7/6: it uses a CSS class, tidy.
  31. document.createStyleSheet().addRule(".jsplumb_vml", "behavior:url(#default#VML);position:absolute;");
  32. // these are for VML in IE8. you have to explicitly call out which elements
  33. // you're going to expect to support VML!
  34. //
  35. document.createStyleSheet().addRule("jsplumb\\:textbox", "behavior:url(#default#VML);position:absolute;");
  36. document.createStyleSheet().addRule("jsplumb\\:oval", "behavior:url(#default#VML);position:absolute;");
  37. document.createStyleSheet().addRule("jsplumb\\:rect", "behavior:url(#default#VML);position:absolute;");
  38. document.createStyleSheet().addRule("jsplumb\\:stroke", "behavior:url(#default#VML);position:absolute;");
  39. document.createStyleSheet().addRule("jsplumb\\:shape", "behavior:url(#default#VML);position:absolute;");
  40. // in this page it is also mentioned that IE requires the extra arg to the namespace
  41. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  42. // but someone commented saying they didn't need it, and it seems jsPlumb doesnt need it either.
  43. // var iev = document.documentMode;
  44. //if (!iev || iev < 8)
  45. document.namespaces.add("jsplumb", "urn:schemas-microsoft-com:vml");
  46. //else
  47. // document.namespaces.add("jsplumb", "urn:schemas-microsoft-com:vml", "#default#VML");
  48. }
  49. jsPlumb.vml = {};
  50. var scale = 1000,
  51. _atts = function(o, atts) {
  52. for (var i in atts) {
  53. // IE8 fix: setattribute does not work after an element has been added to the dom!
  54. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  55. //o.setAttribute(i, atts[i]);
  56. o[i] = atts[i];
  57. }
  58. },
  59. _node = function(name, d, atts) {
  60. atts = atts || {};
  61. var o = document.createElement("jsplumb:" + name);
  62. o.className = (atts["class"] ? atts["class"] + " " : "") + "jsplumb_vml";
  63. _pos(o, d);
  64. _atts(o, atts);
  65. return o;
  66. },
  67. _pos = function(o,d) {
  68. o.style.left = d[0] + "px";
  69. o.style.top = d[1] + "px";
  70. o.style.width= d[2] + "px";
  71. o.style.height= d[3] + "px";
  72. o.style.position = "absolute";
  73. },
  74. _conv = jsPlumb.vml.convertValue = function(v) {
  75. return Math.floor(v * scale);
  76. },
  77. // tests if the given style is "transparent" and then sets the appropriate opacity node to 0 if so,
  78. // or 1 if not. TODO in the future, support variable opacity.
  79. _maybeSetOpacity = function(styleToWrite, styleToCheck, type, component) {
  80. if ("transparent" === styleToCheck)
  81. component.setOpacity(type, "0.0");
  82. else
  83. component.setOpacity(type, "1.0");
  84. },
  85. _applyStyles = function(node, style, component) {
  86. var styleToWrite = {};
  87. if (style.strokeStyle) {
  88. styleToWrite["stroked"] = "true";
  89. var strokeColor = jsPlumb.util.convertStyle(style.strokeStyle, true);
  90. styleToWrite["strokecolor"] = strokeColor;
  91. _maybeSetOpacity(styleToWrite, strokeColor, "stroke", component);
  92. styleToWrite["strokeweight"] = style.lineWidth + "px";
  93. }
  94. else styleToWrite["stroked"] = "false";
  95. if (style.fillStyle) {
  96. styleToWrite["filled"] = "true";
  97. var fillColor = jsPlumb.util.convertStyle(style.fillStyle, true);
  98. styleToWrite["fillcolor"] = fillColor;
  99. _maybeSetOpacity(styleToWrite, fillColor, "fill", component);
  100. }
  101. else styleToWrite["filled"] = "false";
  102. if(style["dashstyle"]) {
  103. if (component.strokeNode == null) {
  104. component.strokeNode = _node("stroke", [0,0,0,0], { dashstyle:style["dashstyle"] });
  105. node.appendChild(component.strokeNode);
  106. }
  107. else
  108. component.strokeNode.dashstyle = style["dashstyle"];
  109. }
  110. else if (style["stroke-dasharray"] && style["lineWidth"]) {
  111. var sep = style["stroke-dasharray"].indexOf(",") == -1 ? " " : ",",
  112. parts = style["stroke-dasharray"].split(sep),
  113. styleToUse = "";
  114. for(var i = 0; i < parts.length; i++) {
  115. styleToUse += (Math.floor(parts[i] / style.lineWidth) + sep);
  116. }
  117. if (component.strokeNode == null) {
  118. component.strokeNode = _node("stroke", [0,0,0,0], { dashstyle:styleToUse });
  119. node.appendChild(component.strokeNode);
  120. }
  121. else
  122. component.strokeNode.dashstyle = styleToUse;
  123. }
  124. _atts(node, styleToWrite);
  125. },
  126. /*
  127. * Base class for Vml endpoints and connectors. Extends jsPlumbUIComponent.
  128. */
  129. VmlComponent = function() {
  130. var self = this;
  131. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  132. this.opacityNodes = {
  133. "stroke":null,
  134. "fill":null
  135. };
  136. this.initOpacityNodes = function(vml) {
  137. self.opacityNodes["stroke"] = _node("stroke", [0,0,1,1], {opacity:"0.0"});
  138. self.opacityNodes["fill"] = _node("fill", [0,0,1,1], {opacity:"0.0"});
  139. vml.appendChild(self.opacityNodes["stroke"]);
  140. vml.appendChild(self.opacityNodes["fill"]);
  141. };
  142. this.setOpacity = function(type, value) {
  143. var node = self.opacityNodes[type];
  144. if (node) node["opacity"] = "" + value;
  145. };
  146. },
  147. /*
  148. * Base class for Vml connectors. extends VmlComponent.
  149. */
  150. VmlConnector = jsPlumb.VmlConnector = function(params) {
  151. var self = this;
  152. self.strokeNode = null;
  153. self.canvas = null;
  154. VmlComponent.apply(this, arguments);
  155. var clazz = self._jsPlumb.connectorClass + (params.cssClass ? (" " + params.cssClass) : "");
  156. this.paint = function(d, style, anchor) {
  157. if (style != null) {
  158. var path = self.getPath(d), p = { "path":path };
  159. if (style.outlineColor) {
  160. var outlineWidth = style.outlineWidth || 1,
  161. outlineStrokeWidth = style.lineWidth + (2 * outlineWidth),
  162. outlineStyle = {
  163. strokeStyle : jsPlumb.util.convertStyle(style.outlineColor),
  164. lineWidth : outlineStrokeWidth
  165. };
  166. for (var aa in vmlAttributeMap) outlineStyle[aa] = style[aa];
  167. if (self.bgCanvas == null) {
  168. p["class"] = clazz;
  169. p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
  170. self.bgCanvas = _node("shape", d, p);
  171. jsPlumb.appendElement(self.bgCanvas, params.parent);
  172. _pos(self.bgCanvas, d);
  173. displayElements.push(self.bgCanvas);
  174. }
  175. else {
  176. p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
  177. _pos(self.bgCanvas, d);
  178. _atts(self.bgCanvas, p);
  179. }
  180. _applyStyles(self.bgCanvas, outlineStyle, self);
  181. }
  182. if (self.canvas == null) {
  183. p["class"] = clazz;
  184. p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
  185. if (self.tooltip) p["label"] = self.tooltip;
  186. self.canvas = _node("shape", d, p);
  187. jsPlumb.appendElement(self.canvas, params.parent);
  188. displayElements.push(self.canvas);
  189. self.attachListeners(self.canvas, self);
  190. self.initOpacityNodes(self.canvas, ["stroke"]);
  191. }
  192. else {
  193. p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
  194. _pos(self.canvas, d);
  195. _atts(self.canvas, p);
  196. }
  197. _applyStyles(self.canvas, style, self);
  198. }
  199. };
  200. var displayElements = [ self.canvas ];
  201. this.getDisplayElements = function() {
  202. return displayElements;
  203. };
  204. this.appendDisplayElement = function(el) {
  205. self.canvas.parentNode.appendChild(el);
  206. displayElements.push(el);
  207. };
  208. this.reattachListeners = function() {
  209. if (self.canvas) self.reattachListenersForElement(self.canvas, self);
  210. };
  211. },
  212. /*
  213. *
  214. * Base class for Vml Endpoints. extends VmlComponent.
  215. *
  216. */
  217. VmlEndpoint = function(params) {
  218. VmlComponent.apply(this, arguments);
  219. var vml = null, self = this, opacityStrokeNode = null, opacityFillNode = null;
  220. self.canvas = document.createElement("div");
  221. self.canvas.style["position"] = "absolute";
  222. jsPlumb.appendElement(self.canvas, params.parent);
  223. if (self.tooltip) self.canvas.setAttribute("label", self.tooltip);
  224. this.paint = function(d, style, anchor) {
  225. var p = { };
  226. jsPlumb.sizeCanvas(self.canvas, d[0], d[1], d[2], d[3]);
  227. if (vml == null) {
  228. p["class"] = jsPlumb.endpointClass;
  229. vml = self.getVml([0,0, d[2], d[3]], p, anchor);
  230. self.canvas.appendChild(vml);
  231. self.attachListeners(vml, self);
  232. self.initOpacityNodes(vml, ["fill"]);
  233. }
  234. else {
  235. //p["coordsize"] = "1,1";//(d[2] * scale) + "," + (d[3] * scale); again, unsure.
  236. _pos(vml, [0,0, d[2], d[3]]);
  237. _atts(vml, p);
  238. }
  239. _applyStyles(vml, style, self);
  240. };
  241. this.reattachListeners = function() {
  242. if (vml) self.reattachListenersForElement(vml, self);
  243. };
  244. };
  245. jsPlumb.Connectors.vml.Bezier = function() {
  246. jsPlumb.Connectors.Bezier.apply(this, arguments);
  247. VmlConnector.apply(this, arguments);
  248. this.getPath = function(d) {
  249. return "m" + _conv(d[4]) + "," + _conv(d[5]) +
  250. " c" + _conv(d[8]) + "," + _conv(d[9]) + "," + _conv(d[10]) + "," + _conv(d[11]) + "," + _conv(d[6]) + "," + _conv(d[7]) + " e";
  251. };
  252. };
  253. jsPlumb.Connectors.vml.Straight = function() {
  254. jsPlumb.Connectors.Straight.apply(this, arguments);
  255. VmlConnector.apply(this, arguments);
  256. this.getPath = function(d) {
  257. return "m" + _conv(d[4]) + "," + _conv(d[5]) + " l" + _conv(d[6]) + "," + _conv(d[7]) + " e";
  258. };
  259. };
  260. jsPlumb.Connectors.vml.Flowchart = function() {
  261. jsPlumb.Connectors.Flowchart.apply(this, arguments);
  262. VmlConnector.apply(this, arguments);
  263. this.getPath = function(dimensions) {
  264. var p = "m " + _conv(dimensions[4]) + "," + _conv(dimensions[5]) + " l";
  265. // loop through extra points
  266. for (var i = 0; i < dimensions[8]; i++) {
  267. p = p + " " + _conv(dimensions[9 + (i*2)]) + "," + _conv(dimensions[10 + (i*2)]);
  268. }
  269. // finally draw a line to the end
  270. p = p + " " + _conv(dimensions[6]) + "," + _conv(dimensions[7]) + " e";
  271. return p;
  272. };
  273. };
  274. jsPlumb.Endpoints.vml.Dot = function() {
  275. jsPlumb.Endpoints.Dot.apply(this, arguments);
  276. VmlEndpoint.apply(this, arguments);
  277. this.getVml = function(d, atts, anchor) { return _node("oval", d, atts); };
  278. };
  279. jsPlumb.Endpoints.vml.Rectangle = function() {
  280. jsPlumb.Endpoints.Rectangle.apply(this, arguments);
  281. VmlEndpoint.apply(this, arguments);
  282. this.getVml = function(d, atts, anchor) { return _node("rect", d, atts); };
  283. };
  284. /*
  285. * VML Image Endpoint is the same as the default image endpoint.
  286. */
  287. jsPlumb.Endpoints.vml.Image = jsPlumb.Endpoints.Image;
  288. /**
  289. * placeholder for Blank endpoint in vml renderer.
  290. */
  291. jsPlumb.Endpoints.vml.Blank = jsPlumb.Endpoints.Blank;
  292. /**
  293. * VML Label renderer. uses the default label renderer (which adds an element to the DOM)
  294. */
  295. jsPlumb.Overlays.vml.Label = jsPlumb.Overlays.Label;
  296. var AbstractVmlArrowOverlay = function(superclass, originalArgs) {
  297. superclass.apply(this, originalArgs);
  298. VmlComponent.apply(this, arguments);
  299. var self = this, path = null;
  300. self.canvas = null;
  301. var getPath = function(d, connectorDimensions) {
  302. return "m " + _conv(d.hxy.x) + "," + _conv(d.hxy.y) +
  303. " l " + _conv(d.tail[0].x) + "," + _conv(d.tail[0].y) +
  304. " " + _conv(d.cxy.x) + "," + _conv(d.cxy.y) +
  305. " " + _conv(d.tail[1].x) + "," + _conv(d.tail[1].y) +
  306. " x e";
  307. };
  308. this.paint = function(connector, d, lineWidth, strokeStyle, fillStyle, connectorDimensions) {
  309. var p = {};
  310. if (strokeStyle) {
  311. p["stroked"] = "true";
  312. p["strokecolor"] = jsPlumb.util.convertStyle(strokeStyle, true);
  313. }
  314. if (lineWidth) p["strokeweight"] = lineWidth + "px";
  315. if (fillStyle) {
  316. p["filled"] = "true";
  317. p["fillcolor"] = fillStyle;
  318. }
  319. var xmin = Math.min(d.hxy.x, d.tail[0].x, d.tail[1].x, d.cxy.x),
  320. ymin = Math.min(d.hxy.y, d.tail[0].y, d.tail[1].y, d.cxy.y),
  321. xmax = Math.max(d.hxy.x, d.tail[0].x, d.tail[1].x, d.cxy.x),
  322. ymax = Math.max(d.hxy.y, d.tail[0].y, d.tail[1].y, d.cxy.y),
  323. w = Math.abs(xmax - xmin),
  324. h = Math.abs(ymax - ymin),
  325. dim = [xmin, ymin, w, h];
  326. // for VML, we create overlays using shapes that have the same dimensions and
  327. // coordsize as their connector - overlays calculate themselves relative to the
  328. // connector (it's how it's been done since the original canvas implementation, because
  329. // for canvas that makes sense).
  330. p["path"] = getPath(d, connectorDimensions);
  331. p["coordsize"] = (connectorDimensions[2] * scale) + "," + (connectorDimensions[3] * scale);
  332. dim[0] = connectorDimensions[0];
  333. dim[1] = connectorDimensions[1];
  334. dim[2] = connectorDimensions[2];
  335. dim[3] = connectorDimensions[3];
  336. if (self.canvas == null) {
  337. //p["class"] = jsPlumb.overlayClass; // TODO currentInstance?
  338. self.canvas = _node("shape", dim, p);
  339. connector.appendDisplayElement(self.canvas);
  340. self.attachListeners(self.canvas, connector);
  341. }
  342. else {
  343. _pos(self.canvas, dim);
  344. _atts(self.canvas, p);
  345. }
  346. };
  347. this.reattachListeners = function() {
  348. if (self.canvas) self.reattachListenersForElement(self.canvas, self);
  349. };
  350. };
  351. jsPlumb.Overlays.vml.Arrow = function() {
  352. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.Arrow, arguments]);
  353. };
  354. jsPlumb.Overlays.vml.PlainArrow = function() {
  355. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.PlainArrow, arguments]);
  356. };
  357. jsPlumb.Overlays.vml.Diamond = function() {
  358. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.Diamond, arguments]);
  359. };
  360. })();