jsPlumb-1.2.1-RC1.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /*
  2. * jsPlumb 1.2.1-RC1
  3. *
  4. * Provides a way to visually connect elements on an HTML page.
  5. *
  6. * Several enhancements are planned for 1.2.1:
  7. *
  8. * - speed enhancements for dragging/animation (fewer element positioning lookups etc)
  9. *
  10. * - the ability to label connectors. each connector type will have to tell us the right place
  11. * for a label to go.
  12. *
  13. * - the ability to interact with connectors/endpoints using the mouse
  14. *
  15. * - the log function should hook up to the console of whichever browser it is in. or library.
  16. *
  17. * - reinstate the context node to put all our canvases in
  18. *
  19. * - possibly support multiple jsplumb instances on the same page. this would not be too hard;
  20. * it would just need to stop being a singleton. 07/05/10: this is done now.
  21. *
  22. * - support for devices with touch events.
  23. *
  24. * http://morrisonpitt.com/jsPlumb/demo.html
  25. * http://code.google.com/p/jsPlumb
  26. *
  27. */
  28. (function() {
  29. /*
  30. Class: jsPlumb
  31. This is the main entry point to jsPlumb. There are a bunch of static methods you can
  32. use to connect/disconnect etc. Some methods are also added to jQuery's "$.fn" object itself, but
  33. in general jsPlumb is moving away from that model, preferring instead the static class
  34. approach. One of the reasons for this is that with no methods added to the jQuery $.fn object,
  35. it will be easier to provide support for other libraries such as MooTools.
  36. */
  37. var jsPlumbInstance = function() {
  38. var _currentInstance = this;
  39. var ie = (/MSIE/.test(navigator.userAgent) && !window.opera);
  40. var log = null;
  41. var repaintFunction = function() { jsPlumb.repaintEverything(); };
  42. var automaticRepaint = true;
  43. function repaintEverything() {
  44. if (automaticRepaint)
  45. repaintFunction();
  46. };
  47. var resizeTimer = null;
  48. /**
  49. * map of element id -> endpoint lists. an element can have an arbitrary number of endpoints on it,
  50. * and not all of them have to be connected to anything.
  51. */
  52. var endpointsByElement = {};
  53. var connectionsByScope = {};
  54. var offsets = [];
  55. var floatingConnections = {};
  56. var draggableStates = {};
  57. var _draggableByDefault = true;
  58. var sizes = [];
  59. var DEFAULT_SCOPE = 'DEFAULT';
  60. var DEFAULT_NEW_CANVAS_SIZE = 1200; // only used for IE; a canvas needs a size before the init call to excanvas (for some reason. no idea why.)
  61. var traced = {};
  62. var _trace = function(category, event) {
  63. var e = traced[category];
  64. if (!e) {
  65. e = {};
  66. traced[category] = e;
  67. }
  68. event = event || 'DEFAULT';
  69. var ee = e[event];
  70. if (!ee) {
  71. ee = 0;
  72. e[event] = ee;
  73. }
  74. e[event]++;
  75. };
  76. var _clearAllTraces = function() {
  77. delete traced;
  78. traced = {};
  79. };
  80. var _clearTrace = function(category, event) {
  81. var c = traced[category];
  82. if (!c) return;
  83. if (event) {
  84. c[event] = 0;
  85. }
  86. else c['DEFAULT'] = 0;
  87. };
  88. var _getTrace = function(category) {
  89. return traced[category] || {'DEFAULT' : 0 };
  90. };
  91. var _findIndex = function( a, v, b, s ) {
  92. var _eq = function(o1, o2) {
  93. if (o1 === o2) return true;
  94. else if (typeof o1 == 'object' && typeof o2 == 'object') {
  95. var same = true;
  96. for(var propertyName in o1) {
  97. if(!_eq(o1[propertyName], o2[propertyName])) {
  98. same = false;
  99. break;
  100. }
  101. }
  102. for(var propertyName in o2) {
  103. if(!_eq(o2[propertyName], o1[propertyName])) {
  104. same = false;
  105. break;
  106. }
  107. }
  108. return same;
  109. }
  110. };
  111. for( var i = +b || 0, l = a.length; i < l; i++ ) {
  112. if( _eq(a[i], v)) return i;
  113. }
  114. return -1;
  115. };
  116. /**
  117. * helper method to add an item to a list, creating the list if it does not yet exist.
  118. */
  119. var _addToList = function(map, key, value) {
  120. var l = map[key];
  121. if (l == null) {
  122. l = [];
  123. map[key] = l;
  124. }
  125. l.push(value);
  126. return l;
  127. };
  128. /**
  129. * Handles the dragging of an element.
  130. * @param element jQuery element
  131. * @param ui UI object from current library's event system
  132. */
  133. var _draw = function(element, ui) {
  134. var id = _getAttribute(element, "id");
  135. var endpoints = endpointsByElement[id];
  136. if (endpoints) {
  137. var timestamp = '' + (new Date().getTime());
  138. _trace('draw');
  139. _updateOffset(id, ui);
  140. var myOffset = offsets[id];
  141. var myWH = sizes[id];
  142. // loop through endpoints for this element
  143. for (var i = 0; i < endpoints.length; i++) {
  144. var e = endpoints[i];
  145. var anchorLoc = e.anchor.compute([myOffset.left, myOffset.top], myWH, e);
  146. e.paint(anchorLoc);
  147. var l = e.connections;
  148. for (var j = 0; j < l.length; j++)
  149. l[j].paint(id, ui, false, timestamp); // ...and paint them.
  150. }
  151. }
  152. };
  153. /**
  154. * helper function: the second argument is a function taking two args - the first is an id
  155. * ui element from the current library (or list of them), and the second is the function
  156. * to run.
  157. */
  158. var _elementProxy = function(element, fn) {
  159. var retVal = null;
  160. // TODO this might be jquery specific. in fact it probably is.
  161. if (typeof element == 'object' && element.length) {
  162. retVal = [];
  163. for (var i = 0; i < element.length; i++) {
  164. var el = _getElementObject(element[i]);
  165. var id = _getAttribute(el, "id");
  166. retVal.push(fn(el, id)); // append return values to what we will return
  167. }
  168. }
  169. else {
  170. var el = _getElementObject(element);
  171. var id = _getAttribute(el, "id");
  172. retVal = fn(el, id);
  173. }
  174. return retVal;
  175. };
  176. var _log = function(msg) {
  177. //if (console) console.log(msg);
  178. };
  179. var _logFnCall = function(fn, args) {
  180. /*if (console) {
  181. var c = args.callee.caller.toString();
  182. var i = c.indexOf("{");
  183. var msg = fn + ' : ' + c.substring(0, i);
  184. console.log(msg);
  185. }*/
  186. };
  187. var cached = {};
  188. var __getElementObject = function(el) {
  189. if (typeof(el)=='string') {
  190. var e = cached[el];
  191. if (!e) {
  192. e = jsPlumb.CurrentLibrary.getElementObject(el);
  193. cached[el] = e;
  194. }
  195. return e;
  196. }
  197. else {
  198. return jsPlumb.CurrentLibrary.getElementObject(el);
  199. }
  200. };
  201. /**
  202. * gets the named attribute from the given element (id or element object)
  203. */
  204. var _getAttribute = function(el, attName) {
  205. var ele = __getElementObject(el);
  206. return jsPlumb.CurrentLibrary.getAttribute(ele, attName);
  207. };
  208. var _setAttribute = function(el, attName, attValue) {
  209. var ele = __getElementObject(el);
  210. jsPlumb.CurrentLibrary.setAttribute(ele, attName, attValue);
  211. };
  212. var _addClass = function(el, clazz) {
  213. var ele = __getElementObject(el);
  214. jsPlumb.CurrentLibrary.addClass(ele, clazz);
  215. };
  216. var _getElementObject = function(elId) {
  217. return __getElementObject(elId);
  218. };
  219. var _getOffset = function(el) {
  220. var ele = __getElementObject(el);
  221. return jsPlumb.CurrentLibrary.getOffset(ele);
  222. };
  223. var _getSize = function(el) {
  224. var ele = __getElementObject(el);
  225. return jsPlumb.CurrentLibrary.getSize(ele);
  226. };
  227. var _appendElement = function(canvas, parent) {
  228. if (!parent) document.body.appendChild(canvas);
  229. else jsPlumb.CurrentLibrary.appendElement(canvas, parent);
  230. };
  231. /**
  232. * gets an id for the given element, creating and setting one if necessary.
  233. */
  234. var _getId = function(element) {
  235. var ele = _getElementObject(element);
  236. var id = _getAttribute(ele, "id");
  237. if (!id) {
  238. id = "_jsPlumb_" + new String((new Date()).getTime());
  239. _setAttribute(ele, "id", id);
  240. }
  241. return id;
  242. };
  243. /**
  244. * inits a draggable if it's not already initialised.
  245. *
  246. * TODO: we need to hook in each library to this method. they need to be given the opportunity to
  247. * wrap/insert lifecycle functions, because each library does different things. for instance, jQuery
  248. * supports the notion of 'revert', which will clean up a dragged endpoint; MooTools does not. jQuery
  249. * also supports 'opacity' and MooTools does not; jQuery supports z-index of the draggable; MooTools
  250. * does not. i could go on. the point is...oh. initDraggable can do this. ok.
  251. */
  252. var _initDraggableIfNecessary = function(element, isDraggable, dragOptions) {
  253. // dragging
  254. var draggable = isDraggable == null ? _draggableByDefault : isDraggable;
  255. if (draggable) {
  256. if (jsPlumb.CurrentLibrary.isDragSupported(element)) {
  257. var options = dragOptions || _currentInstance.Defaults.DragOptions || jsPlumb.Defaults.DragOptions;
  258. options = jsPlumb.extend({}, options); // make a copy.
  259. var dragEvent = jsPlumb.CurrentLibrary.dragEvents['drag'];
  260. options[dragEvent] = _wrap(options[dragEvent], function() {
  261. var ui = jsPlumb.CurrentLibrary.getUIPosition(arguments);
  262. _draw(element, ui);
  263. _addClass(element, "jsPlumb_dragged");
  264. });
  265. var draggable = draggableStates[_getId(element)];
  266. options.disabled = draggable == null ? false : !draggable;
  267. jsPlumb.CurrentLibrary.initDraggable(element, options);
  268. }
  269. }
  270. };
  271. /**
  272. * helper to create a canvas.
  273. * @param clazz optional class name for the canvas.
  274. */
  275. var _newCanvas = function(clazz, parent) {
  276. var canvas = document.createElement("canvas");
  277. _appendElement(canvas, parent);
  278. canvas.style.position="absolute";
  279. if (clazz) { canvas.className=clazz; }
  280. _getId(canvas); // set an id.
  281. if (ie) {
  282. // for IE we have to set a big canvas size. actually you can override this, too, if 1200 pixels
  283. // is not big enough for the biggest connector/endpoint canvas you have at startup.
  284. jsPlumb.sizeCanvas(canvas, 0, 0, DEFAULT_NEW_CANVAS_SIZE, DEFAULT_NEW_CANVAS_SIZE);
  285. canvas = G_vmlCanvasManager.initElement(canvas);
  286. }
  287. return canvas;
  288. };
  289. /**
  290. * performs the given function operation on all the connections found for the given element
  291. * id; this means we find all the endpoints for the given element, and then for each endpoint
  292. * find the connectors connected to it. then we pass each connection in to the given
  293. * function.
  294. */
  295. var _operation = function(elId, func) {
  296. var endpoints = endpointsByElement[elId];
  297. if (endpoints && endpoints.length) {
  298. for (var i = 0; i < endpoints.length; i++) {
  299. for (var j = 0; j < endpoints[i].connections.length; j++) {
  300. var retVal = func(endpoints[i].connections[j]);
  301. // if the function passed in returns true, we exit.
  302. // most functions return false.
  303. if (retVal) return;
  304. }
  305. }
  306. }
  307. };
  308. /**
  309. * perform an operation on all elements.
  310. */
  311. var _operationOnAll = function(func) {
  312. for (var elId in endpointsByElement) {
  313. _operation(elId, func);
  314. }
  315. };
  316. /**
  317. * helper to remove an element from the DOM.
  318. */
  319. var _removeElement = function(element, parent) {
  320. if (element != null) {
  321. if (!parent) {
  322. try { document.body.removeChild(element); }
  323. catch (e) { }
  324. } else {
  325. jsPlumb.CurrentLibrary.removeElement(element, parent);
  326. }
  327. }
  328. };
  329. /**
  330. * helper to remove a list of elements from the DOM.
  331. */
  332. var _removeElements = function(elements, parent) {
  333. for (var i in elements)
  334. _removeElement(elements[i], parent);
  335. };
  336. /**
  337. * helper method to remove an item from a list.
  338. */
  339. var _removeFromList = function(map, key, value) {
  340. var l = map[key];
  341. if (l != null) {
  342. var i = _findIndex(l, value);
  343. if (i >= 0) {
  344. delete( l[i] );
  345. l.splice( i, 1 );
  346. return true;
  347. }
  348. }
  349. return false;
  350. };
  351. /**
  352. * Sets whether or not the given element(s) should be draggable, regardless of what a particular
  353. * plumb command may request.
  354. *
  355. * @param element May be a string, a jQuery elements, or a list of strings/jquery elements.
  356. * @param draggable Whether or not the given element(s) should be draggable.
  357. */
  358. var _setDraggable = function(element, draggable) {
  359. var _helper = function(el, id) {
  360. draggableStates[id] = draggable;
  361. if (jsPlumb.CurrentLibrary.isDragSupported(el)) {
  362. jsPlumb.CurrentLibrary.setDraggable(el, draggable);
  363. }
  364. };
  365. return _elementProxy(element, _helper);
  366. };
  367. /**
  368. * private method to do the business of hiding/showing.
  369. * @param el either Id of the element in question or a jquery object for the element.
  370. * @param state String specifying a value for the css 'display' property ('block' or 'none').
  371. */
  372. var _setVisible = function(el, state) {
  373. var elId = _getAttribute(el, "id");
  374. var f = function(jpc) {
  375. jpc.canvas.style.display = state;
  376. };
  377. _operation(elId, f);
  378. };
  379. /**
  380. * toggles the draggable state of the given element(s).
  381. * @param el either an id, or a jquery object, or a list of ids/jquery objects.
  382. */
  383. var _toggleDraggable = function(el) {
  384. var fn = function(el, elId) {
  385. var state = draggableStates[elId] == null ? _draggableByDefault : draggableStates[elId];
  386. state = !state;
  387. draggableStates[elId] = state;
  388. jsPlumb.CurrentLibrary.setDraggable(el, state);
  389. return state;
  390. };
  391. return _elementProxy(el, fn);
  392. };
  393. /**
  394. * private method to do the business of toggling hiding/showing.
  395. * @param elId Id of the element in question
  396. */
  397. var _toggleVisible = function(elId) {
  398. var f = function(jpc) {
  399. var state = ('none' == jpc.canvas.style.display);
  400. jpc.canvas.style.display = state ? "block" : "none";
  401. };
  402. _operation(elId, f);
  403. //todo this should call _elementProxy, and pass in the _operation(elId, f) call as a function. cos _toggleDraggable does that.
  404. };
  405. /**
  406. * updates the offset and size for a given element, and stores the values.
  407. * if 'offset' is not null we use that (it would have been passed in from a drag call) because it's faster; but if it is null,
  408. * or if 'recalc' is true in order to force a recalculation, we use the offset, outerWidth and outerHeight methods to get
  409. * the current values.
  410. */
  411. var _updateOffset = function(elId, offset, recalc) {
  412. if (recalc || offset == null) { // if forced repaint or no offset available, we recalculate.
  413. // get the current size and offset, and store them
  414. var s = _getElementObject(elId);
  415. sizes[elId] = _getSize(s);
  416. offsets[elId] = _getOffset(s);
  417. } else {
  418. offsets[elId] = offset;
  419. }
  420. };
  421. /**
  422. * wraps one function with another, creating a placeholder for the wrapped function
  423. * if it was null. this is used to wrap the various drag/drop event functions - to allow
  424. * jsPlumb to be notified of important lifecycle events without imposing itself on the user's
  425. * drap/drop functionality.
  426. * TODO: determine whether or not we should support an error handler concept, if one of the functions fails.
  427. */
  428. var _wrap = function(wrappedFunction, newFunction) {
  429. wrappedFunction = wrappedFunction || function() { };
  430. newFunction = newFunction || function() { };
  431. return function() {
  432. try { newFunction.apply(this, arguments); }
  433. catch (e) {
  434. _log('jsPlumb function failed : ' + e);
  435. }
  436. try { wrappedFunction.apply(this, arguments); }
  437. catch (e) {
  438. _log('wrapped function failed : ' + e);
  439. }
  440. };
  441. };
  442. /*
  443. Class: Anchor
  444. Models a position relative to the origin of some element that an Endpoint can be located.
  445. */
  446. /*
  447. Function: Anchor
  448. Constructor for the Anchor class
  449. Parameters:
  450. params - Anchor parameters. This should contain three values, and may optionally have an 'offsets' argument:
  451. - x : the x location of the anchor as a fraction of the total width.
  452. - y : the y location of the anchor as a fraction of the total height.
  453. - orientation : an [x,y] array indicating the general direction a connection from the anchor should go in. for more info on this, see the documentation, or the docs in jquery-jsPlumb-defaults-XXX.js for the default Anchors.
  454. - offsets : an [x,y] array of fixed offsets that should be applied after the x,y position has been figured out. may be null.
  455. */
  456. var Anchor = function(params) {
  457. var self = this;
  458. this.x = params.x || 0; this.y = params.y || 0;
  459. var orientation = params.orientation || [0,0];
  460. var lastTimestamp = null;
  461. var lastReturnValue = null;
  462. this.offsets = params.offsets || [0,0];
  463. //TODO: fix this properly. since Anchors are often static, this timestamping business
  464. // does not work at all well. the timestamp should be inside the Endpoint, because they
  465. // are _never_ static. the method that Connection uses to find an anchor location should
  466. // be done through the Endpoint class, which can then deal with the timestamp.
  467. // and, in fact, we should find out whether or not we even get a speed enhancement from doing
  468. // this.
  469. this.compute = function(xy, wh, element) {
  470. _trace('anchor compute');
  471. lastReturnValue = [ xy[0] + (self.x * wh[0]) + self.offsets[0], xy[1] + (self.y * wh[1]) + self.offsets[1]];
  472. var container = element? element.container : null;
  473. var containerAdjustment = {left:0, top:0 };
  474. if (container != null) {
  475. var eo = _getElementObject(container);
  476. var o = _getOffset(eo);
  477. var sl = jsPlumb.CurrentLibrary.getScrollLeft(eo);
  478. var st = jsPlumb.CurrentLibrary.getScrollTop(eo);
  479. containerAdjustment.left = o.left - sl;
  480. containerAdjustment.top = o.top - st;
  481. lastReturnValue[0] = lastReturnValue[0] - containerAdjustment.left;
  482. lastReturnValue[1] = lastReturnValue[1] - containerAdjustment.top;
  483. }
  484. return lastReturnValue;
  485. };
  486. this.getOrientation = function() { return orientation; };
  487. this.equals = function(anchor) {
  488. if (!anchor) return false;
  489. var ao = anchor.getOrientation();
  490. var o = this.getOrientation();
  491. return this.x == anchor.x && this.y == anchor.y && this.offsets[0] == anchor.offsets[0] && this.offsets[1] == anchor.offsets[1] && o[0] == ao[0] && o[1] == ao[1];
  492. };
  493. };
  494. /**
  495. * An Anchor that floats. its orientation is computed dynamically from its position relative to the anchor it is floating relative to.
  496. *
  497. */
  498. var FloatingAnchor = function(params) {
  499. // this is the anchor that this floating anchor is referenced to for purposes of calculating the orientation.
  500. var ref = params.reference;
  501. // the canvas this refers to.
  502. var refCanvas = params.referenceCanvas;
  503. var size = _getSize(_getElementObject(refCanvas));
  504. // these are used to store the current relative position of our anchor wrt the reference anchor. they only indicate
  505. // direction, so have a value of 1 or -1 (or, very rarely, 0). these values are written by the compute method, and read
  506. // by the getOrientation method.
  507. var xDir = 0, yDir = 0;
  508. // temporary member used to store an orientation when the floating anchor is hovering over another anchor.
  509. var orientation = null;
  510. this.compute = function(xy, wh, txy, twh) {
  511. // set these for the getOrientation method to use.
  512. xDir = 0;//xy[0] < txy[0] ? -1 : xy[0] == txy[0] ? 0 : 1;
  513. yDir = 0;//xy[1] < txy[1] ? -1 : xy[1] == txy[1] ? 0 : 1;
  514. return [xy[0] + (size[0] / 2), xy[1] + (size[1] / 2) ]; // return origin of the element. we may wish to improve this so that any object can be the drag proxy.
  515. };
  516. this.getOrientation = function() {
  517. if (orientation) return orientation;
  518. else {
  519. var o = ref.getOrientation();
  520. // here we take into account the orientation of the other anchor: if it declares zero for some direction, we declare zero too.
  521. // this might not be the most awesome. perhaps we can come up with a better way. it's just so that the line we draw looks
  522. // like it makes sense. maybe this wont make sense.
  523. return [Math.abs(o[0]) * xDir * -1, Math.abs(o[1]) * yDir * -1];
  524. }
  525. };
  526. /**
  527. * notification the endpoint associated with this anchor is hovering over another anchor;
  528. * we want to assume that anchor's orientation for the duration of the hover.
  529. */
  530. this.over = function(anchor) {
  531. orientation = anchor.getOrientation();
  532. };
  533. /**
  534. * notification the endpoint associated with this anchor is no longer hovering
  535. * over another anchor; we should resume calculating orientation as we normally do.
  536. */
  537. this.out = function() {
  538. orientation = null;
  539. };
  540. };
  541. // ************** connection
  542. // ****************************************
  543. /**
  544. * allowed params:
  545. * source: source element (string or a jQuery element) (required)
  546. * target: target element (string or a jQuery element) (required)
  547. *
  548. * anchors: optional array of anchor placements. defaults to BottomCenter for source
  549. * and TopCenter for target.
  550. */
  551. var Connection = function(params) {
  552. // ************** get the source and target and register the connection. *******************
  553. var self = this;
  554. var id = new String('_jsplumb_c_' + (new Date()).getTime());
  555. this.getId = function() { return id; };
  556. this.container = params.container || _currentInstance.Defaults.Container; // may be null; we will append to the body if so.
  557. // get source and target as jQuery objects
  558. this.source = _getElementObject(params.source);
  559. this.target = _getElementObject(params.target);
  560. this.sourceId = _getAttribute(this.source, "id");
  561. this.targetId = _getAttribute(this.target, "id");
  562. this.endpointsOnTop = params.endpointsOnTop != null ? params.endpointsOnTop : true;
  563. this.scope = params.scope; // scope may have been passed in to the connect call.
  564. // if it wasn't, we will pull it from the source endpoint,
  565. // after having initialised the endpoints.
  566. // init endpoints
  567. this.endpoints = [];
  568. this.endpointStyles = [];
  569. var prepareEndpoint = function(existing, index, params, element) {
  570. if (existing) {
  571. self.endpoints[index] = existing;
  572. existing.addConnection(self);
  573. }
  574. else {
  575. if(!params.endpoints) params.endpoints = [null,null];
  576. var ep = params.endpoints[index] || params.endpoint || _currentInstance.Defaults.Endpoints[index] || jsPlumb.Defaults.Endpoints[index] || _currentInstance.Defaults.Endpoint || jsPlumb.Defaults.Endpoint || new jsPlumb.Endpoints.Dot();
  577. if (!params.endpointStyles) params.endpointStyles = [null,null];
  578. var es = params.endpointStyles[index] || params.endpointStyle || _currentInstance.Defaults.EndpointStyles[index] || jsPlumb.Defaults.EndpointStyles[index] || _currentInstance.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle;
  579. var a = params.anchors ? params.anchors[index] : _currentInstance.Defaults.Anchors[index] || jsPlumb.Defaults.Anchors[index] || _currentInstance.Defaults.Anchor || jsPlumb.Defaults.Anchor || jsPlumb.Anchors.BottomCenter;
  580. var e = new Endpoint({style:es, endpoint:ep, connections:[self], anchor:a, source:element, container:self.container });
  581. self.endpoints[index] = e;
  582. return e;
  583. }
  584. };
  585. var eS = prepareEndpoint(params.sourceEndpoint, 0, params, self.source);
  586. if (eS) _addToList(endpointsByElement, this.sourceId, eS);
  587. var eT = prepareEndpoint(params.targetEndpoint, 1, params, self.target);
  588. if (eT) _addToList(endpointsByElement, this.targetId, eT);
  589. // if scope not set, set it to be the scope for the source endpoint.
  590. if (!this.scope) this.scope = this.endpoints[0].scope;
  591. this.connector = this.endpoints[0].connector || this.endpoints[1].connector || params.connector || _currentInstance.Defaults.Connector || jsPlumb.Defaults.Connector || new jsPlumb.Connectors.Bezier();
  592. this.paintStyle = this.endpoints[0].connectorStyle || this.endpoints[1].connectorStyle || params.paintStyle || _currentInstance.Defaults.PaintStyle || jsPlumb.Defaults.PaintStyle;
  593. _updateOffset(this.sourceId);
  594. _updateOffset(this.targetId);
  595. // paint the endpoints
  596. var myOffset = offsets[this.sourceId], myWH = sizes[this.sourceId];
  597. var anchorLoc = this.endpoints[0].anchor.compute([myOffset.left, myOffset.top], myWH, this.endpoints[0]);
  598. this.endpoints[0].paint(anchorLoc);
  599. myOffset = offsets[this.targetId]; myWH = sizes[this.targetId];
  600. anchorLoc = this.endpoints[1].anchor.compute([myOffset.left, myOffset.top], myWH, this.endpoints[1]);
  601. this.endpoints[1].paint(anchorLoc);
  602. // *************** create canvas on which the connection will be drawn ************
  603. var canvas = _newCanvas(jsPlumb.connectorClass, self.container);
  604. this.canvas = canvas;
  605. /**
  606. * paints the connection.
  607. * @param elId Id of the element that is in motion
  608. * @param ui current library's event system ui object (present if we came from a drag to get here)
  609. * @param recalc whether or not to recalculate element sizes. this is true if a repaint caused this to be painted.
  610. */
  611. this.paint = function(elId, ui, recalc, timestamp) {
  612. if (log) log.debug("Painting Connection; element in motion is " + elId + "; ui is [" + ui + "]; recalc is [" + recalc + "]");
  613. var fai = self.floatingAnchorIndex;
  614. // if the moving object is not the source we must transpose the two references.
  615. var swap = !(elId == this.sourceId);
  616. var tId = swap ? this.sourceId : this.targetId, sId = swap ? this.targetId : this.sourceId;
  617. var tIdx = swap ? 0 : 1, sIdx = swap ? 1 : 0;
  618. var el = swap ? this.target : this.source;
  619. if (this.canvas.getContext) {
  620. //if (recalc) {
  621. _updateOffset(elId, ui, recalc);
  622. _updateOffset(tId); // update the target if this is a forced repaint. otherwise, only the source has been moved.
  623. //}
  624. var myOffset = offsets[elId];
  625. var otherOffset = offsets[tId];
  626. var myWH = sizes[elId];
  627. var otherWH = sizes[tId];
  628. var ctx = canvas.getContext('2d');
  629. //TODO: why are these calculated again? they were calculated in the _draw function.
  630. var sAnchorP = this.endpoints[sIdx].anchor.compute([myOffset.left, myOffset.top], myWH, this.endpoints[sIdx]);
  631. var sAnchorO = this.endpoints[sIdx].anchor.getOrientation();
  632. var tAnchorP = this.endpoints[tIdx].anchor.compute([otherOffset.left, otherOffset.top], otherWH, this.endpoints[tIdx]);
  633. var tAnchorO = this.endpoints[tIdx].anchor.getOrientation();
  634. var dim = this.connector.compute(sAnchorP, tAnchorP, this.endpoints[sIdx].anchor, this.endpoints[tIdx].anchor, this.paintStyle.lineWidth);
  635. jsPlumb.sizeCanvas(canvas, dim[0], dim[1], dim[2], dim[3]);
  636. jsPlumb.extend(ctx, this.paintStyle);
  637. if (this.paintStyle.gradient && !ie) {
  638. var g = swap ? ctx.createLinearGradient(dim[4], dim[5], dim[6], dim[7]) : ctx.createLinearGradient(dim[6], dim[7], dim[4], dim[5]);
  639. for (var i = 0; i < this.paintStyle.gradient.stops.length; i++)
  640. g.addColorStop(this.paintStyle.gradient.stops[i][0],this.paintStyle.gradient.stops[i][1]);
  641. ctx.strokeStyle = g;
  642. }
  643. this.connector.paint(dim, ctx);
  644. }
  645. };
  646. //TODO: should this take a timestamp? probably. it reduces the amount of time
  647. // spent figuring out anchor locations.
  648. this.repaint = function() {
  649. this.paint(this.sourceId, null, true);
  650. };
  651. _initDraggableIfNecessary(self.source, params.draggable, params.dragOptions);
  652. _initDraggableIfNecessary(self.target, params.draggable, params.dragOptions);
  653. // resizing (using the jquery.ba-resize plugin). todo: decide whether to include or not.
  654. if (this.source.resize) {
  655. this.source.resize(function(e) {
  656. jsPlumb.repaint(self.sourceId);
  657. });
  658. }
  659. };
  660. /*
  661. Class: Endpoint
  662. Models an endpoint. Can have one to N connections emanating from it (although how to handle that in the UI is
  663. a very good question). also has a Canvas and paint style.
  664. */
  665. /*
  666. Function: Endpoint
  667. This is the Endpoint class constructor.
  668. Parameters:
  669. anchor - anchor for the endpoint, of type jsPlumb.Anchor. may be null.
  670. endpoint - endpoint object, of type jsPlumb.Endpoint. may be null.
  671. style - endpoint style, a js object. may be null.
  672. source - element the endpoint is attached to, of type jquery object. Required.
  673. canvas - canvas element to use. may be, and most often is, null.
  674. connections - optional list of connections to configure the endpoint with.
  675. isSource - boolean. indicates the endpoint can act as a source of new connections. optional.
  676. dragOptions - if isSource is set to true, you can supply arguments for the jquery draggable method. optional.
  677. connectionStyle - if isSource is set to true, this is the paint style for connections from this endpoint. optional.
  678. connector - optional connector type to use.
  679. isTarget - boolean. indicates the endpoint can act as a target of new connections. optional.
  680. dropOptions - if isTarget is set to true, you can supply arguments for the jquery droppable method. optional.
  681. reattach - optional boolean that determines whether or not the connections reattach after they
  682. have been dragged off an endpoint and left floating. defaults to false: connections
  683. dropped in this way will just be deleted.
  684. */
  685. var Endpoint = function(params) {
  686. params = params || {};
  687. // make a copy. then we can use the wrapper function.
  688. jsPlumb.extend({}, params);
  689. var self = this;
  690. var id = new String('_jsplumb_e_' + (new Date()).getTime());
  691. this.getId = function() { return id; };
  692. self.anchor = params.anchor || jsPlumb.Anchors.TopCenter;
  693. var _endpoint = params.endpoint || new jsPlumb.Endpoints.Dot();
  694. var _style = params.style || _currentInstance.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle;
  695. this.connectorStyle = params.connectorStyle;
  696. this.connector = params.connector;
  697. var _element = params.source;
  698. this.container = params.container || jsPlumb.Defaults.Container;
  699. var _elementId = _getAttribute(_element, "id");
  700. var _maxConnections = params.maxConnections || 1; // maximum number of connections this endpoint can be the source of.
  701. this.canvas = params.canvas || _newCanvas(jsPlumb.endpointClass, this.container);
  702. this.connections = params.connections || [];
  703. this.scope = params.scope || DEFAULT_SCOPE;
  704. var _reattach = params.reattach || false;
  705. var floatingEndpoint = null;
  706. var inPlaceCopy = null;
  707. this.addConnection = function(connection) {
  708. self.connections.push(connection);
  709. };
  710. this.removeConnection = function(connection) {
  711. var idx = _findIndex(self.connections, connection);
  712. if (idx >= 0)
  713. self.connections.splice(idx, 1);
  714. };
  715. this.makeInPlaceCopy = function() {
  716. var e = new Endpoint({anchor:self.anchor, source:_element, style:_style, endpoint:_endpoint});
  717. return e;
  718. };
  719. /**
  720. * returns whether or not this endpoint is connected to the given endpoint.
  721. * @param endpoint Endpoint to test.
  722. * @since 1.1.1
  723. *
  724. * todo: needs testing. will this work if the endpoint passed in is the source?
  725. */
  726. this.isConnectedTo = function(endpoint) {
  727. var found = false;
  728. if (endpoint) {
  729. for (var i = 0; i < self.connections.length; i++) {
  730. if (self.connections[i].endpoints[1] == endpoint) {
  731. found = true;
  732. break;
  733. }
  734. }
  735. }
  736. return found;
  737. };
  738. this.isFloating = function() { return floatingEndpoint != null; };
  739. /**
  740. * first pass at default ConnectorSelector: returns the first connection, if we have any.
  741. * modified a little, 5/10/2010: now it only returns a connector if we have not got equal to or more than _maxConnector connectors
  742. * attached. otherwise it is assumed a new connector is ok. but note with this setup we can't select any other connection than the first
  743. * one. what if this could return a list? that implies everything should work with a list - dragging etc. could be nasty. could also
  744. * be cool.
  745. */
  746. var connectorSelector = function() {
  747. return self.connections.length == 0 || self.connections.length < _maxConnections ? null : self.connections[0];
  748. };
  749. this.isFull = function() { return _maxConnections < 1 ? false : (self.connections.length >= _maxConnections); };
  750. /**
  751. * a deep equals check. everything must match, including the anchor, styles, everything.
  752. * TODO: finish Endpoint.equals
  753. */
  754. this.equals = function(endpoint) {
  755. return this.anchor.equals(endpoint.anchor) &&
  756. true;
  757. };
  758. /**
  759. * paints the Endpoint, recalculating offset and anchor positions if necessary.
  760. */
  761. this.paint = function(anchorPoint, connectorPaintStyle, canvas) {
  762. if (log) log.debug("Painting Endpoint with elementId [" + _elementId + "]; anchorPoint is [" + anchorPoint + "]");
  763. if (anchorPoint == null) {
  764. // do we always want to force a repaint here? i dont think so!
  765. var xy = offsets[_elementId];
  766. var wh = sizes[_elementId];
  767. if (xy == null || wh == null) {
  768. _updateOffset(_elementId);
  769. xy = offsets[_elementId];
  770. wh = sizes[_elementId];
  771. }
  772. anchorPoint = self.anchor.compute([xy.left, xy.top], wh, self);
  773. }
  774. _endpoint.paint(anchorPoint, self.anchor.getOrientation(), canvas || self.canvas, _style, connectorPaintStyle || _style);
  775. };
  776. // is this a connection source? we make it draggable and have the drag listener
  777. // maintain a connection with a floating endpoint.
  778. if (params.isSource && jsPlumb.CurrentLibrary.isDragSupported(_element)) {
  779. var n = null, id = null, jpc = null, existingJpc = false, existingJpcParams = null;
  780. // first the question is, how many connections are on this endpoint? if it's only one, then excellent. otherwise we will either need a way
  781. // to select one connection from the list, or drag them all. if we had a pluggable 'ConnectorSelector' interface we could probably
  782. // provide a way for people to implement their own UI components to do the connector selection. the question in that particular case would be how much
  783. // information the interface needs from jsPlumb at execution time. if, however, we leave out the connector selection, and drag them all,
  784. // that wouldn't be too hard to organise. perhaps that behaviour would be on a switch for the endpoint, or perhaps the ConnectorSelector
  785. // interface returns a List, with the default implementation just returning everything. i think i like that.
  786. //
  787. // let's say for now that there is just one endpoint, cos we need to get this going before we can consider a list of them anyway.
  788. // the major difference between that case and the case of a new endpoint is actually quite small - it's a question of where the
  789. // Connection comes from. for a new one, we create a new one. obviously. otherwise we just get the jpc from the Endpoint
  790. // (remember we're only assuming one connection right now). so all of the UI stuff we do to create the floating endpoint etc
  791. // will still be valid, but when we stop dragging, we'll have to do something different. if we stop with a valid drop i think it will
  792. // be the same process. but if we stop with an invalid drop we have to reset the Connection to how it was when we got it.
  793. var start = function() {
  794. inPlaceCopy = self.makeInPlaceCopy();
  795. inPlaceCopy.paint();
  796. n = document.createElement("div");
  797. var nE = _getElementObject(n);
  798. _appendElement(n); //
  799. // create and assign an id, and initialize the offset.
  800. //TODO can't this be replaced with a _getId call?
  801. var id = "" + new String(new Date().getTime());
  802. _setAttribute(nE, "id", id);
  803. _updateOffset(id);
  804. // store the id of the dragging div and the source element. the drop function
  805. // will pick these up.
  806. _setAttribute(_getElementObject(self.canvas), "dragId", id);
  807. _setAttribute(_getElementObject(self.canvas), "elId", _elementId);
  808. // create a floating anchor
  809. var floatingAnchor = new FloatingAnchor({reference:self.anchor, referenceCanvas:self.canvas});
  810. floatingEndpoint = new Endpoint({ style:{fillStyle:'rgba(0,0,0,0)'}, endpoint:_endpoint, anchor:floatingAnchor, source:nE });
  811. jpc = connectorSelector();
  812. if (jpc == null) {
  813. // create a connection. one end is this endpoint, the other is a floating endpoint.
  814. jpc = new Connection({
  815. sourceEndpoint:self,
  816. targetEndpoint:floatingEndpoint,
  817. source:_getElementObject(_element),
  818. target:_getElementObject(n),
  819. anchors:[self.anchor, floatingAnchor],
  820. paintStyle : params.connectorStyle, // this can be null. Connection will use the default.
  821. connector: params.connector
  822. });
  823. } else {
  824. existingJpc = true;
  825. var anchorIdx = jpc.sourceId == _elementId ? 0 : 1;
  826. jpc.floatingAnchorIndex = anchorIdx;
  827. // probably we should remove the connection? and add it back if the user
  828. // does not drop it somewhere proper.
  829. self.removeConnection(jpc);
  830. if (anchorIdx == 0){
  831. existingJpcParams = [jpc.source, jpc.sourceId];
  832. jpc.source = _getElementObject(n);
  833. jpc.sourceId = id;
  834. }else {
  835. existingJpcParams = [jpc.target, jpc.targetId];
  836. jpc.target = _getElementObject(n);
  837. jpc.targetId = id;
  838. }
  839. jpc.suspendedEndpoint = jpc.endpoints[anchorIdx];
  840. jpc.endpoints[anchorIdx] = floatingEndpoint;
  841. }
  842. // register it.
  843. floatingConnections[id] = jpc;
  844. // todo unregister on stop
  845. floatingEndpoint.addConnection(jpc);
  846. // only register for the target endpoint; we will not be dragging the source at any time
  847. // before this connection is either discarded or made into a permanent connection.
  848. _addToList(endpointsByElement, id, floatingEndpoint);
  849. };
  850. var dragOptions = params.dragOptions || { };
  851. var defaultOpts = jsPlumb.extend({}, jsPlumb.CurrentLibrary.defaultDragOptions);
  852. dragOptions = jsPlumb.extend(defaultOpts, dragOptions);
  853. dragOptions.scope = dragOptions.scope || self.scope;
  854. var startEvent = jsPlumb.CurrentLibrary.dragEvents['start'];
  855. var stopEvent = jsPlumb.CurrentLibrary.dragEvents['stop'];
  856. var dragEvent = jsPlumb.CurrentLibrary.dragEvents['drag'];
  857. dragOptions[startEvent] = _wrap(dragOptions[startEvent], start);
  858. dragOptions[dragEvent] = _wrap(dragOptions[dragEvent], function() {
  859. var _ui = jsPlumb.CurrentLibrary.getUIPosition(arguments);
  860. _draw(_getElementObject(n), _ui);
  861. });
  862. dragOptions[stopEvent] = _wrap(dragOptions[stopEvent],
  863. function() {
  864. _removeFromList(endpointsByElement, id, floatingEndpoint);
  865. _removeElements([n, floatingEndpoint.canvas]); // TODO: clean up the connection canvas (if the user aborted)
  866. _removeElement(inPlaceCopy.canvas, _element);
  867. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  868. if (jpc.endpoints[idx] == floatingEndpoint) {
  869. // if the connection was an existing one:
  870. if (existingJpc && jpc.suspendedEndpoint) {
  871. if (_reattach) {
  872. jpc.floatingAnchorIndex = null;
  873. if (idx == 0) {
  874. jpc.source = existingJpcParams[0];
  875. jpc.sourceId = existingJpcParams[1];
  876. } else {
  877. jpc.target = existingJpcParams[0];
  878. jpc.targetId = existingJpcParams[1];
  879. }
  880. jpc.endpoints[idx] = jpc.suspendedEndpoint;
  881. jpc.suspendedEndpoint.addConnection(jpc);
  882. jsPlumb.repaint(existingJpcParams[1]);
  883. }
  884. else {
  885. jpc.endpoints[0].removeConnection(jpc);
  886. jpc.endpoints[1].removeConnection(jpc);
  887. _removeElement(jpc.canvas);
  888. _removeFromList(connectionsByScope, jpc.scope, jpc);
  889. }
  890. } else {
  891. _removeElement(jpc.canvas);
  892. self.removeConnection(jpc);
  893. }
  894. }
  895. jpc = null;
  896. delete floatingEndpoint;
  897. delete inPlaceCopy;
  898. self.paint();
  899. }
  900. );
  901. var i = _getElementObject(self.canvas);
  902. jsPlumb.CurrentLibrary.initDraggable(i, dragOptions);
  903. };
  904. // connector target
  905. if (params.isTarget && jsPlumb.CurrentLibrary.isDropSupported(_element)) {
  906. var dropOptions = params.dropOptions || _currentInstance.Defaults.DropOptions || jsPlumb.Defaults.DropOptions;
  907. dropOptions = jsPlumb.extend({}, dropOptions);
  908. dropOptions.scope = dropOptions.scope || self.scope;
  909. var originalAnchor = null;
  910. var dropEvent = jsPlumb.CurrentLibrary.dragEvents['drop'];
  911. var overEvent = jsPlumb.CurrentLibrary.dragEvents['over'];
  912. var outEvent = jsPlumb.CurrentLibrary.dragEvents['out'];
  913. dropOptions[dropEvent]= _wrap(dropOptions[dropEvent], function() {
  914. var draggable = jsPlumb.CurrentLibrary.getDragObject(arguments);
  915. var id = _getAttribute(_getElementObject(draggable), "dragId");
  916. var elId = _getAttribute(_getElementObject(draggable),"elId");
  917. var jpc = floatingConnections[id];
  918. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  919. if (idx == 0) {
  920. jpc.source = _element;
  921. jpc.sourceId = _elementId;
  922. } else {
  923. jpc.target = _element;
  924. jpc.targetId = _elementId;
  925. }
  926. // remove this jpc from the current endpoint
  927. jpc.endpoints[idx].removeConnection(jpc);
  928. if (jpc.suspendedEndpoint)
  929. jpc.suspendedEndpoint.removeConnection(jpc);
  930. jpc.endpoints[idx] = self;
  931. self.addConnection(jpc);
  932. _addToList(connectionsByScope, jpc.scope, jpc);
  933. _initDraggableIfNecessary(_element, params.draggable, {});
  934. jsPlumb.repaint(elId);
  935. delete floatingConnections[id];
  936. });
  937. dropOptions[overEvent]= _wrap(dropOptions[overEvent], function() {
  938. var draggable = jsPlumb.CurrentLibrary.getDragObject(arguments);
  939. var id = _getAttribute(_getElementObject(draggable),"dragId");
  940. var jpc = floatingConnections[id];
  941. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  942. jpc.endpoints[idx].anchor.over(self.anchor);
  943. });
  944. dropOptions[outEvent] = _wrap(dropOptions[outEvent], function() {
  945. var draggable = jsPlumb.CurrentLibrary.getDragObject(arguments);
  946. var id = _getAttribute(_getElementObject(draggable),"dragId");
  947. var jpc = floatingConnections[id];
  948. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  949. jpc.endpoints[idx].anchor.out();
  950. });
  951. jsPlumb.CurrentLibrary.initDroppable(_getElementObject(self.canvas), dropOptions);
  952. }
  953. return self;
  954. };
  955. /*
  956. Property: Defaults
  957. These are the default settings for jsPlumb, that is what will be used if you do not supply specific pieces of information
  958. to the various API calls. A convenient way to implement your own look and feel can be to override these defaults by including a script
  959. somewhere after the jsPlumb include, but before you make any calls to jsPlumb, for instance in this example we set the PaintStyle to be
  960. a blue line of 27 pixels:
  961. > jsPlumb.Defaults.PaintStyle = { lineWidth:27, strokeStyle:'blue' }
  962. */
  963. this.Defaults = {
  964. Anchor : null,
  965. Anchors : [ null, null ],
  966. Connector : null,
  967. Container : null,
  968. DragOptions: { },
  969. DropOptions: { },
  970. Endpoint : null,
  971. Endpoints : [ null, null ],
  972. EndpointStyle : { fillStyle : null },
  973. EndpointStyles : [ null, null ],
  974. MaxConnections : null,
  975. PaintStyle : { lineWidth : 10, strokeStyle : 'red' },
  976. Scope : "_jsPlumb_DefaultScope"
  977. };
  978. /*
  979. Property: connectorClass
  980. The CSS class to set on Connection canvas elements. This value is a String and can have multiple classes; the entire String is appended as-is.
  981. */
  982. this.connectorClass = '_jsPlumb_connector';
  983. /*
  984. Property: endpointClass
  985. The CSS class to set on Endpoint canvas elements. This value is a String and can have multiple classes; the entire String is appended as-is.
  986. */
  987. this.endpointClass = '_jsPlumb_endpoint';
  988. /*
  989. Property: Anchors
  990. Default jsPlumb Anchors. These are supplied in the file jsPlumb-defaults-x.x.x.js, which is merged in with the main jsPlumb script
  991. to form <library>.jsPlumb-all-x.x.x.js. You can provide your own Anchors by supplying them in a script that is loaded after jsPlumb, for instance:
  992. > jsPlumb.Anchors.MyAnchor = { ....anchor code here. see the documentation. }
  993. */
  994. this.Anchors = {};
  995. /*
  996. Property: Connectors
  997. Default jsPlumb Connectors. These are supplied in the file jsPlumb-defaults-x.x.x.js, which is merged in with the main jsPlumb script
  998. to form <library>.jsPlumb-all-x.x.x.js. You can provide your own Connectors by supplying them in a script that is loaded after jsPlumb, for instance:
  999. > jsPlumb.Connectors.MyConnector = { ....connector code here. see the documentation. }
  1000. */
  1001. this.Connectors = {};
  1002. /*
  1003. Property: Endpoints
  1004. Default jsPlumb Endpoints. These are supplied in the file jsPlumb-defaults-x.x.x.js, which is merged in with the main jsPlumb script
  1005. to form <library>.jsPlumb-all-x.x.x.js. You can provide your own Endpoints by supplying them in a script that is loaded after jsPlumb, for instance:
  1006. > jsPlumb.Endpoints.MyEndpoint = { ....endpoint code here. see the documentation. }
  1007. */
  1008. this.Endpoints = {};
  1009. /*
  1010. Function: addEndpoint
  1011. Adds an Endpoint to a given element.
  1012. Parameters:
  1013. target - Element to add the endpoint to. either an element id, or a selector representing some element.
  1014. params - Object containing Endpoint options (more info required)
  1015. Returns:
  1016. The newly created Endpoint.
  1017. See Also:
  1018. <addEndpoints>
  1019. */
  1020. this.addEndpoint = function(target, params) {
  1021. params = jsPlumb.extend({}, params);
  1022. var el = _getElementObject(target);
  1023. var id = _getAttribute(el,"id");
  1024. params.source = el;
  1025. _updateOffset(id);
  1026. var e = new Endpoint(params);
  1027. _addToList(endpointsByElement, id, e);
  1028. var myOffset = offsets[id];
  1029. var myWH = sizes[id];
  1030. var anchorLoc = e.anchor.compute([myOffset.left, myOffset.top], myWH, e);
  1031. e.paint(anchorLoc);
  1032. return e;
  1033. };
  1034. /*
  1035. Function: addEndpoint
  1036. Adds a list of Endpoints to a given element.
  1037. Parameters:
  1038. target - element to add the endpoint to. either an element id, or a selector representing some element.
  1039. endpoints - List of objects containing Endpoint options. one Endpoint is created for each entry in this list.
  1040. Returns:
  1041. List of newly created Endpoints, one for each entry in the 'endpoints' argument.
  1042. See Also:
  1043. <addEndpoint>
  1044. */
  1045. this.addEndpoints = function(target, endpoints) {
  1046. var results = [];
  1047. for (var i = 0; i < endpoints.length; i++) {
  1048. results.push(jsPlumb.addEndpoint(target, endpoints[i]));
  1049. }
  1050. return results;
  1051. };
  1052. /*
  1053. Function: animate
  1054. Wrapper around supporting library's animate function; injects a call to jsPlumb in the 'step' function (creating it if necessary).
  1055. This only supports the two-arg version of the animate call in jQuery - the one that takes an 'options' object as the second arg.
  1056. MooTools has only one method - a two arg one. Which is handy.
  1057. Parameters:
  1058. el - Element to animate. Either an id, or a selector representing the element.
  1059. properties - The 'properties' argument you want passed to the standard jQuery animate call.
  1060. options - The 'options' argument you want passed to the standard jQuery animate call.
  1061. Returns:
  1062. void
  1063. */
  1064. this.animate = function(el, properties, options) {
  1065. var ele = _getElementObject(el);
  1066. var id = _getAttribute(el,"id");
  1067. //TODO this is not agnostic yet.
  1068. options = options || {};
  1069. var stepFunction = jsPlumb.CurrentLibrary.dragEvents['step'];
  1070. var completeFunction = jsPlumb.CurrentLibrary.dragEvents['complete'];
  1071. options[stepFunction] = _wrap(options[stepFunction], function()
  1072. {
  1073. _currentInstance.repaint(id);
  1074. });
  1075. // you know, probably onComplete should repaint too. that will help keep up
  1076. // with fast animations.
  1077. options[completeFunction] = _wrap(options[completeFunction], function()
  1078. {
  1079. _currentInstance.repaint(id);
  1080. });
  1081. jsPlumb.CurrentLibrary.animate(ele, properties, options);
  1082. };
  1083. /**
  1084. * clears the cache used to lookup elements by their id. if you remove any elements
  1085. * from the DOM you should call this to ensure that if you add them back in jsPlumb does not
  1086. * have a stale handle.
  1087. */
  1088. this.clearCache = function() {
  1089. delete cached;
  1090. cached = {};
  1091. };
  1092. /*
  1093. Function: connect
  1094. Establishes a connection between two elements.
  1095. Parameters:
  1096. params - Object containing setup for the connection. see documentation.
  1097. Returns:
  1098. The newly created Connection.
  1099. */
  1100. this.connect = function(params) {
  1101. if (params.sourceEndpoint && params.sourceEndpoint.isFull()) {
  1102. _log("could not add connection; source endpoint is full");
  1103. return;
  1104. }
  1105. if (params.targetEndpoint && params.targetEndpoint.isFull()) {
  1106. _log("could not add connection; target endpoint is full");
  1107. return;
  1108. }
  1109. var jpc = new Connection(params);
  1110. // add to list of connections (by scope).
  1111. _addToList(connectionsByScope, jpc.scope, jpc);
  1112. // force a paint
  1113. _draw(jpc.source);
  1114. return jpc;
  1115. };
  1116. /**
  1117. * not implemented yet. params object will have sourceEndpoint and targetEndpoint members; these will be Endpoints.
  1118. connectEndpoints : function(params) {
  1119. var jpc = Connection(params);
  1120. },*/
  1121. /*
  1122. Function: detach
  1123. Removes a connection.
  1124. Parameters:
  1125. sourceId - Id of the first element in the connection. A String.
  1126. targetId - iI of the second element in the connection. A String.
  1127. Returns:
  1128. true if successful, false if not.
  1129. */
  1130. this.detach = function(sourceId, targetId) {
  1131. var f = function(jpc) {
  1132. if ((jpc.sourceId == sourceId && jpc.targetId == targetId) || (jpc.targetId == sourceId && jpc.sourceId == targetId)) {
  1133. _removeElement(jpc.canvas, jpc.container);
  1134. jpc.endpoints[0].removeConnection(jpc);
  1135. jpc.endpoints[1].removeConnection(jpc);
  1136. _removeFromList(connectionsByScope, jpc.scope, jpc);
  1137. return true;
  1138. }
  1139. };
  1140. // todo: how to cleanup the actual storage? a third arg to _operation?
  1141. _operation(sourceId, f);
  1142. };
  1143. /*
  1144. Function: detachAll
  1145. Removes all an element's connections.
  1146. Parameters:
  1147. el - either the id of the element, or a selector for the element.
  1148. Returns:
  1149. void
  1150. */
  1151. this.detachAll = function(el) {
  1152. var id = _getAttribute(el, "id");
  1153. var endpoints = endpointsByElement[id];
  1154. if (endpoints && endpoints.length) {
  1155. for (var i = 0; i < endpoints.length; i++) {
  1156. var c = endpoints[i].connections.length;
  1157. if (c > 0) {
  1158. for (var j = 0; j < c; j++) {
  1159. var jpc = endpoints[i].connections[0];
  1160. _removeElement(jpc.canvas, jpc.container);
  1161. jpc.endpoints[0].removeConnection(jpc);
  1162. jpc.endpoints[1].removeConnection(jpc);
  1163. _removeFromList(connectionsByScope, jpc.scope, jpc);
  1164. }
  1165. }
  1166. }
  1167. }
  1168. };
  1169. /*
  1170. Function: detachEverything
  1171. Remove all Connections from all elements, but leaves Endpoints in place.
  1172. Returns:
  1173. void
  1174. See Also:
  1175. <removeEveryEndpoint>
  1176. */
  1177. this.detachEverything = function() {
  1178. for(var id in endpointsByElement) {
  1179. var endpoints = endpointsByElement[id];
  1180. if (endpoints && endpoints.length) {
  1181. for (var i = 0; i < endpoints.length; i++) {
  1182. var c = endpoints[i].connections.length;
  1183. if (c > 0) {
  1184. for (var j = 0; j < c; j++) {
  1185. var jpc = endpoints[i].connections[0];
  1186. _removeElement(jpc.canvas, jpc.container);
  1187. jpc.endpoints[0].removeConnection(jpc);
  1188. jpc.endpoints[1].removeConnection(jpc);
  1189. }
  1190. }
  1191. }
  1192. }
  1193. }
  1194. delete connectionsByScope;
  1195. connectionsByScope = {};
  1196. };
  1197. /*
  1198. Function: draggable
  1199. initialises the draggability of some element or elements.
  1200. Parameters:
  1201. el - either an element id, a list of element ids, or a selector.
  1202. options - options to pass through to the underlying library
  1203. Returns:
  1204. void
  1205. */
  1206. this.draggable = function(el, options) {
  1207. if (typeof el == 'object' && el.length) {
  1208. for (var i = 0; i < el.length; i++)
  1209. {
  1210. var ele = _getElementObject(el[i]);
  1211. if (ele) _initDraggableIfNecessary(ele, true, options);
  1212. }
  1213. }
  1214. else {
  1215. var ele = _getElementObject(el);
  1216. if (ele)
  1217. _initDraggableIfNecessary(ele, true, options);
  1218. }
  1219. };
  1220. /*
  1221. Function: extend
  1222. Wraps the underlying library's extend functionality.
  1223. Parameters:
  1224. o1 - object to extend
  1225. o2 - object to extend o1 with
  1226. Returns:
  1227. o1, extended with all properties from o2.
  1228. */
  1229. this.extend = function(o1, o2) {
  1230. return jsPlumb.CurrentLibrary.extend(o1, o2);
  1231. };
  1232. /*
  1233. Function: getConnections
  1234. Gets all or a subset of connections currently managed by this jsPlumb instance.
  1235. Parameters:
  1236. options - a JS object that holds options defining what sort of connections you're looking for. Valid values are:
  1237. scope this may be a String or a list of Strings. jsPlumb will only return Connections whose scope matches what this option
  1238. defines. If you omit it, you will be given Connections of every scope.
  1239. source may be a string or a list of strings; constraints results to have only Connections whose source is this object.
  1240. target may be a string or a list of strings; constraints results to have only Connections whose target is this object.
  1241. The return value is a dictionary in this format:
  1242. {
  1243. 'scope1': [
  1244. {sourceId:'window1', targetId:'window2'},
  1245. {sourceId:'window3', targetId:'window4'},
  1246. {sourceId:'window1', targetId:'window3'}
  1247. ],
  1248. 'scope2': [
  1249. {sourceId:'window1', targetId:'window3'}
  1250. ]
  1251. }
  1252. */
  1253. this.getConnections = function(options) {
  1254. var r = {};
  1255. options = options || {};
  1256. var prepareList = function(input) {
  1257. var r = [];
  1258. if (input) {
  1259. if (typeof input == 'string') r.push(input);
  1260. else r = input;
  1261. }
  1262. return r;
  1263. };
  1264. var scopes = prepareList(options.scope);
  1265. var sources = prepareList(options.source);
  1266. var targets = prepareList(options.target);
  1267. var filter = function(list, value) {
  1268. return list.length > 0 ? _findIndex(list, value) != -1 : true;
  1269. };
  1270. for (var i in connectionsByScope) {
  1271. if (filter(scopes, i)) {
  1272. r[i] = [];
  1273. for (var j = 0; j < connectionsByScope[i].length; j++) {
  1274. var c = connectionsByScope[i][j];
  1275. if (filter(sources, c.sourceId) && filter(targets, c.targetId))
  1276. r[i].push({sourceId:c.sourceId, targetId:c.targetId});
  1277. }
  1278. }
  1279. }
  1280. return r;
  1281. };
  1282. /*
  1283. Function: getDefaultScope
  1284. Gets the default scope for connections and endpoints. a scope defines a type of endpoint/connection; supplying a scope to an endpoint
  1285. or connection allows you to support different types of connections in the same UI. but if you're only interested in one type of connection,
  1286. you don't need to supply a scope. this method will probably be used by very few people; it's good for testing though.
  1287. */
  1288. this.getDefaultScope = function() {
  1289. return DEFAULT_SCOPE;
  1290. };
  1291. /*
  1292. Function: hide
  1293. Sets an element's connections to be hidden.
  1294. Parameters:
  1295. el - either the id of the element, or a selector for the element.
  1296. Returns:
  1297. void
  1298. */
  1299. this.hide = function(el) {
  1300. _setVisible(el, "none");
  1301. };
  1302. /*
  1303. Function: makeAnchor
  1304. Creates an anchor with the given params.
  1305. Parameters:
  1306. x - the x location of the anchor as a fraction of the total width.
  1307. y - the y location of the anchor as a fraction of the total height.
  1308. xOrientation - value indicating the general direction a connection from the anchor should go in, in the x direction.
  1309. yOrientation - value indicating the general direction a connection from the anchor should go in, in the y direction.
  1310. xOffset - a fixed offset that should be applied in the x direction that should be applied after the x position has been figured out. optional. defaults to 0.
  1311. yOffset - a fixed offset that should be applied in the y direction that should be applied after the y position has been figured out. optional. defaults to 0.
  1312. Returns:
  1313. The newly created Anchor.
  1314. */
  1315. this.makeAnchor = function(x, y, xOrientation, yOrientation, xOffset, yOffset) {
  1316. // backwards compatibility here. we used to require an object passed in but that makes the call very verbose. easier to use
  1317. // by just passing in four/six values. but for backwards compatibility if we are given only one value we assume it's a call in the old form.
  1318. var params = {};
  1319. if (arguments.length == 1) jsPlumb.extend(params, x);
  1320. else {
  1321. params = {x:x, y:y};
  1322. if (arguments.length >= 4) {
  1323. params.orientation = [arguments[2], arguments[3]];
  1324. }
  1325. if (arguments.length == 6) params.offsets = [arguments[4], arguments[5]];
  1326. }
  1327. var a = new Anchor(params);
  1328. a.clone = function() { return new Anchor(params); };
  1329. return a;
  1330. };
  1331. /*
  1332. Function: repaint
  1333. Repaints an element and its connections. This method gets new sizes for the elements before painting anything.
  1334. Parameters:
  1335. el - either the id of the element or a selector representing the element.
  1336. Returns:
  1337. void
  1338. See Also:
  1339. <repaintEverything>
  1340. */
  1341. this.repaint = function(el) {
  1342. var _processElement = function(el) {
  1343. var ele = _getElementObject(el);
  1344. _draw(ele);
  1345. };
  1346. // TODO: support a jQuery result object too!
  1347. // support both lists...
  1348. if (typeof el =='object') {
  1349. for (var i = 0; i < el.length; i++)
  1350. _processElement(el[i]);
  1351. } // ...and single strings.
  1352. else _processElement(el);
  1353. };
  1354. /*
  1355. Function: repaintEverything
  1356. Repaints all connections.
  1357. Returns:
  1358. void
  1359. See Also:
  1360. <repaint>
  1361. */
  1362. this.repaintEverything = function() {
  1363. for (var elId in endpointsByElement) {
  1364. _draw(_getElementObject(elId));
  1365. }
  1366. };
  1367. /*
  1368. Function: removeAllEndpoints
  1369. Removes all Endpoints associated with a given element. Also removes all Connections associated with each Endpoint it removes.
  1370. Parameters:
  1371. el - either an element id, or a selector for an element.
  1372. Returns:
  1373. void
  1374. See Also:
  1375. <removeEndpoint>
  1376. */
  1377. this.removeAllEndpoints = function(el) {
  1378. var elId = _getAttribute(el, "id");
  1379. // first remove all Connections.
  1380. jsPlumb.detachAll(elId);
  1381. var ebe = endpointsByElement[elId];
  1382. for (var i in ebe) {
  1383. _removeElement(ebe[i].canvas);
  1384. }
  1385. endpointsByElement[elId] = [];
  1386. };
  1387. /*
  1388. Function: removeEveryEndpoint
  1389. Removes every Endpoint in this instance of jsPlumb.
  1390. Returns:
  1391. void
  1392. See Also:
  1393. <removeAllEndpoints> <removeEndpoint>
  1394. */
  1395. this.removeEveryEndpoint = function() {
  1396. for(var id in endpointsByElement) {
  1397. var endpoints = endpointsByElement[id];
  1398. if (endpoints && endpoints.length) {
  1399. for (var i = 0; i < endpoints.length; i++) {
  1400. _removeElement(endpoints[i].canvas, endpoints[i].container);
  1401. }
  1402. }
  1403. }
  1404. delete endpointsByElement;
  1405. endpointsByElement = {};
  1406. };
  1407. /*
  1408. Function: removeEndpoint
  1409. Removes the given Endpoint from the given element.
  1410. Parameters:
  1411. el - either an element id, or a selector for an element.
  1412. endpoint - Endpoint to remove. this is an Endpoint object, such as would have been returned from a call to addEndpoint.
  1413. Returns:
  1414. void
  1415. See Also:
  1416. <removeAllEndpoints> <removeEveryEndpoint>
  1417. */
  1418. this.removeEndpoint = function(el, endpoint) {
  1419. var elId = _getAttribute(el, "id");
  1420. var ebe = endpointsByElement[elId];
  1421. if (ebe) {
  1422. if(_removeFromList(endpointsByElement, elId, endpoint))
  1423. _removeElement(endpoint.canvas);
  1424. }
  1425. };
  1426. /*
  1427. Function:reset
  1428. removes all endpoints and connections and clears the element cache.
  1429. */
  1430. this.reset = function() {
  1431. this.detachEverything();
  1432. this.removeEveryEndpoint();
  1433. this.clearCache();
  1434. };
  1435. /*
  1436. Function: setAutomaticRepaint
  1437. Sets/unsets automatic repaint on window resize.
  1438. Parameters:
  1439. value - whether or not to automatically repaint when the window is resized.
  1440. Returns:
  1441. void
  1442. */
  1443. this.setAutomaticRepaint = function(value) {
  1444. automaticRepaint = value;
  1445. };
  1446. /*
  1447. Function: setDefaultNewCanvasSize
  1448. Sets the default size jsPlumb will use for a new canvas (we create a square canvas so one value is all that is required).
  1449. This is a hack for IE, because ExplorerCanvas seems to need for a canvas to be larger than what you are going to draw on
  1450. it at initialisation time. The default value of this is 1200 pixels, which is quite large, but if for some reason you're
  1451. drawing connectors that are bigger, you should adjust this value appropriately.
  1452. Parameters:
  1453. size - The default size to use. jsPlumb will use a square canvas so you need only supply one value.
  1454. Returns:
  1455. void
  1456. */
  1457. this.setDefaultNewCanvasSize = function(size) {
  1458. DEFAULT_NEW_CANVAS_SIZE = size;
  1459. };
  1460. /*
  1461. Function: setDefaultScope
  1462. Sets the default scope for connections and endpoints. a scope defines a type of endpoint/connection; supplying a scope to an endpoint
  1463. or connection allows you to support different types of connections in the same UI. but if you're only interested in one type of connection,
  1464. you don't need to supply a scope. this method will probably be used by very few people; it just instructs jsPlumb to use a different key
  1465. for the default scope.
  1466. */
  1467. this.setDefaultScope = function(scope) {
  1468. DEFAULT_SCOPE = scope;
  1469. };
  1470. /*
  1471. Function: setDraggable
  1472. Sets whether or not a given element is draggable, regardless of what any plumb command may request.
  1473. Parameters:
  1474. el - either the id for the element, or a selector representing the element.
  1475. Returns:
  1476. void
  1477. */
  1478. this.setDraggable = _setDraggable;
  1479. /*
  1480. Function: setDraggableByDefault
  1481. Sets whether or not elements are draggable by default. Default for this is true.
  1482. Parameters:
  1483. draggable - value to set
  1484. Returns:
  1485. void
  1486. */
  1487. this.setDraggableByDefault = function(draggable) {
  1488. _draggableByDefault = draggable;
  1489. };
  1490. this.setDebugLog = function(debugLog) {
  1491. log = debugLog;
  1492. };
  1493. /*
  1494. Function: setRepaintFunction
  1495. Sets the function to fire when the window size has changed and a repaint was fired.
  1496. Parameters:
  1497. f - Function to execute.
  1498. Returns:
  1499. void
  1500. */
  1501. this.setRepaintFunction = function(f) {
  1502. repaintFunction = f;
  1503. };
  1504. /*
  1505. Function: show
  1506. Sets an element's connections to be visible.
  1507. Parameters:
  1508. el - either the id of the element, or a selector for the element.
  1509. Returns:
  1510. void
  1511. */
  1512. this.show = function(el) {
  1513. _setVisible(el, "block");
  1514. };
  1515. /*
  1516. Function: sizeCanvas
  1517. Helper to size a canvas. You would typically use this when writing your own Connector or Endpoint implementation.
  1518. Parameters:
  1519. x - [int] x position for the Canvas origin
  1520. y - [int] y position for the Canvas origin
  1521. w - [int] width of the canvas
  1522. h - [int] height of the canvas
  1523. Returns:
  1524. void
  1525. */
  1526. this.sizeCanvas = function(canvas, x, y, w, h) {
  1527. if (canvas) {
  1528. canvas.style.height = h + "px"; canvas.height = h;
  1529. canvas.style.width = w + "px"; canvas.width = w;
  1530. canvas.style.left = x + "px"; canvas.style.top = y + "px";
  1531. // _currentInstance.CurrentLibrary.setPosition(canvas, x, y);
  1532. }
  1533. };
  1534. /**
  1535. * gets some test hooks. nothing writable.
  1536. */
  1537. this.getTestHarness = function() {
  1538. return {
  1539. endpointCount : function(elId) {
  1540. var e = endpointsByElement[elId];
  1541. return e ? e.length : 0;
  1542. },
  1543. connectionCount : function(scope) {
  1544. scope = scope || DEFAULT_SCOPE;
  1545. var c = connectionsByScope[scope];
  1546. return c ? c.length : 0;
  1547. },
  1548. findIndex : _findIndex,
  1549. getId : _getId
  1550. };
  1551. };
  1552. /**
  1553. * Toggles visibility of an element's connections. kept for backwards compatibility
  1554. */
  1555. this.toggle = _toggleVisible;
  1556. /*
  1557. Function: toggleVisible
  1558. Toggles visibility of an element's connections.
  1559. Parameters:
  1560. el - either the element's id, or a selector representing the element.
  1561. Returns:
  1562. void, but should be updated to return the current state
  1563. */
  1564. //TODO: update this method to return the current state.
  1565. this.toggleVisible = _toggleVisible;
  1566. /*
  1567. Function: toggleDraggable
  1568. Toggles draggability (sic) of an element's connections.
  1569. Parameters:
  1570. el - either the element's id, or a selector representing the element.
  1571. Returns:
  1572. The current draggable state.
  1573. */
  1574. this.toggleDraggable = _toggleDraggable;
  1575. /*
  1576. Function: unload
  1577. Unloads jsPlumb, deleting all storage. You should call this from an onunload attribute on the <body> element
  1578. Returns:
  1579. void
  1580. */
  1581. this.unload = function() {
  1582. delete endpointsByElement;
  1583. delete offsets;
  1584. delete sizes;
  1585. delete floatingConnections;
  1586. delete draggableStates;
  1587. };
  1588. /*
  1589. Function: wrap
  1590. Helper method to wrap an existing function with one of your own. This is used by the various
  1591. implementations to wrap event callbacks for drag/drop etc; it allows jsPlumb to be
  1592. transparent in its handling of these things. If a user supplies their own event callback,
  1593. for anything, it will always be called.
  1594. Parameters:
  1595. */
  1596. this.wrap = _wrap;
  1597. this.trace = _trace;
  1598. this.clearTrace = _clearTrace;
  1599. this.clearAllTraces = _clearAllTraces;
  1600. this.getTrace = _getTrace;
  1601. };
  1602. var jsPlumb = window.jsPlumb = new jsPlumbInstance();
  1603. jsPlumb.getInstance = function(_defaults) {
  1604. var j = new jsPlumbInstance();
  1605. if (_defaults)
  1606. jsPlumb.extend(j.Defaults, _defaults);
  1607. return j;
  1608. };
  1609. })();