jsPlumb-renderers-vml-1.3.3-RC1.js 12 KB

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