jsPlumb-tests.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. var _getContextNode = function() {
  2. return $("#container");
  3. };
  4. var assertContextExists = function() {
  5. ok(_getContextNode().length == 1, 'context node exists');
  6. };
  7. var assertContextSize = function(elementCount) {
  8. equals(_getContextNode().children().length, elementCount, 'context has ' + elementCount + ' children');
  9. };
  10. var assertContextEmpty = function() {
  11. equals(_getContextNode().children.length, 0, 'context empty');
  12. };
  13. var assertEndpointCount = function(elId, count) {
  14. equals(jsPlumb.getTestHarness().endpointCount(elId), count, elId + " has " + count + (count > 1) ? "endpoints" : "endpoint");
  15. };
  16. var assertConnectionCount = function(endpoint, count) {
  17. equals(endpoint.connections.length, count, "endpoint has " + count + " connections");
  18. };
  19. var assertConnectionByScopeCount = function(scope, count) {
  20. equals(jsPlumb.getTestHarness().connectionCount(scope), count, 'Scope ' + scope + " has " + count + (count > 1) ? "connections" : "connection");
  21. };
  22. var _divs = [];
  23. var _addDiv = function(id) {
  24. var d1 = document.createElement("div");
  25. document.body.appendChild(d1);
  26. $(d1).attr("id", id);
  27. _divs.push(id);
  28. return $(d1);
  29. };
  30. var _cleanup = function() {
  31. for (var i in _divs) {
  32. $("#" + _divs[i]).remove();
  33. }
  34. _divs.splice(0, _divs.length - 1);
  35. jsPlumb.reset();
  36. };
  37. $(document).ready(function() {
  38. module("jsPlumb", {teardown: _cleanup});
  39. // setup the container
  40. var container = document.createElement("div");
  41. container.id = "container";
  42. document.body.appendChild(container);
  43. jsPlumb.Defaults.Container = "container";
  44. test('findIndex method', function() {
  45. var array = [ 1,2,3, "test", "a string", { 'foo':'bar', 'baz':1 }, { 'ding':'dong' } ];
  46. equals(jsPlumb.getTestHarness().findIndex(array, 1), 0, "find works for integers");
  47. equals(jsPlumb.getTestHarness().findIndex(array, "test"), 3, "find works for strings");
  48. equals(jsPlumb.getTestHarness().findIndex(array, { 'foo':'bar', 'baz':1 }), 5, "find works for objects");
  49. equals(jsPlumb.getTestHarness().findIndex(array, { 'ding':'dong', 'baz':1 }), -1, "find works properly for objects (objects have different length but some same properties)");
  50. });
  51. test('jsPlumb setup', function() {
  52. ok(jsPlumb, "loaded");
  53. ok($.fn.plumb, "plumb function");
  54. ok($.fn.detach, "detach");
  55. ok($.fn.detachAll, "detachAll");
  56. ok($.fn.addEndpoint, "addEndpoint");
  57. ok($.fn.addEndpoints, "addEndpoints");
  58. });
  59. test('getId', function() {
  60. var d10 = _addDiv('d10');
  61. equals(jsPlumb.getTestHarness().getId(d10), "d10");
  62. });
  63. test('plumb two divs with default options', function() {
  64. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  65. $(d2).plumb({target:"d1"});
  66. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  67. assertContextSize(3); // two endpoint canvases and a connection.
  68. assertEndpointCount("d1", 1);
  69. assertEndpointCount("d2", 1);
  70. });
  71. test('create a simple endpoint', function() {
  72. var d1 = _addDiv("d1");
  73. var e = $("#d1").addEndpoint({});
  74. ok(e, 'endpoint exists');
  75. assertEndpointCount("d1", 1);
  76. });
  77. test('create and remove a simple endpoint', function() {
  78. var d1 = _addDiv("d1");
  79. var ee = $("#d1").addEndpoint({uuid:"78978597593"});
  80. ok(ee != null, "endpoint exists");
  81. assertEndpointCount("d1", 1);
  82. assertContextSize(1); // one Endpoint canvas.
  83. jsPlumb.removeEndpoint(d1, ee);
  84. assertEndpointCount("d1", 0);
  85. var e = jsPlumb.getEndpoint("78978597593");
  86. equals(e, null, "the endpoint has been deleted");
  87. assertContextSize(0); // no Endpoint canvases.
  88. });
  89. test('draggable silently ignored when jquery ui not present', function() {
  90. var d1 = _addDiv("d1");
  91. var e = jsPlumb.addEndpoint(d1, {isSource:true});
  92. ok(e, 'endpoint exists');
  93. });
  94. test('droppable silently ignored when jquery ui not present', function() {
  95. var d1 = _addDiv("d1")
  96. var e = jsPlumb.addEndpoint(d1, {isTarget:true});
  97. ok(e, 'endpoint exists');
  98. });
  99. test('defaultEndpointMaxConnections', function() {
  100. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  101. var e3 = jsPlumb.addEndpoint(d3, {isSource:true});
  102. ok(e3.anchor, 'endpoint 3 has an anchor');
  103. var e4 = jsPlumb.addEndpoint(d4, {isSource:true});
  104. assertEndpointCount("d3", 1);
  105. assertEndpointCount("d4", 1);
  106. assertContextSize(2); // one canvas for each of our two endpoints.
  107. ok(!e3.isFull(), "endpoint 3 is not full.");
  108. jsPlumb.connect({source:d3, target:'d4', sourceEndpoint:e3, targetEndpoint:e4});
  109. assertConnectionCount(e3, 1); // we have one connection
  110. jsPlumb.connect({source:d3, target:'d4', sourceEndpoint:e3, targetEndpoint:e4});
  111. assertConnectionCount(e3, 1); // should have refused the connection; default max is 1.
  112. assertContextSize(3); // now we have one more canvas, for the Connection that was accepted.
  113. });
  114. test('specifiedEndpointMaxConnections', function() {
  115. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  116. var e5 = jsPlumb.addEndpoint(d5, {isSource:true, maxConnections:3});
  117. ok(e5.anchor, 'endpoint 5 has an anchor');
  118. var e6 = jsPlumb.addEndpoint(d6, {isSource:true, maxConnections:2}); // this one has max TWO
  119. assertEndpointCount("d5", 1); assertEndpointCount("d6", 1);
  120. assertContextSize(2);
  121. ok(!e5.isFull(), "endpoint 5 is not full.");
  122. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  123. assertConnectionCount(e5, 1); // we have one connection
  124. assertContextSize(3);
  125. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  126. assertConnectionCount(e5, 2); // two connections
  127. assertContextSize(4);
  128. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  129. assertConnectionCount(e5, 2); // should have refused; max is 2, for d4.
  130. assertContextSize(4);
  131. });
  132. test('noEndpointMaxConnections', function() {
  133. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  134. var e3 = jsPlumb.addEndpoint(d3, {isSource:true, maxConnections:-1});
  135. var e4 = jsPlumb.addEndpoint(d4, {isSource:true, maxConnections:-1});
  136. jsPlumb.connect({sourceEndpoint:e3, targetEndpoint:e4});
  137. assertContextSize(3);
  138. assertConnectionCount(e3, 1); // we have one connection
  139. jsPlumb.connect({sourceEndpoint:e3, targetEndpoint:e4});
  140. assertConnectionCount(e3, 2); // we have two. etc (default was one. this proves max is working).
  141. assertContextSize(4);
  142. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  143. var e5 = jsPlumb.addEndpoint(d3, {isSource:true, maxConnections:-1});
  144. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e4});
  145. assertConnectionCount(e4, 3);
  146. assertContextSize(6);
  147. });
  148. test('anchors equal', function() {
  149. var a1 = jsPlumb.makeAnchor(0, 1, 1, 1);
  150. var a2 = jsPlumb.makeAnchor(0, 1, 1, 1);
  151. ok(a1.equals(a2), "anchors are the same");
  152. });
  153. test('anchors equal with offsets', function() {
  154. var a1 = jsPlumb.makeAnchor(0, 1, 1, 1, 10, 13);
  155. var a2 = jsPlumb.makeAnchor(0, 1, 1, 1, 10, 13);
  156. ok(a1.equals(a2), "anchors are the same");
  157. });
  158. test('anchors not equal', function() {
  159. var a1 = jsPlumb.makeAnchor(0, 1, 0, 1);
  160. var a2 = jsPlumb.makeAnchor(0, 1, 1, 1);
  161. ok(!a1.equals(a2), "anchors are different");
  162. });
  163. test('anchor not equal with offsets', function() {
  164. var a1 = jsPlumb.makeAnchor(0, 1, 1, 1, 10, 13);
  165. var a2 = jsPlumb.makeAnchor(0, 1, 1, 1);
  166. ok(!a1.equals(a2), "anchors are different");
  167. });
  168. test('detach plays nice when no target given', function() {
  169. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  170. jsPlumb.connect({source:d3, target:d4});
  171. jsPlumb.detach();
  172. });
  173. //TODO make sure you run this test with a single detach call, to ensure that
  174. // single detach calls result in the connection being removed. detachEverything can
  175. // just blow away the connectionsByScope array and recreate it.
  176. test('jsPlumb.getConnections (simple case, default scope)', function() {
  177. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  178. jsPlumb.connect({source:d5, target:d6});
  179. assertContextSize(3);
  180. var c = jsPlumb.getConnections(); // will get all connections
  181. equals(c[jsPlumb.getDefaultScope()].length, 1, "there is one connection");
  182. });
  183. test('jsPlumb.getConnections (simple case, default scope; detach by element id)', function() {
  184. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  185. jsPlumb.connect({source:d5, target:d6});
  186. jsPlumb.connect({source:d6, target:d7});
  187. assertContextSize(6);
  188. var c = jsPlumb.getConnections(); // will get all connections
  189. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  190. jsPlumb.detach('d5', 'd6');
  191. c = jsPlumb.getConnections(); // will get all connections
  192. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  193. assertContextSize(5);
  194. });
  195. test('jsPlumb.getConnections (simple case, default scope; detach by element id using params object)', function() {
  196. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  197. jsPlumb.connect({source:d5, target:d6});
  198. jsPlumb.connect({source:d6, target:d7});
  199. assertContextSize(6);
  200. var c = jsPlumb.getConnections(); // will get all connections
  201. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  202. jsPlumb.detach({source:'d5', target:'d6'});
  203. c = jsPlumb.getConnections(); // will get all connections
  204. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  205. assertContextSize(5);
  206. });
  207. test('jsPlumb.getConnections (simple case, default scope; detach by element object)', function() {
  208. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  209. jsPlumb.connect({source:d5, target:d6});
  210. jsPlumb.connect({source:d6, target:d7});
  211. assertContextSize(6);
  212. var c = jsPlumb.getConnections(); // will get all connections
  213. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  214. jsPlumb.detach(d5, d6);
  215. c = jsPlumb.getConnections(); // will get all connections
  216. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  217. assertContextSize(5);
  218. });
  219. test('jsPlumb.getConnections (simple case, default scope; detach by element object using params object)', function() {
  220. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  221. jsPlumb.connect({source:d5, target:d6});
  222. jsPlumb.connect({source:d6, target:d7});
  223. assertContextSize(6);
  224. var c = jsPlumb.getConnections(); // will get all connections
  225. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  226. jsPlumb.detach({source:d5, target:d6});
  227. c = jsPlumb.getConnections(); // will get all connections
  228. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  229. assertContextSize(5);
  230. });
  231. test('jsPlumb.getConnections (simple case, default scope; detach by Connection)', function() {
  232. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  233. var c56 = jsPlumb.connect({source:d5, target:d6});
  234. var c67 = jsPlumb.connect({source:d6, target:d7});
  235. assertContextSize(6);
  236. var c = jsPlumb.getConnections(); // will get all connections
  237. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  238. jsPlumb.detach(c56);
  239. assertContextSize(5); // check that the connection canvas was removed.
  240. c = jsPlumb.getConnections(); // will get all connections
  241. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  242. });
  243. test('jsPlumb.getConnections (scope testScope)', function() {
  244. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  245. jsPlumb.connect({source:d5, target:d6, scope:'testScope'});
  246. var c = jsPlumb.getConnections(); // will get all connections
  247. equals(c['testScope'].length, 1, "there is one connection");
  248. equals(c['testScope'][0].sourceId, 'd5', "the connection's source is d5");
  249. equals(c['testScope'][0].targetId, 'd6', "the connection's source is d6");
  250. });
  251. test('jsPlumb.getConnections (filtered by scope)', function() {
  252. var d8 = _addDiv("d8"), d9 = _addDiv("d9"), d10 = _addDiv('d10');
  253. jsPlumb.connect({source:d8, target:d9, scope:'testScope'});
  254. jsPlumb.connect({source:d9, target:d10}); // default scope
  255. var c = jsPlumb.getConnections({scope:'testScope'}); // will get all connections
  256. equals(c[jsPlumb.getDefaultScope()], null);
  257. equals(c['testScope'].length, 1);
  258. // now supply a list of scopes
  259. c = jsPlumb.getConnections({scope:[jsPlumb.getDefaultScope(),'testScope']}); // will get all connections
  260. equals(c[jsPlumb.getDefaultScope()].length, 1);
  261. equals(c['testScope'].length, 1, "there is one connection in 'testScope'");
  262. });
  263. test('jsPlumb.getConnections (filtered by scope and sourceId)', function() {
  264. var d8 = _addDiv("d8"), d9 = _addDiv("d9"), d10 = _addDiv('d10');
  265. jsPlumb.connect({source:d8, target:d9, scope:'testScope'});
  266. jsPlumb.connect({source:d9, target:d8, scope:'testScope'});
  267. jsPlumb.connect({source:d9, target:d10}); // default scope
  268. var c = jsPlumb.getConnections({scope:'testScope', source:'d8'}); // will get all connections with sourceId 'd8'
  269. equals(c[jsPlumb.getDefaultScope()], null);
  270. equals(c['testScope'].length, 1, "there is one connection in 'testScope' from d8");
  271. });
  272. test('jsPlumb.getConnections (filtered by scope, source id and target id)', function() {
  273. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  274. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  275. jsPlumb.connect({source:d12, target:d13, scope:'testScope'});
  276. jsPlumb.connect({source:d11, target:d13, scope:'testScope'});
  277. var c = jsPlumb.getConnections({scope:'testScope', source:'d11', target:'d13'});
  278. equals(c['testScope'].length, 1, "there is one connection from d11 to d13");
  279. });
  280. test('jsPlumb.getConnections (filtered by a list of scopes)', function() {
  281. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  282. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  283. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  284. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  285. var c = jsPlumb.getConnections({scope:['testScope','testScope3']});
  286. equals(c['testScope'].length, 1, 'there is one connection in testScope');
  287. equals(c['testScope3'].length, 1, 'there is one connection in testScope3');
  288. equals(c['testScope2'], null, 'there are no connections in testScope2');
  289. });
  290. test('jsPlumb.getConnections (filtered by a list of scopes and source ids)', function() {
  291. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  292. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  293. jsPlumb.connect({source:d13, target:d12, scope:'testScope'});
  294. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  295. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  296. var c = jsPlumb.getConnections({scope:['testScope','testScope3'], source:['d11']});
  297. equals(c['testScope'].length, 1, 'there is one connection in testScope');
  298. equals(c['testScope3'].length, 1, 'there is one connection in testScope3');
  299. equals(c['testScope2'], null, 'there are no connections in testScope2');
  300. });
  301. test('jsPlumb.getConnections (filtered by a list of scopes, source ids and target ids)', function() {
  302. jsPlumb.detachEverything();
  303. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13'), d14=_addDiv("d14"), d15=_addDiv("d15");
  304. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  305. jsPlumb.connect({source:d13, target:d12, scope:'testScope'});
  306. jsPlumb.connect({source:d11, target:d14, scope:'testScope'});
  307. jsPlumb.connect({source:d11, target:d15, scope:'testScope'});
  308. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  309. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  310. assertContextSize(18);
  311. var c = jsPlumb.getConnections({scope:['testScope','testScope3'], source:['d11'], target:['d14', 'd15']});
  312. equals(c['testScope'].length, 2, 'there are two connections in testScope');
  313. equals(c['testScope3'].length, 0, 'there is no connections in testScope3');
  314. equals(c['testScope2'], null, 'there are no connections in testScope2');
  315. var anEntry = c['testScope'][0];
  316. ok(anEntry.sourceEndpoint != null, "Source endpoint is set");
  317. ok(anEntry.targetEndpoint != null, "Target endpoint is set");
  318. equals(anEntry.source.attr("id"), "d11", "Source is div d11");
  319. equals(anEntry.target.attr("id"), "d14", "Target is div d14");
  320. });
  321. test('connection event listeners', function() {
  322. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  323. var returnedParams = null/*, returnedParams2 = null*/;
  324. jsPlumb.addListener(["jsPlumbConnection"], {
  325. // signature of the 'interface' method is jsPlumbConnection. params
  326. // has source, target, sourceId, targetId, sourceEndpoint, targetEndpoint
  327. jsPlumbConnection : function(params) {
  328. returnedParams = $.extend({}, params);
  329. }
  330. });
  331. jsPlumb.connect({source:d1, target:d2});
  332. ok(returnedParams != null, "new connection listener event was fired");
  333. equals(returnedParams.sourceId, "d1", 'sourceid is set');
  334. equals(returnedParams.targetId, "d2", 'targetid is set');
  335. equals(returnedParams.source.attr("id"), "d1", 'source is set');
  336. equals(returnedParams.target.attr("id"), "d2" , 'target is set');
  337. ok(returnedParams.sourceEndpoint != null, "source endpoint is not null");
  338. ok(returnedParams.targetEndpoint != null, "target endpoint is not null");
  339. //jsPlumb.detachAll("d1");
  340. //ok(returnedParams2 != null, "removed connection listener event was fired");
  341. });
  342. test('detach event listeners (detach by connection)', function() {
  343. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  344. var returnedParams = null;
  345. jsPlumb.addListener(["jsPlumbConnectionDetached"], {
  346. jsPlumbConnectionDetached : function(params) {
  347. returnedParams = $.extend({}, params);
  348. }
  349. });
  350. var conn = jsPlumb.connect({source:d1, target:d2});
  351. jsPlumb.detach(conn);
  352. ok(returnedParams != null, "removed connection listener event was fired");
  353. });
  354. test('detach event listeners (detach by element ids)', function() {
  355. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  356. var returnedParams = null;
  357. jsPlumb.addListener(["jsPlumbConnectionDetached"], {
  358. jsPlumbConnectionDetached : function(params) {
  359. returnedParams = $.extend({}, params);
  360. }
  361. });
  362. var conn = jsPlumb.connect({source:d1, target:d2});
  363. jsPlumb.detach("d1","d2");
  364. ok(returnedParams != null, "removed connection listener event was fired");
  365. });
  366. test('detach event listeners (detach by elements)', function() {
  367. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  368. var returnedParams = null;
  369. jsPlumb.addListener(["jsPlumbConnectionDetached"], {
  370. jsPlumbConnectionDetached : function(params) {
  371. returnedParams = $.extend({}, params);
  372. }
  373. });
  374. var conn = jsPlumb.connect({source:d1, target:d2});
  375. jsPlumb.detach(d1,d2);
  376. ok(returnedParams != null, "removed connection listener event was fired");
  377. });
  378. test('detach event listeners (via Endpoint.detach method)', function() {
  379. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  380. var e1 = jsPlumb.addEndpoint(d1, {});
  381. var e2 = jsPlumb.addEndpoint(d2, {});
  382. var returnedParams = null;
  383. jsPlumb.addListener(["jsPlumbConnectionDetached"], {
  384. jsPlumbConnectionDetached : function(params) {
  385. returnedParams = $.extend({}, params);
  386. }
  387. });
  388. var conn = jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  389. assertContextSize(3);
  390. e1.detach(conn);
  391. ok(returnedParams != null, "removed connection listener event was fired");
  392. assertContextSize(2);
  393. });
  394. test('detach event listeners (via Endpoint.detachFrom method)', function() {
  395. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  396. var e1 = jsPlumb.addEndpoint(d1, {});
  397. var e2 = jsPlumb.addEndpoint(d2, {});
  398. var returnedParams = null;
  399. jsPlumb.addListener(["jsPlumbConnectionDetached"], {
  400. jsPlumbConnectionDetached : function(params) {
  401. returnedParams = $.extend({}, params);
  402. }
  403. });
  404. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  405. assertContextSize(3);
  406. e1.detachFrom(e2);
  407. ok(returnedParams != null, "removed connection listener event was fired");
  408. assertContextSize(2);
  409. });
  410. test('detach event listeners (via Endpoint.detachAll method)', function() {
  411. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  412. var e1 = jsPlumb.addEndpoint(d1, {});
  413. var e2 = jsPlumb.addEndpoint(d2, {});
  414. var returnedParams = null;
  415. jsPlumb.addListener(["jsPlumbConnectionDetached"], {
  416. jsPlumbConnectionDetached : function(params) {
  417. returnedParams = $.extend({}, params);
  418. }
  419. });
  420. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  421. assertContextSize(3);
  422. e1.detachAll();
  423. ok(returnedParams != null, "removed connection listener event was fired");
  424. assertContextSize(2);
  425. });
  426. test('detach event listeners (via jsPlumb.deleteEndpoint method)', function() {
  427. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  428. var e1 = jsPlumb.addEndpoint(d1, {});
  429. var e2 = jsPlumb.addEndpoint(d2, {});
  430. var returnedParams = null;
  431. jsPlumb.addListener(["jsPlumbConnectionDetached"], {
  432. jsPlumbConnectionDetached : function(params) {
  433. returnedParams = $.extend({}, params);
  434. }
  435. });
  436. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  437. assertContextSize(3);
  438. jsPlumb.deleteEndpoint(e1);
  439. ok(returnedParams != null, "removed connection listener event was fired");
  440. assertContextSize(1);
  441. });
  442. test('detach event listeners (ensure cleared by jsPlumb.reset)', function() {
  443. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  444. var returnedParams = null;
  445. jsPlumb.addListener(["jsPlumbConnectionDetached"], {
  446. jsPlumbConnectionDetached : function(params) {
  447. returnedParams = $.extend({}, params);
  448. }
  449. });
  450. var conn = jsPlumb.connect({source:d1, target:d2});
  451. jsPlumb.detach(d1,d2);
  452. ok(returnedParams != null, "removed connection listener event was fired");
  453. returnedParams = null;
  454. jsPlumb.reset();
  455. var conn = jsPlumb.connect({source:d1, target:d2});
  456. jsPlumb.detach(d1,d2);
  457. ok(returnedParams == null, "connection listener was cleared by jsPlumb.reset()");
  458. });
  459. test('connection events that throw errors', function() {
  460. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  461. var returnedParams = null, returnedParams2 = null;
  462. jsPlumb.addListener(["jsPlumbConnection"], {
  463. // signature of the 'interface' method is jsPlumbConnection. params
  464. // has source, target, sourceId, targetId, sourceEndpoint, targetEndpoint
  465. jsPlumbConnection : function(params) {
  466. returnedParams = $.extend({}, params);
  467. throw "oh no!";
  468. }
  469. });
  470. jsPlumb.connect({source:d1, target:d2});
  471. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  472. jsPlumb.connect({source:d3, target:d4});
  473. ok(returnedParams != null, "new connection listener event was fired; we threw an error, jsPlumb survived.");
  474. });
  475. test("Endpoint.detachFrom", function() {
  476. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  477. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  478. ok(e16.anchor, 'endpoint 16 has an anchor');
  479. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  480. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  481. assertConnectionCount(e16, 1);
  482. assertConnectionCount(e17, 1);
  483. assertContextSize(3);
  484. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  485. e16.detachFrom(e17);
  486. assertContextSize(2); // the endpoint canvases should remain
  487. // but the connection should be gone, meaning not registered by jsPlumb and not registered on either Endpoint:
  488. assertConnectionCount(e16, 0);
  489. assertConnectionCount(e17, 0);
  490. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  491. });
  492. test("Endpoint.detachFromConnection", function() {
  493. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  494. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  495. ok(e16.anchor, 'endpoint 16 has an anchor');
  496. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  497. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  498. assertConnectionCount(e16, 1);
  499. assertConnectionCount(e17, 1);
  500. assertContextSize(3);
  501. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  502. e16.detachFromConnection(conn);
  503. assertContextSize(3); // all canvases should remain; the connection was not removed.
  504. // but endpoint e16 should have no connections now.
  505. assertConnectionCount(e16, 0);
  506. assertConnectionCount(e17, 1);
  507. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  508. });
  509. test("Endpoint.detachAll", function() {
  510. var d16 = _addDiv("d16"), d17 = _addDiv("d17"); d18 = _addDiv("d18");
  511. var e16 = $("#d16").addEndpoint({isSource:true,maxConnections:-1});
  512. ok(e16.anchor, 'endpoint 16 has an anchor');
  513. var e17 = $("#d17").addEndpoint({isSource:true});
  514. var e18 = $("#d18").addEndpoint({isSource:true});
  515. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  516. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e18});
  517. assertConnectionCount(e16, 2);
  518. assertConnectionCount(e17, 1);
  519. assertConnectionCount(e18, 1);
  520. assertContextSize(5);
  521. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 2);
  522. e16.detachAll();
  523. assertConnectionCount(e16, 0);
  524. assertConnectionCount(e17, 0);
  525. assertContextSize(3);
  526. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  527. });
  528. test("Endpoint.detach", function() {
  529. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  530. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  531. ok(e16.anchor, 'endpoint 16 has an anchor');
  532. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  533. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  534. assertConnectionCount(e16, 1);
  535. assertConnectionCount(e17, 1);
  536. assertContextSize(3);
  537. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  538. e16.detach(conn);
  539. assertContextSize(2); // the endpoint canvases should remain
  540. // but the connection should be gone, meaning not registered by jsPlumb and not registered on either Endpoint:
  541. assertConnectionCount(e16, 0);
  542. assertConnectionCount(e17, 0);
  543. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  544. });
  545. test("Endpoint.isConnectedTo", function() {
  546. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  547. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  548. ok(e16.anchor, 'endpoint 16 has an anchor');
  549. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  550. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  551. equals(e16.isConnectedTo(e17), true, "e16 and e17 are connected");
  552. });
  553. test("setting endpoint uuid", function() {
  554. var uuid = "14785937583175927504313";
  555. var d16 = _addDiv("d16"), d17 = _addDiv("d17"); d18 = _addDiv("d18");
  556. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  557. equals(e16.getUuid(), uuid, "endpoint's uuid was set correctly");
  558. });
  559. test("jsPlumb.getEndpoint (by uuid)", function() {
  560. var uuid = "14785937583175927504313";
  561. var d16 = _addDiv("d16");
  562. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  563. var e = jsPlumb.getEndpoint(uuid);
  564. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  565. });
  566. test("jsPlumb.deleteEndpoint (by uuid, simple case)", function() {
  567. var uuid = "14785937583175927504313";
  568. var d16 = _addDiv("d16");
  569. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  570. var e = jsPlumb.getEndpoint(uuid);
  571. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  572. jsPlumb.deleteEndpoint(uuid);
  573. var f = jsPlumb.getEndpoint(uuid);
  574. equals(f, null, "endpoint has been deleted");
  575. var ebe = jsPlumb.getTestHarness().endpointsByElement["d16"];
  576. equals(ebe.length, 0, "no endpoints registered for element d16 anymore");
  577. assertContextSize(0);
  578. });
  579. test("jsPlumb.deleteEndpoint (by uuid, connections too)", function() {
  580. // create two endpoints (one with a uuid), add them to two divs and then connect them.
  581. var uuid = "14785937583175927504313";
  582. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  583. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  584. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  585. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  586. // check that the connection was ok.
  587. equals(e16.connections.length, 1, "e16 has one connection");
  588. equals(e17.connections.length, 1, "e17 has one connection");
  589. assertContextSize(3);
  590. // delete the endpoint that has a uuid. verify that the endpoint cannot be retrieved and that the connection has been removed, but that
  591. // element d17 still has its Endpoint.
  592. jsPlumb.deleteEndpoint(uuid);
  593. var f = jsPlumb.getEndpoint(uuid);
  594. equals(f, null, "endpoint has been deleted");
  595. equals(e16.connections.length, 0, "e16 has no connections");
  596. equals(e17.connections.length, 0, "e17 has no connections");
  597. var ebe = jsPlumb.getTestHarness().endpointsByElement["d16"];
  598. equals(ebe.length, 0, "no endpoints registered for element d16 anymore");
  599. ebe = jsPlumb.getTestHarness().endpointsByElement["d17"];
  600. equals(ebe.length, 1, "element d17 still has its Endpoint");
  601. assertContextSize(1);
  602. // now delete d17's endpoint and check that it has gone.
  603. jsPlumb.deleteEndpoint(e17);
  604. f = jsPlumb.getEndpoint(e17);
  605. equals(f, null, "endpoint has been deleted");
  606. ebe = jsPlumb.getTestHarness().endpointsByElement["d17"];
  607. equals(ebe.length, 0, "element d17 no longer has any Endpoints");
  608. assertContextSize(0);
  609. });
  610. test("jsPlumb.deleteEndpoint (by reference, simple case)", function() {
  611. var uuid = "14785937583175927504313";
  612. var d16 = _addDiv("d16");
  613. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  614. var e = jsPlumb.getEndpoint(uuid);
  615. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  616. jsPlumb.deleteEndpoint(e16);
  617. var f = jsPlumb.getEndpoint(uuid);
  618. equals(f, null, "endpoint has been deleted");
  619. assertContextSize(0);
  620. });
  621. test("jsPlumb.deleteEndpoint (by reference, connections too)", function() {
  622. var uuid = "14785937583175927504313";
  623. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  624. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  625. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  626. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  627. equals(e16.connections.length, 1, "e16 has one connection");
  628. equals(e17.connections.length, 1, "e17 has one connection");
  629. assertContextSize(3);
  630. jsPlumb.deleteEndpoint(e16);
  631. var f = jsPlumb.getEndpoint(uuid);
  632. equals(f, null, "endpoint has been deleted");
  633. equals(e16.connections.length, 0, "e16 has no connections");
  634. equals(e17.connections.length, 0, "e17 has no connections");
  635. assertContextSize(1);
  636. });
  637. test("jsPlumb.deleteEveryEndpoint", function() {
  638. var uuid = "14785937583175927504313";
  639. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  640. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  641. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  642. assertContextSize(2);
  643. var e = jsPlumb.getEndpoint(uuid);
  644. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  645. jsPlumb.deleteEveryEndpoint();
  646. var f = jsPlumb.getEndpoint(uuid);
  647. equals(f, null, "endpoint e16 has been deleted");
  648. var g = jsPlumb.getEndpoint(e17);
  649. equals(g, null, "endpoint e17 has been deleted");
  650. assertContextSize(0);
  651. });
  652. test("jsPlumb.deleteEveryEndpoint (connections too)", function() {
  653. var uuid = "14785937583175927504313";
  654. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  655. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  656. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  657. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  658. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  659. assertContextSize(3);
  660. var e = jsPlumb.getEndpoint(uuid);
  661. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  662. jsPlumb.deleteEveryEndpoint();
  663. var f = jsPlumb.getEndpoint(uuid);
  664. equals(f, null, "endpoint e16 has been deleted");
  665. var g = jsPlumb.getEndpoint(e17);
  666. equals(g, null, "endpoint e17 has been deleted");
  667. assertContextSize(0); // no canvases. so all connection canvases have been cleaned up too.
  668. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  669. });
  670. test('jsPlumb.connect (between two Endpoints)', function() {
  671. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  672. var e = jsPlumb.addEndpoint(d1, {});
  673. var e2 = jsPlumb.addEndpoint(d2, {});
  674. ok(e, 'endpoint e exists');
  675. ok(e2, 'endpoint e2 exists');
  676. assertContextSize(2); // should have a canvas for each endpoint now.
  677. assertEndpointCount("d1", 1);
  678. assertEndpointCount("d2", 1);
  679. jsPlumb.connect({target:'d2', sourceEndpoint:e, targetEndpoint:e2});
  680. assertEndpointCount("d1", 1); // no new endpoint should have been added
  681. assertEndpointCount("d2", 1); // no new endpoint should have been added
  682. assertContextSize(3); // now we should also have a canvas for the connection.
  683. });
  684. test('jsPlumb.connect (by endpoint)', function() {
  685. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  686. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  687. ok(e16.anchor, 'endpoint 16 has an anchor');
  688. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  689. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  690. assertContextSize(3);
  691. });
  692. test("jsPlumb.connect (two Endpoints - that have been already added - by UUID)", function() {
  693. var srcEndpointUuid = "14785937583175927504313", dstEndpointUuid = "14785937583175927534325";
  694. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  695. var e1 = jsPlumb.addEndpoint("d16", {isSource:true, maxConnections:-1, uuid:srcEndpointUuid});
  696. var e2 = jsPlumb.addEndpoint("d17", {isSource:true, maxConnections:-1, uuid:dstEndpointUuid});
  697. jsPlumb.connect({ uuids: [ srcEndpointUuid, dstEndpointUuid ] });
  698. assertConnectionCount(e1, 1);
  699. assertConnectionCount(e2, 1);
  700. assertContextSize(3);
  701. });
  702. test("jsPlumb.connect (two Endpoints - that have not been already added - by UUID)", function() {
  703. var srcEndpointUuid = "14785937583175927504313", dstEndpointUuid = "14785937583175927534325";
  704. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  705. jsPlumb.connect({ uuids: [ srcEndpointUuid, dstEndpointUuid ], source:d16, target:d17 });
  706. assertContextSize(3);
  707. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  708. var e1 = jsPlumb.getEndpoint(srcEndpointUuid);
  709. ok(e1 != null, "endpoint with src uuid added");
  710. ok(e1.canvas != null);
  711. var e2 = jsPlumb.getEndpoint(dstEndpointUuid);
  712. ok(e2 != null, "endpoint with target uuid added");
  713. assertConnectionCount(e1, 1);
  714. assertConnectionCount(e2, 1);
  715. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  716. });
  717. test("jsPlumb.connect (two Endpoints - that have been already added - by endpoint reference)", function() {
  718. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  719. var e1 = jsPlumb.addEndpoint("d16", {isSource:true, maxConnections:-1});
  720. var e2 = jsPlumb.addEndpoint("d17", {isSource:true, maxConnections:-1});
  721. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  722. assertContextSize(3);
  723. assertConnectionCount(e1, 1);
  724. assertConnectionCount(e2, 1);
  725. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  726. });
  727. test("jsPlumb.connect (two elements)", function() {
  728. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  729. jsPlumb.connect({ source:d16, target:d17 });
  730. assertContextSize(3);
  731. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  732. });
  733. test("jsPlumb.connect (Connector test)", function() {
  734. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  735. var conn = jsPlumb.connect({ source:d16, target:d17, connector:new jsPlumb.Connectors.Straight() });
  736. assertContextSize(3);
  737. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  738. equals(conn.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  739. });
  740. test("jsPlumb.connect (Connector as string test)", function() {
  741. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  742. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight" });
  743. assertContextSize(3);
  744. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  745. equals(conn.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  746. });
  747. test("jsPlumb.connect (Endpoint test)", function() {
  748. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  749. var conn = jsPlumb.connect({ source:d16, target:d17, endpoint:new jsPlumb.Endpoints.Rectangle() });
  750. assertContextSize(3);
  751. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  752. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection source");
  753. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection target");
  754. });
  755. test("jsPlumb.connect (Endpoint as string test)", function() {
  756. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  757. var conn = jsPlumb.connect({ source:d16, target:d17, endpoint:"Rectangle" });
  758. assertContextSize(3);
  759. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  760. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection source");
  761. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection target");
  762. });
  763. test("jsPlumb.connect (Endpoints test)", function() {
  764. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  765. var conn = jsPlumb.connect({ source:d16, target:d17, endpoints:[new jsPlumb.Endpoints.Rectangle(),new jsPlumb.Endpoints.Dot()] });
  766. assertContextSize(3);
  767. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  768. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection source");
  769. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints.Dot, "Dot endpoint chosen for connection target");
  770. });
  771. test("jsPlumb.connect (Endpoint as string test)", function() {
  772. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  773. var conn = jsPlumb.connect({ source:d16, target:d17, endpoints:["Rectangle", "Dot" ] });
  774. assertContextSize(3);
  775. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  776. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection source");
  777. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints.Dot, "Dot endpoint chosen for connection target");
  778. });
  779. test("jsPlumb.connect (by Endpoints, connector test)", function() {
  780. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  781. var e16 = jsPlumb.addEndpoint(d16, {});
  782. var e17 = jsPlumb.addEndpoint(d17, {});
  783. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:new jsPlumb.Connectors.Straight() });
  784. assertContextSize(3);
  785. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  786. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  787. });
  788. test("jsPlumb.connect (by Endpoints, connector as string test)", function() {
  789. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  790. var e16 = jsPlumb.addEndpoint(d16, {});
  791. var e17 = jsPlumb.addEndpoint(d17, {});
  792. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  793. assertContextSize(3);
  794. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  795. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  796. });
  797. test("jsPlumb.connect (by Endpoints, anchors as string test)", function() {
  798. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  799. var a16 = "TopCenter", a17 = "BottomCenter";
  800. var e16 = jsPlumb.addEndpoint(d16, {anchor:a16});
  801. var e17 = jsPlumb.addEndpoint(d17, {anchor:a17});
  802. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  803. assertContextSize(3);
  804. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  805. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  806. equals(e16.anchor.x, 0.5, "endpoint 16 is at top center");equals(e16.anchor.y, 0, "endpoint 16 is at top center");
  807. equals(e17.anchor.x, 0.5, "endpoint 17 is at bottom center");equals(e17.anchor.y, 1, "endpoint 17 is at bottom center");
  808. });
  809. test("jsPlumb.connect (by Endpoints, endpoints create anchors)", function() {
  810. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  811. var a16 = [0,0.5,0,-1], a17 = [1,0.0,-1,-1];
  812. var e16 = jsPlumb.addEndpoint(d16, {anchor:a16});
  813. var e17 = jsPlumb.addEndpoint(d17, {anchor:a17});
  814. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  815. assertContextSize(3);
  816. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  817. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  818. equals(e16.anchor.x, a16[0]);equals(e16.anchor.y, a16[1]);
  819. equals(e17.anchor.x, a17[0]);equals(e17.anchor.y, a17[1]);
  820. equals(e16.anchor.getOrientation()[0], a16[2]); equals(e16.anchor.getOrientation()[1], a16[3]);
  821. equals(e17.anchor.getOrientation()[0], a17[2]); equals(e17.anchor.getOrientation()[1], a17[3]);
  822. });
  823. test("jsPlumb.connect (by Endpoints, endpoints create dynamic anchors; anchors specified by 'anchor')", function() {
  824. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  825. var e16 = jsPlumb.addEndpoint(d16, {anchor:[[0,0.5,0,-1], [1,0.5,0,1]]});
  826. var e17 = jsPlumb.addEndpoint(d17, {anchor:["TopCenter", "BottomCenter"]});
  827. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  828. assertContextSize(3);
  829. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  830. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  831. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  832. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  833. });
  834. test("jsPlumb.connect (by Endpoints, endpoints create dynamic anchors; anchors specified by 'anchors')", function() {
  835. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  836. var e16 = jsPlumb.addEndpoint(d16, {anchors:[[0,0.5,0,-1], [1,0.5,0,1]]});
  837. var e17 = jsPlumb.addEndpoint(d17, {anchors:["TopCenter", "BottomCenter"]});
  838. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  839. assertContextSize(3);
  840. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  841. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  842. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  843. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  844. });
  845. // this test is for the original detach function; it should stay working after i mess with it
  846. // a little.
  847. test("jsPlumb.detach (by element ids)", function() {
  848. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  849. var e1 = jsPlumb.addEndpoint(d1);
  850. var e2 = jsPlumb.addEndpoint(d2);
  851. var e3 = jsPlumb.addEndpoint(d1);
  852. var e4 = jsPlumb.addEndpoint(d2);
  853. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  854. jsPlumb.connect({ sourceEndpoint:e3, targetEndpoint:e4 }); // make two connections to be sure this works ;)
  855. assertConnectionCount(e1, 1);
  856. assertConnectionCount(e2, 1);
  857. assertConnectionCount(e3, 1);
  858. assertConnectionCount(e4, 1);
  859. jsPlumb.detach("d1", "d2");
  860. assertConnectionCount(e1, 0);
  861. assertConnectionCount(e2, 0);
  862. assertConnectionCount(e3, 0);
  863. assertConnectionCount(e4, 0);
  864. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  865. });
  866. // detach is being made to operate more like connect - by taking one argument with a whole
  867. // bunch of possible params in it. if two args are passed in it will continue working
  868. // in the old way.
  869. test("jsPlumb.detach (params object, using element ids)", function() {
  870. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  871. var e1 = jsPlumb.addEndpoint(d1);
  872. var e2 = jsPlumb.addEndpoint(d2);
  873. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  874. assertConnectionCount(e1, 1);
  875. assertConnectionCount(e2, 1);
  876. jsPlumb.detach({source:"d1", target:"d2"});
  877. assertConnectionCount(e1, 0);
  878. assertConnectionCount(e2, 0);
  879. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  880. });
  881. test("jsPlumb.detach (params object, using element objects)", function() {
  882. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  883. var e1 = jsPlumb.addEndpoint(d1);
  884. var e2 = jsPlumb.addEndpoint(d2);
  885. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  886. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  887. assertContextSize(3);
  888. assertConnectionCount(e1, 1);
  889. assertConnectionCount(e2, 1);
  890. jsPlumb.detach({source:d1, target:d2});
  891. assertConnectionCount(e1, 0);
  892. assertConnectionCount(e2, 0);
  893. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  894. assertContextSize(2);
  895. });
  896. test("jsPlumb.detach (source and target as endpoint UUIDs)", function() {
  897. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  898. var e1 = jsPlumb.addEndpoint(d1, {uuid:"abcdefg"});
  899. ok(jsPlumb.getEndpoint("abcdefg") != null, "e1 exists");
  900. var e2 = jsPlumb.addEndpoint(d2, {uuid:"hijklmn"});
  901. ok(jsPlumb.getEndpoint("hijklmn") != null, "e2 exists");
  902. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  903. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  904. assertContextSize(3);
  905. assertConnectionCount(e1, 1);
  906. assertConnectionCount(e2, 1);
  907. jsPlumb.detach({uuids:["abcdefg", "hijklmn"]});
  908. assertConnectionCount(e1, 0);
  909. assertConnectionCount(e2, 0);
  910. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  911. assertContextSize(2);
  912. });
  913. test("jsPlumb.detach (sourceEndpoint and targetEndpoint supplied)", function() {
  914. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  915. var e1 = jsPlumb.addEndpoint(d1);
  916. var e2 = jsPlumb.addEndpoint(d2);
  917. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  918. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  919. assertContextSize(3);
  920. assertConnectionCount(e1, 1);
  921. assertConnectionCount(e2, 1);
  922. jsPlumb.detach({ sourceEndpoint:e1, targetEndpoint:e2 });
  923. assertConnectionCount(e1, 0);
  924. assertConnectionCount(e2, 0);
  925. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  926. assertContextSize(2);
  927. });
  928. test("jsPlumb.connect (connect by element, default endpoint, supplied dynamic anchors)", function() {
  929. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  930. var anchors = [ [0.25, 0, 0, -1], [1, 0.25, 1, 0], [0.75, 1, 0, 1], [0, 0.75, -1, 0] ];
  931. jsPlumb.connect({source:d1, target:d2, dynamicAnchors:anchors}); // auto connect with default endpoint and provided anchors
  932. assertContextSize(3);
  933. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  934. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  935. jsPlumb.detach({source:d1, target:d2});
  936. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  937. assertContextSize(2);
  938. });
  939. test("jsPlumb.connect (connect by element, supplied endpoint and dynamic anchors)", function() {
  940. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  941. var endpoint = { isSource:true };
  942. var e1 = jsPlumb.addEndpoint(d1, endpoint);
  943. var e2 = jsPlumb.addEndpoint(d2, endpoint);
  944. var anchors = [ "TopCenter", "BottomCenter" ];
  945. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2, dynamicAnchors:anchors});
  946. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  947. assertContextSize(3);
  948. jsPlumb.detach({source:d1, target:d2});
  949. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  950. assertContextSize(2);
  951. });
  952. test("jsPlumb.connect (testing for connection event callback)", function() {
  953. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  954. var connectCallback = null, detachCallback = null;
  955. jsPlumb.bind(["jsPlumbConnection", "jsPlumbConnectionDetached"], {
  956. jsPlumbConnection : function(params) {
  957. connectCallback = $.extend({}, params);
  958. },
  959. jsPlumbConnectionDetached : function(params) {
  960. detachCallback = $.extend({}, params);
  961. }
  962. });
  963. jsPlumb.connect({source:d1, target:d2}); // auto connect with default endpoint and anchor set
  964. ok(connectCallback != null, "connect callback was made");
  965. assertContextSize(3);
  966. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  967. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  968. jsPlumb.detach({source:d1, target:d2});
  969. assertContextSize(2);
  970. ok(detachCallback != null, "detach callback was made");
  971. });
  972. test("jsPlumb.makeDynamicAnchors (longhand)", function() {
  973. var anchors = [jsPlumb.makeAnchor(0.2, 0, 0, -1), jsPlumb.makeAnchor(1, 0.2, 1, 0),
  974. jsPlumb.makeAnchor(0.8, 1, 0, 1), jsPlumb.makeAnchor(0, 0.8, -1, 0) ];
  975. var dynamicAnchor = jsPlumb.makeDynamicAnchor(anchors);
  976. var a = dynamicAnchor.getAnchors();
  977. equals(a.length, 4, "Dynamic Anchors has four anchors");
  978. for (var i = 0; i < a.length; i++)
  979. ok(a[i].compute.constructor == Function, "anchor " + i + " well formed");
  980. });
  981. test("jsPlumb.makeDynamicAnchors (shorthand)", function() {
  982. var anchors = [[0.2, 0, 0, -1], [1, 0.2, 1, 0],
  983. [0.8, 1, 0, 1], [0, 0.8, -1, 0] ];
  984. var dynamicAnchor = jsPlumb.makeDynamicAnchor(anchors);
  985. var a = dynamicAnchor.getAnchors();
  986. equals(a.length, 4, "Dynamic Anchors has four anchors");
  987. for (var i = 0; i < a.length; i++)
  988. ok(a[i].compute.constructor == Function, "anchor " + i + " well formed");
  989. });
  990. /**
  991. * leave this test at the bottom!
  992. */
  993. test('unload test', function() {
  994. jsPlumb.unload();
  995. var contextNode = $("._jsPlumb_context");
  996. ok(contextNode.length == 0, 'context node unloaded');
  997. });
  998. });