jsPlumb-1.3.5-tests.js 134 KB

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