jsPlumb-1.2.6-tests.js 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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. });
  54. test('getId', function() {
  55. var d10 = _addDiv('d10');
  56. equals(jsPlumb.getTestHarness().getId(d10), "d10");
  57. });
  58. test('create a simple endpoint', function() {
  59. var d1 = _addDiv("d1");
  60. var e = jsPlumb.addEndpoint($("#d1"), {});
  61. ok(e, 'endpoint exists');
  62. assertEndpointCount("d1", 1);
  63. });
  64. test('create and remove a simple endpoint', function() {
  65. var d1 = _addDiv("d1");
  66. var ee = jsPlumb.addEndpoint(d1, {uuid:"78978597593"});
  67. ok(ee != null, "endpoint exists");
  68. assertEndpointCount("d1", 1);
  69. assertContextSize(1); // one Endpoint canvas.
  70. jsPlumb.removeEndpoint(d1, ee);
  71. assertEndpointCount("d1", 0);
  72. var e = jsPlumb.getEndpoint("78978597593");
  73. equals(e, null, "the endpoint has been deleted");
  74. assertContextSize(0); // no Endpoint canvases.
  75. });
  76. test('draggable silently ignored when jquery ui not present', function() {
  77. var d1 = _addDiv("d1");
  78. var e = jsPlumb.addEndpoint(d1, {isSource:true});
  79. ok(e, 'endpoint exists');
  80. });
  81. test('droppable silently ignored when jquery ui not present', function() {
  82. var d1 = _addDiv("d1")
  83. var e = jsPlumb.addEndpoint(d1, {isTarget:true});
  84. ok(e, 'endpoint exists');
  85. });
  86. test('defaultEndpointMaxConnections', function() {
  87. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  88. var e3 = jsPlumb.addEndpoint(d3, {isSource:true});
  89. ok(e3.anchor, 'endpoint 3 has an anchor');
  90. var e4 = jsPlumb.addEndpoint(d4, {isSource:true});
  91. assertEndpointCount("d3", 1);
  92. assertEndpointCount("d4", 1);
  93. assertContextSize(2); // one canvas for each of our two endpoints.
  94. ok(!e3.isFull(), "endpoint 3 is not full.");
  95. jsPlumb.connect({source:d3, target:'d4', sourceEndpoint:e3, targetEndpoint:e4});
  96. assertConnectionCount(e3, 1); // we have one connection
  97. jsPlumb.connect({source:d3, target:'d4', sourceEndpoint:e3, targetEndpoint:e4});
  98. assertConnectionCount(e3, 1); // should have refused the connection; default max is 1.
  99. assertContextSize(3); // now we have one more canvas, for the Connection that was accepted.
  100. });
  101. test('specifiedEndpointMaxConnections', function() {
  102. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  103. var e5 = jsPlumb.addEndpoint(d5, {isSource:true, maxConnections:3});
  104. ok(e5.anchor, 'endpoint 5 has an anchor');
  105. var e6 = jsPlumb.addEndpoint(d6, {isSource:true, maxConnections:2}); // this one has max TWO
  106. assertEndpointCount("d5", 1); assertEndpointCount("d6", 1);
  107. assertContextSize(2);
  108. ok(!e5.isFull(), "endpoint 5 is not full.");
  109. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  110. assertConnectionCount(e5, 1); // we have one connection
  111. assertContextSize(3);
  112. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  113. assertConnectionCount(e5, 2); // two connections
  114. assertContextSize(4);
  115. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  116. assertConnectionCount(e5, 2); // should have refused; max is 2, for d4.
  117. assertContextSize(4);
  118. });
  119. test('noEndpointMaxConnections', function() {
  120. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  121. var e3 = jsPlumb.addEndpoint(d3, {isSource:true, maxConnections:-1});
  122. var e4 = jsPlumb.addEndpoint(d4, {isSource:true, maxConnections:-1});
  123. jsPlumb.connect({sourceEndpoint:e3, targetEndpoint:e4});
  124. assertContextSize(3);
  125. assertConnectionCount(e3, 1); // we have one connection
  126. jsPlumb.connect({sourceEndpoint:e3, targetEndpoint:e4});
  127. assertConnectionCount(e3, 2); // we have two. etc (default was one. this proves max is working).
  128. assertContextSize(4);
  129. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  130. var e5 = jsPlumb.addEndpoint(d3, {isSource:true, maxConnections:-1});
  131. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e4});
  132. assertConnectionCount(e4, 3);
  133. assertContextSize(6);
  134. });
  135. test('anchors equal', function() {
  136. var a1 = jsPlumb.makeAnchor(0, 1, 1, 1);
  137. var a2 = jsPlumb.makeAnchor(0, 1, 1, 1);
  138. ok(a1.equals(a2), "anchors are the same");
  139. });
  140. test('anchors equal with offsets', function() {
  141. var a1 = jsPlumb.makeAnchor(0, 1, 1, 1, 10, 13);
  142. var a2 = jsPlumb.makeAnchor(0, 1, 1, 1, 10, 13);
  143. ok(a1.equals(a2), "anchors are the same");
  144. });
  145. test('anchors not equal', function() {
  146. var a1 = jsPlumb.makeAnchor(0, 1, 0, 1);
  147. var a2 = jsPlumb.makeAnchor(0, 1, 1, 1);
  148. ok(!a1.equals(a2), "anchors are different");
  149. });
  150. test('anchor not equal with offsets', function() {
  151. var a1 = jsPlumb.makeAnchor(0, 1, 1, 1, 10, 13);
  152. var a2 = jsPlumb.makeAnchor(0, 1, 1, 1);
  153. ok(!a1.equals(a2), "anchors are different");
  154. });
  155. test('detach plays nice when no target given', function() {
  156. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  157. jsPlumb.connect({source:d3, target:d4});
  158. jsPlumb.detach();
  159. });
  160. // issue 81
  161. test('jsPlumb.detach should fire only one detach event (pass Connection as argument)', function() {
  162. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  163. var conn = jsPlumb.connect({source:d5, target:d6});
  164. var eventCount = 0;
  165. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  166. jsPlumb.detach(conn);
  167. equals(eventCount, 1);
  168. });
  169. // issue 81
  170. test('jsPlumb.detach should fire only one detach event (pass Connection as param in argument)', function() {
  171. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  172. var conn = jsPlumb.connect({source:d5, target:d6});
  173. var eventCount = 0;
  174. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  175. jsPlumb.detach({connection:conn});
  176. equals(eventCount, 1);
  177. });
  178. // issue 81
  179. test('jsPlumb.detach should fire only one detach event (pass source and targets as strings as arguments in params object)', function() {
  180. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  181. var conn = jsPlumb.connect({source:d5, target:d6});
  182. var eventCount = 0;
  183. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  184. jsPlumb.detach({source:"d5", target:"d6"});
  185. equals(eventCount, 1);
  186. });
  187. // issue 81
  188. test('jsPlumb.detach should fire only one detach event (pass source and targets as divs as arguments in params object)', function() {
  189. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  190. var conn = jsPlumb.connect({source:d5, target:d6});
  191. var eventCount = 0;
  192. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  193. jsPlumb.detach({source:d5, target:d6});
  194. equals(eventCount, 1);
  195. });
  196. // issue 81
  197. test('jsPlumb.detach should fire only one detach event (pass source and targets as divs as arguments)', function() {
  198. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  199. var conn = jsPlumb.connect({source:d5, target:d6});
  200. var eventCount = 0;
  201. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  202. jsPlumb.detach(d5, d6);
  203. equals(eventCount, 1);
  204. });
  205. //TODO make sure you run this test with a single detach call, to ensure that
  206. // single detach calls result in the connection being removed. detachEverything can
  207. // just blow away the connectionsByScope array and recreate it.
  208. test('jsPlumb.getConnections (simple case, default scope)', function() {
  209. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  210. jsPlumb.connect({source:d5, target:d6});
  211. assertContextSize(3);
  212. var c = jsPlumb.getConnections(); // will get all connections
  213. equals(c[jsPlumb.getDefaultScope()].length, 1, "there is one connection");
  214. });
  215. test('jsPlumb.getConnections (simple case, default scope; detach by element id)', function() {
  216. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  217. jsPlumb.connect({source:d5, target:d6});
  218. jsPlumb.connect({source:d6, target:d7});
  219. assertContextSize(6);
  220. var c = jsPlumb.getConnections(); // will get all connections
  221. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  222. jsPlumb.detach('d5', 'd6');
  223. c = jsPlumb.getConnections(); // will get all connections
  224. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  225. assertContextSize(5);
  226. });
  227. test('jsPlumb.getConnections (simple case, default scope; detach by element id using params object)', function() {
  228. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  229. jsPlumb.connect({source:d5, target:d6});
  230. jsPlumb.connect({source:d6, target:d7});
  231. assertContextSize(6);
  232. var c = jsPlumb.getConnections(); // will get all connections
  233. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  234. jsPlumb.detach({source:'d5', target:'d6'});
  235. c = jsPlumb.getConnections(); // will get all connections
  236. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  237. assertContextSize(5);
  238. });
  239. test('jsPlumb.getConnections (simple case, default scope; detach by element object)', function() {
  240. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  241. jsPlumb.connect({source:d5, target:d6});
  242. jsPlumb.connect({source:d6, target:d7});
  243. assertContextSize(6);
  244. var c = jsPlumb.getConnections(); // will get all connections
  245. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  246. jsPlumb.detach(d5, d6);
  247. c = jsPlumb.getConnections(); // will get all connections
  248. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  249. assertContextSize(5);
  250. });
  251. test('jsPlumb.getConnections (simple case, default scope; detach by element object using params object)', function() {
  252. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  253. jsPlumb.connect({source:d5, target:d6});
  254. jsPlumb.connect({source:d6, target:d7});
  255. assertContextSize(6);
  256. var c = jsPlumb.getConnections(); // will get all connections
  257. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  258. jsPlumb.detach({source:d5, target:d6});
  259. c = jsPlumb.getConnections(); // will get all connections
  260. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  261. assertContextSize(5);
  262. });
  263. test('jsPlumb.getConnections (simple case, default scope; detach by Connection)', function() {
  264. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  265. var c56 = jsPlumb.connect({source:d5, target:d6});
  266. var c67 = jsPlumb.connect({source:d6, target:d7});
  267. assertContextSize(6);
  268. var c = jsPlumb.getConnections(); // will get all connections
  269. equals(c[jsPlumb.getDefaultScope()].length, 2, "there are two connections initially");
  270. jsPlumb.detach(c56);
  271. assertContextSize(5); // check that the connection canvas was removed.
  272. c = jsPlumb.getConnections(); // will get all connections
  273. equals(c[jsPlumb.getDefaultScope()].length, 1, "after detaching one, there is now one connection.");
  274. });
  275. test('jsPlumb.getConnections (scope testScope)', function() {
  276. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  277. jsPlumb.connect({source:d5, target:d6, scope:'testScope'});
  278. var c = jsPlumb.getConnections(); // will get all connections
  279. equals(c['testScope'].length, 1, "there is one connection");
  280. equals(c['testScope'][0].sourceId, 'd5', "the connection's source is d5");
  281. equals(c['testScope'][0].targetId, 'd6', "the connection's source is d6");
  282. });
  283. test('jsPlumb.getConnections (filtered by scope)', function() {
  284. var d8 = _addDiv("d8"), d9 = _addDiv("d9"), d10 = _addDiv('d10');
  285. jsPlumb.connect({source:d8, target:d9, scope:'testScope'});
  286. jsPlumb.connect({source:d9, target:d10}); // default scope
  287. var c = jsPlumb.getConnections({scope:'testScope'}); // will get all connections
  288. equals(c[jsPlumb.getDefaultScope()], null);
  289. equals(c['testScope'].length, 1);
  290. // now supply a list of scopes
  291. c = jsPlumb.getConnections({scope:[jsPlumb.getDefaultScope(),'testScope']}); // will get all connections
  292. equals(c[jsPlumb.getDefaultScope()].length, 1);
  293. equals(c['testScope'].length, 1, "there is one connection in 'testScope'");
  294. });
  295. test('jsPlumb.getConnections (filtered by scope and sourceId)', function() {
  296. var d8 = _addDiv("d8"), d9 = _addDiv("d9"), d10 = _addDiv('d10');
  297. jsPlumb.connect({source:d8, target:d9, scope:'testScope'});
  298. jsPlumb.connect({source:d9, target:d8, scope:'testScope'});
  299. jsPlumb.connect({source:d9, target:d10}); // default scope
  300. var c = jsPlumb.getConnections({scope:'testScope', source:'d8'}); // will get all connections with sourceId 'd8'
  301. equals(c[jsPlumb.getDefaultScope()], null);
  302. equals(c['testScope'].length, 1, "there is one connection in 'testScope' from d8");
  303. });
  304. test('jsPlumb.getConnections (filtered by scope, source id and target id)', function() {
  305. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  306. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  307. jsPlumb.connect({source:d12, target:d13, scope:'testScope'});
  308. jsPlumb.connect({source:d11, target:d13, scope:'testScope'});
  309. var c = jsPlumb.getConnections({scope:'testScope', source:'d11', target:'d13'});
  310. equals(c['testScope'].length, 1, "there is one connection from d11 to d13");
  311. });
  312. test('jsPlumb.getConnections (filtered by a list of scopes)', function() {
  313. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  314. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  315. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  316. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  317. var c = jsPlumb.getConnections({scope:['testScope','testScope3']});
  318. equals(c['testScope'].length, 1, 'there is one connection in testScope');
  319. equals(c['testScope3'].length, 1, 'there is one connection in testScope3');
  320. equals(c['testScope2'], null, 'there are no connections in testScope2');
  321. });
  322. test('jsPlumb.getConnections (filtered by a list of scopes and source ids)', function() {
  323. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  324. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  325. jsPlumb.connect({source:d13, target:d12, scope:'testScope'});
  326. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  327. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  328. var c = jsPlumb.getConnections({scope:['testScope','testScope3'], source:['d11']});
  329. equals(c['testScope'].length, 1, 'there is one connection in testScope');
  330. equals(c['testScope3'].length, 1, 'there is one connection in testScope3');
  331. equals(c['testScope2'], null, 'there are no connections in testScope2');
  332. });
  333. test('jsPlumb.getConnections (filtered by a list of scopes, source ids and target ids)', function() {
  334. jsPlumb.detachEverything();
  335. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13'), d14=_addDiv("d14"), d15=_addDiv("d15");
  336. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  337. jsPlumb.connect({source:d13, target:d12, scope:'testScope'});
  338. jsPlumb.connect({source:d11, target:d14, scope:'testScope'});
  339. jsPlumb.connect({source:d11, target:d15, scope:'testScope'});
  340. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  341. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  342. assertContextSize(18);
  343. var c = jsPlumb.getConnections({scope:['testScope','testScope3'], source:['d11'], target:['d14', 'd15']});
  344. equals(c['testScope'].length, 2, 'there are two connections in testScope');
  345. equals(c['testScope3'].length, 0, 'there is no connections in testScope3');
  346. equals(c['testScope2'], null, 'there are no connections in testScope2');
  347. var anEntry = c['testScope'][0];
  348. ok(anEntry.sourceEndpoint != null, "Source endpoint is set");
  349. ok(anEntry.targetEndpoint != null, "Target endpoint is set");
  350. equals(anEntry.source.attr("id"), "d11", "Source is div d11");
  351. equals(anEntry.target.attr("id"), "d14", "Target is div d14");
  352. });
  353. test('connection event listeners', function() {
  354. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  355. var returnedParams = null/*, returnedParams2 = null*/;
  356. jsPlumb.bind("jsPlumbConnection", function(params) {
  357. returnedParams = $.extend({}, params);
  358. });
  359. jsPlumb.connect({source:d1, target:d2});
  360. ok(returnedParams != null, "new connection listener event was fired");
  361. equals(returnedParams.sourceId, "d1", 'sourceid is set');
  362. equals(returnedParams.targetId, "d2", 'targetid is set');
  363. equals(returnedParams.source.attr("id"), "d1", 'source is set');
  364. equals(returnedParams.target.attr("id"), "d2" , 'target is set');
  365. ok(returnedParams.sourceEndpoint != null, "source endpoint is not null");
  366. ok(returnedParams.targetEndpoint != null, "target endpoint is not null");
  367. //jsPlumb.detachAll("d1");
  368. //ok(returnedParams2 != null, "removed connection listener event was fired");
  369. });
  370. test('detach event listeners (detach by connection)', function() {
  371. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  372. var returnedParams = null;
  373. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  374. returnedParams = $.extend({}, params);
  375. });
  376. var conn = jsPlumb.connect({source:d1, target:d2});
  377. jsPlumb.detach(conn);
  378. ok(returnedParams != null, "removed connection listener event was fired");
  379. });
  380. test('detach event listeners (detach by element ids)', function() {
  381. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  382. var returnedParams = null;
  383. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  384. returnedParams = $.extend({}, params);
  385. });
  386. var conn = jsPlumb.connect({source:d1, target:d2});
  387. jsPlumb.detach("d1","d2");
  388. ok(returnedParams != null, "removed connection listener event was fired");
  389. });
  390. test('detach event listeners (detach by elements)', function() {
  391. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  392. var returnedParams = null;
  393. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  394. returnedParams = $.extend({}, params);
  395. });
  396. var conn = jsPlumb.connect({source:d1, target:d2});
  397. jsPlumb.detach(d1,d2);
  398. ok(returnedParams != null, "removed connection listener event was fired");
  399. });
  400. test('detach event listeners (via Endpoint.detach method)', function() {
  401. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  402. var e1 = jsPlumb.addEndpoint(d1, {});
  403. var e2 = jsPlumb.addEndpoint(d2, {});
  404. var returnedParams = null;
  405. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  406. returnedParams = $.extend({}, params);
  407. });
  408. var conn = jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  409. assertContextSize(3);
  410. e1.detach(conn);
  411. ok(returnedParams != null, "removed connection listener event was fired");
  412. assertContextSize(2);
  413. });
  414. test('detach event listeners (via Endpoint.detachFrom method)', function() {
  415. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  416. var e1 = jsPlumb.addEndpoint(d1, {});
  417. var e2 = jsPlumb.addEndpoint(d2, {});
  418. var returnedParams = null;
  419. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  420. returnedParams = $.extend({}, params);
  421. });
  422. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  423. assertContextSize(3);
  424. e1.detachFrom(e2);
  425. ok(returnedParams != null, "removed connection listener event was fired");
  426. assertContextSize(2);
  427. });
  428. test('detach event listeners (via Endpoint.detachAll method)', function() {
  429. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  430. var e1 = jsPlumb.addEndpoint(d1, {});
  431. var e2 = jsPlumb.addEndpoint(d2, {});
  432. var returnedParams = null;
  433. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  434. returnedParams = $.extend({}, params);
  435. });
  436. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  437. assertContextSize(3);
  438. e1.detachAll();
  439. ok(returnedParams != null, "removed connection listener event was fired");
  440. assertContextSize(2);
  441. });
  442. test('detach event listeners (via jsPlumb.deleteEndpoint method)', function() {
  443. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  444. var e1 = jsPlumb.addEndpoint(d1, {});
  445. var e2 = jsPlumb.addEndpoint(d2, {});
  446. var returnedParams = null;
  447. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  448. returnedParams = $.extend({}, params);
  449. });
  450. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  451. assertContextSize(3);
  452. jsPlumb.deleteEndpoint(e1);
  453. ok(returnedParams != null, "removed connection listener event was fired");
  454. assertContextSize(1);
  455. });
  456. test('detach event listeners (ensure cleared by jsPlumb.reset)', function() {
  457. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  458. var returnedParams = null;
  459. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  460. returnedParams = $.extend({}, params);
  461. });
  462. var conn = jsPlumb.connect({source:d1, target:d2});
  463. jsPlumb.detach(d1,d2);
  464. ok(returnedParams != null, "removed connection listener event was fired");
  465. returnedParams = null;
  466. jsPlumb.reset();
  467. var conn = jsPlumb.connect({source:d1, target:d2});
  468. jsPlumb.detach(d1,d2);
  469. ok(returnedParams == null, "connection listener was cleared by jsPlumb.reset()");
  470. });
  471. test('connection events that throw errors', function() {
  472. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  473. var returnedParams = null, returnedParams2 = null;
  474. jsPlumb.bind("jsPlumbConnection", function(params) {
  475. returnedParams = $.extend({}, params);
  476. throw "oh no!";
  477. });
  478. jsPlumb.connect({source:d1, target:d2});
  479. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  480. jsPlumb.connect({source:d3, target:d4});
  481. ok(returnedParams != null, "new connection listener event was fired; we threw an error, jsPlumb survived.");
  482. });
  483. test("Endpoint.detachFrom", function() {
  484. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  485. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  486. ok(e16.anchor, 'endpoint 16 has an anchor');
  487. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  488. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  489. assertConnectionCount(e16, 1);
  490. assertConnectionCount(e17, 1);
  491. assertContextSize(3);
  492. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  493. e16.detachFrom(e17);
  494. assertContextSize(2); // the endpoint canvases should remain
  495. // but the connection should be gone, meaning not registered by jsPlumb and not registered on either Endpoint:
  496. assertConnectionCount(e16, 0);
  497. assertConnectionCount(e17, 0);
  498. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  499. });
  500. test("Endpoint.detachFromConnection", function() {
  501. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  502. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  503. ok(e16.anchor, 'endpoint 16 has an anchor');
  504. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  505. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  506. assertConnectionCount(e16, 1);
  507. assertConnectionCount(e17, 1);
  508. assertContextSize(3);
  509. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  510. e16.detachFromConnection(conn);
  511. assertContextSize(3); // all canvases should remain; the connection was not removed.
  512. // but endpoint e16 should have no connections now.
  513. assertConnectionCount(e16, 0);
  514. assertConnectionCount(e17, 1);
  515. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  516. });
  517. test("Endpoint.detachAll", function() {
  518. var d16 = _addDiv("d16"), d17 = _addDiv("d17"), d18 = _addDiv("d18");
  519. var e16 = jsPlumb.addEndpoint($("#d16"), {isSource:true,maxConnections:-1});
  520. ok(e16.anchor, 'endpoint 16 has an anchor');
  521. var e17 = jsPlumb.addEndpoint($("#d17"), {isSource:true});
  522. var e18 = jsPlumb.addEndpoint($("#d18"), {isSource:true});
  523. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  524. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e18});
  525. assertConnectionCount(e16, 2);
  526. assertConnectionCount(e17, 1);
  527. assertConnectionCount(e18, 1);
  528. assertContextSize(5);
  529. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 2);
  530. e16.detachAll();
  531. assertConnectionCount(e16, 0);
  532. assertConnectionCount(e17, 0);
  533. assertContextSize(3);
  534. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  535. });
  536. test("Endpoint.detach", function() {
  537. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  538. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  539. ok(e16.anchor, 'endpoint 16 has an anchor');
  540. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  541. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  542. assertConnectionCount(e16, 1);
  543. assertConnectionCount(e17, 1);
  544. assertContextSize(3);
  545. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  546. e16.detach(conn);
  547. assertContextSize(2); // the endpoint canvases should remain
  548. // but the connection should be gone, meaning not registered by jsPlumb and not registered on either Endpoint:
  549. assertConnectionCount(e16, 0);
  550. assertConnectionCount(e17, 0);
  551. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  552. });
  553. test("Endpoint.isConnectedTo", function() {
  554. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  555. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  556. ok(e16.anchor, 'endpoint 16 has an anchor');
  557. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  558. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  559. equals(e16.isConnectedTo(e17), true, "e16 and e17 are connected");
  560. });
  561. test("setting endpoint uuid", function() {
  562. var uuid = "14785937583175927504313";
  563. var d16 = _addDiv("d16"), d17 = _addDiv("d17"), d18 = _addDiv("d18");
  564. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  565. equals(e16.getUuid(), uuid, "endpoint's uuid was set correctly");
  566. });
  567. test("jsPlumb.getEndpoint (by uuid)", function() {
  568. var uuid = "14785937583175927504313";
  569. var d16 = _addDiv("d16");
  570. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  571. var e = jsPlumb.getEndpoint(uuid);
  572. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  573. });
  574. test("jsPlumb.deleteEndpoint (by uuid, simple case)", function() {
  575. var uuid = "14785937583175927504313";
  576. var d16 = _addDiv("d16");
  577. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  578. var e = jsPlumb.getEndpoint(uuid);
  579. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  580. jsPlumb.deleteEndpoint(uuid);
  581. var f = jsPlumb.getEndpoint(uuid);
  582. equals(f, null, "endpoint has been deleted");
  583. var ebe = jsPlumb.getTestHarness().endpointsByElement["d16"];
  584. equals(ebe.length, 0, "no endpoints registered for element d16 anymore");
  585. assertContextSize(0);
  586. });
  587. test("jsPlumb.deleteEndpoint (by uuid, connections too)", function() {
  588. // create two endpoints (one with a uuid), add them to two divs and then connect them.
  589. var uuid = "14785937583175927504313";
  590. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  591. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  592. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  593. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  594. // check that the connection was ok.
  595. equals(e16.connections.length, 1, "e16 has one connection");
  596. equals(e17.connections.length, 1, "e17 has one connection");
  597. assertContextSize(3);
  598. // delete the endpoint that has a uuid. verify that the endpoint cannot be retrieved and that the connection has been removed, but that
  599. // element d17 still has its Endpoint.
  600. jsPlumb.deleteEndpoint(uuid);
  601. var f = jsPlumb.getEndpoint(uuid);
  602. equals(f, null, "endpoint has been deleted");
  603. equals(e16.connections.length, 0, "e16 has no connections");
  604. equals(e17.connections.length, 0, "e17 has no connections");
  605. var ebe = jsPlumb.getTestHarness().endpointsByElement["d16"];
  606. equals(ebe.length, 0, "no endpoints registered for element d16 anymore");
  607. ebe = jsPlumb.getTestHarness().endpointsByElement["d17"];
  608. equals(ebe.length, 1, "element d17 still has its Endpoint");
  609. assertContextSize(1);
  610. // now delete d17's endpoint and check that it has gone.
  611. jsPlumb.deleteEndpoint(e17);
  612. f = jsPlumb.getEndpoint(e17);
  613. equals(f, null, "endpoint has been deleted");
  614. ebe = jsPlumb.getTestHarness().endpointsByElement["d17"];
  615. equals(ebe.length, 0, "element d17 no longer has any Endpoints");
  616. assertContextSize(0);
  617. });
  618. test("jsPlumb.deleteEndpoint (by reference, simple case)", function() {
  619. var uuid = "14785937583175927504313";
  620. var d16 = _addDiv("d16");
  621. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  622. var e = jsPlumb.getEndpoint(uuid);
  623. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  624. jsPlumb.deleteEndpoint(e16);
  625. var f = jsPlumb.getEndpoint(uuid);
  626. equals(f, null, "endpoint has been deleted");
  627. assertContextSize(0);
  628. });
  629. test("jsPlumb.deleteEndpoint (by reference, connections too)", function() {
  630. var uuid = "14785937583175927504313";
  631. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  632. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  633. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  634. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  635. equals(e16.connections.length, 1, "e16 has one connection");
  636. equals(e17.connections.length, 1, "e17 has one connection");
  637. assertContextSize(3);
  638. jsPlumb.deleteEndpoint(e16);
  639. var f = jsPlumb.getEndpoint(uuid);
  640. equals(f, null, "endpoint has been deleted");
  641. equals(e16.connections.length, 0, "e16 has no connections");
  642. equals(e17.connections.length, 0, "e17 has no connections");
  643. assertContextSize(1);
  644. });
  645. test("jsPlumb.deleteEveryEndpoint", function() {
  646. var uuid = "14785937583175927504313";
  647. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  648. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  649. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  650. assertContextSize(2);
  651. var e = jsPlumb.getEndpoint(uuid);
  652. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  653. jsPlumb.deleteEveryEndpoint();
  654. var f = jsPlumb.getEndpoint(uuid);
  655. equals(f, null, "endpoint e16 has been deleted");
  656. var g = jsPlumb.getEndpoint(e17);
  657. equals(g, null, "endpoint e17 has been deleted");
  658. assertContextSize(0);
  659. });
  660. test("jsPlumb.deleteEveryEndpoint (connections too)", function() {
  661. var uuid = "14785937583175927504313";
  662. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  663. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  664. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  665. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  666. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  667. assertContextSize(3);
  668. var e = jsPlumb.getEndpoint(uuid);
  669. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  670. jsPlumb.deleteEveryEndpoint();
  671. var f = jsPlumb.getEndpoint(uuid);
  672. equals(f, null, "endpoint e16 has been deleted");
  673. var g = jsPlumb.getEndpoint(e17);
  674. equals(g, null, "endpoint e17 has been deleted");
  675. assertContextSize(0); // no canvases. so all connection canvases have been cleaned up too.
  676. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  677. });
  678. test("jsPlumb.addEndpoint (simple case)", function() {
  679. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  680. var e16 = jsPlumb.addEndpoint(d16, {anchor:[0,0.5,0,-1]});
  681. var e17 = jsPlumb.addEndpoint(d17, {anchor:"TopCenter"});
  682. assertContextSize(2);
  683. equals(e16.anchor.x, 0);
  684. equals(e16.anchor.y, 0.5);
  685. equals(e17.anchor.x, 0.5);
  686. equals(e17.anchor.y, 0);
  687. });
  688. test("jsPlumb.addEndpoint (simple case, dynamic anchors)", function() {
  689. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  690. var e16 = jsPlumb.addEndpoint(d16, {anchors:[[0,0.5,0,-1], [1,0.5,0,1]]});
  691. var e17 = jsPlumb.addEndpoint(d17, {anchors:["TopCenter", "BottomCenter"]});
  692. assertContextSize(2);
  693. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  694. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  695. });
  696. test("jsPlumb.addEndpoint (simple case, two arg method)", function() {
  697. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  698. var e16 = jsPlumb.addEndpoint(d16, {isSource:true, isTarget:false}, {anchor:[0,0.5,0,-1]});
  699. var e17 = jsPlumb.addEndpoint(d17, {isTarget:true, isSource:false}, {anchor:"TopCenter"});
  700. assertContextSize(2);
  701. equals(e16.anchor.x, 0);
  702. equals(e16.anchor.y, 0.5);
  703. equals(false, e16.isTarget);
  704. equals(true, e16.isSource);
  705. equals(e17.anchor.x, 0.5);
  706. equals(e17.anchor.y, 0);
  707. equals(true, e17.isTarget);
  708. equals(false, e17.isSource);
  709. });
  710. test("jsPlumb.addEndpoints (simple case)", function() {
  711. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  712. var e16 = jsPlumb.addEndpoints(d16, [{isSource:true, isTarget:false, anchor:[0,0.5,0,-1] }, { isTarget:true, isSource:false, anchor:"TopCenter" }]);
  713. assertContextSize(2);
  714. equals(e16[0].anchor.x, 0);
  715. equals(e16[0].anchor.y, 0.5);
  716. equals(false, e16[0].isTarget);
  717. equals(true, e16[0].isSource);
  718. equals(e16[1].anchor.x, 0.5);
  719. equals(e16[1].anchor.y, 0);
  720. equals(true, e16[1].isTarget);
  721. equals(false, e16[1].isSource);
  722. });
  723. test("jsPlumb.addEndpoints (with reference params)", function() {
  724. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  725. var refParams = {anchor:"RightMiddle"};
  726. var e16 = jsPlumb.addEndpoints(d16, [{isSource:true, isTarget:false}, { isTarget:true, isSource:false }], refParams);
  727. assertContextSize(2);
  728. equals(e16[0].anchor.x, 1);
  729. equals(e16[0].anchor.y, 0.5);
  730. equals(false, e16[0].isTarget);
  731. equals(true, e16[0].isSource);
  732. equals(e16[1].anchor.x, 1);
  733. equals(e16[1].anchor.y, 0.5);
  734. equals(true, e16[1].isTarget);
  735. equals(false, e16[1].isSource);
  736. });
  737. test("jsPlumb.addEndpoint (simple case, dynamic anchors, two arg method)", function() {
  738. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  739. var e16 = jsPlumb.addEndpoint(d16, {isSource:true, isTarget:false}, {anchors:[[0,0.5,0,-1], [1,0.5,0,1]]});
  740. var e17 = jsPlumb.addEndpoint(d17, {isTarget:true, isSource:false}, {anchors:["TopCenter", "BottomCenter"]});
  741. assertContextSize(2);
  742. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  743. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  744. });
  745. test('jsPlumb.connect (between two Endpoints)', function() {
  746. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  747. var e = jsPlumb.addEndpoint(d1, {});
  748. var e2 = jsPlumb.addEndpoint(d2, {});
  749. ok(e, 'endpoint e exists');
  750. ok(e2, 'endpoint e2 exists');
  751. assertContextSize(2); // should have a canvas for each endpoint now.
  752. assertEndpointCount("d1", 1);
  753. assertEndpointCount("d2", 1);
  754. jsPlumb.connect({target:'d2', sourceEndpoint:e, targetEndpoint:e2});
  755. assertEndpointCount("d1", 1); // no new endpoint should have been added
  756. assertEndpointCount("d2", 1); // no new endpoint should have been added
  757. assertContextSize(3); // now we should also have a canvas for the connection.
  758. });
  759. test('jsPlumb.connect (between two Endpoints, and dont supply any parameters to the Endpoints.)', function() {
  760. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  761. var e = jsPlumb.addEndpoint(d1);
  762. var e2 = jsPlumb.addEndpoint(d2);
  763. ok(e, 'endpoint e exists');
  764. ok(e2, 'endpoint e2 exists');
  765. assertContextSize(2); // should have a canvas for each endpoint now.
  766. assertEndpointCount("d1", 1);
  767. assertEndpointCount("d2", 1);
  768. jsPlumb.connect({target:'d2', sourceEndpoint:e, targetEndpoint:e2});
  769. assertEndpointCount("d1", 1); // no new endpoint should have been added
  770. assertEndpointCount("d2", 1); // no new endpoint should have been added
  771. assertContextSize(3); // now we should also have a canvas for the connection.
  772. });
  773. test('jsPlumb.connect (by endpoint)', function() {
  774. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  775. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  776. ok(e16.anchor, 'endpoint 16 has an anchor');
  777. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  778. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  779. assertContextSize(3);
  780. });
  781. test("jsPlumb.connect (two Endpoints - that have been already added - by UUID)", function() {
  782. var srcEndpointUuid = "14785937583175927504313", dstEndpointUuid = "14785937583175927534325";
  783. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  784. var e1 = jsPlumb.addEndpoint("d16", {isSource:true, maxConnections:-1, uuid:srcEndpointUuid});
  785. var e2 = jsPlumb.addEndpoint("d17", {isSource:true, maxConnections:-1, uuid:dstEndpointUuid});
  786. jsPlumb.connect({ uuids: [ srcEndpointUuid, dstEndpointUuid ] });
  787. assertConnectionCount(e1, 1);
  788. assertConnectionCount(e2, 1);
  789. assertContextSize(3);
  790. });
  791. test("jsPlumb.connect (two Endpoints - that have not been already added - by UUID)", function() {
  792. var srcEndpointUuid = "14785937583175927504313", dstEndpointUuid = "14785937583175927534325";
  793. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  794. jsPlumb.connect({ uuids: [ srcEndpointUuid, dstEndpointUuid ], source:d16, target:d17 });
  795. assertContextSize(3);
  796. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  797. var e1 = jsPlumb.getEndpoint(srcEndpointUuid);
  798. ok(e1 != null, "endpoint with src uuid added");
  799. ok(e1.canvas != null);
  800. var e2 = jsPlumb.getEndpoint(dstEndpointUuid);
  801. ok(e2 != null, "endpoint with target uuid added");
  802. assertConnectionCount(e1, 1);
  803. assertConnectionCount(e2, 1);
  804. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  805. });
  806. test("jsPlumb.connect (two Endpoints - that have been already added - by endpoint reference)", function() {
  807. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  808. var e1 = jsPlumb.addEndpoint("d16", {isSource:true, maxConnections:-1});
  809. var e2 = jsPlumb.addEndpoint("d17", {isSource:true, maxConnections:-1});
  810. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  811. assertContextSize(3);
  812. assertConnectionCount(e1, 1);
  813. assertConnectionCount(e2, 1);
  814. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  815. });
  816. test("jsPlumb.connect (two elements)", function() {
  817. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  818. jsPlumb.connect({ source:d16, target:d17 });
  819. assertContextSize(3);
  820. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  821. });
  822. test("jsPlumb.connect (Connector test, straight)", function() {
  823. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  824. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight" });
  825. assertContextSize(3);
  826. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  827. equals(conn.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  828. });
  829. test("jsPlumb.connect (Connector test, bezier, no params)", function() {
  830. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  831. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Bezier" });
  832. assertContextSize(3);
  833. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  834. equals(conn.connector.constructor, jsPlumb.Connectors.Bezier, "Bezier connector chosen for connection");
  835. equals(conn.connector.majorAnchor, 150, "Bezier connector chose 150 curviness");
  836. });
  837. test("jsPlumb.connect (Connector test, bezier, curviness as int)", function() {
  838. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  839. var conn = jsPlumb.connect({ source:d16, target:d17, connector:["Bezier", 200] });
  840. assertContextSize(3);
  841. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  842. equals(conn.connector.constructor, jsPlumb.Connectors.Bezier, "Bezier connector chosen for connection");
  843. equals(conn.connector.majorAnchor, 200, "Bezier connector chose 200 curviness");
  844. });
  845. test("jsPlumb.connect (Connector test, bezier, curviness as named option)", function() {
  846. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  847. var conn = jsPlumb.connect({ source:d16, target:d17, connector:["Bezier", {curviness:300}] });
  848. assertContextSize(3);
  849. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  850. equals(conn.connector.constructor, jsPlumb.Connectors.Bezier, "Bezier connector chosen for connection");
  851. equals(conn.connector.majorAnchor, 300, "Bezier connector chose 300 curviness");
  852. });
  853. test("jsPlumb.connect (anchors registered correctly; source and target anchors given, as arrays)", function() {
  854. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  855. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight", anchors:[[0.3,0.3,1,0], [0.7,0.7,0,1]] });
  856. assertContextSize(3);
  857. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  858. equals(conn.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  859. equals(0.3, conn.endpoints[0].anchor.x, "source anchor x");
  860. equals(0.3, conn.endpoints[0].anchor.y, "source anchor y");
  861. equals(0.7, conn.endpoints[1].anchor.x, "target anchor x");
  862. equals(0.7, conn.endpoints[1].anchor.y, "target anchor y");
  863. });
  864. test("jsPlumb.connect (anchors registered correctly; source and target anchors given, as strings)", function() {
  865. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  866. var conn = jsPlumb.connect({ source:d16, target:d17, connector:new jsPlumb.Connectors.Straight(), anchors:["LeftMiddle", "RightMiddle"] });
  867. assertContextSize(3);
  868. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  869. equals(conn.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  870. equals(0, conn.endpoints[0].anchor.x, "source anchor x");
  871. equals(0.5, conn.endpoints[0].anchor.y, "source anchor y");
  872. equals(1, conn.endpoints[1].anchor.x, "target anchor x");
  873. equals(0.5, conn.endpoints[1].anchor.y, "target anchor y");
  874. });
  875. test("jsPlumb.connect (anchors registered correctly; source and target anchors given, as arrays)", function() {
  876. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  877. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight", anchors:[[0.3,0.3,1,0], [0.7,0.7,0,1]] });
  878. assertContextSize(3);
  879. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  880. equals(conn.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  881. equals(0.3, conn.endpoints[0].anchor.x, "source anchor x");
  882. equals(0.3, conn.endpoints[0].anchor.y, "source anchor y");
  883. equals(0.7, conn.endpoints[1].anchor.x, "target anchor x");
  884. equals(0.7, conn.endpoints[1].anchor.y, "target anchor y");
  885. });
  886. test("jsPlumb.connect (two argument method in which some data is reused across connections)", function() {
  887. var d16 = _addDiv("d16"), d17 = _addDiv("d17"), d18 = _addDiv("d18"), d19 = _addDiv("d19");
  888. var sharedData = { connector:"Straight", anchors:[[0.3,0.3,1,0], [0.7,0.7,0,1]] };
  889. var conn = jsPlumb.connect({ source:d16, target:d17}, sharedData);
  890. var conn2 = jsPlumb.connect({ source:d18, target:d19}, sharedData);
  891. assertContextSize(6);
  892. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 2);
  893. equals(conn.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  894. equals(conn2.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  895. equals(0.3, conn.endpoints[0].anchor.x, "source anchor x");
  896. equals(0.3, conn.endpoints[0].anchor.y, "source anchor y");
  897. equals(0.7, conn.endpoints[1].anchor.x, "target anchor x");
  898. equals(0.7, conn.endpoints[1].anchor.y, "target anchor y");
  899. equals(0.3, conn2.endpoints[0].anchor.x, "source anchor x");
  900. equals(0.3, conn2.endpoints[0].anchor.y, "source anchor y");
  901. equals(0.7, conn2.endpoints[1].anchor.x, "target anchor x");
  902. equals(0.7, conn2.endpoints[1].anchor.y, "target anchor y");
  903. });
  904. test("jsPlumb.connect (Connector as string test)", function() {
  905. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  906. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight" });
  907. assertContextSize(3);
  908. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  909. equals(conn.connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  910. });
  911. test("jsPlumb.connect (Endpoint test)", function() {
  912. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  913. var conn = jsPlumb.connect({ source:d16, target:d17, endpoint:new jsPlumb.Endpoints.Rectangle() });
  914. assertContextSize(3);
  915. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  916. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection source");
  917. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection target");
  918. });
  919. test("jsPlumb.connect (Endpoint as string test)", function() {
  920. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  921. var conn = jsPlumb.connect({ source:d16, target:d17, endpoint:"Rectangle" });
  922. assertContextSize(3);
  923. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  924. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection source");
  925. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection target");
  926. });
  927. test("jsPlumb.connect (Endpoints test)", function() {
  928. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  929. var conn = jsPlumb.connect({ source:d16, target:d17, endpoints:[new jsPlumb.Endpoints.Rectangle(),new jsPlumb.Endpoints.Dot()] });
  930. assertContextSize(3);
  931. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  932. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection source");
  933. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints.Dot, "Dot endpoint chosen for connection target");
  934. });
  935. test("jsPlumb.connect (Endpoint as string test)", function() {
  936. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  937. var conn = jsPlumb.connect({ source:d16, target:d17, endpoints:["Rectangle", "Dot" ] });
  938. assertContextSize(3);
  939. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  940. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints.Rectangle, "Rectangle endpoint chosen for connection source");
  941. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints.Dot, "Dot endpoint chosen for connection target");
  942. });
  943. test("jsPlumb.connect (by Endpoints, connector test)", function() {
  944. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  945. var e16 = jsPlumb.addEndpoint(d16, {});
  946. var e17 = jsPlumb.addEndpoint(d17, {});
  947. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:new jsPlumb.Connectors.Straight() });
  948. assertContextSize(3);
  949. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  950. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  951. });
  952. test("jsPlumb.connect (by Endpoints, connector as string test)", function() {
  953. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  954. var e16 = jsPlumb.addEndpoint(d16, {});
  955. var e17 = jsPlumb.addEndpoint(d17, {});
  956. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  957. assertContextSize(3);
  958. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  959. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  960. });
  961. test("jsPlumb.connect (by Endpoints, anchors as string test)", function() {
  962. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  963. var a16 = "TopCenter", a17 = "BottomCenter";
  964. var e16 = jsPlumb.addEndpoint(d16, {anchor:a16});
  965. var e17 = jsPlumb.addEndpoint(d17, {anchor:a17});
  966. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  967. assertContextSize(3);
  968. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  969. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  970. equals(e16.anchor.x, 0.5, "endpoint 16 is at top center");equals(e16.anchor.y, 0, "endpoint 16 is at top center");
  971. equals(e17.anchor.x, 0.5, "endpoint 17 is at bottom center");equals(e17.anchor.y, 1, "endpoint 17 is at bottom center");
  972. });
  973. test("jsPlumb.connect (by Endpoints, endpoints create anchors)", function() {
  974. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  975. var a16 = [0,0.5,0,-1], a17 = [1,0.0,-1,-1];
  976. var e16 = jsPlumb.addEndpoint(d16, {anchor:a16});
  977. var e17 = jsPlumb.addEndpoint(d17, {anchor:a17});
  978. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  979. assertContextSize(3);
  980. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  981. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  982. equals(e16.anchor.x, a16[0]);equals(e16.anchor.y, a16[1]);
  983. equals(e17.anchor.x, a17[0]);equals(e17.anchor.y, a17[1]);
  984. equals(e16.anchor.getOrientation()[0], a16[2]); equals(e16.anchor.getOrientation()[1], a16[3]);
  985. equals(e17.anchor.getOrientation()[0], a17[2]); equals(e17.anchor.getOrientation()[1], a17[3]);
  986. });
  987. test("jsPlumb.connect (by Endpoints, endpoints create dynamic anchors; anchors specified by 'anchor')", function() {
  988. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  989. var e16 = jsPlumb.addEndpoint(d16, {anchor:[[0,0.5,0,-1], [1,0.5,0,1]]});
  990. var e17 = jsPlumb.addEndpoint(d17, {anchor:["TopCenter", "BottomCenter"]});
  991. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  992. assertContextSize(3);
  993. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  994. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  995. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  996. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  997. });
  998. test("jsPlumb.connect (by Endpoints, endpoints create dynamic anchors; anchors specified by 'anchors')", function() {
  999. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1000. var e16 = jsPlumb.addEndpoint(d16, {anchors:[[0,0.5,0,-1], [1,0.5,0,1]]});
  1001. var e17 = jsPlumb.addEndpoint(d17, {anchors:["TopCenter", "BottomCenter"]});
  1002. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  1003. assertContextSize(3);
  1004. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1005. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors.Straight, "Straight connector chosen for connection");
  1006. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  1007. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  1008. });
  1009. test("jsPlumb.connect (connect by element, default endpoint, supplied dynamic anchors)", function() {
  1010. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1011. var anchors = [ [0.25, 0, 0, -1], [1, 0.25, 1, 0], [0.75, 1, 0, 1], [0, 0.75, -1, 0] ];
  1012. jsPlumb.connect({source:d1, target:d2, dynamicAnchors:anchors}); // auto connect with default endpoint and provided anchors
  1013. assertContextSize(3);
  1014. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1015. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1016. jsPlumb.detach({source:d1, target:d2});
  1017. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1018. assertContextSize(2);
  1019. });
  1020. test("jsPlumb.connect (connect by element, supplied endpoint and dynamic anchors)", function() {
  1021. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1022. var endpoint = { isSource:true };
  1023. var e1 = jsPlumb.addEndpoint(d1, endpoint);
  1024. var e2 = jsPlumb.addEndpoint(d2, endpoint);
  1025. var anchors = [ "TopCenter", "BottomCenter" ];
  1026. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2, dynamicAnchors:anchors});
  1027. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1028. assertContextSize(3);
  1029. jsPlumb.detach({source:d1, target:d2});
  1030. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1031. assertContextSize(2);
  1032. });
  1033. test("jsPlumb.connect (connect by element, supplied endpoints using 'source' and 'target' (this test is identical to the one above apart from the param names), and dynamic anchors)", function() {
  1034. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1035. var endpoint = { isSource:true };
  1036. var e1 = jsPlumb.addEndpoint(d1, endpoint);
  1037. var e2 = jsPlumb.addEndpoint(d2, endpoint);
  1038. var anchors = [ "TopCenter", "BottomCenter" ];
  1039. jsPlumb.connect({source:e1, target:e2, dynamicAnchors:anchors});
  1040. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1041. assertContextSize(3);
  1042. jsPlumb.detach({source:d1, target:d2});
  1043. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1044. assertContextSize(2);
  1045. });
  1046. test("jsPlumb.connect (testing for connection event callback)", function() {
  1047. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1048. var connectCallback = null, detachCallback = null;
  1049. jsPlumb.bind("jsPlumbConnection", function(params) {
  1050. connectCallback = $.extend({}, params);
  1051. });
  1052. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  1053. detachCallback = $.extend({}, params);
  1054. });
  1055. jsPlumb.connect({source:d1, target:d2}); // auto connect with default endpoint and anchor set
  1056. ok(connectCallback != null, "connect callback was made");
  1057. assertContextSize(3);
  1058. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1059. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1060. jsPlumb.detach({source:d1, target:d2});
  1061. assertContextSize(2);
  1062. ok(detachCallback != null, "detach callback was made");
  1063. });
  1064. test("jsPlumb.connect (overlays, long-hand version)", function() {
  1065. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1066. var imageEventListener = function() { };
  1067. var arrowSpec = {width:40,length:40,location:0.7, foldback:0, paintStyle:{lineWidth:1, strokeStyle:"#000000"}};
  1068. var connection1 = jsPlumb.connect({
  1069. source:d1,
  1070. target:d2,
  1071. anchors:["BottomCenter", [ 0.75,0,0,-1 ]],
  1072. overlays : [ new jsPlumb.Overlays.Image({src:"../img/littledot.png", events:{"click":imageEventListener("window1", "window2")}}),
  1073. new jsPlumb.Overlays.Label({label:"CONNECTION 1", location:0.3}),
  1074. new jsPlumb.Overlays.Arrow(arrowSpec) ]
  1075. });
  1076. equals(3, connection1.overlays.length);
  1077. equals(jsPlumb.Overlays.Image, connection1.overlays[0].constructor);
  1078. equals(jsPlumb.Overlays.Label, connection1.overlays[1].constructor);
  1079. equals(jsPlumb.Overlays.Arrow, connection1.overlays[2].constructor);
  1080. equals(0.7, connection1.overlays[2].loc);
  1081. equals(40, connection1.overlays[2].width);
  1082. equals(40, connection1.overlays[2].length);
  1083. });
  1084. test("jsPlumb.connect (overlays, short-hand version)", function() {
  1085. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1086. var imageEventListener = function() { };
  1087. var loc = { location:0.7 };
  1088. var arrowSpec = { width:40,length:40, foldback:0, paintStyle:{lineWidth:1, strokeStyle:"#000000"} };
  1089. var connection1 = jsPlumb.connect({
  1090. source:d1,
  1091. target:d2,
  1092. anchors:["BottomCenter", [ 0.75,0,0,-1 ]],
  1093. overlays : [ [ "Image", { src:"../img/littledot.png", events:{"click":imageEventListener("window1", "window2")}} ],
  1094. ["Label", {label:"CONNECTION 1", location:0.3}],
  1095. ["Arrow", arrowSpec, loc] ]
  1096. });
  1097. equals(3, connection1.overlays.length);
  1098. equals(jsPlumb.Overlays.Image, connection1.overlays[0].constructor);
  1099. equals(jsPlumb.Overlays.Label, connection1.overlays[1].constructor);
  1100. equals(jsPlumb.Overlays.Arrow, connection1.overlays[2].constructor);
  1101. equals(0.7, connection1.overlays[2].loc);
  1102. equals(40, connection1.overlays[2].width);
  1103. equals(40, connection1.overlays[2].length);
  1104. });
  1105. test("jsPlumb.connect, specify arrow overlay using string identifier only", function() {
  1106. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1107. var conn = jsPlumb.connect({source:d1,target:d2,overlays:["Arrow"]});
  1108. equals(jsPlumb.Overlays.Arrow, conn.overlays[0].constructor);
  1109. });
  1110. // this test is for the original detach function; it should stay working after i mess with it
  1111. // a little.
  1112. test("jsPlumb.detach (by element ids)", function() {
  1113. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1114. var e1 = jsPlumb.addEndpoint(d1);
  1115. var e2 = jsPlumb.addEndpoint(d2);
  1116. var e3 = jsPlumb.addEndpoint(d1);
  1117. var e4 = jsPlumb.addEndpoint(d2);
  1118. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1119. jsPlumb.connect({ sourceEndpoint:e3, targetEndpoint:e4 }); // make two connections to be sure this works ;)
  1120. assertConnectionCount(e1, 1);
  1121. assertConnectionCount(e2, 1);
  1122. assertConnectionCount(e3, 1);
  1123. assertConnectionCount(e4, 1);
  1124. jsPlumb.detach("d1", "d2");
  1125. assertConnectionCount(e1, 0);
  1126. assertConnectionCount(e2, 0);
  1127. assertConnectionCount(e3, 0);
  1128. assertConnectionCount(e4, 0);
  1129. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1130. });
  1131. // detach is being made to operate more like connect - by taking one argument with a whole
  1132. // bunch of possible params in it. if two args are passed in it will continue working
  1133. // in the old way.
  1134. test("jsPlumb.detach (params object, using element ids)", function() {
  1135. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1136. var e1 = jsPlumb.addEndpoint(d1);
  1137. var e2 = jsPlumb.addEndpoint(d2);
  1138. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1139. assertConnectionCount(e1, 1);
  1140. assertConnectionCount(e2, 1);
  1141. jsPlumb.detach({source:"d1", target:"d2"});
  1142. assertConnectionCount(e1, 0);
  1143. assertConnectionCount(e2, 0);
  1144. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1145. });
  1146. test("jsPlumb.detach (params object, using element objects)", function() {
  1147. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1148. var e1 = jsPlumb.addEndpoint(d1);
  1149. var e2 = jsPlumb.addEndpoint(d2);
  1150. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1151. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1152. assertContextSize(3);
  1153. assertConnectionCount(e1, 1);
  1154. assertConnectionCount(e2, 1);
  1155. jsPlumb.detach({source:d1, target:d2});
  1156. assertConnectionCount(e1, 0);
  1157. assertConnectionCount(e2, 0);
  1158. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1159. assertContextSize(2);
  1160. });
  1161. test("jsPlumb.detach (source and target as endpoint UUIDs)", function() {
  1162. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1163. var e1 = jsPlumb.addEndpoint(d1, {uuid:"abcdefg"});
  1164. ok(jsPlumb.getEndpoint("abcdefg") != null, "e1 exists");
  1165. var e2 = jsPlumb.addEndpoint(d2, {uuid:"hijklmn"});
  1166. ok(jsPlumb.getEndpoint("hijklmn") != null, "e2 exists");
  1167. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1168. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1169. assertContextSize(3);
  1170. assertConnectionCount(e1, 1);
  1171. assertConnectionCount(e2, 1);
  1172. jsPlumb.detach({uuids:["abcdefg", "hijklmn"]});
  1173. assertConnectionCount(e1, 0);
  1174. assertConnectionCount(e2, 0);
  1175. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1176. assertContextSize(2);
  1177. });
  1178. test("jsPlumb.detach (sourceEndpoint and targetEndpoint supplied)", function() {
  1179. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1180. var e1 = jsPlumb.addEndpoint(d1);
  1181. var e2 = jsPlumb.addEndpoint(d2);
  1182. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1183. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1184. assertContextSize(3);
  1185. assertConnectionCount(e1, 1);
  1186. assertConnectionCount(e2, 1);
  1187. jsPlumb.detach({ sourceEndpoint:e1, targetEndpoint:e2 });
  1188. assertConnectionCount(e1, 0);
  1189. assertConnectionCount(e2, 0);
  1190. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1191. assertContextSize(2);
  1192. });
  1193. test("jsPlumb.makeDynamicAnchors (longhand)", function() {
  1194. var anchors = [jsPlumb.makeAnchor(0.2, 0, 0, -1), jsPlumb.makeAnchor(1, 0.2, 1, 0),
  1195. jsPlumb.makeAnchor(0.8, 1, 0, 1), jsPlumb.makeAnchor(0, 0.8, -1, 0) ];
  1196. var dynamicAnchor = jsPlumb.makeDynamicAnchor(anchors);
  1197. var a = dynamicAnchor.getAnchors();
  1198. equals(a.length, 4, "Dynamic Anchors has four anchors");
  1199. for (var i = 0; i < a.length; i++)
  1200. ok(a[i].compute.constructor == Function, "anchor " + i + " well formed");
  1201. });
  1202. test("jsPlumb.makeDynamicAnchors (shorthand)", function() {
  1203. var anchors = [[0.2, 0, 0, -1], [1, 0.2, 1, 0],
  1204. [0.8, 1, 0, 1], [0, 0.8, -1, 0] ];
  1205. var dynamicAnchor = jsPlumb.makeDynamicAnchor(anchors);
  1206. var a = dynamicAnchor.getAnchors();
  1207. equals(a.length, 4, "Dynamic Anchors has four anchors");
  1208. for (var i = 0; i < a.length; i++)
  1209. ok(a[i].compute.constructor == Function, "anchor " + i + " well formed");
  1210. });
  1211. test("Connection.isVisible/setVisible", function() {
  1212. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1213. var c1 = jsPlumb.connect({source:d1,target:d2});
  1214. equals(true, c1.isVisible(), "Connection is visible after creation.");
  1215. c1.setVisible(false);
  1216. equals(false, c1.isVisible(), "Connection is not visible after calling setVisible(false).");
  1217. equals($(c1.canvas).css("display"), "none");
  1218. c1.setVisible(true);
  1219. equals(true, c1.isVisible(), "Connection is visible after calling setVisible(true).");
  1220. equals($(c1.canvas).css("display"), "block");
  1221. });
  1222. test("Endpoint.isVisible/setVisible basic test (no connections)", function() {
  1223. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1224. var e1 = jsPlumb.addEndpoint(d1);
  1225. equals(true, e1.isVisible(), "Endpoint is visible after creation.");
  1226. e1.setVisible(false);
  1227. equals(false, e1.isVisible(), "Endpoint is not visible after calling setVisible(false).");
  1228. equals($(e1.canvas).css("display"), "none");
  1229. e1.setVisible(true);
  1230. equals(true, e1.isVisible(), "Endpoint is visible after calling setVisible(true).");
  1231. equals($(e1.canvas).css("display"), "block");
  1232. });
  1233. test("Endpoint.isVisible/setVisible (one connection, other Endpoint's visibility should track changes in the source, because it has only this connection.)", function() {
  1234. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1235. var e1 = jsPlumb.addEndpoint(d1), e2 = jsPlumb.addEndpoint(d2);
  1236. equals(true, e1.isVisible(), "Endpoint is visible after creation.");
  1237. var c1 = jsPlumb.connect({source:e1, target:e2});
  1238. e1.setVisible(false);
  1239. equals(false, e1.isVisible(), "Endpoint is not visible after calling setVisible(false).");
  1240. equals(false, e2.isVisible(), "other Endpoint is not visible either.");
  1241. equals(false, c1.isVisible(), "connection between the two is not visible either.");
  1242. e1.setVisible(true);
  1243. equals(true, e1.isVisible(), "Endpoint is visible after calling setVisible(true).");
  1244. equals(true, e2.isVisible(), "other Endpoint is visible too");
  1245. equals(true, c1.isVisible(), "connection between the two is visible too.");
  1246. });
  1247. test("Endpoint.isVisible/setVisible (one connection, other Endpoint's visibility should not track changes in the source, because it has another connection.)", function() {
  1248. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1249. var e1 = jsPlumb.addEndpoint(d1), e2 = jsPlumb.addEndpoint(d2, { maxConnections:2 }), e3 = jsPlumb.addEndpoint(d3);
  1250. equals(true, e1.isVisible(), "Endpoint is visible after creation.");
  1251. var c1 = jsPlumb.connect({source:e1, target:e2});
  1252. var c2 = jsPlumb.connect({source:e2, target:e3});
  1253. e1.setVisible(false);
  1254. equals(false, e1.isVisible(), "Endpoint is not visible after calling setVisible(false).");
  1255. equals(true, e2.isVisible(), "other Endpoint should still be visible.");
  1256. equals(true, e3.isVisible(), "third Endpoint should still be visible.");
  1257. equals(false, c1.isVisible(), "connection between the two is not visible either.");
  1258. equals(true, c2.isVisible(), "other connection is visible.");
  1259. e1.setVisible(true);
  1260. equals(true, e1.isVisible(), "Endpoint is visible after calling setVisible(true).");
  1261. equals(true, e2.isVisible(), "other Endpoint is visible too");
  1262. equals(true, c1.isVisible(), "connection between the two is visible too.");
  1263. equals(true, c2.isVisible(), "other connection is visible.");
  1264. });
  1265. /**
  1266. * leave this test at the bottom!
  1267. */
  1268. test('unload test', function() {
  1269. jsPlumb.unload();
  1270. var contextNode = $("._jsPlumb_context");
  1271. ok(contextNode.length == 0, 'context node unloaded');
  1272. });
  1273. });