jsPlumb-1.3.4-tests.js 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  1. // jsplumb qunit tests.
  2. var _getContextNode = function() {
  3. return $("#container");
  4. };
  5. var assertContextExists = function() {
  6. ok(_getContextNode().length == 1, "context node exists");
  7. };
  8. var assertContextSize = function(elementCount) {
  9. //equals(_getContextNode().children().length - _divs.length, elementCount, 'context has ' + elementCount + ' children');
  10. };
  11. var assertContextEmpty = function() {
  12. equals(_getContextNode().children.length, 0, "context empty");
  13. };
  14. var assertEndpointCount = function(elId, count) {
  15. equals(jsPlumb.getTestHarness().endpointCount(elId), count, elId + " has " + count + (count > 1) ? "endpoints" : "endpoint");
  16. };
  17. var assertConnectionCount = function(endpoint, count) {
  18. equals(endpoint.connections.length, count, "endpoint has " + count + " connections");
  19. };
  20. var assertConnectionByScopeCount = function(scope, count) {
  21. equals(jsPlumb.getTestHarness().connectionCount(scope), count, 'Scope ' + scope + " has " + count + (count > 1) ? "connections" : "connection");
  22. };
  23. var _divs = [];
  24. var _addDiv = function(id) {
  25. var d1 = document.createElement("div");
  26. _getContextNode().append(d1);
  27. d1 = jsPlumb.CurrentLibrary.getElementObject(d1);
  28. jsPlumb.CurrentLibrary.setAttribute(d1, "id", id);
  29. _divs.push(id);
  30. return d1;
  31. };
  32. var _cleanup = function() {
  33. jsPlumb.reset();
  34. jsPlumb.Defaults.Container = null;
  35. jsPlumb.Defaults.ConnectionsDetachable = true;
  36. for (var i in _divs) {
  37. $("#" + _divs[i]).remove();
  38. }
  39. _divs.splice(0, _divs.length - 1);
  40. };
  41. var testSuite = function(renderMode) {
  42. module("jsPlumb", {teardown: _cleanup});
  43. // setup the container
  44. var container = document.createElement("div");
  45. container.id = "container";
  46. document.body.appendChild(container);
  47. jsPlumb.setRenderMode(renderMode);
  48. test(renderMode + ': findIndex method', function() {
  49. var array = [ 1,2,3, "test", "a string", { 'foo':'bar', 'baz':1 }, { 'ding':'dong' } ];
  50. equals(jsPlumb.getTestHarness().findIndex(array, 1), 0, "find works for integers");
  51. equals(jsPlumb.getTestHarness().findIndex(array, "test"), 3, "find works for strings");
  52. equals(jsPlumb.getTestHarness().findIndex(array, { 'foo':'bar', 'baz':1 }), 5, "find works for objects");
  53. equals(jsPlumb.getTestHarness().findIndex(array, { 'ding':'dong', 'baz':1 }), -1, "find works properly for objects (objects have different length but some same properties)");
  54. });
  55. test(renderMode + ': jsPlumb setup', function() {
  56. ok(jsPlumb, "loaded");
  57. });
  58. test(renderMode + ': getId', function() {
  59. var d10 = _addDiv('d10');
  60. equals(jsPlumb.getTestHarness().getId(d10), "d10");
  61. });
  62. test(renderMode + ': create a simple endpoint', function() {
  63. var d1 = _addDiv("d1");
  64. var e = jsPlumb.addEndpoint($("#d1"), {});
  65. ok(e, 'endpoint exists');
  66. assertEndpointCount("d1", 1);
  67. ok(e.id != null, "endpoint has had an id assigned");
  68. });
  69. test(renderMode + ': create and remove a simple endpoint', function() {
  70. var d1 = _addDiv("d1");
  71. var ee = jsPlumb.addEndpoint(d1, {uuid:"78978597593"});
  72. ok(ee != null, "endpoint exists");
  73. var e = jsPlumb.getEndpoint("78978597593");
  74. ok(e != null, "the endpoint could be retrieved by UUID");
  75. ok(e.id != null, "the endpoint has had an id assigned to it");
  76. assertEndpointCount("d1", 1);
  77. assertContextSize(1); // one Endpoint canvas.
  78. jsPlumb.removeEndpoint(d1, ee);
  79. assertEndpointCount("d1", 0);
  80. e = jsPlumb.getEndpoint("78978597593");
  81. equals(e, null, "the endpoint has been deleted");
  82. assertContextSize(0); // no Endpoint canvases.
  83. });
  84. test(renderMode + ': create two simple endpoints, registered using a selector', function() {
  85. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  86. d1.addClass("window");d2.addClass("window");
  87. var endpoints = jsPlumb.addEndpoint($(".window"), {});
  88. equals(endpoints.length, 2, "endpoint added to both windows");
  89. assertEndpointCount("d1", 1);
  90. assertEndpointCount("d2", 1);
  91. });
  92. test(renderMode + ': create two simple endpoints, registered using an array of element ids', function() {
  93. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  94. d1.addClass("window");d2.addClass("window");
  95. var endpoints = jsPlumb.addEndpoint(["d1", "d2"], {});
  96. equals(endpoints.length, 2, "endpoint added to both windows");
  97. assertEndpointCount("d1", 1);
  98. assertEndpointCount("d2", 1);
  99. });
  100. test(renderMode + ': draggable silently ignored when jquery ui not present', function() {
  101. var d1 = _addDiv("d1");
  102. var e = jsPlumb.addEndpoint(d1, {isSource:true});
  103. ok(e, 'endpoint exists');
  104. });
  105. test(renderMode + ': droppable silently ignored when jquery ui not present', function() {
  106. var d1 = _addDiv("d1")
  107. var e = jsPlumb.addEndpoint(d1, {isTarget:true});
  108. ok(e, 'endpoint exists');
  109. });
  110. test(renderMode + ': defaultEndpointMaxConnections', function() {
  111. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  112. var e3 = jsPlumb.addEndpoint(d3, {isSource:true});
  113. ok(e3.anchor, 'endpoint 3 has an anchor');
  114. var e4 = jsPlumb.addEndpoint(d4, {isSource:true});
  115. assertEndpointCount("d3", 1);
  116. assertEndpointCount("d4", 1);
  117. assertContextSize(2); // one canvas for each of our two endpoints.
  118. ok(!e3.isFull(), "endpoint 3 is not full.");
  119. jsPlumb.connect({source:d3, target:'d4', sourceEndpoint:e3, targetEndpoint:e4});
  120. assertConnectionCount(e3, 1); // we have one connection
  121. jsPlumb.connect({source:d3, target:'d4', sourceEndpoint:e3, targetEndpoint:e4});
  122. assertConnectionCount(e3, 1); // should have refused the connection; default max is 1.
  123. assertContextSize(3); // now we have one more canvas, for the Connection that was accepted.
  124. });
  125. test(renderMode + ': specifiedEndpointMaxConnections', function() {
  126. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  127. var e5 = jsPlumb.addEndpoint(d5, {isSource:true, maxConnections:3});
  128. ok(e5.anchor, 'endpoint 5 has an anchor');
  129. var e6 = jsPlumb.addEndpoint(d6, {isSource:true, maxConnections:2}); // this one has max TWO
  130. assertEndpointCount("d5", 1); assertEndpointCount("d6", 1);
  131. assertContextSize(2);
  132. ok(!e5.isFull(), "endpoint 5 is not full.");
  133. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  134. assertConnectionCount(e5, 1); // we have one connection
  135. assertContextSize(3);
  136. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  137. assertConnectionCount(e5, 2); // two connections
  138. assertContextSize(4);
  139. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e6});
  140. assertConnectionCount(e5, 2); // should have refused; max is 2, for d4.
  141. assertContextSize(4);
  142. });
  143. test(renderMode + ': noEndpointMaxConnections', function() {
  144. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  145. var e3 = jsPlumb.addEndpoint(d3, {isSource:true, maxConnections:-1});
  146. var e4 = jsPlumb.addEndpoint(d4, {isSource:true, maxConnections:-1});
  147. jsPlumb.connect({sourceEndpoint:e3, targetEndpoint:e4});
  148. assertContextSize(3);
  149. assertConnectionCount(e3, 1); // we have one connection
  150. jsPlumb.connect({sourceEndpoint:e3, targetEndpoint:e4});
  151. assertConnectionCount(e3, 2); // we have two. etc (default was one. this proves max is working).
  152. assertContextSize(4);
  153. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  154. var e5 = jsPlumb.addEndpoint(d3, {isSource:true, maxConnections:-1});
  155. jsPlumb.connect({sourceEndpoint:e5, targetEndpoint:e4});
  156. assertConnectionCount(e4, 3);
  157. assertContextSize(6);
  158. });
  159. test(renderMode + ': anchors equal', function() {
  160. var a1 = jsPlumb.makeAnchor([0, 1, 1, 1], null, jsPlumb);
  161. var a2 = jsPlumb.makeAnchor([0, 1, 1, 1], null, jsPlumb);
  162. ok(a1.equals(a2), "anchors are the same");
  163. });
  164. test(renderMode + ': anchors equal with offsets', function() {
  165. var a1 = jsPlumb.makeAnchor([0, 1, 1, 1, 10, 13], null, jsPlumb);
  166. var a2 = jsPlumb.makeAnchor([0, 1, 1, 1, 10, 13], null, jsPlumb);
  167. ok(a1.equals(a2), "anchors are the same");
  168. });
  169. test(renderMode + ': anchors not equal', function() {
  170. var a1 = jsPlumb.makeAnchor([0, 1, 0, 1], null, jsPlumb);
  171. var a2 = jsPlumb.makeAnchor([0, 1, 1, 1], null, jsPlumb);
  172. ok(!a1.equals(a2), "anchors are different");
  173. });
  174. test(renderMode + ': anchor not equal with offsets', function() {
  175. var a1 = jsPlumb.makeAnchor([0, 1, 1, 1, 10, 13], null, jsPlumb);
  176. var a2 = jsPlumb.makeAnchor([0, 1, 1, 1], null, jsPlumb);
  177. ok(!a1.equals(a2), "anchors are different");
  178. });
  179. test(renderMode + ': detach does not fail when no arguments are provided', function() {
  180. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  181. jsPlumb.connect({source:d3, target:d4});
  182. jsPlumb.detach();
  183. });
  184. // test that detach does not fire an event by default
  185. test(renderMode + ': jsPlumb.detach should fire detach event by default', function() {
  186. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  187. var conn = jsPlumb.connect({source:d5, target:d6});
  188. var eventCount = 0;
  189. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  190. jsPlumb.detach(conn);
  191. equals(eventCount, 1);
  192. });
  193. // test that detach does not fire an event by default
  194. test(renderMode + ': jsPlumb.detach should fire detach event by default, using params object', function() {
  195. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  196. var conn = jsPlumb.connect({source:d5, target:d6});
  197. var eventCount = 0;
  198. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  199. jsPlumb.detach({connection:conn});
  200. equals(eventCount, 1);
  201. });
  202. // test that detach fires an event when instructed to do so
  203. test(renderMode + ': jsPlumb.detach should not fire detach event when instructed to not do so', function() {
  204. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  205. var conn = jsPlumb.connect({source:d5, target:d6});
  206. var eventCount = 0;
  207. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  208. jsPlumb.detach(conn, {fireEvent:false});
  209. equals(eventCount, 0);
  210. });
  211. // issue 81
  212. test(renderMode + ': jsPlumb.detach should fire only one detach event (pass Connection as argument)', function() {
  213. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  214. var conn = jsPlumb.connect({source:d5, target:d6});
  215. var eventCount = 0;
  216. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  217. jsPlumb.detach(conn);
  218. equals(eventCount, 1);
  219. });
  220. // issue 81
  221. test(renderMode + ': jsPlumb.detach should fire only one detach event (pass Connection as param in argument)', function() {
  222. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  223. var conn = jsPlumb.connect({source:d5, target:d6});
  224. var eventCount = 0;
  225. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  226. jsPlumb.detach({connection:conn});
  227. equals(eventCount, 1);
  228. });
  229. // issue 81
  230. test(renderMode + ': jsPlumb.detach should fire only one detach event (pass source and targets as strings as arguments in params object)', function() {
  231. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  232. var conn = jsPlumb.connect({source:d5, target:d6});
  233. var eventCount = 0;
  234. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  235. jsPlumb.detach({source:"d5", target:"d6"});
  236. equals(eventCount, 1);
  237. });
  238. // issue 81
  239. test(renderMode + ': jsPlumb.detach should fire only one detach event (pass source and targets as divs as arguments in params object)', function() {
  240. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  241. var conn = jsPlumb.connect({source:d5, target:d6});
  242. var eventCount = 0;
  243. jsPlumb.bind("jsPlumbConnectionDetached", function(c) { eventCount++; });
  244. jsPlumb.detach({source:d5, target:d6, fireEvent:true});
  245. equals(eventCount, 1);
  246. });
  247. //TODO make sure you run this test with a single detach call, to ensure that
  248. // single detach calls result in the connection being removed. detachEveryConnection can
  249. // just blow away the connectionsByScope array and recreate it.
  250. test(renderMode + ': jsPlumb.getConnections (simple case, default scope)', function() {
  251. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  252. jsPlumb.connect({source:d5, target:d6});
  253. assertContextSize(3);
  254. var c = jsPlumb.getConnections(); // will get all connections in the default scope.
  255. equals(c.length, 1, "there is one connection");
  256. });
  257. test(renderMode + ': jsPlumb.getConnections (simple case, default scope; detach by element id using params object)', function() {
  258. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  259. jsPlumb.connect({source:d5, target:d6});
  260. jsPlumb.connect({source:d6, target:d7});
  261. assertContextSize(6);
  262. var c = jsPlumb.getConnections(); // will get all connections
  263. equals(c.length, 2, "there are two connections initially");
  264. jsPlumb.detach({source:'d5', target:'d6'});
  265. c = jsPlumb.getConnections(); // will get all connections
  266. equals(c.length, 1, "after detaching one, there is now one connection.");
  267. assertContextSize(5);
  268. });
  269. test(renderMode + ': jsPlumb.getConnections (simple case, default scope; detach by id using params object)', function() {
  270. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  271. jsPlumb.connect({source:d5, target:d6});
  272. jsPlumb.connect({source:d6, target:d7});
  273. assertContextSize(6);
  274. var c = jsPlumb.getConnections(); // will get all connections
  275. equals(c.length, 2, "there are two connections initially");
  276. jsPlumb.detach({source:"d5", target:"d6"});
  277. c = jsPlumb.getConnections(); // will get all connections
  278. equals(c.length, 1, "after detaching one, there is now one connection.");
  279. assertContextSize(5);
  280. });
  281. test(renderMode + ': jsPlumb.getConnections (simple case, default scope; detach by element object using params object)', function() {
  282. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  283. jsPlumb.connect({source:d5, target:d6});
  284. jsPlumb.connect({source:d6, target:d7});
  285. assertContextSize(6);
  286. var c = jsPlumb.getConnections(); // will get all connections
  287. equals(c.length, 2, "there are two connections initially");
  288. jsPlumb.detach({source:d5, target:d6});
  289. c = jsPlumb.getConnections(); // will get all connections
  290. equals(c.length, 1, "after detaching one, there is now one connection.");
  291. assertContextSize(5);
  292. });
  293. test(renderMode + ': jsPlumb.getConnections (simple case, default scope; detach by Connection)', function() {
  294. var d5 = _addDiv("d5"), d6 = _addDiv("d6"), d7 = _addDiv("d7");
  295. var c56 = jsPlumb.connect({source:d5, target:d6});
  296. var c67 = jsPlumb.connect({source:d6, target:d7});
  297. assertContextSize(6);
  298. var c = jsPlumb.getConnections(); // will get all connections
  299. equals(c.length, 2, "there are two connections initially");
  300. jsPlumb.detach(c56);
  301. assertContextSize(5); // check that the connection canvas was removed.
  302. c = jsPlumb.getConnections(); // will get all connections
  303. equals(c.length, 1, "after detaching one, there is now one connection.");
  304. });
  305. // beforeDetach functionality
  306. test(renderMode + ": jsPlumb.detach; beforeDetach on connect call returns false", function() {
  307. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  308. var c = jsPlumb.connect({source:d1, target:d2, beforeDetach:function(conn) { return false; }});
  309. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection initially");
  310. jsPlumb.detach(c);
  311. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection after detach call was denied");
  312. });
  313. test(renderMode + ": jsPlumb.detach; beforeDetach on connect call returns true", function() {
  314. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  315. var c = jsPlumb.connect({source:d1, target:d2, beforeDetach:function(conn) { return true; }});
  316. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection initially");
  317. jsPlumb.detach(c);
  318. equals(c.endpoints[0].connections.length, 0, "source endpoint has no connections after detach call was allowed");
  319. });
  320. test(renderMode + ": jsPlumb.detach; beforeDetach on connect call throws an exception; we treat it with the contempt it deserves and pretend it said the detach was ok.", function() {
  321. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  322. var c = jsPlumb.connect({source:d1, target:d2, beforeDetach:function(conn) { throw "i am badly coded"; }});
  323. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection initially");
  324. jsPlumb.detach(c);
  325. equals(c.endpoints[0].connections.length, 0, "source endpoint has no connections after detach call was allowed");
  326. });
  327. test(renderMode + ": jsPlumb.detach; beforeDetach on addEndpoint call to source Endpoint returns false", function() {
  328. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  329. var e1 = jsPlumb.addEndpoint(d1, { isSource:true, beforeDetach:function(conn) { return false; } }),
  330. e2 = jsPlumb.addEndpoint(d2, { isTarget:true });
  331. var c = jsPlumb.connect({source:e1,target:e2});
  332. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection initially");
  333. jsPlumb.detach(c);
  334. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection after detach call was denied");
  335. });
  336. test(renderMode + ": jsPlumb.detach; beforeDetach on addEndpoint call to source Endpoint returns true", function() {
  337. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  338. var e1 = jsPlumb.addEndpoint(d1, { isSource:true, beforeDetach:function(conn) { return true; } }),
  339. e2 = jsPlumb.addEndpoint(d2, { isTarget:true });
  340. var c = jsPlumb.connect({source:e1,target:e2});
  341. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection initially");
  342. jsPlumb.detach(c);
  343. equals(c.endpoints[0].connections.length, 0, "source endpoint has no connections after detach call was allowed");
  344. });
  345. test(renderMode + ": Endpoint.detach; beforeDetach on addEndpoint call to source Endpoint returns false; Endpoint.detach returns false too (the UI needs it to)", function() {
  346. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  347. var e1 = jsPlumb.addEndpoint(d1, { isSource:true, beforeDetach:function(conn) { return false; } }),
  348. e2 = jsPlumb.addEndpoint(d2, { isTarget:true });
  349. var c = jsPlumb.connect({source:e1,target:e2});
  350. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection initially");
  351. var success = e1.detach(c);
  352. equals(c.endpoints[0].connections.length, 1, "source endpoint has a connection after detach call was denied");
  353. ok(!success, "Endpoint reported detach did not execute");
  354. });
  355. test(renderMode + ": jsPlumb.detach; beforeDetach on addEndpoint call to target Endpoint returns false", function() {
  356. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  357. var e1 = jsPlumb.addEndpoint(d1, { isSource:true }),
  358. e2 = jsPlumb.addEndpoint(d2, { isTarget:true, beforeDetach:function(conn) { return false; } });
  359. var c = jsPlumb.connect({source:e1,target:e2});
  360. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection initially");
  361. jsPlumb.detach(c);
  362. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection after detach call was denied");
  363. });
  364. test(renderMode + ": jsPlumb.detach; beforeDetach on addEndpoint call to target Endpoint returns false but detach is forced", function() {
  365. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  366. var e1 = jsPlumb.addEndpoint(d1, { isSource:true }),
  367. e2 = jsPlumb.addEndpoint(d2, { isTarget:true, beforeDetach:function(conn) { return false; } });
  368. var c = jsPlumb.connect({source:e1,target:e2});
  369. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection initially");
  370. jsPlumb.detach(c, {forceDetach:true});
  371. equals(c.endpoints[0].connections.length, 0, "source endpoint has no connections after detach call was denied but forced anyway");
  372. equals(c.endpoints[1].connections.length, 0, "target endpoint has no connections after detach call was denied but forced anyway");
  373. });
  374. test(renderMode + ": jsPlumb.detach; beforeDetach on addEndpoint call to target Endpoint returns true", function() {
  375. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  376. var e1 = jsPlumb.addEndpoint(d1, { isSource:true }),
  377. e2 = jsPlumb.addEndpoint(d2, { isTarget:true, beforeDetach:function(conn) { return true; } });
  378. var c = jsPlumb.connect({source:e1,target:e2});
  379. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection initially");
  380. jsPlumb.detach(c);
  381. equals(c.endpoints[1].connections.length, 0, "target endpoint has no connections after detach call was allowed");
  382. });
  383. test(renderMode + ": jsPlumb.detach; beforeDetach bound to jsPlumb returns false", function() {
  384. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  385. var e1 = jsPlumb.addEndpoint(d1, { isSource:true }),
  386. e2 = jsPlumb.addEndpoint(d2, { isTarget:true });
  387. var c = jsPlumb.connect({source:e1,target:e2});
  388. jsPlumb.bind("beforeDetach", function(connection) {
  389. ok(connection.sourceId === "d1", "connection is provided and configured with correct source");
  390. ok(connection.targetId === "d2", "connection is provided and configured with correct target");
  391. return false;
  392. });
  393. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection initially");
  394. jsPlumb.detach(c);
  395. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection after detach call was denied");
  396. });
  397. test(renderMode + ": jsPlumb.detach; beforeDetach bound to jsPlumb returns true", function() {
  398. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  399. var e1 = jsPlumb.addEndpoint(d1, { isSource:true }),
  400. e2 = jsPlumb.addEndpoint(d2, { isTarget:true });
  401. var c = jsPlumb.connect({source:e1,target:e2});
  402. jsPlumb.bind("beforeDetach", function(connection) {
  403. ok(connection.sourceId === "d1", "connection is provided and configured with correct source");
  404. ok(connection.targetId === "d2", "connection is provided and configured with correct target");
  405. return true;
  406. });
  407. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection initially");
  408. jsPlumb.detach(c);
  409. equals(c.endpoints[1].connections.length, 0, "target endpoint has no connections after detach call was denied");
  410. });
  411. test(renderMode + ": jsPlumb.detachAllConnections ; beforeDetach on addEndpoint call to target Endpoint returns false but we should detach anyway", function() {
  412. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  413. var e1 = jsPlumb.addEndpoint(d1, { isSource:true }),
  414. e2 = jsPlumb.addEndpoint(d2, { isTarget:true, beforeDetach:function(conn) { return false; } });
  415. var c = jsPlumb.connect({source:e1,target:e2});
  416. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection initially");
  417. jsPlumb.detachAllConnections(d1);
  418. equals(c.endpoints[1].connections.length, 0, "target endpoint has no connections after detachAll was called");
  419. });
  420. test(renderMode + ": jsPlumb.detachEveryConnection ; beforeDetach on addEndpoint call to target Endpoint returns false but we should detach anyway", function() {
  421. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  422. var e1 = jsPlumb.addEndpoint(d1, { isSource:true }),
  423. e2 = jsPlumb.addEndpoint(d2, { isTarget:true, beforeDetach:function(conn) { return false; } });
  424. var c = jsPlumb.connect({source:e1,target:e2});
  425. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection initially");
  426. jsPlumb.detachEveryConnection();
  427. equals(c.endpoints[1].connections.length, 0, "target endpoint has no connections after detachEveryConnection was called");
  428. });
  429. test(renderMode + ": Endpoint.detachAll ; beforeDetach on addEndpoint call to target Endpoint returns false but we should detach anyway", function() {
  430. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  431. var e1 = jsPlumb.addEndpoint(d1, { isSource:true }),
  432. e2 = jsPlumb.addEndpoint(d2, { isTarget:true, beforeDetach:function(conn) { return false; } });
  433. var c = jsPlumb.connect({source:e1,target:e2});
  434. equals(c.endpoints[1].connections.length, 1, "target endpoint has a connection initially");
  435. e1.detachAll();
  436. equals(c.endpoints[1].connections.length, 0, "target endpoint has no connections after detachAllConnections was called");
  437. });
  438. // ******** end of beforeDetach tests **************
  439. // detachEveryConnection/detachAllConnections fireEvent overrides tests
  440. test(renderMode + ": jsPlumb.detachEveryConnection fires events", function() {
  441. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), connCount = 0;
  442. jsPlumb.bind("jsPlumbConnection", function() { connCount++; });
  443. jsPlumb.bind("jsPlumbConnectionDetached", function() { connCount--; });
  444. jsPlumb.connect({source:d1, target:d2});
  445. jsPlumb.connect({source:d1, target:d2});
  446. equals(connCount, 2, "two connections registered");
  447. jsPlumb.detachEveryConnection();
  448. equals(connCount, 0, "no connections registered");
  449. });
  450. test(renderMode + ": jsPlumb.detachEveryConnection doesn't fire events when instructed not to", function() {
  451. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), connCount = 0;
  452. jsPlumb.bind("jsPlumbConnection", function() { connCount++; });
  453. jsPlumb.bind("jsPlumbConnectionDetached", function() { connCount--; });
  454. jsPlumb.connect({source:d1, target:d2});
  455. jsPlumb.connect({source:d1, target:d2});
  456. equals(connCount, 2, "two connections registered");
  457. jsPlumb.detachEveryConnection({fireEvent:false});
  458. equals(connCount, 2, "two connections registered");
  459. });
  460. test(renderMode + ": jsPlumb.detachAllConnections fires events", function() {
  461. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), connCount = 0,
  462. e1 = jsPlumb.addEndpoint(d1), e2 = jsPlumb.addEndpoint(d2);
  463. jsPlumb.bind("jsPlumbConnection", function() { connCount++; });
  464. jsPlumb.bind("jsPlumbConnectionDetached", function() { connCount--; });
  465. jsPlumb.connect({source:d1, target:d2});
  466. jsPlumb.connect({source:d1, target:d2});
  467. equals(connCount, 2, "two connections registered");
  468. jsPlumb.detachAllConnections("d1");
  469. equals(connCount, 0, "no connections registered");
  470. });
  471. test(renderMode + ": jsPlumb.detachAllConnections doesn't fire events when instructed not to", function() {
  472. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), connCount = 0,
  473. e1 = jsPlumb.addEndpoint(d1), e2 = jsPlumb.addEndpoint(d2);
  474. jsPlumb.bind("jsPlumbConnection", function() { connCount++; });
  475. jsPlumb.bind("jsPlumbConnectionDetached", function() { connCount--; });
  476. jsPlumb.connect({source:d1, target:d2});
  477. jsPlumb.connect({source:d1, target:d2});
  478. equals(connCount, 2, "two connections registered");
  479. jsPlumb.detachAllConnections("d1", {fireEvent:false});
  480. equals(connCount, 2, "two connections registered");
  481. });
  482. test(renderMode + ': jsPlumb.getConnections (scope testScope)', function() {
  483. var d5 = _addDiv("d5"), d6 = _addDiv("d6");
  484. jsPlumb.connect({source:d5, target:d6, scope:'testScope'});
  485. var c = jsPlumb.getConnections("testScope"); // will get all connections in testScope
  486. equals(c.length, 1, "there is one connection");
  487. equals(c[0].sourceId, 'd5', "the connection's source is d5");
  488. equals(c[0].targetId, 'd6', "the connection's source is d6");
  489. c = jsPlumb.getConnections(); // will get all connections in default scope; should be none.
  490. equals(c.length, 0, "there are no connections in the default scope");
  491. });
  492. test(renderMode + ': jsPlumb.getAllConnections (filtered by scope)', function() {
  493. var d8 = _addDiv("d8"), d9 = _addDiv("d9"), d10 = _addDiv('d10');
  494. jsPlumb.connect({source:d8, target:d9, scope:'testScope'});
  495. jsPlumb.connect({source:d9, target:d10}); // default scope
  496. var c = jsPlumb.getAllConnections(); // will get all connections
  497. equals(c[jsPlumb.getDefaultScope()].length, 1);
  498. equals(c['testScope'].length, 1);
  499. // now supply a list of scopes
  500. c = jsPlumb.getConnections();
  501. equals(c.length, 1);
  502. c = jsPlumb.getConnections("testScope");
  503. equals(c.length, 1, "there is one connection in 'testScope'");
  504. });
  505. test(renderMode + ': jsPlumb.getConnections (filtered by scope and sourceId)', function() {
  506. var d8 = _addDiv("d8"), d9 = _addDiv("d9"), d10 = _addDiv('d10');
  507. jsPlumb.connect({source:d8, target:d9, scope:'testScope'});
  508. jsPlumb.connect({source:d9, target:d8, scope:'testScope'});
  509. jsPlumb.connect({source:d9, target:d10}); // default scope
  510. var c = jsPlumb.getConnections({scope:'testScope', source:'d8'}); // will get all connections with sourceId 'd8'
  511. equals(c.length, 1, "there is one connection in 'testScope' from d8");
  512. });
  513. test(renderMode + ': jsPlumb.getConnections (filtered by scope, source id and target id)', function() {
  514. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  515. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  516. jsPlumb.connect({source:d12, target:d13, scope:'testScope'});
  517. jsPlumb.connect({source:d11, target:d13, scope:'testScope'});
  518. var c = jsPlumb.getConnections({scope:'testScope', source:'d11', target:'d13'});
  519. equals(c.length, 1, "there is one connection from d11 to d13");
  520. });
  521. test(renderMode + ': jsPlumb.getConnections (filtered by a list of scopes)', function() {
  522. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  523. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  524. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  525. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  526. var c = jsPlumb.getConnections({scope:['testScope','testScope3']});
  527. equals(c['testScope'].length, 1, 'there is one connection in testScope');
  528. equals(c['testScope3'].length, 1, 'there is one connection in testScope3');
  529. equals(c['testScope2'], null, 'there are no connections in testScope2');
  530. });
  531. test(renderMode + ': jsPlumb.getConnections (filtered by a list of scopes and source ids)', function() {
  532. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13');
  533. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  534. jsPlumb.connect({source:d13, target:d12, scope:'testScope'});
  535. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  536. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  537. var c = jsPlumb.getConnections({scope:['testScope','testScope3'], source:['d11']});
  538. equals(c['testScope'].length, 1, 'there is one connection in testScope');
  539. equals(c['testScope3'].length, 1, 'there is one connection in testScope3');
  540. equals(c['testScope2'], null, 'there are no connections in testScope2');
  541. });
  542. test(renderMode + ': jsPlumb.getConnections (filtered by a list of scopes, source ids and target ids)', function() {
  543. jsPlumb.detachEveryConnection();
  544. var d11 = _addDiv("d11"), d12 = _addDiv("d12"), d13 = _addDiv('d13'), d14=_addDiv("d14"), d15=_addDiv("d15");
  545. jsPlumb.connect({source:d11, target:d12, scope:'testScope'});
  546. jsPlumb.connect({source:d13, target:d12, scope:'testScope'});
  547. jsPlumb.connect({source:d11, target:d14, scope:'testScope'});
  548. jsPlumb.connect({source:d11, target:d15, scope:'testScope'});
  549. jsPlumb.connect({source:d12, target:d13, scope:'testScope2'});
  550. jsPlumb.connect({source:d11, target:d13, scope:'testScope3'});
  551. assertContextSize(18);
  552. var c = jsPlumb.getConnections({scope:['testScope','testScope3'], source:['d11'], target:['d14', 'd15']});
  553. equals(c['testScope'].length, 2, 'there are two connections in testScope');
  554. equals(c['testScope3'], null, 'there are no connections in testScope3');
  555. equals(c['testScope2'], null, 'there are no connections in testScope2');
  556. var anEntry = c['testScope'][0];
  557. ok(anEntry.endpoints[0] != null, "Source endpoint is set");
  558. ok(anEntry.endpoints[1] != null, "Target endpoint is set");
  559. equals(anEntry.source.attr("id"), "d11", "Source is div d11");
  560. equals(anEntry.target.attr("id"), "d14", "Target is div d14");
  561. });
  562. test(renderMode + ': getEndpoints, one Endpoint added by addEndpoint, get Endpoints by selector', function() {
  563. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  564. jsPlumb.addEndpoint(d1);
  565. var e = jsPlumb.getEndpoints(d1);
  566. equals(e.length, 1, "there is one endpoint for element d1");
  567. });
  568. test(renderMode + ': getEndpoints, one Endpoint added by addEndpoint, get Endpoints by id', function() {
  569. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  570. jsPlumb.addEndpoint(d1);
  571. var e = jsPlumb.getEndpoints("d1");
  572. equals(e.length, 1, "there is one endpoint for element d1");
  573. });
  574. test(renderMode + ': connection event listeners', function() {
  575. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  576. var returnedParams = null/*, returnedParams2 = null*/;
  577. jsPlumb.bind("jsPlumbConnection", function(params) {
  578. returnedParams = $.extend({}, params);
  579. });
  580. var c = jsPlumb.connect({source:d1, target:d2});
  581. ok(returnedParams != null, "new connection listener event was fired");
  582. ok(returnedParams.connection != null, 'connection is set');
  583. equals(returnedParams.sourceId, "d1", 'sourceid is set');
  584. equals(returnedParams.targetId, "d2", 'targetid is set');
  585. equals(returnedParams.source.attr("id"), "d1", 'source is set');
  586. equals(returnedParams.target.attr("id"), "d2" , 'target is set');
  587. ok(returnedParams.sourceEndpoint != null, "source endpoint is not null");
  588. ok(returnedParams.targetEndpoint != null, "target endpoint is not null");
  589. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  590. returnedParams = $.extend({}, params);
  591. });
  592. jsPlumb.detach(c);
  593. ok(returnedParams.connection != null, 'connection is set');
  594. //jsPlumb.detachAll("d1");
  595. //ok(returnedParams2 != null, "removed connection listener event was fired");
  596. });
  597. test(renderMode + ': detach event listeners (detach by connection)', function() {
  598. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  599. var returnedParams = null;
  600. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  601. returnedParams = $.extend({}, params);
  602. });
  603. var conn = jsPlumb.connect({source:d1, target:d2});
  604. jsPlumb.detach(conn);
  605. ok(returnedParams != null, "removed connection listener event was fired");
  606. ok(returnedParams.connection != null, "removed connection listener event passed in connection");
  607. });
  608. test(renderMode + ': detach event listeners (detach by element ids)', function() {
  609. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  610. var returnedParams = null;
  611. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  612. returnedParams = $.extend({}, params);
  613. });
  614. var conn = jsPlumb.connect({source:d1, target:d2});
  615. jsPlumb.detach({source:"d1",target:"d2"});
  616. ok(returnedParams != null, "removed connection listener event was fired");
  617. });
  618. test(renderMode + ': detach event listeners (detach by elements)', function() {
  619. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  620. var returnedParams = null;
  621. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  622. returnedParams = $.extend({}, params);
  623. });
  624. var conn = jsPlumb.connect({source:d1, target:d2});
  625. jsPlumb.detach({source:d1,target:d2});
  626. ok(returnedParams != null, "removed connection listener event was fired");
  627. });
  628. test(renderMode + ': detach event listeners (via Endpoint.detach method)', function() {
  629. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  630. var e1 = jsPlumb.addEndpoint(d1, {});
  631. var e2 = jsPlumb.addEndpoint(d2, {});
  632. var returnedParams = null;
  633. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  634. returnedParams = $.extend({}, params);
  635. });
  636. var conn = jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  637. assertContextSize(3);
  638. e1.detach(conn);
  639. ok(returnedParams != null, "removed connection listener event was fired");
  640. assertContextSize(2);
  641. });
  642. test(renderMode + ': detach event listeners (via Endpoint.detachFrom method)', function() {
  643. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  644. var e1 = jsPlumb.addEndpoint(d1, {});
  645. var e2 = jsPlumb.addEndpoint(d2, {});
  646. var returnedParams = null;
  647. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  648. returnedParams = $.extend({}, params);
  649. });
  650. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  651. assertContextSize(3);
  652. e1.detachFrom(e2);
  653. ok(returnedParams != null, "removed connection listener event was fired");
  654. assertContextSize(2);
  655. });
  656. test(renderMode + ': detach event listeners (via Endpoint.detachAll method)', function() {
  657. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  658. var e1 = jsPlumb.addEndpoint(d1, {});
  659. var e2 = jsPlumb.addEndpoint(d2, {});
  660. var returnedParams = null;
  661. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  662. returnedParams = $.extend({}, params);
  663. });
  664. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  665. assertContextSize(3);
  666. e1.detachAll();
  667. ok(returnedParams != null, "removed connection listener event was fired");
  668. assertContextSize(2);
  669. });
  670. test(renderMode + ': detach event listeners (via jsPlumb.deleteEndpoint method)', function() {
  671. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  672. var e1 = jsPlumb.addEndpoint(d1, {});
  673. var e2 = jsPlumb.addEndpoint(d2, {});
  674. var returnedParams = null;
  675. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  676. returnedParams = $.extend({}, params);
  677. });
  678. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2});
  679. assertContextSize(3);
  680. jsPlumb.deleteEndpoint(e1);
  681. ok(returnedParams != null, "removed connection listener event was fired");
  682. assertContextSize(1);
  683. });
  684. test(renderMode + ': detach event listeners (ensure cleared by jsPlumb.reset)', function() {
  685. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  686. var returnedParams = null;
  687. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  688. returnedParams = $.extend({}, params);
  689. });
  690. var conn = jsPlumb.connect({source:d1, target:d2});
  691. jsPlumb.detach({source:d1,target:d2, fireEvent:true});
  692. ok(returnedParams != null, "removed connection listener event was fired");
  693. returnedParams = null;
  694. jsPlumb.reset();
  695. var conn = jsPlumb.connect({source:d1, target:d2});
  696. jsPlumb.detach(d1,d2);
  697. ok(returnedParams == null, "connection listener was cleared by jsPlumb.reset()");
  698. });
  699. test(renderMode + ': connection events that throw errors', function() {
  700. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  701. var returnedParams = null, returnedParams2 = null;
  702. jsPlumb.bind("jsPlumbConnection", function(params) {
  703. returnedParams = $.extend({}, params);
  704. throw "oh no!";
  705. });
  706. jsPlumb.connect({source:d1, target:d2});
  707. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  708. jsPlumb.connect({source:d3, target:d4});
  709. ok(returnedParams != null, "new connection listener event was fired; we threw an error, jsPlumb survived.");
  710. });
  711. test(renderMode + ": Endpoint.detachFrom", function() {
  712. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  713. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  714. ok(e16.anchor, 'endpoint 16 has an anchor');
  715. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  716. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  717. assertConnectionCount(e16, 1);
  718. assertConnectionCount(e17, 1);
  719. assertContextSize(3);
  720. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  721. e16.detachFrom(e17);
  722. assertContextSize(2); // the endpoint canvases should remain
  723. // but the connection should be gone, meaning not registered by jsPlumb and not registered on either Endpoint:
  724. assertConnectionCount(e16, 0);
  725. assertConnectionCount(e17, 0);
  726. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  727. });
  728. test(renderMode + ": Endpoint.detachFromConnection", function() {
  729. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  730. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  731. ok(e16.anchor, 'endpoint 16 has an anchor');
  732. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  733. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  734. assertConnectionCount(e16, 1);
  735. assertConnectionCount(e17, 1);
  736. assertContextSize(3);
  737. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  738. e16.detachFromConnection(conn);
  739. assertContextSize(3); // all canvases should remain; the connection was not removed.
  740. // but endpoint e16 should have no connections now.
  741. assertConnectionCount(e16, 0);
  742. assertConnectionCount(e17, 1);
  743. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  744. });
  745. test(renderMode + ": Endpoint.detachAll", function() {
  746. var d16 = _addDiv("d16"), d17 = _addDiv("d17"), d18 = _addDiv("d18");
  747. var e16 = jsPlumb.addEndpoint($("#d16"), {isSource:true,maxConnections:-1});
  748. ok(e16.anchor, 'endpoint 16 has an anchor');
  749. var e17 = jsPlumb.addEndpoint($("#d17"), {isSource:true});
  750. var e18 = jsPlumb.addEndpoint($("#d18"), {isSource:true});
  751. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  752. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e18});
  753. assertConnectionCount(e16, 2);
  754. assertConnectionCount(e17, 1);
  755. assertConnectionCount(e18, 1);
  756. assertContextSize(5);
  757. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 2);
  758. e16.detachAll();
  759. assertConnectionCount(e16, 0);
  760. assertConnectionCount(e17, 0);
  761. assertContextSize(3);
  762. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  763. });
  764. test(renderMode + ": Endpoint.detach", function() {
  765. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  766. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  767. ok(e16.anchor, 'endpoint 16 has an anchor');
  768. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  769. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  770. assertConnectionCount(e16, 1);
  771. assertConnectionCount(e17, 1);
  772. assertContextSize(3);
  773. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  774. e16.detach(conn);
  775. assertContextSize(2); // the endpoint canvases should remain
  776. // but the connection should be gone, meaning not registered by jsPlumb and not registered on either Endpoint:
  777. assertConnectionCount(e16, 0);
  778. assertConnectionCount(e17, 0);
  779. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  780. });
  781. test(renderMode + ": Endpoint.isConnectedTo", function() {
  782. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  783. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  784. ok(e16.anchor, 'endpoint 16 has an anchor');
  785. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  786. var conn = jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  787. equals(e16.isConnectedTo(e17), true, "e16 and e17 are connected");
  788. });
  789. test(renderMode + ": setting endpoint uuid", function() {
  790. var uuid = "14785937583175927504313";
  791. var d16 = _addDiv("d16"), d17 = _addDiv("d17"), d18 = _addDiv("d18");
  792. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  793. equals(e16.getUuid(), uuid, "endpoint's uuid was set correctly");
  794. });
  795. test(renderMode + ": jsPlumb.getEndpoint (by uuid)", function() {
  796. var uuid = "14785937583175927504313";
  797. var d16 = _addDiv("d16");
  798. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  799. var e = jsPlumb.getEndpoint(uuid);
  800. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  801. });
  802. test(renderMode + ": jsPlumb.deleteEndpoint (by uuid, simple case)", function() {
  803. var uuid = "14785937583175927504313";
  804. var d16 = _addDiv("d16");
  805. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  806. var e = jsPlumb.getEndpoint(uuid);
  807. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  808. jsPlumb.deleteEndpoint(uuid);
  809. var f = jsPlumb.getEndpoint(uuid);
  810. equals(f, null, "endpoint has been deleted");
  811. var ebe = jsPlumb.getTestHarness().endpointsByElement["d16"];
  812. equals(ebe.length, 0, "no endpoints registered for element d16 anymore");
  813. assertContextSize(0);
  814. });
  815. test(renderMode + ": jsPlumb.deleteEndpoint (by uuid, connections too)", function() {
  816. // create two endpoints (one with a uuid), add them to two divs and then connect them.
  817. var uuid = "14785937583175927504313";
  818. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  819. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  820. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  821. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  822. // check that the connection was ok.
  823. equals(e16.connections.length, 1, "e16 has one connection");
  824. equals(e17.connections.length, 1, "e17 has one connection");
  825. assertContextSize(3);
  826. // delete the endpoint that has a uuid. verify that the endpoint cannot be retrieved and that the connection has been removed, but that
  827. // element d17 still has its Endpoint.
  828. jsPlumb.deleteEndpoint(uuid);
  829. var f = jsPlumb.getEndpoint(uuid);
  830. equals(f, null, "endpoint has been deleted");
  831. equals(e16.connections.length, 0, "e16 has no connections");
  832. equals(e17.connections.length, 0, "e17 has no connections");
  833. var ebe = jsPlumb.getTestHarness().endpointsByElement["d16"];
  834. equals(ebe.length, 0, "no endpoints registered for element d16 anymore");
  835. ebe = jsPlumb.getTestHarness().endpointsByElement["d17"];
  836. equals(ebe.length, 1, "element d17 still has its Endpoint");
  837. assertContextSize(1);
  838. // now delete d17's endpoint and check that it has gone.
  839. jsPlumb.deleteEndpoint(e17);
  840. f = jsPlumb.getEndpoint(e17);
  841. equals(f, null, "endpoint has been deleted");
  842. ebe = jsPlumb.getTestHarness().endpointsByElement["d17"];
  843. equals(ebe.length, 0, "element d17 no longer has any Endpoints");
  844. assertContextSize(0);
  845. });
  846. test(renderMode + ": jsPlumb.deleteEndpoint (by reference, simple case)", function() {
  847. var uuid = "14785937583175927504313";
  848. var d16 = _addDiv("d16");
  849. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  850. var e = jsPlumb.getEndpoint(uuid);
  851. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  852. jsPlumb.deleteEndpoint(e16);
  853. var f = jsPlumb.getEndpoint(uuid);
  854. equals(f, null, "endpoint has been deleted");
  855. assertContextSize(0);
  856. });
  857. test(renderMode + ": jsPlumb.deleteEndpoint (by reference, connections too)", function() {
  858. var uuid = "14785937583175927504313";
  859. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  860. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  861. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  862. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  863. equals(e16.connections.length, 1, "e16 has one connection");
  864. equals(e17.connections.length, 1, "e17 has one connection");
  865. assertContextSize(3);
  866. jsPlumb.deleteEndpoint(e16);
  867. var f = jsPlumb.getEndpoint(uuid);
  868. equals(f, null, "endpoint has been deleted");
  869. equals(e16.connections.length, 0, "e16 has no connections");
  870. equals(e17.connections.length, 0, "e17 has no connections");
  871. assertContextSize(1);
  872. });
  873. test(renderMode + ": jsPlumb.deleteEveryEndpoint", function() {
  874. var uuid = "14785937583175927504313";
  875. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  876. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  877. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  878. assertContextSize(2);
  879. var e = jsPlumb.getEndpoint(uuid);
  880. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  881. jsPlumb.deleteEveryEndpoint();
  882. var f = jsPlumb.getEndpoint(uuid);
  883. equals(f, null, "endpoint e16 has been deleted");
  884. var g = jsPlumb.getEndpoint(e17);
  885. equals(g, null, "endpoint e17 has been deleted");
  886. assertContextSize(0);
  887. });
  888. test(renderMode + ": jsPlumb.deleteEveryEndpoint (connections too)", function() {
  889. var uuid = "14785937583175927504313";
  890. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  891. var e16 = jsPlumb.addEndpoint(d16, {isSource:true,maxConnections:-1, uuid:uuid});
  892. var e17 = jsPlumb.addEndpoint(d17, {isSource:true,maxConnections:-1});
  893. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  894. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  895. assertContextSize(3);
  896. var e = jsPlumb.getEndpoint(uuid);
  897. equals(e.getUuid(), uuid, "retrieved endpoint by uuid");
  898. jsPlumb.deleteEveryEndpoint();
  899. var f = jsPlumb.getEndpoint(uuid);
  900. equals(f, null, "endpoint e16 has been deleted");
  901. var g = jsPlumb.getEndpoint(e17);
  902. equals(g, null, "endpoint e17 has been deleted");
  903. assertContextSize(0); // no canvases. so all connection canvases have been cleaned up too.
  904. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  905. });
  906. test(renderMode + ": jsPlumb.addEndpoint (simple case)", function() {
  907. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  908. var e16 = jsPlumb.addEndpoint(d16, {anchor:[0,0.5,0,-1]});
  909. var e17 = jsPlumb.addEndpoint(d17, {anchor:"TopCenter"});
  910. assertContextSize(2);
  911. equals(e16.anchor.x, 0);
  912. equals(e16.anchor.y, 0.5);
  913. equals(e17.anchor.x, 0.5);
  914. equals(e17.anchor.y, 0);
  915. });
  916. test(renderMode + ": jsPlumb.addEndpoint (tooltip)", function() {
  917. var d16 = _addDiv("d16"), d17 = _addDiv("d17"),
  918. e16 = jsPlumb.addEndpoint(d16, {tooltip:"FOO"}),
  919. e17 = jsPlumb.addEndpoint(d17, {tooltip:"BAZ"});
  920. assertContextSize(2);
  921. equals(e16.canvas.getAttribute("label"), "FOO");
  922. equals(e17.canvas.getAttribute("label"), "BAZ");
  923. });
  924. test(renderMode + ": jsPlumb.addEndpoint (simple case, dynamic anchors)", function() {
  925. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  926. var e16 = jsPlumb.addEndpoint(d16, {anchors:[[0,0.5,0,-1], [1,0.5,0,1]]});
  927. var e17 = jsPlumb.addEndpoint(d17, {anchors:["TopCenter", "BottomCenter"]});
  928. assertContextSize(2);
  929. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  930. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  931. });
  932. test(renderMode + ": jsPlumb.addEndpoint (simple case, two arg method)", function() {
  933. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  934. var e16 = jsPlumb.addEndpoint(d16, {isSource:true, isTarget:false}, {anchor:[0,0.5,0,-1]});
  935. var e17 = jsPlumb.addEndpoint(d17, {isTarget:true, isSource:false}, {anchor:"TopCenter"});
  936. assertContextSize(2);
  937. equals(e16.anchor.x, 0);
  938. equals(e16.anchor.y, 0.5);
  939. equals(false, e16.isTarget);
  940. equals(true, e16.isSource);
  941. equals(e17.anchor.x, 0.5);
  942. equals(e17.anchor.y, 0);
  943. equals(true, e17.isTarget);
  944. equals(false, e17.isSource);
  945. });
  946. test(renderMode + ": jsPlumb.addEndpoints (simple case)", function() {
  947. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  948. var e16 = jsPlumb.addEndpoints(d16, [{isSource:true, isTarget:false, anchor:[0,0.5,0,-1] }, { isTarget:true, isSource:false, anchor:"TopCenter" }]);
  949. assertContextSize(2);
  950. equals(e16[0].anchor.x, 0);
  951. equals(e16[0].anchor.y, 0.5);
  952. equals(false, e16[0].isTarget);
  953. equals(true, e16[0].isSource);
  954. equals(e16[1].anchor.x, 0.5);
  955. equals(e16[1].anchor.y, 0);
  956. equals(true, e16[1].isTarget);
  957. equals(false, e16[1].isSource);
  958. });
  959. test(renderMode + ": jsPlumb.addEndpoints (with reference params)", function() {
  960. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  961. var refParams = {anchor:"RightMiddle"};
  962. var e16 = jsPlumb.addEndpoints(d16, [{isSource:true, isTarget:false}, { isTarget:true, isSource:false }], refParams);
  963. assertContextSize(2);
  964. equals(e16[0].anchor.x, 1);
  965. equals(e16[0].anchor.y, 0.5);
  966. equals(false, e16[0].isTarget);
  967. equals(true, e16[0].isSource);
  968. equals(e16[1].anchor.x, 1);
  969. equals(e16[1].anchor.y, 0.5);
  970. equals(true, e16[1].isTarget);
  971. equals(false, e16[1].isSource);
  972. });
  973. test(renderMode + ": jsPlumb.addEndpoint (simple case, dynamic anchors, two arg method)", function() {
  974. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  975. var e16 = jsPlumb.addEndpoint(d16, {isSource:true, isTarget:false}, {anchors:[[0,0.5,0,-1], [1,0.5,0,1]]});
  976. var e17 = jsPlumb.addEndpoint(d17, {isTarget:true, isSource:false}, {anchors:["TopCenter", "BottomCenter"]});
  977. assertContextSize(2);
  978. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  979. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  980. });
  981. test(renderMode + ": jsPlumb.makeTarget (simple case)", function() {
  982. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  983. var e16 = jsPlumb.addEndpoint(d16, {isSource:true, isTarget:false}, {anchors:[[0,0.5,0,-1], [1,0.5,0,1]]});
  984. jsPlumb.makeTarget(d17, { isTarget:true,anchor:"TopCenter" });
  985. equals(true, jsPlumb.CurrentLibrary.hasClass(d17, "ui-droppable"));
  986. });
  987. test(renderMode + ": jsPlumb.makeTarget (specify two divs in an array)", function() {
  988. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  989. jsPlumb.makeTarget([d16, d17], { isTarget:true,anchor:"TopCenter" });
  990. equals(true, jsPlumb.CurrentLibrary.hasClass(d16, "ui-droppable"));
  991. equals(true, jsPlumb.CurrentLibrary.hasClass(d17, "ui-droppable"));
  992. });
  993. test(renderMode + ": jsPlumb.makeTarget (specify two divs by id in an array)", function() {
  994. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  995. jsPlumb.makeTarget(["d16", "d17"], { isTarget:true,anchor:"TopCenter" });
  996. equals(true, jsPlumb.CurrentLibrary.hasClass(d16, "ui-droppable"));
  997. equals(true, jsPlumb.CurrentLibrary.hasClass(d17, "ui-droppable"));
  998. });
  999. test(renderMode + ": jsPlumb.makeTarget (specify divs by selector)", function() {
  1000. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1001. d16.addClass("FOO");d17.addClass("FOO");
  1002. jsPlumb.makeTarget($(".FOO"), { isTarget:true,anchor:"TopCenter" });
  1003. equals(true, jsPlumb.CurrentLibrary.hasClass(d16, "ui-droppable"));
  1004. equals(true, jsPlumb.CurrentLibrary.hasClass(d17, "ui-droppable"));
  1005. });
  1006. test(renderMode + ': jsPlumb.connect (between two Endpoints)', function() {
  1007. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1008. var e = jsPlumb.addEndpoint(d1, {});
  1009. var e2 = jsPlumb.addEndpoint(d2, {});
  1010. ok(e, 'endpoint e exists');
  1011. ok(e2, 'endpoint e2 exists');
  1012. assertContextSize(2); // should have a canvas for each endpoint now.
  1013. assertEndpointCount("d1", 1);
  1014. assertEndpointCount("d2", 1);
  1015. var c = jsPlumb.connect({target:'d2', sourceEndpoint:e, targetEndpoint:e2});
  1016. assertEndpointCount("d1", 1); // no new endpoint should have been added
  1017. assertEndpointCount("d2", 1); // no new endpoint should have been added
  1018. assertContextSize(3); // now we should also have a canvas for the connection.
  1019. ok(c.id != null, "connection has had an id assigned");
  1020. });
  1021. test(renderMode + ': jsPlumb.connect (Endpoint connectorTooltip parameter)', function() {
  1022. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1023. var e = jsPlumb.addEndpoint(d1, {connectorTooltip:"FOO"});
  1024. var e2 = jsPlumb.addEndpoint(d2, {});
  1025. var c = jsPlumb.connect({target:'d2', sourceEndpoint:e, targetEndpoint:e2});
  1026. equals(c.connector.canvas.getAttribute("label"), "FOO", "connector canvas has label attribute set");
  1027. });
  1028. test(renderMode + ': jsPlumb.connect (tooltip parameter)', function() {
  1029. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1030. var e = jsPlumb.addEndpoint(d1, {});
  1031. var e2 = jsPlumb.addEndpoint(d2, {});
  1032. var c = jsPlumb.connect({target:'d2', sourceEndpoint:e, targetEndpoint:e2, tooltip:"FOO"});
  1033. equals(c.connector.canvas.getAttribute("label"), "FOO", "connector canvas has label attribute set");
  1034. });
  1035. test(renderMode + ': jsPlumb.connect (between two Endpoints, and dont supply any parameters to the Endpoints.)', function() {
  1036. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1037. var e = jsPlumb.addEndpoint(d1);
  1038. var e2 = jsPlumb.addEndpoint(d2);
  1039. ok(e, 'endpoint e exists');
  1040. ok(e2, 'endpoint e2 exists');
  1041. assertContextSize(2); // should have a canvas for each endpoint now.
  1042. assertEndpointCount("d1", 1);
  1043. assertEndpointCount("d2", 1);
  1044. jsPlumb.connect({target:'d2', sourceEndpoint:e, targetEndpoint:e2});
  1045. assertEndpointCount("d1", 1); // no new endpoint should have been added
  1046. assertEndpointCount("d2", 1); // no new endpoint should have been added
  1047. assertContextSize(3); // now we should also have a canvas for the connection.
  1048. });
  1049. test(renderMode + ': jsPlumb.connect (by endpoint)', function() {
  1050. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1051. var e16 = jsPlumb.addEndpoint(d16, {isSource:true});
  1052. ok(e16.anchor, 'endpoint 16 has an anchor');
  1053. var e17 = jsPlumb.addEndpoint(d17, {isSource:true});
  1054. jsPlumb.connect({sourceEndpoint:e16, targetEndpoint:e17});
  1055. assertContextSize(3);
  1056. });
  1057. test(renderMode + ": jsPlumb.connect (two Endpoints - that have been already added - by UUID)", function() {
  1058. var srcEndpointUuid = "14785937583175927504313", dstEndpointUuid = "14785937583175927534325";
  1059. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1060. var e1 = jsPlumb.addEndpoint("d16", {isSource:true, maxConnections:-1, uuid:srcEndpointUuid});
  1061. var e2 = jsPlumb.addEndpoint("d17", {isSource:true, maxConnections:-1, uuid:dstEndpointUuid});
  1062. jsPlumb.connect({ uuids: [ srcEndpointUuid, dstEndpointUuid ] });
  1063. assertConnectionCount(e1, 1);
  1064. assertConnectionCount(e2, 1);
  1065. assertContextSize(3);
  1066. });
  1067. test(renderMode + ": jsPlumb.connect (two Endpoints - that have not been already added - by UUID)", function() {
  1068. var srcEndpointUuid = "14785937583175927504313", dstEndpointUuid = "14785937583175927534325";
  1069. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1070. jsPlumb.connect({ uuids: [ srcEndpointUuid, dstEndpointUuid ], source:d16, target:d17 });
  1071. assertContextSize(3);
  1072. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1073. var e1 = jsPlumb.getEndpoint(srcEndpointUuid);
  1074. ok(e1 != null, "endpoint with src uuid added");
  1075. ok(e1.canvas != null);
  1076. var e2 = jsPlumb.getEndpoint(dstEndpointUuid);
  1077. ok(e2 != null, "endpoint with target uuid added");
  1078. assertConnectionCount(e1, 1);
  1079. assertConnectionCount(e2, 1);
  1080. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1081. });
  1082. test(renderMode + ": jsPlumb.connect (two Endpoints - that have been already added - by endpoint reference)", function() {
  1083. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1084. var e1 = jsPlumb.addEndpoint("d16", {isSource:true, maxConnections:-1});
  1085. var e2 = jsPlumb.addEndpoint("d17", {isSource:true, maxConnections:-1});
  1086. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1087. assertContextSize(3);
  1088. assertConnectionCount(e1, 1);
  1089. assertConnectionCount(e2, 1);
  1090. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1091. });
  1092. test(renderMode + ": jsPlumb.connect (two elements)", function() {
  1093. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1094. jsPlumb.connect({ source:d16, target:d17 });
  1095. assertContextSize(3);
  1096. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1097. });
  1098. test(renderMode + ": jsPlumb.connect (Connector test, straight)", function() {
  1099. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1100. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight" });
  1101. assertContextSize(3);
  1102. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1103. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1104. });
  1105. test(renderMode + ": jsPlumb.connect (Connector test, bezier, no params)", function() {
  1106. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1107. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Bezier" });
  1108. assertContextSize(3);
  1109. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1110. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Bezier, "Bezier connector chosen for connection");
  1111. equals(conn.connector.majorAnchor, 150, "Bezier connector chose 150 curviness");
  1112. });
  1113. test(renderMode + ": jsPlumb.connect (Connector test, bezier, curviness as int)", function() {
  1114. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1115. var conn = jsPlumb.connect({ source:d16, target:d17, connector:["Bezier", { curviness:200 }] });
  1116. assertContextSize(3);
  1117. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1118. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Bezier, "Canvas Bezier connector chosen for connection");
  1119. equals(conn.connector.majorAnchor, 200, "Bezier connector chose 200 curviness");
  1120. });
  1121. test(renderMode + ": jsPlumb.connect (Connector test, bezier, curviness as named option)", function() {
  1122. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1123. var conn = jsPlumb.connect({ source:d16, target:d17, connector:["Bezier", {curviness:300}] });
  1124. assertContextSize(3);
  1125. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1126. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Bezier, "Bezier connector chosen for connection");
  1127. equals(conn.connector.majorAnchor, 300, "Bezier connector chose 300 curviness");
  1128. });
  1129. test(renderMode + ": jsPlumb.connect (anchors registered correctly; source and target anchors given, as arrays)", function() {
  1130. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1131. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight", anchors:[[0.3,0.3,1,0], [0.7,0.7,0,1]] });
  1132. assertContextSize(3);
  1133. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1134. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Canvas Straight connector chosen for connection");
  1135. equals(0.3, conn.endpoints[0].anchor.x, "source anchor x");
  1136. equals(0.3, conn.endpoints[0].anchor.y, "source anchor y");
  1137. equals(0.7, conn.endpoints[1].anchor.x, "target anchor x");
  1138. equals(0.7, conn.endpoints[1].anchor.y, "target anchor y");
  1139. });
  1140. test(renderMode + ": jsPlumb.connect (anchors registered correctly; source and target anchors given, as strings)", function() {
  1141. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1142. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight", anchors:["LeftMiddle", "RightMiddle"] });
  1143. assertContextSize(3);
  1144. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1145. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1146. equals(0, conn.endpoints[0].anchor.x, "source anchor x");
  1147. equals(0.5, conn.endpoints[0].anchor.y, "source anchor y");
  1148. equals(1, conn.endpoints[1].anchor.x, "target anchor x");
  1149. equals(0.5, conn.endpoints[1].anchor.y, "target anchor y");
  1150. });
  1151. test(renderMode + ": jsPlumb.connect (anchors registered correctly; source and target anchors given, as arrays)", function() {
  1152. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1153. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight", anchors:[[0.3,0.3,1,0], [0.7,0.7,0,1]] });
  1154. assertContextSize(3);
  1155. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1156. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1157. equals(0.3, conn.endpoints[0].anchor.x, "source anchor x");
  1158. equals(0.3, conn.endpoints[0].anchor.y, "source anchor y");
  1159. equals(0.7, conn.endpoints[1].anchor.x, "target anchor x");
  1160. equals(0.7, conn.endpoints[1].anchor.y, "target anchor y");
  1161. });
  1162. test(renderMode + ": jsPlumb.connect (two argument method in which some data is reused across connections)", function() {
  1163. var d16 = _addDiv("d16"), d17 = _addDiv("d17"), d18 = _addDiv("d18"), d19 = _addDiv("d19");
  1164. var sharedData = { connector:"Straight", anchors:[[0.3,0.3,1,0], [0.7,0.7,0,1]] };
  1165. var conn = jsPlumb.connect({ source:d16, target:d17}, sharedData);
  1166. var conn2 = jsPlumb.connect({ source:d18, target:d19}, sharedData);
  1167. assertContextSize(6);
  1168. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 2);
  1169. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1170. equals(conn2.connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1171. equals(0.3, conn.endpoints[0].anchor.x, "source anchor x");
  1172. equals(0.3, conn.endpoints[0].anchor.y, "source anchor y");
  1173. equals(0.7, conn.endpoints[1].anchor.x, "target anchor x");
  1174. equals(0.7, conn.endpoints[1].anchor.y, "target anchor y");
  1175. equals(0.3, conn2.endpoints[0].anchor.x, "source anchor x");
  1176. equals(0.3, conn2.endpoints[0].anchor.y, "source anchor y");
  1177. equals(0.7, conn2.endpoints[1].anchor.x, "target anchor x");
  1178. equals(0.7, conn2.endpoints[1].anchor.y, "target anchor y");
  1179. });
  1180. test(renderMode + ": jsPlumb.connect (Connector as string test)", function() {
  1181. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1182. var conn = jsPlumb.connect({ source:d16, target:d17, connector:"Straight" });
  1183. assertContextSize(3);
  1184. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1185. equals(conn.connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1186. });
  1187. test(renderMode + ": jsPlumb.connect (Endpoint test)", function() {
  1188. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1189. var conn = jsPlumb.connect({ source:d16, target:d17, endpoint:"Rectangle" });
  1190. assertContextSize(3);
  1191. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1192. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints[renderMode].Rectangle, "Rectangle endpoint chosen for connection source");
  1193. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints[renderMode].Rectangle, "Rectangle endpoint chosen for connection target");
  1194. });
  1195. test(renderMode + ": jsPlumb.connect (Endpoint as string test)", function() {
  1196. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1197. var conn = jsPlumb.connect({ source:d16, target:d17, endpoint:"Rectangle" });
  1198. assertContextSize(3);
  1199. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1200. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints[renderMode].Rectangle, "Rectangle endpoint chosen for connection source");
  1201. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints[renderMode].Rectangle, "Rectangle endpoint chosen for connection target");
  1202. });
  1203. test(renderMode + ": jsPlumb.connect (Endpoints test)", function() {
  1204. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1205. var conn = jsPlumb.connect({ source:d16, target:d17, endpoints:["Rectangle", "Dot" ] });
  1206. assertContextSize(3);
  1207. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1208. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints[renderMode].Rectangle, "Rectangle endpoint chosen for connection source");
  1209. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints[renderMode].Dot, "Dot endpoint chosen for connection target");
  1210. });
  1211. test(renderMode + ": jsPlumb.connect (Blank Endpoint specified via 'endpoint' param)", function() {
  1212. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1213. var conn = jsPlumb.connect({ source:d16, target:d17, endpoint:"Blank" });
  1214. assertContextSize(3);
  1215. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1216. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints[renderMode].Blank, "Blank endpoint chosen for connection source");
  1217. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints[renderMode].Blank, "Blank endpoint chosen for connection target");
  1218. });
  1219. test(renderMode + ": jsPlumb.connect (Blank Endpoint specified via 'endpoints' param)", function() {
  1220. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1221. var conn = jsPlumb.connect({ source:d16, target:d17, endpoints:["Blank", "Blank" ] });
  1222. assertContextSize(3);
  1223. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1224. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints[renderMode].Blank, "Blank endpoint chosen for connection source");
  1225. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints[renderMode].Blank, "Blank endpoint chosen for connection target");
  1226. });
  1227. test(renderMode + ": jsPlumb.connect (Endpoint as string test)", function() {
  1228. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1229. var conn = jsPlumb.connect({ source:d16, target:d17, endpoints:["Rectangle", "Dot" ] });
  1230. assertContextSize(3);
  1231. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1232. equals(conn.endpoints[0].endpoint.constructor, jsPlumb.Endpoints[renderMode].Rectangle, "Rectangle endpoint chosen for connection source");
  1233. equals(conn.endpoints[1].endpoint.constructor, jsPlumb.Endpoints[renderMode].Dot, "Dot endpoint chosen for connection target");
  1234. });
  1235. test(renderMode + ": jsPlumb.connect (by Endpoints, connector test)", function() {
  1236. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1237. var e16 = jsPlumb.addEndpoint(d16, {});
  1238. var e17 = jsPlumb.addEndpoint(d17, {});
  1239. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  1240. assertContextSize(3);
  1241. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1242. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1243. });
  1244. test(renderMode + ": jsPlumb.connect (by Endpoints, connector as string test)", function() {
  1245. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1246. var e16 = jsPlumb.addEndpoint(d16, {});
  1247. var e17 = jsPlumb.addEndpoint(d17, {});
  1248. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  1249. assertContextSize(3);
  1250. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1251. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1252. });
  1253. test(renderMode + ": jsPlumb.connect (by Endpoints, anchors as string test)", function() {
  1254. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1255. var a16 = "TopCenter", a17 = "BottomCenter";
  1256. var e16 = jsPlumb.addEndpoint(d16, {anchor:a16});
  1257. var e17 = jsPlumb.addEndpoint(d17, {anchor:a17});
  1258. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  1259. assertContextSize(3);
  1260. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1261. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1262. equals(e16.anchor.x, 0.5, "endpoint 16 is at top center");equals(e16.anchor.y, 0, "endpoint 16 is at top center");
  1263. equals(e17.anchor.x, 0.5, "endpoint 17 is at bottom center");equals(e17.anchor.y, 1, "endpoint 17 is at bottom center");
  1264. });
  1265. test(renderMode + ": jsPlumb.connect (by Endpoints, endpoints create anchors)", function() {
  1266. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1267. var a16 = [0,0.5,0,-1], a17 = [1,0.0,-1,-1];
  1268. var e16 = jsPlumb.addEndpoint(d16, {anchor:a16});
  1269. var e17 = jsPlumb.addEndpoint(d17, {anchor:a17});
  1270. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  1271. assertContextSize(3);
  1272. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1273. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1274. equals(e16.anchor.x, a16[0]);equals(e16.anchor.y, a16[1]);
  1275. equals(e17.anchor.x, a17[0]);equals(e17.anchor.y, a17[1]);
  1276. equals(e16.anchor.getOrientation()[0], a16[2]); equals(e16.anchor.getOrientation()[1], a16[3]);
  1277. equals(e17.anchor.getOrientation()[0], a17[2]); equals(e17.anchor.getOrientation()[1], a17[3]);
  1278. });
  1279. test(renderMode + ": jsPlumb.connect (by Endpoints, endpoints create dynamic anchors; anchors specified by 'anchor')", function() {
  1280. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1281. var e16 = jsPlumb.addEndpoint(d16, {anchor:[[0,0.5,0,-1], [1,0.5,0,1]]});
  1282. var e17 = jsPlumb.addEndpoint(d17, {anchor:["TopCenter", "BottomCenter"]});
  1283. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  1284. assertContextSize(3);
  1285. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1286. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1287. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  1288. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  1289. });
  1290. test(renderMode + ": jsPlumb.connect (by Endpoints, endpoints create dynamic anchors; anchors specified by 'anchors')", function() {
  1291. var d16 = _addDiv("d16"), d17 = _addDiv("d17");
  1292. var e16 = jsPlumb.addEndpoint(d16, {anchors:[[0,0.5,0,-1], [1,0.5,0,1]]});
  1293. var e17 = jsPlumb.addEndpoint(d17, {anchors:["TopCenter", "BottomCenter"]});
  1294. var conn = jsPlumb.connect({ sourceEndpoint:e16, targetEndpoint:e17, connector:"Straight" });
  1295. assertContextSize(3);
  1296. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1297. equals(e16.connections[0].connector.constructor, jsPlumb.Connectors[renderMode].Straight, "Straight connector chosen for connection");
  1298. equals(e16.anchor.isDynamic, true, "Endpoint 16 has a dynamic anchor");
  1299. equals(e17.anchor.isDynamic, true, "Endpoint 17 has a dynamic anchor");
  1300. });
  1301. test(renderMode + ": jsPlumb.connect (connect by element, default endpoint, supplied dynamic anchors)", function() {
  1302. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1303. var anchors = [ [0.25, 0, 0, -1], [1, 0.25, 1, 0], [0.75, 1, 0, 1], [0, 0.75, -1, 0] ];
  1304. jsPlumb.connect({source:d1, target:d2, dynamicAnchors:anchors}); // auto connect with default endpoint and provided anchors
  1305. assertContextSize(3);
  1306. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1307. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1308. jsPlumb.detach({source:d1, target:d2});
  1309. // this changed in 1.3.5, because auto generated endpoints are now removed by detach. so i added the test below this one
  1310. // to check that the deleteEndpointsOnDetach flag is honoured.
  1311. assertEndpointCount("d1", 0);assertEndpointCount("d2", 0);
  1312. assertContextSize(2);
  1313. });
  1314. test(renderMode + ": jsPlumb.connect (connect by element, default endpoint, supplied dynamic anchors, delete on detach false)", function() {
  1315. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1316. var anchors = [ [0.25, 0, 0, -1], [1, 0.25, 1, 0], [0.75, 1, 0, 1], [0, 0.75, -1, 0] ];
  1317. jsPlumb.connect({
  1318. source:d1,
  1319. target:d2,
  1320. dynamicAnchors:anchors,
  1321. deleteEndpointsOnDetach:false
  1322. }); // auto connect with default endpoint and provided anchors
  1323. assertContextSize(3);
  1324. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1325. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1326. jsPlumb.detach({source:d1, target:d2});
  1327. // this changed in 1.3.5, because auto generated endpoints are now removed by detach. so i added this test
  1328. // to check that the deleteEndpointsOnDetach flag is honoured.
  1329. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1330. assertContextSize(2);
  1331. });
  1332. test(renderMode + ": jsPlumb.connect (connect by element, supplied endpoint and dynamic anchors)", function() {
  1333. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1334. var endpoint = { isSource:true };
  1335. var e1 = jsPlumb.addEndpoint(d1, endpoint);
  1336. var e2 = jsPlumb.addEndpoint(d2, endpoint);
  1337. var anchors = [ "TopCenter", "BottomCenter" ];
  1338. jsPlumb.connect({sourceEndpoint:e1, targetEndpoint:e2, dynamicAnchors:anchors});
  1339. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1340. assertContextSize(3);
  1341. jsPlumb.detach({source:d1, target:d2});
  1342. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1343. assertContextSize(2);
  1344. });
  1345. test(renderMode + ": 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() {
  1346. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1347. var endpoint = { isSource:true };
  1348. var e1 = jsPlumb.addEndpoint(d1, endpoint);
  1349. var e2 = jsPlumb.addEndpoint(d2, endpoint);
  1350. var anchors = [ "TopCenter", "BottomCenter" ];
  1351. jsPlumb.connect({source:e1, target:e2, dynamicAnchors:anchors});
  1352. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1353. assertContextSize(3);
  1354. jsPlumb.detach({source:d1, target:d2});
  1355. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1356. assertContextSize(2);
  1357. });
  1358. test(renderMode + " detachable parameter defaults to true on jsPlumb.connect", function() {
  1359. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1360. c = jsPlumb.connect({source:d1, target:d2});
  1361. equals(c.isDetachable(), true, "connections detachable by default");
  1362. });
  1363. test(renderMode + " detachable parameter set to false on jsPlumb.connect", function() {
  1364. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1365. c = jsPlumb.connect({source:d1, target:d2, detachable:false});
  1366. equals(c.isDetachable(), false, "connection detachable");
  1367. });
  1368. test(renderMode + " setDetachable on initially detachable connection", function() {
  1369. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1370. c = jsPlumb.connect({source:d1, target:d2});
  1371. equals(c.isDetachable(), true, "connection initially detachable");
  1372. c.setDetachable(false);
  1373. equals(c.isDetachable(), false, "connection not detachable");
  1374. });
  1375. test(renderMode + " setDetachable on initially not detachable connection", function() {
  1376. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1377. c = jsPlumb.connect({source:d1, target:d2, detachable:false });
  1378. equals(c.isDetachable(), false, "connection not initially detachable");
  1379. c.setDetachable(true);
  1380. equals(c.isDetachable(), true, "connection now detachable");
  1381. });
  1382. test(renderMode + " jsPlumb.Defaults.ConnectionsDetachable", function() {
  1383. jsPlumb.Defaults.ConnectionsDetachable = false;
  1384. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1385. c = jsPlumb.connect({source:d1, target:d2});
  1386. equals(c.isDetachable(), false, "connections not detachable by default (overrode the defaults)");
  1387. jsPlumb.Defaults.ConnectionsDetachable = true;
  1388. });
  1389. test(renderMode + ": jsPlumb.connect (testing for connection event callback)", function() {
  1390. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1391. var connectCallback = null, detachCallback = null;
  1392. jsPlumb.bind("jsPlumbConnection", function(params) {
  1393. connectCallback = $.extend({}, params);
  1394. });
  1395. jsPlumb.bind("jsPlumbConnectionDetached", function(params) {
  1396. detachCallback = $.extend({}, params);
  1397. });
  1398. jsPlumb.connect({source:d1, target:d2}); // auto connect with default endpoint and anchor set
  1399. ok(connectCallback != null, "connect callback was made");
  1400. assertContextSize(3);
  1401. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1402. assertEndpointCount("d1", 1);assertEndpointCount("d2", 1);
  1403. jsPlumb.detach({source:d1, target:d2});
  1404. assertContextSize(2);
  1405. ok(detachCallback != null, "detach callback was made");
  1406. });
  1407. test(renderMode + ": jsPlumb.connect (setting cssClass on Connector)", function() {
  1408. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1409. var c = jsPlumb.connect({source:d1,target:d2,cssClass:"CSS"});
  1410. var has = function(clazz) {
  1411. var cn = c.connector.canvas.className,
  1412. cns = cn.constructor == String ? cn : cn.baseVal;
  1413. return cns.indexOf(clazz) != -1;
  1414. };
  1415. ok(has("CSS"), "custom cssClass set correctly");
  1416. ok(has(jsPlumb.connectorClass), "basic connector class set correctly");
  1417. });
  1418. test(renderMode + ": jsPlumb.connect (overlays, long-hand version)", function() {
  1419. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1420. var imageEventListener = function() { };
  1421. var arrowSpec = {width:40,length:40,location:0.7, foldback:0, paintStyle:{lineWidth:1, strokeStyle:"#000000"}};
  1422. var connection1 = jsPlumb.connect({
  1423. source:d1,
  1424. target:d2,
  1425. anchors:["BottomCenter", [ 0.75,0,0,-1 ]],
  1426. overlays : [ ["Label",{label:"CONNECTION 1", location:0.3}],
  1427. ["Arrow",arrowSpec ] ]
  1428. });
  1429. equals(2, connection1.overlays.length);
  1430. equals(jsPlumb.Overlays[renderMode].Label, connection1.overlays[0].constructor);
  1431. equals(jsPlumb.Overlays[renderMode].Arrow, connection1.overlays[1].constructor);
  1432. equals(0.7, connection1.overlays[1].loc);
  1433. equals(40, connection1.overlays[1].width);
  1434. equals(40, connection1.overlays[1].length);
  1435. });
  1436. test(renderMode + ": jsPlumb.connect (overlays, long-hand version, IDs specified)", function() {
  1437. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1438. var imageEventListener = function() { };
  1439. var arrowSpec = {
  1440. width:40,
  1441. length:40,
  1442. location:0.7,
  1443. foldback:0,
  1444. paintStyle:{lineWidth:1, strokeStyle:"#000000"},
  1445. id:"anArrow"
  1446. };
  1447. var connection1 = jsPlumb.connect({
  1448. source:d1,
  1449. target:d2,
  1450. anchors:["BottomCenter", [ 0.75,0,0,-1 ]],
  1451. overlays : [ ["Label",{label:"CONNECTION 1", location:0.3, id:"aLabel"}],
  1452. ["Arrow",arrowSpec ] ]
  1453. });
  1454. equals(2, connection1.overlays.length);
  1455. equals(jsPlumb.Overlays[renderMode].Label, connection1.overlays[0].constructor);
  1456. equals("aLabel", connection1.overlays[0].id);
  1457. equals(jsPlumb.Overlays[renderMode].Arrow, connection1.overlays[1].constructor);
  1458. equals(0.7, connection1.overlays[1].loc);
  1459. equals(40, connection1.overlays[1].width);
  1460. equals(40, connection1.overlays[1].length);
  1461. equals("anArrow", connection1.overlays[1].id);
  1462. });
  1463. test(renderMode + ": jsPlumb.connect (remove single overlay by id)", function() {
  1464. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1465. var arrowSpec = {
  1466. width:40,
  1467. length:40,
  1468. location:0.7,
  1469. foldback:0,
  1470. paintStyle:{lineWidth:1, strokeStyle:"#000000"},
  1471. id:"anArrow"
  1472. };
  1473. var connection1 = jsPlumb.connect({
  1474. source:d1,
  1475. target:d2,
  1476. anchors:["BottomCenter", [ 0.75,0,0,-1 ]],
  1477. overlays : [ ["Label",{label:"CONNECTION 1", location:0.3, id:"aLabel"}],
  1478. ["Arrow",arrowSpec ] ]
  1479. });
  1480. equals(2, connection1.overlays.length);
  1481. connection1.removeOverlay("aLabel");
  1482. equals(1, connection1.overlays.length);
  1483. equals("anArrow", connection1.overlays[0].id);
  1484. });
  1485. test(renderMode + ": jsPlumb.connect (remove multiple overlays by id)", function() {
  1486. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1487. var arrowSpec = {
  1488. width:40,
  1489. length:40,
  1490. location:0.7,
  1491. foldback:0,
  1492. paintStyle:{lineWidth:1, strokeStyle:"#000000"},
  1493. id:"anArrow"
  1494. };
  1495. var connection1 = jsPlumb.connect({
  1496. source:d1,
  1497. target:d2,
  1498. anchors:["BottomCenter", [ 0.75,0,0,-1 ]],
  1499. overlays : [ ["Label",{label:"CONNECTION 1", location:0.3, id:"aLabel"}],
  1500. ["Arrow",arrowSpec ] ]
  1501. });
  1502. equals(2, connection1.overlays.length);
  1503. connection1.removeOverlays("aLabel", "anArrow");
  1504. equals(0, connection1.overlays.length);
  1505. });
  1506. test(renderMode + ": jsPlumb.connect (overlays, short-hand version)", function() {
  1507. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1508. var imageEventListener = function() { };
  1509. var loc = { location:0.7 };
  1510. var arrowSpec = { width:40,length:40, foldback:0, paintStyle:{lineWidth:1, strokeStyle:"#000000"} };
  1511. var connection1 = jsPlumb.connect({
  1512. source:d1,
  1513. target:d2,
  1514. anchors:["BottomCenter", [ 0.75,0,0,-1 ]],
  1515. overlays : [ ["Label", {label:"CONNECTION 1", location:0.3}],
  1516. ["Arrow", arrowSpec, loc] ]
  1517. });
  1518. equals(2, connection1.overlays.length);
  1519. equals(jsPlumb.Overlays[renderMode].Label, connection1.overlays[0].constructor);
  1520. equals(jsPlumb.Overlays[renderMode].Arrow, connection1.overlays[1].constructor);
  1521. equals(0.7, connection1.overlays[1].loc);
  1522. equals(40, connection1.overlays[1].width);
  1523. equals(40, connection1.overlays[1].length);
  1524. });
  1525. test(renderMode + ": jsPlumb.connect (removeAllOverlays)", function() {
  1526. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1527. var imageEventListener = function() { };
  1528. var loc = { location:0.7 };
  1529. var arrowSpec = { width:40,length:40, foldback:0, paintStyle:{lineWidth:1, strokeStyle:"#000000"} };
  1530. var connection1 = jsPlumb.connect({
  1531. source:d1,
  1532. target:d2,
  1533. anchors:["BottomCenter", [ 0.75,0,0,-1 ]],
  1534. overlays : [ ["Label", {label:"CONNECTION 1", location:0.3}],
  1535. ["Arrow", arrowSpec, loc] ]
  1536. });
  1537. equals(2, connection1.overlays.length);
  1538. equals(jsPlumb.Overlays[renderMode].Label, connection1.overlays[0].constructor);
  1539. equals(jsPlumb.Overlays[renderMode].Arrow, connection1.overlays[1].constructor);
  1540. equals(0.7, connection1.overlays[1].loc);
  1541. equals(40, connection1.overlays[1].width);
  1542. equals(40, connection1.overlays[1].length);
  1543. connection1.removeAllOverlays();
  1544. equals(0, connection1.overlays.length);
  1545. });
  1546. test(renderMode + ": jsPlumb.connect, specify arrow overlay using string identifier only", function() {
  1547. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1548. var conn = jsPlumb.connect({source:d1,target:d2,overlays:["Arrow"]});
  1549. equals(jsPlumb.Overlays[renderMode].Arrow, conn.overlays[0].constructor);
  1550. });
  1551. test(renderMode + ": Connection.getOverlay method, existing overlay", function() {
  1552. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1553. var conn = jsPlumb.connect({source:d1,target:d2,overlays:[ [ "Arrow", { id:"arrowOverlay" } ] ] });
  1554. var overlay = conn.getOverlay("arrowOverlay");
  1555. ok(overlay != null);
  1556. });
  1557. test(renderMode + ": Connection.getOverlay method, non-existent overlay", function() {
  1558. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1559. var conn = jsPlumb.connect({source:d1,target:d2,overlays:[ [ "Arrow", { id:"arrowOverlay" } ] ] });
  1560. var overlay = conn.getOverlay("IDONTEXIST");
  1561. ok(overlay == null);
  1562. });
  1563. test(renderMode + ": Overlay.setVisible method", function() {
  1564. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1565. var conn = jsPlumb.connect({source:d1,target:d2,overlays:[ [ "Arrow", { id:"arrowOverlay" } ] ] });
  1566. var overlay = conn.getOverlay("arrowOverlay");
  1567. ok(overlay.isVisible());
  1568. overlay.setVisible(false);
  1569. ok(!overlay.isVisible());
  1570. overlay.setVisible(true);
  1571. ok(overlay.isVisible());
  1572. });
  1573. // this test is for the original detach function; it should stay working after i mess with it
  1574. // a little.
  1575. test(renderMode + ": jsPlumb.detach (by element ids)", function() {
  1576. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1577. var e1 = jsPlumb.addEndpoint(d1);
  1578. var e2 = jsPlumb.addEndpoint(d2);
  1579. var e3 = jsPlumb.addEndpoint(d1);
  1580. var e4 = jsPlumb.addEndpoint(d2);
  1581. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1582. jsPlumb.connect({ sourceEndpoint:e3, targetEndpoint:e4 }); // make two connections to be sure this works ;)
  1583. assertConnectionCount(e1, 1);
  1584. assertConnectionCount(e2, 1);
  1585. assertConnectionCount(e3, 1);
  1586. assertConnectionCount(e4, 1);
  1587. jsPlumb.detach({source:"d1", target:"d2"});
  1588. assertConnectionCount(e1, 0);
  1589. assertConnectionCount(e2, 0);
  1590. assertConnectionCount(e3, 0);
  1591. assertConnectionCount(e4, 0);
  1592. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1593. });
  1594. // detach is being made to operate more like connect - by taking one argument with a whole
  1595. // bunch of possible params in it. if two args are passed in it will continue working
  1596. // in the old way.
  1597. test(renderMode + ": jsPlumb.detach (params object, using element ids)", function() {
  1598. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1599. var e1 = jsPlumb.addEndpoint(d1);
  1600. var e2 = jsPlumb.addEndpoint(d2);
  1601. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1602. assertConnectionCount(e1, 1);
  1603. assertConnectionCount(e2, 1);
  1604. jsPlumb.detach({source:"d1", target:"d2"});
  1605. assertConnectionCount(e1, 0);
  1606. assertConnectionCount(e2, 0);
  1607. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1608. });
  1609. test(renderMode + ": jsPlumb.detach (params object, using element objects)", function() {
  1610. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1611. var e1 = jsPlumb.addEndpoint(d1);
  1612. var e2 = jsPlumb.addEndpoint(d2);
  1613. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1614. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1615. assertContextSize(3);
  1616. assertConnectionCount(e1, 1);
  1617. assertConnectionCount(e2, 1);
  1618. jsPlumb.detach({source:d1, target:d2});
  1619. assertConnectionCount(e1, 0);
  1620. assertConnectionCount(e2, 0);
  1621. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1622. assertContextSize(2);
  1623. });
  1624. test(renderMode + ": jsPlumb.detach (source and target as endpoint UUIDs)", function() {
  1625. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1626. var e1 = jsPlumb.addEndpoint(d1, {uuid:"abcdefg"});
  1627. ok(jsPlumb.getEndpoint("abcdefg") != null, "e1 exists");
  1628. var e2 = jsPlumb.addEndpoint(d2, {uuid:"hijklmn"});
  1629. ok(jsPlumb.getEndpoint("hijklmn") != null, "e2 exists");
  1630. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1631. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1632. assertContextSize(3);
  1633. assertConnectionCount(e1, 1);
  1634. assertConnectionCount(e2, 1);
  1635. jsPlumb.detach({uuids:["abcdefg", "hijklmn"]});
  1636. assertConnectionCount(e1, 0);
  1637. assertConnectionCount(e2, 0);
  1638. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1639. assertContextSize(2);
  1640. });
  1641. test(renderMode + ": jsPlumb.detach (sourceEndpoint and targetEndpoint supplied)", function() {
  1642. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1643. var e1 = jsPlumb.addEndpoint(d1);
  1644. var e2 = jsPlumb.addEndpoint(d2);
  1645. jsPlumb.connect({ sourceEndpoint:e1, targetEndpoint:e2 });
  1646. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 1);
  1647. assertContextSize(3);
  1648. assertConnectionCount(e1, 1);
  1649. assertConnectionCount(e2, 1);
  1650. jsPlumb.detach({ sourceEndpoint:e1, targetEndpoint:e2 });
  1651. assertConnectionCount(e1, 0);
  1652. assertConnectionCount(e2, 0);
  1653. assertConnectionByScopeCount(jsPlumb.getDefaultScope(), 0);
  1654. assertContextSize(2);
  1655. });
  1656. test(renderMode + ": jsPlumb.makeDynamicAnchors (longhand)", function() {
  1657. var anchors = [jsPlumb.makeAnchor([0.2, 0, 0, -1], null, jsPlumb), jsPlumb.makeAnchor([1, 0.2, 1, 0], null, jsPlumb),
  1658. jsPlumb.makeAnchor([0.8, 1, 0, 1], null, jsPlumb), jsPlumb.makeAnchor([0, 0.8, -1, 0], null, jsPlumb) ];
  1659. var dynamicAnchor = jsPlumb.makeDynamicAnchor(anchors);
  1660. var a = dynamicAnchor.getAnchors();
  1661. equals(a.length, 4, "Dynamic Anchors has four anchors");
  1662. for (var i = 0; i < a.length; i++)
  1663. ok(a[i].compute.constructor == Function, "anchor " + i + " well formed");
  1664. });
  1665. test(renderMode + ": jsPlumb.makeDynamicAnchors (shorthand)", function() {
  1666. var anchors = [[0.2, 0, 0, -1], [1, 0.2, 1, 0],
  1667. [0.8, 1, 0, 1], [0, 0.8, -1, 0] ];
  1668. var dynamicAnchor = jsPlumb.makeDynamicAnchor(anchors);
  1669. var a = dynamicAnchor.getAnchors();
  1670. equals(a.length, 4, "Dynamic Anchors has four anchors");
  1671. for (var i = 0; i < a.length; i++)
  1672. ok(a[i].compute.constructor == Function, "anchor " + i + " well formed");
  1673. });
  1674. test(renderMode + ": Connection.isVisible/setVisible", function() {
  1675. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1676. var c1 = jsPlumb.connect({source:d1,target:d2});
  1677. equals(true, c1.isVisible(), "Connection is visible after creation.");
  1678. c1.setVisible(false);
  1679. equals(false, c1.isVisible(), "Connection is not visible after calling setVisible(false).");
  1680. equals($(c1.connector.canvas).css("display"), "none");
  1681. c1.setVisible(true);
  1682. equals(true, c1.isVisible(), "Connection is visible after calling setVisible(true).");
  1683. equals($(c1.connector.canvas).css("display"), "block");
  1684. });
  1685. test(renderMode + ": Endpoint.isVisible/setVisible basic test (no connections)", function() {
  1686. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1687. var e1 = jsPlumb.addEndpoint(d1);
  1688. equals(true, e1.isVisible(), "Endpoint is visible after creation.");
  1689. e1.setVisible(false);
  1690. equals(false, e1.isVisible(), "Endpoint is not visible after calling setVisible(false).");
  1691. equals($(e1.canvas).css("display"), "none");
  1692. e1.setVisible(true);
  1693. equals(true, e1.isVisible(), "Endpoint is visible after calling setVisible(true).");
  1694. equals($(e1.canvas).css("display"), "block");
  1695. });
  1696. test(renderMode + ": Endpoint.isVisible/setVisible (one connection, other Endpoint's visibility should track changes in the source, because it has only this connection.)", function() {
  1697. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1698. var e1 = jsPlumb.addEndpoint(d1), e2 = jsPlumb.addEndpoint(d2);
  1699. equals(true, e1.isVisible(), "Endpoint is visible after creation.");
  1700. var c1 = jsPlumb.connect({source:e1, target:e2});
  1701. e1.setVisible(false);
  1702. equals(false, e1.isVisible(), "Endpoint is not visible after calling setVisible(false).");
  1703. equals(false, e2.isVisible(), "other Endpoint is not visible either.");
  1704. equals(false, c1.isVisible(), "connection between the two is not visible either.");
  1705. e1.setVisible(true);
  1706. equals(true, e1.isVisible(), "Endpoint is visible after calling setVisible(true).");
  1707. equals(true, e2.isVisible(), "other Endpoint is visible too");
  1708. equals(true, c1.isVisible(), "connection between the two is visible too.");
  1709. });
  1710. test(renderMode + ": Endpoint.isVisible/setVisible (one connection, other Endpoint's visibility should not track changes in the source, because it has another connection.)", function() {
  1711. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1712. var e1 = jsPlumb.addEndpoint(d1), e2 = jsPlumb.addEndpoint(d2, { maxConnections:2 }), e3 = jsPlumb.addEndpoint(d3);
  1713. equals(true, e1.isVisible(), "Endpoint is visible after creation.");
  1714. var c1 = jsPlumb.connect({source:e1, target:e2});
  1715. var c2 = jsPlumb.connect({source:e2, target:e3});
  1716. e1.setVisible(false);
  1717. equals(false, e1.isVisible(), "Endpoint is not visible after calling setVisible(false).");
  1718. equals(true, e2.isVisible(), "other Endpoint should still be visible.");
  1719. equals(true, e3.isVisible(), "third Endpoint should still be visible.");
  1720. equals(false, c1.isVisible(), "connection between the two is not visible either.");
  1721. equals(true, c2.isVisible(), "other connection is visible.");
  1722. e1.setVisible(true);
  1723. equals(true, e1.isVisible(), "Endpoint is visible after calling setVisible(true).");
  1724. equals(true, e2.isVisible(), "other Endpoint is visible too");
  1725. equals(true, c1.isVisible(), "connection between the two is visible too.");
  1726. equals(true, c2.isVisible(), "other connection is visible.");
  1727. });
  1728. // tests of the functionality that allows a user to specify that they want elements appended to the document body
  1729. test(renderMode + " jsPlumb.Defaults.Container, specified with a selector", function() {
  1730. jsPlumb.Defaults.Container = $("body");
  1731. equals(0, $("#container")[0].childNodes.length);
  1732. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1733. equals(2, $("#container")[0].childNodes.length); // the divs we added have been added to the 'container' div.
  1734. // but we have told jsPlumb to add its canvas to the body, so this connect call should not add another few elements to the container:
  1735. jsPlumb.connect({source:d1, target:d2});
  1736. equals(2, $("#container")[0].childNodes.length);
  1737. });
  1738. // tests of the functionality that allows a user to specify that they want elements appended to some specific container.
  1739. test(renderMode + " jsPlumb.Defaults.Container, specified with DOM element", function() {
  1740. jsPlumb.Defaults.Container = document.getElementsByTagName("body")[0];
  1741. equals(0, $("#container")[0].childNodes.length);
  1742. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1743. equals(2, $("#container")[0].childNodes.length, "two divs added to the container"); // the divs we added have been added to the 'container' div.
  1744. // but we have told jsPlumb to add its canvas to the body, so this connect call should not add another few elements to the container:
  1745. var bodyElementCount = $("body")[0].childNodes.length;
  1746. jsPlumb.connect({source:d1, target:d2});
  1747. equals(2, $("#container")[0].childNodes.length, "still only two children in container; elements were added to the body by jsPlumb");
  1748. // test to see if 3 elements have been added
  1749. equals(bodyElementCount + 3, $("body")[0].childNodes.length, "3 new elements added to the document body");
  1750. });
  1751. test(renderMode + " container specified to connect call, with a selector", function() {
  1752. equals(0, $("#container")[0].childNodes.length);
  1753. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1754. equals(2, $("#container")[0].childNodes.length); // the divs we added have been added to the 'container' div.
  1755. var bodyElementCount = $("body")[0].childNodes.length;
  1756. // but here we tell jsPlumb to add its elements to the body, so this connect call should not add another few elements to the container:
  1757. jsPlumb.connect({source:d1, target:d2, container:$("body")});
  1758. equals(2, $("#container")[0].childNodes.length);
  1759. equals(bodyElementCount + 3, $("body")[0].childNodes.length, "3 new elements added to the document body");
  1760. });
  1761. test(renderMode + " container specified to connect call, with a string ID", function() {
  1762. equals(0, $("#container")[0].childNodes.length);
  1763. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1764. equals(3, $("#container")[0].childNodes.length, "container has divs we added"); // the divs we added have been added to the 'container' div.
  1765. var d3ElementCount = $("#d3")[0].childNodes.length;
  1766. // but here we tell jsPlumb to add its elements to "d3", so this connect call should not add another few elements to the container:
  1767. jsPlumb.connect({source:d1, target:d2, container:"d3"});
  1768. equals(3, $("#container")[0].childNodes.length, "container still has only the divs we added");
  1769. equals(d3ElementCount + 3, $("#d3")[0].childNodes.length, "3 new elements added to div d3");
  1770. });
  1771. test(renderMode + " container specified to addEndpoint call, with a selector", function() {
  1772. equals(0, $("#container")[0].childNodes.length);
  1773. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1774. equals(2, $("#container")[0].childNodes.length); // the divs we added have been added to the 'container' div.
  1775. var bodyElementCount = $("body")[0].childNodes.length;
  1776. // but here we tell jsPlumb to add its elements to the body, so this connect call should not add another few elements to the container:
  1777. jsPlumb.addEndpoint(d1, {container:$("body")});
  1778. equals(2, $("#container")[0].childNodes.length);
  1779. equals(bodyElementCount + 1, $("body")[0].childNodes.length, "1 new element added to the document body");
  1780. });
  1781. test(renderMode + " container specified to addEndpoint call, with a string ID", function() {
  1782. equals(0, $("#container")[0].childNodes.length);
  1783. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3");
  1784. equals(3, $("#container")[0].childNodes.length, "container has divs we added"); // the divs we added have been added to the 'container' div.
  1785. var d3ElementCount = $("#d3")[0].childNodes.length;
  1786. // but here we tell jsPlumb to add its elements to "d3", so this connect call should not add another few elements to the container:
  1787. jsPlumb.addEndpoint(d1, { container:"d3" });
  1788. equals(3, $("#container")[0].childNodes.length, "container still has only the divs we added");
  1789. equals(d3ElementCount + 1, $("#d3")[0].childNodes.length, "1 new element added to div d3");
  1790. });
  1791. test(renderMode + " detachable defaults to true when connection made between two endpoints", function() {
  1792. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1793. e1 = jsPlumb.addEndpoint(d1), e2 = jsPlumb.addEndpoint(d2),
  1794. c = jsPlumb.connect({source:e1, target:e2});
  1795. equals(c.isDetachable(), true, "connection not detachable");
  1796. });
  1797. test(renderMode + " connection detachable when target endpoint has connectionsDetachable set to true", function() {
  1798. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1799. e1 = jsPlumb.addEndpoint(d1), e2 = jsPlumb.addEndpoint(d2, {connectionsDetachable:true}),
  1800. c = jsPlumb.connect({source:e1, target:e2});
  1801. equals(c.isDetachable(), true, "connection detachable because connectionsDetachable was set on target endpoint");
  1802. });
  1803. test(renderMode + " connection detachable when source endpoint has connectionsDetachable set to true", function() {
  1804. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1805. e1 = jsPlumb.addEndpoint(d1, {connectionsDetachable:true}), e2 = jsPlumb.addEndpoint(d2),
  1806. c = jsPlumb.connect({source:e1, target:e2});
  1807. equals(c.isDetachable(), true, "connection detachable because connectionsDetachable was set on source endpoint");
  1808. });
  1809. test(renderMode + " Connector has 'type' member set", function() {
  1810. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1811. var c = jsPlumb.connect({source:d1, target:d2});
  1812. equals(c.connector.type, "Bezier", "Bezier connector has type set");
  1813. var c2 = jsPlumb.connect({source:d1, target:d2, connector:"Straight"});
  1814. equals(c2.connector.type, "Straight", "Straight connector has type set");
  1815. var c3 = jsPlumb.connect({source:d1, target:d2, connector:"Flowchart"});
  1816. equals(c3.connector.type, "Flowchart", "Flowchart connector has type set");
  1817. });
  1818. test(renderMode + " Endpoints have 'type' member set", function() {
  1819. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1820. var c = jsPlumb.connect({source:d1, target:d2});
  1821. equals(c.endpoints[0].type, "Dot", "Dot endpoint has type set");
  1822. var c2 = jsPlumb.connect({source:d1, target:d2, endpoints:["Rectangle", "Blank"]});
  1823. equals(c2.endpoints[1].type, "Blank", "Blank endpoint has type set");
  1824. equals(c2.endpoints[0].type, "Rectangle", "Rectangle endpoint has type set");
  1825. });
  1826. test(renderMode + " Overlays have 'type' member set", function() {
  1827. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1828. var c = jsPlumb.connect({
  1829. source:d1,
  1830. target:d2,
  1831. overlays:[ "Arrow", "Label", "PlainArrow", "Diamond" ]
  1832. });
  1833. equals(c.overlays[0].type, "Arrow", "Arrow overlay has type set");
  1834. equals(c.overlays[1].type, "Label", "Label overlay has type set");
  1835. equals(c.overlays[2].type, "PlainArrow", "PlainArrow overlay has type set");
  1836. equals(c.overlays[3].type, "Diamond", "Diamond overlay has type set");
  1837. });
  1838. test(renderMode + " jsPlumb.hide, original one-arg version", function() {
  1839. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1840. e = { isSource:true, isTarget:true, maxConnections:-1 },
  1841. e1 = jsPlumb.addEndpoint(d1, e),
  1842. e2 = jsPlumb.addEndpoint(d2, e),
  1843. c1 = jsPlumb.connect({source:e1, target:e2});
  1844. equals(true, c1.isVisible(), "Connection 1 is visible after creation.");
  1845. equals(true, e1.isVisible(), "endpoint 1 is visible after creation.");
  1846. equals(true, e2.isVisible(), "endpoint 2 is visible after creation.");
  1847. jsPlumb.hide(d1);
  1848. equals(false, c1.isVisible(), "Connection 1 is no longer visible.");
  1849. equals(true, e1.isVisible(), "endpoint 1 is still visible.");
  1850. equals(true, e2.isVisible(), "endpoint 2 is still visible.");
  1851. jsPlumb.show(d1);
  1852. equals(true, c1.isVisible(), "Connection 1 is visible again.");
  1853. });
  1854. test(renderMode + " jsPlumb.hide, two-arg version, endpoints should also be hidden", function() {
  1855. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1856. e = { isSource:true, isTarget:true, maxConnections:-1 },
  1857. e1 = jsPlumb.addEndpoint(d1, e),
  1858. e2 = jsPlumb.addEndpoint(d2, e),
  1859. c1 = jsPlumb.connect({source:e1, target:e2});
  1860. equals(true, c1.isVisible(), "Connection 1 is visible after creation.");
  1861. equals(true, e1.isVisible(), "endpoint 1 is visible after creation.");
  1862. equals(true, e2.isVisible(), "endpoint 2 is visible after creation.");
  1863. jsPlumb.hide("d1", true);
  1864. equals(false, c1.isVisible(), "Connection 1 is no longer visible.");
  1865. equals(false, e1.isVisible(), "endpoint 1 is no longer visible.");
  1866. equals(true, e2.isVisible(), "endpoint 2 is still visible.");
  1867. jsPlumb.show(d1); // now show d1, but do not alter the endpoints. e1 should still be hidden
  1868. equals(true, c1.isVisible(), "Connection 1 is visible again.");
  1869. equals(false, e1.isVisible(), "endpoint 1 is no longer visible.");
  1870. equals(true, e2.isVisible(), "endpoint 2 is still visible.");
  1871. });
  1872. test(renderMode + " jsPlumb.show, two-arg version, endpoints should become visible", function() {
  1873. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1874. e = { isSource:true, isTarget:true, maxConnections:-1 },
  1875. e1 = jsPlumb.addEndpoint(d1, e),
  1876. e2 = jsPlumb.addEndpoint(d2, e),
  1877. c1 = jsPlumb.connect({source:e1, target:e2});
  1878. jsPlumb.hide("d1", true);
  1879. equals(false, c1.isVisible(), "Connection 1 is no longer visible.");
  1880. equals(false, e1.isVisible(), "endpoint 1 is no longer visible.");
  1881. equals(true, e2.isVisible(), "endpoint 2 is still visible.");
  1882. jsPlumb.show(d1, true); // now show d1, and alter the endpoints. e1 should be visible.
  1883. equals(true, c1.isVisible(), "Connection 1 is visible again.");
  1884. equals(true, e1.isVisible(), "endpoint 1 is visible again.");
  1885. equals(true, e2.isVisible(), "endpoint 2 is still visible.");
  1886. });
  1887. test(renderMode + " jsPlumb.show, two-arg version, endpoints should become visible, but not all connections, because some other endpoints are not visible.", function() {
  1888. var d1 = _addDiv("d1"), d2 = _addDiv("d2"), d3 = _addDiv("d3"),
  1889. e = { isSource:true, isTarget:true, maxConnections:-1 },
  1890. e1 = jsPlumb.addEndpoint(d1, e),
  1891. e11 = jsPlumb.addEndpoint(d1, e),
  1892. e2 = jsPlumb.addEndpoint(d2, e),
  1893. e3 = jsPlumb.addEndpoint(d3, e),
  1894. c1 = jsPlumb.connect({source:e1, target:e2}),
  1895. c2 = jsPlumb.connect({source:e11, target:e3});
  1896. // we now have d1 connected to both d3 and d2. we'll hide d1, and everything on d1 should be hidden.
  1897. jsPlumb.hide("d1", true);
  1898. equals(false, c1.isVisible(), "connection 1 is no longer visible.");
  1899. equals(false, c2.isVisible(), "connection 2 is no longer visible.");
  1900. equals(false, e1.isVisible(), "endpoint 1 is no longer visible.");
  1901. equals(false, e11.isVisible(), "endpoint 1 is no longer visible.");
  1902. equals(true, e2.isVisible(), "endpoint 2 is still visible.");
  1903. equals(true, e3.isVisible(), "endpoint 3 is still visible.");
  1904. // now, we will also hide d3. making d1 visible again should NOT result in c2 becoming visible, because the other endpoint
  1905. // for c2 is e3, which is not visible.
  1906. jsPlumb.hide(d3, true);
  1907. equals(false, e3.isVisible(), "endpoint 3 is no longer visible.");
  1908. jsPlumb.show(d1, true); // now show d1, and alter the endpoints. e1 should be visible, c1 should be visible, but c2 should not.
  1909. equals(true, c1.isVisible(), "Connection 1 is visible again.");
  1910. equals(false, c2.isVisible(), "Connection 2 is not visible.");
  1911. equals(true, e1.isVisible(), "endpoint 1 is visible again.");
  1912. equals(true, e11.isVisible(), "endpoint 11 is visible again.");
  1913. equals(true, e2.isVisible(), "endpoint 2 is still visible.");
  1914. equals(false, e3.isVisible(), "endpoint 3 is still not visible.");
  1915. });
  1916. /*
  1917. test(renderMode + " jsPlumb.hide, two-arg version, endpoints should also be hidden", function() {
  1918. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1919. e = { isSource:true, isTarget:true, maxConnections:-1 },
  1920. e1 = jsPlumb.addEndpoint(d1, e),
  1921. e2 = jsPlumb.addEndpoint(d2, e),
  1922. c1 = jsPlumb.connect({source:e1, target:e2});
  1923. equals(true, c1.isVisible(), "Connection 1 is visible after creation.");
  1924. equals(true, e1.isVisible(), "endpoint 1 is visible after creation.");
  1925. equals(true, e2.isVisible(), "endpoint 2 is visible after creation.");
  1926. jsPlumb.hide("d1", true);
  1927. equals(false, c1.isVisible(), "Connection 1 is no longer visible.");
  1928. equals(false, e1.isVisible(), "endpoint 1 is no longer visible.");
  1929. equals(true, e2.isVisible(), "endpoint 2 is still visible.");
  1930. });
  1931. /**
  1932. * test for issue 132: label leaves its element in the DOM after it has been
  1933. * removed from a connection.
  1934. */
  1935. test(renderMode + " label cleans itself up properly", function() {
  1936. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1937. var c = jsPlumb.connect({source:d1,target:d2, overlays:[
  1938. [ "Label", {id:"label", cssClass:"foo"}]
  1939. ]});
  1940. ok($(".foo").length == 1, "label element exists in DOM");
  1941. c.removeOverlay("label");
  1942. ok($(".foo").length == 0, "label element does not exist in DOM");
  1943. });
  1944. test(renderMode + " arrow cleans itself up properly", function() {
  1945. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1946. var c = jsPlumb.connect({source:d1,target:d2, overlays:[
  1947. [ "Arrow", {id:"arrow"}]
  1948. ]});
  1949. ok(c.getOverlay("arrow") != null, "arrow overlay exists");
  1950. c.removeOverlay("arrow");
  1951. ok(c.getOverlay("arrow") == null, "arrow overlay has been removed");
  1952. });
  1953. test(renderMode + " label overlay getElement function", function() {
  1954. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1955. var c = jsPlumb.connect({source:d1,target:d2, overlays:[
  1956. [ "Label", {id:"label"}]
  1957. ]});
  1958. ok(c.getOverlay("label").getElement() != null, "label overlay exposes element via getElement method");
  1959. });
  1960. test(renderMode + " label overlay provides getLabel and setLabel methods", function() {
  1961. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  1962. var c = jsPlumb.connect({source:d1,target:d2, overlays:[
  1963. [ "Label", {id:"label", label:"foo"}]
  1964. ]});
  1965. var o = c.getOverlay("label"), e = o.getElement();
  1966. ok(e.innerHTML == "foo", "label text is set to original value");
  1967. o.setLabel("baz");
  1968. ok(e.innerHTML == "baz", "label text is set to new value 'baz'");
  1969. ok(o.getLabel() === "baz", "getLabel function works correctly with String");
  1970. // now try functions
  1971. var aFunction = function() { return "aFunction"; };
  1972. o.setLabel(aFunction);
  1973. ok(e.innerHTML == "aFunction", "label text is set to new value from Function");
  1974. ok(o.getLabel() === aFunction, "getLabel function works correctly with Function");
  1975. });
  1976. test(renderMode + " parameters object works for Endpoint", function() {
  1977. var d1 = _addDiv("d1"),
  1978. f = function() { alert("FOO!"); },
  1979. e = jsPlumb.addEndpoint(d1, {
  1980. isSource:true,
  1981. parameters:{
  1982. "string":"param1",
  1983. "int":4,
  1984. "function":f
  1985. }
  1986. });
  1987. ok(e.getParameter("string") === "param1", "getParameter(String) works correctly");
  1988. ok(e.getParameter("int") === 4, "getParameter(int) works correctly");
  1989. ok(e.getParameter("function") == f, "getParameter(Function) works correctly");
  1990. });
  1991. test(renderMode + " parameters object works for Connection", function() {
  1992. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  1993. f = function() { alert("FOO!"); };
  1994. var c = jsPlumb.connect({
  1995. source:d1,
  1996. target:d2,
  1997. parameters:{
  1998. "string":"param1",
  1999. "int":4,
  2000. "function":f
  2001. }
  2002. });
  2003. ok(c.getParameter("string") === "param1", "getParameter(String) works correctly");
  2004. ok(c.getParameter("int") === 4, "getParameter(int) works correctly");
  2005. ok(c.getParameter("function") == f, "getParameter(Function) works correctly");
  2006. });
  2007. test(renderMode + " parameters set on Endpoints and Connections are all merged, and merged correctly at that.", function() {
  2008. var d1 = _addDiv("d1"),
  2009. d2 = _addDiv("d2"),
  2010. e = jsPlumb.addEndpoint(d1, {
  2011. isSource:true,
  2012. parameters:{
  2013. "string":"sourceEndpoint",
  2014. "int":0,
  2015. "function":function() { return "sourceEndpoint"; }
  2016. }
  2017. }),
  2018. e2 = jsPlumb.addEndpoint(d2, {
  2019. isTarget:true,
  2020. parameters:{
  2021. "int":1,
  2022. "function":function() { return "targetEndpoint"; }
  2023. }
  2024. }),
  2025. c = jsPlumb.connect({source:e, target:e2, parameters:{
  2026. "function":function() { return "connection"; }
  2027. }});
  2028. ok(c.getParameter("string") === "sourceEndpoint", "getParameter(String) works correctly");
  2029. ok(c.getParameter("int") === 1, "getParameter(int) works correctly");
  2030. ok(c.getParameter("function")() == "connection", "getParameter(Function) works correctly");
  2031. });
  2032. // anchor manager tests. a new and more comprehensive way of managing the paint, introduced in 1.3.5
  2033. test(renderMode + " anchorManager registers standard connection", function() {
  2034. var d1 = _addDiv("d1"), d2 = _addDiv("d2");
  2035. var c = jsPlumb.connect({source:d1, target:d2});
  2036. equals(jsPlumb.anchorManager.get("d1")["standard"].length, 1);
  2037. equals(jsPlumb.anchorManager.get("d1")["endpoints"].length, 1);
  2038. equals(jsPlumb.anchorManager.get("d2")["standard"].length, 1);
  2039. equals(jsPlumb.anchorManager.get("d2")["endpoints"].length, 1);
  2040. var c2 = jsPlumb.connect({source:d1, target:d2});
  2041. equals(jsPlumb.anchorManager.get("d1")["standard"].length, 2);
  2042. equals(jsPlumb.anchorManager.get("d2")["standard"].length, 2);
  2043. equals(jsPlumb.anchorManager.get("d1")["endpoints"].length, 2);
  2044. equals(jsPlumb.anchorManager.get("d2")["endpoints"].length, 2);
  2045. });
  2046. // anchor manager tests. a new and more comprehensive way of managing the paint, introduced in 1.3.5
  2047. test(renderMode + " anchorManager registers dynamic anchor connection, and removes it.", function() {
  2048. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  2049. var c = jsPlumb.connect({source:d3, target:d4, anchors:["AutoDefault", "AutoDefault"]});
  2050. equals(jsPlumb.anchorManager.get("d3")["standard"].length, 1);
  2051. var c2 = jsPlumb.connect({source:d3, target:d4});
  2052. equals(jsPlumb.anchorManager.get("d3")["standard"].length, 2);
  2053. equals(jsPlumb.anchorManager.get("d4")["standard"].length, 2);
  2054. equals(jsPlumb.anchorManager.get("d3")["endpoints"].length, 2);
  2055. jsPlumb.detach(c);
  2056. equals(jsPlumb.anchorManager.get("d3")["standard"].length, 1);
  2057. });
  2058. // anchor manager tests. a new and more comprehensive way of managing the paint, introduced in 1.3.5
  2059. test(renderMode + " anchorManager registers continuous anchor connection, and removes it.", function() {
  2060. var d3 = _addDiv("d3"), d4 = _addDiv("d4");
  2061. var c = jsPlumb.connect({source:d3, target:d4, anchors:["Continuous", "Continuous"]});
  2062. equals(jsPlumb.anchorManager.get("d3")["standard"].length, 0);
  2063. equals(jsPlumb.anchorManager.get("d3")["continuous"].length, 1);
  2064. equals(jsPlumb.anchorManager.get("d4")["standard"].length, 0);
  2065. equals(jsPlumb.anchorManager.get("d4")["continuous"].length, 1);
  2066. jsPlumb.detach(c);
  2067. equals(jsPlumb.anchorManager.get("d3")["continuous"].length, 0);
  2068. equals(jsPlumb.anchorManager.get("d4")["continuous"].length, 0);
  2069. jsPlumb.reset();
  2070. equals(jsPlumb.anchorManager.get("d4")["continuousAnchorEndpoints"].length, 0);
  2071. });
  2072. // ------------- utility functions - math stuff, mostly --------------------------
  2073. var tolerance = 0.00000005, withinTolerance = function(v1, v2, msg) {
  2074. if (Math.abs(v1 - v2) < tolerance) ok(true, msg + "; expected " + v1 + " and got it");
  2075. else {
  2076. ok(false, msg + "; expected " + v1 + " got " + v2);
  2077. }
  2078. };
  2079. test(renderMode + " jsPlumb.util.gradient, segment 1", function() {
  2080. var p1 = [2,2], p2 = [3,1], m = jsPlumb.util.gradient(p1,p2);
  2081. equals(m, -1, "gradient calculated correctly for simple case");
  2082. });
  2083. test(renderMode + " jsPlumb.util.normal, segment 1", function() {
  2084. var p1 = [2,2], p2 = [3,1], m = jsPlumb.util.normal(p1,p2);
  2085. equals(m, 1, "normal calculated correctly for simple case");
  2086. });
  2087. test(renderMode + " jsPlumb.util.gradient, segment 2", function() {
  2088. var p1 = [2,2], p2 = [3,3], m = jsPlumb.util.gradient(p1,p2);
  2089. equals(m, 1, "gradient calculated correctly for simple case");
  2090. });
  2091. test(renderMode + " jsPlumb.util.normal, segment 2", function() {
  2092. var p1 = [2,2], p2 = [3,3], m = jsPlumb.util.normal(p1,p2);
  2093. equals(m, -1, "normal calculated correctly for simple case");
  2094. });
  2095. test(renderMode + " jsPlumb.util.gradient, segment 3", function() {
  2096. var p1 = [2,2], p2 = [1,3], m = jsPlumb.util.gradient(p1,p2);
  2097. equals(m, -1, "gradient calculated correctly for simple case");
  2098. });
  2099. test(renderMode + " jsPlumb.util.normal, segment 3", function() {
  2100. var p1 = [2,2], p2 = [1,3], m = jsPlumb.util.normal(p1,p2);
  2101. equals(m, 1, "normal calculated correctly for simple case");
  2102. });
  2103. test(renderMode + " jsPlumb.util.gradient, segment 4", function() {
  2104. var p1 = [2,2], p2 = [1,1], m = jsPlumb.util.gradient(p1,p2);
  2105. equals(m, 1, "gradient calculated correctly for simple case");
  2106. });
  2107. test(renderMode + " jsPlumb.util.normal, segment 4", function() {
  2108. var p1 = [2,2], p2 = [1,1], m = jsPlumb.util.normal(p1,p2);
  2109. equals(m, -1, "normal calculated correctly for simple case");
  2110. });
  2111. test(renderMode + "jsPlumb.util.pointOnLine, segment 1", function() {
  2112. var p1 = {x:2,y:2}, p2={x:3, y:1},
  2113. target = jsPlumb.util.pointOnLine(p1, p2, Math.sqrt(2));
  2114. withinTolerance(p2.x, target.x, "x is calculated correctly");
  2115. withinTolerance(p2.y, target.y, "y is calculated correctly");
  2116. });
  2117. test(renderMode + "jsPlumb.util.pointOnLine, segment 2", function() {
  2118. var p1 = {x:2,y:2}, p2={x:3, y:3},
  2119. target = jsPlumb.util.pointOnLine(p1, p2, Math.sqrt(2));
  2120. withinTolerance(p2.x, target.x, "x is calculated correctly");
  2121. withinTolerance(p2.y, target.y, "y is calculated correctly");
  2122. });
  2123. test(renderMode + "jsPlumb.util.pointOnLine, segment 3", function() {
  2124. var p1 = {x:2,y:2}, p2={x:1, y:3},
  2125. target = jsPlumb.util.pointOnLine(p1, p2, Math.sqrt(2));
  2126. withinTolerance(p2.x, target.x, "x is calculated correctly");
  2127. withinTolerance(p2.y, target.y, "y is calculated correctly");
  2128. });
  2129. test(renderMode + "jsPlumb.util.pointOnLine, segment 4", function() {
  2130. var p1 = {x:2,y:2}, p2={x:1, y:1},
  2131. target = jsPlumb.util.pointOnLine(p1, p2, Math.sqrt(2));
  2132. withinTolerance(p2.x, target.x, "x is calculated correctly");
  2133. withinTolerance(p2.y, target.y, "y is calculated correctly");
  2134. });
  2135. test(renderMode + "jsPlumb.util.perpendicularLineTo, segment 1", function() {
  2136. var p1 = {x:2, y:2}, p2={x:3, y:1}, m = jsPlumb.util.gradient(p1, p2),
  2137. l = jsPlumb.util.perpendicularLineTo(p1, p2, 2 * Math.sqrt(2));
  2138. withinTolerance(4, l[0].x, "point 1 x is correct");
  2139. withinTolerance(2, l[0].y, "point 1 y is correct");
  2140. withinTolerance(2, l[1].x, "point 2 x is correct");
  2141. withinTolerance(0, l[1].y, "point 2 y is correct");
  2142. });
  2143. test(renderMode + "jsPlumb.util.perpendicularLineTo, segment 2", function() {
  2144. var p1 = {x:2, y:2}, p2={x:3, y:3}, m = jsPlumb.util.gradient(p1, p2),
  2145. l = jsPlumb.util.perpendicularLineTo(p1, p2, 2 * Math.sqrt(2));
  2146. withinTolerance(4, l[0].x, "point 1 x is correct");
  2147. withinTolerance(2, l[0].y, "point 1 y is correct");
  2148. withinTolerance(2, l[1].x, "point 2 x is correct");
  2149. withinTolerance(4, l[1].y, "point 2 y is correct");
  2150. });
  2151. test(renderMode + "jsPlumb.util.perpendicularLineTo, segment 3", function() {
  2152. var p1 = {x:2, y:2}, p2={x:1, y:3}, m = jsPlumb.util.gradient(p1, p2),
  2153. l = jsPlumb.util.perpendicularLineTo(p1, p2, 2 * Math.sqrt(2));
  2154. withinTolerance(2, l[0].x, "point 1 x is correct");
  2155. withinTolerance(4, l[0].y, "point 1 y is correct");
  2156. withinTolerance(0, l[1].x, "point 2 x is correct");
  2157. withinTolerance(2, l[1].y, "point 2 y is correct");
  2158. });
  2159. test(renderMode + "jsPlumb.util.perpendicularLineTo, segment 4", function() {
  2160. var p1 = {x:2, y:2}, p2={x:1, y:1}, m = jsPlumb.util.gradient(p1, p2),
  2161. l = jsPlumb.util.perpendicularLineTo(p1, p2, 2 * Math.sqrt(2));
  2162. withinTolerance(2, l[0].x, "point 1 x is correct");
  2163. withinTolerance(0, l[0].y, "point 1 y is correct");
  2164. withinTolerance(0, l[1].x, "point 2 x is correct");
  2165. withinTolerance(2, l[1].y, "point 2 y is correct");
  2166. });
  2167. test(renderMode + "jsPlumb.util.setImage on Endpoint", function() {
  2168. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  2169. e = {
  2170. endpoint:[ "Image", { src:"../../img/endpointTest1.png" } ]
  2171. },
  2172. ep = jsPlumb.addEndpoint(d1, e);
  2173. ep.setImage("../../img/littledot.png");
  2174. });
  2175. test(renderMode + "jsPlumb.util.setImage on Endpoint, with supplied onload", function() {
  2176. var d1 = _addDiv("d1"), d2 = _addDiv("d2"),
  2177. e = {
  2178. endpoint:[ "Image", {
  2179. src:"../../img/endpointTest1.png",
  2180. onload:function(imgEp) {
  2181. equals("../../img/endpointTest1.png", imgEp.img.src);
  2182. }
  2183. } ]
  2184. },
  2185. ep = jsPlumb.addEndpoint(d1, e);
  2186. ep.setImage("../../img/littledot.png", function(imgEp) {
  2187. equals("../../img/littledot.png", imgEp.img.src);
  2188. });
  2189. });
  2190. test(renderMode + "attach endpoint to table when desired element was td", function() {
  2191. var table = document.createElement("table"),
  2192. tr = document.createElement("tr"),
  2193. td = document.createElement("td");
  2194. table.appendChild(tr); tr.appendChild(td);
  2195. document.body.appendChild(table);
  2196. var ep = jsPlumb.addEndpoint(td);
  2197. equals(ep.canvas.parentNode.tagName.toLowerCase(), "table");
  2198. });
  2199. /**
  2200. * leave this test at the bottom!
  2201. */
  2202. test(renderMode + ': unload test', function() {
  2203. jsPlumb.unload();
  2204. var contextNode = $("._jsPlumb_context");
  2205. ok(contextNode.length == 0, 'context node unloaded');
  2206. });
  2207. };