mootools.jsPlumb-1.2.2-all.js 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681
  1. /*
  2. * jsPlumb 1.2.2
  3. *
  4. * Provides a way to visually connect elements on an HTML page.
  5. *
  6. * additions since 1.2.1:
  7. *
  8. * - checks drag target to ensure it is not full when dropping a new connection (issue 13)
  9. *
  10. * http://morrisonpitt.com/jsPlumb/demo.html
  11. * http://code.google.com/p/jsPlumb
  12. *
  13. * Dual licensed under MIT and GPL2.
  14. *
  15. */
  16. (function() {
  17. /*
  18. Class: jsPlumb
  19. This is the main entry point to jsPlumb. There are a bunch of static methods you can
  20. use to connect/disconnect etc. Some methods are also added to jQuery's "$.fn" object itself, but
  21. in general jsPlumb is moving away from that model, preferring instead the static class
  22. approach. One of the reasons for this is that with no methods added to the jQuery $.fn object,
  23. it will be easier to provide support for other libraries such as MooTools.
  24. */
  25. var jsPlumbInstance = function() {
  26. var _currentInstance = this;
  27. var ie = (/MSIE/.test(navigator.userAgent) && !window.opera);
  28. var log = null;
  29. var repaintFunction = function() { jsPlumb.repaintEverything(); };
  30. var automaticRepaint = true;
  31. function repaintEverything() {
  32. if (automaticRepaint)
  33. repaintFunction();
  34. };
  35. var resizeTimer = null;
  36. /**
  37. * map of element id -> endpoint lists. an element can have an arbitrary number of endpoints on it,
  38. * and not all of them have to be connected to anything.
  39. */
  40. var endpointsByElement = {};
  41. var endpointsByUUID = {};
  42. var connectionsByScope = {};
  43. var offsets = [];
  44. var floatingConnections = {};
  45. var draggableStates = {};
  46. var _draggableByDefault = true;
  47. var sizes = [];
  48. var listeners = {}; // a map: keys are event types, values are lists of listeners.
  49. var DEFAULT_SCOPE = 'DEFAULT';
  50. 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.)
  51. var _findIndex = function( a, v, b, s ) {
  52. var _eq = function(o1, o2) {
  53. if (o1 === o2) return true;
  54. else if (typeof o1 == 'object' && typeof o2 == 'object') {
  55. var same = true;
  56. for(var propertyName in o1) {
  57. if(!_eq(o1[propertyName], o2[propertyName])) {
  58. same = false;
  59. break;
  60. }
  61. }
  62. for(var propertyName in o2) {
  63. if(!_eq(o2[propertyName], o1[propertyName])) {
  64. same = false;
  65. break;
  66. }
  67. }
  68. return same;
  69. }
  70. };
  71. for( var i = +b || 0, l = a.length; i < l; i++ ) {
  72. if( _eq(a[i], v)) return i;
  73. }
  74. return -1;
  75. };
  76. /**
  77. * adds a listener for the specified event type.
  78. */
  79. var _addListener = function(eventType, listener) {
  80. var doOne = function(e, l) {
  81. _addToList(listeners, e, l);
  82. };
  83. if (typeof eventType == 'object' && eventType.length) {
  84. for (var i = 0; i < eventType.length; i++) doOne(eventType[i], listener);
  85. } else doOne(eventType, listener);
  86. };
  87. /**
  88. * helper method to add an item to a list, creating the list if it does not yet exist.
  89. */
  90. var _addToList = function(map, key, value) {
  91. var l = map[key];
  92. if (l == null) {
  93. l = [];
  94. map[key] = l;
  95. }
  96. l.push(value);
  97. return l;
  98. };
  99. var _appendElement = function(canvas, parent) {
  100. if (!parent) document.body.appendChild(canvas);
  101. else jsPlumb.CurrentLibrary.appendElement(canvas, parent);
  102. };
  103. /**
  104. * Handles the dragging of an element.
  105. * @param element jQuery element
  106. * @param ui UI object from current library's event system
  107. */
  108. var _draw = function(element, ui) {
  109. var id = _getAttribute(element, "id");
  110. var endpoints = endpointsByElement[id];
  111. if (endpoints) {
  112. var timestamp = '' + (new Date().getTime());
  113. _updateOffset(id, ui);
  114. var myOffset = offsets[id];
  115. var myWH = sizes[id];
  116. // loop through endpoints for this element
  117. for (var i = 0; i < endpoints.length; i++) {
  118. var e = endpoints[i];
  119. var anchorLoc = e.anchor.compute([myOffset.left, myOffset.top], myWH, e);
  120. e.paint(anchorLoc);
  121. var l = e.connections;
  122. for (var j = 0; j < l.length; j++)
  123. l[j].paint(id, ui, false, timestamp); // ...and paint them.
  124. }
  125. }
  126. };
  127. /**
  128. * helper function: the second argument is a function taking two args - the first is an id
  129. * ui element from the current library (or list of them), and the second is the function
  130. * to run.
  131. */
  132. var _elementProxy = function(element, fn) {
  133. var retVal = null;
  134. // TODO this might be jquery specific. in fact it probably is.
  135. if (typeof element == 'object' && element.length) {
  136. retVal = [];
  137. for (var i = 0; i < element.length; i++) {
  138. var el = _getElementObject(element[i]);
  139. var id = _getAttribute(el, "id");
  140. retVal.push(fn(el, id)); // append return values to what we will return
  141. }
  142. }
  143. else {
  144. var el = _getElementObject(element);
  145. var id = _getAttribute(el, "id");
  146. retVal = fn(el, id);
  147. }
  148. return retVal;
  149. };
  150. /**
  151. * fires an event of the given type.
  152. */
  153. var _fireEvent = function(eventType, data) {
  154. var l = listeners[eventType];
  155. if (l) {
  156. for (var i in l) {
  157. try {
  158. l[i][eventType](data);
  159. }
  160. catch (e) {
  161. _log("while firing event [" + eventType + "]; listener failed like this: " + e);
  162. }
  163. }
  164. }
  165. };
  166. var _log = function(msg) {
  167. //if (console) console.log(msg);
  168. };
  169. var _logFnCall = function(fn, args) {
  170. /*if (console) {
  171. var c = args.callee.caller.toString();
  172. var i = c.indexOf("{");
  173. var msg = fn + ' : ' + c.substring(0, i);
  174. console.log(msg);
  175. }*/
  176. };
  177. var cached = {};
  178. var __getElementObject = function(el) {
  179. if (typeof(el)=='string') {
  180. var e = cached[el];
  181. if (!e) {
  182. e = jsPlumb.CurrentLibrary.getElementObject(el);
  183. cached[el] = e;
  184. }
  185. return e;
  186. }
  187. else {
  188. return jsPlumb.CurrentLibrary.getElementObject(el);
  189. }
  190. };
  191. /**
  192. * gets the named attribute from the given element (id or element object)
  193. */
  194. var _getAttribute = function(el, attName) {
  195. var ele = __getElementObject(el);
  196. return jsPlumb.CurrentLibrary.getAttribute(ele, attName);
  197. };
  198. var _setAttribute = function(el, attName, attValue) {
  199. var ele = __getElementObject(el);
  200. jsPlumb.CurrentLibrary.setAttribute(ele, attName, attValue);
  201. };
  202. var _addClass = function(el, clazz) {
  203. var ele = __getElementObject(el);
  204. jsPlumb.CurrentLibrary.addClass(ele, clazz);
  205. };
  206. var _getElementObject = function(elId) {
  207. return __getElementObject(elId);
  208. };
  209. var _getOffset = function(el) {
  210. var ele = __getElementObject(el);
  211. return jsPlumb.CurrentLibrary.getOffset(ele);
  212. };
  213. var _getSize = function(el) {
  214. var ele = __getElementObject(el);
  215. return jsPlumb.CurrentLibrary.getSize(ele);
  216. };
  217. /**
  218. * gets an id for the given element, creating and setting one if necessary.
  219. */
  220. var _getId = function(element, uuid) {
  221. var ele = _getElementObject(element);
  222. var id = _getAttribute(ele, "id");
  223. if (!id) {
  224. //check if fixed uuid parameter is given
  225. if(arguments.length == 2)
  226. id = uuid;
  227. else
  228. id = "_jsPlumb_" + new String((new Date()).getTime());
  229. _setAttribute(ele, "id", id);
  230. }
  231. return id;
  232. };
  233. /**
  234. * gets an Endpoint by uuid.
  235. */
  236. var _getEndpoint = function(uuid) {
  237. return endpointsByUUID[uuid];
  238. };
  239. /**
  240. * inits a draggable if it's not already initialised.
  241. *
  242. * TODO: we need to hook in each library to this method. they need to be given the opportunity to
  243. * wrap/insert lifecycle functions, because each library does different things. for instance, jQuery
  244. * supports the notion of 'revert', which will clean up a dragged endpoint; MooTools does not. jQuery
  245. * also supports 'opacity' and MooTools does not; jQuery supports z-index of the draggable; MooTools
  246. * does not. i could go on. the point is...oh. initDraggable can do this. ok.
  247. */
  248. var _initDraggableIfNecessary = function(element, isDraggable, dragOptions) {
  249. // dragging
  250. var draggable = isDraggable == null ? _draggableByDefault : isDraggable;
  251. if (draggable) {
  252. if (jsPlumb.CurrentLibrary.isDragSupported(element)) {
  253. var options = dragOptions || _currentInstance.Defaults.DragOptions || jsPlumb.Defaults.DragOptions;
  254. options = jsPlumb.extend({}, options); // make a copy.
  255. var dragEvent = jsPlumb.CurrentLibrary.dragEvents['drag'];
  256. options[dragEvent] = _wrap(options[dragEvent], function() {
  257. var ui = jsPlumb.CurrentLibrary.getUIPosition(arguments);
  258. _draw(element, ui);
  259. _addClass(element, "jsPlumb_dragged");
  260. });
  261. var draggable = draggableStates[_getId(element)];
  262. options.disabled = draggable == null ? false : !draggable;
  263. jsPlumb.CurrentLibrary.initDraggable(element, options);
  264. }
  265. }
  266. };
  267. /**
  268. * helper to create a canvas.
  269. * @param clazz optional class name for the canvas.
  270. */
  271. var _newCanvas = function(clazz, parent, uuid) {
  272. var canvas = document.createElement("canvas");
  273. _appendElement(canvas, parent);
  274. canvas.style.position="absolute";
  275. if (clazz) { canvas.className=clazz; }
  276. // set an id. if no id on the element and if uuid was supplied it will be used, otherwise we'll create one.
  277. _getId(canvas, uuid);
  278. if (ie) {
  279. // for IE we have to set a big canvas size. actually you can override this, too, if 1200 pixels
  280. // is not big enough for the biggest connector/endpoint canvas you have at startup.
  281. jsPlumb.sizeCanvas(canvas, 0, 0, DEFAULT_NEW_CANVAS_SIZE, DEFAULT_NEW_CANVAS_SIZE);
  282. canvas = G_vmlCanvasManager.initElement(canvas);
  283. }
  284. return canvas;
  285. };
  286. /**
  287. * performs the given function operation on all the connections found for the given element
  288. * id; this means we find all the endpoints for the given element, and then for each endpoint
  289. * find the connectors connected to it. then we pass each connection in to the given
  290. * function.
  291. */
  292. var _operation = function(elId, func) {
  293. var endpoints = endpointsByElement[elId];
  294. if (endpoints && endpoints.length) {
  295. for (var i = 0; i < endpoints.length; i++) {
  296. for (var j = 0; j < endpoints[i].connections.length; j++) {
  297. var retVal = func(endpoints[i].connections[j]);
  298. // if the function passed in returns true, we exit.
  299. // most functions return false.
  300. if (retVal) return;
  301. }
  302. }
  303. }
  304. };
  305. /**
  306. * perform an operation on all elements.
  307. */
  308. var _operationOnAll = function(func) {
  309. for (var elId in endpointsByElement) {
  310. _operation(elId, func);
  311. }
  312. };
  313. /**
  314. * helper to remove an element from the DOM.
  315. */
  316. var _removeElement = function(element, parent) {
  317. if (element != null) {
  318. if (!parent) {
  319. try { document.body.removeChild(element); }
  320. catch (e) { }
  321. } else {
  322. jsPlumb.CurrentLibrary.removeElement(element, parent);
  323. }
  324. }
  325. };
  326. /**
  327. * helper to remove a list of elements from the DOM.
  328. */
  329. var _removeElements = function(elements, parent) {
  330. for (var i in elements)
  331. _removeElement(elements[i], parent);
  332. };
  333. /**
  334. * helper method to remove an item from a list.
  335. */
  336. var _removeFromList = function(map, key, value) {
  337. var l = map[key];
  338. if (l != null) {
  339. var i = _findIndex(l, value);
  340. if (i >= 0) {
  341. delete( l[i] );
  342. l.splice( i, 1 );
  343. return true;
  344. }
  345. }
  346. return false;
  347. };
  348. /**
  349. * Sets whether or not the given element(s) should be draggable, regardless of what a particular
  350. * plumb command may request.
  351. *
  352. * @param element May be a string, a jQuery elements, or a list of strings/jquery elements.
  353. * @param draggable Whether or not the given element(s) should be draggable.
  354. */
  355. var _setDraggable = function(element, draggable) {
  356. var _helper = function(el, id) {
  357. draggableStates[id] = draggable;
  358. if (jsPlumb.CurrentLibrary.isDragSupported(el)) {
  359. jsPlumb.CurrentLibrary.setDraggable(el, draggable);
  360. }
  361. };
  362. return _elementProxy(element, _helper);
  363. };
  364. /**
  365. * private method to do the business of hiding/showing.
  366. * @param el either Id of the element in question or a jquery object for the element.
  367. * @param state String specifying a value for the css 'display' property ('block' or 'none').
  368. */
  369. var _setVisible = function(el, state) {
  370. var elId = _getAttribute(el, "id");
  371. var f = function(jpc) {
  372. jpc.canvas.style.display = state;
  373. };
  374. _operation(elId, f);
  375. };
  376. /**
  377. * toggles the draggable state of the given element(s).
  378. * @param el either an id, or a jquery object, or a list of ids/jquery objects.
  379. */
  380. var _toggleDraggable = function(el) {
  381. var fn = function(el, elId) {
  382. var state = draggableStates[elId] == null ? _draggableByDefault : draggableStates[elId];
  383. state = !state;
  384. draggableStates[elId] = state;
  385. jsPlumb.CurrentLibrary.setDraggable(el, state);
  386. return state;
  387. };
  388. return _elementProxy(el, fn);
  389. };
  390. /**
  391. * private method to do the business of toggling hiding/showing.
  392. * @param elId Id of the element in question
  393. */
  394. var _toggleVisible = function(elId) {
  395. var f = function(jpc) {
  396. var state = ('none' == jpc.canvas.style.display);
  397. jpc.canvas.style.display = state ? "block" : "none";
  398. };
  399. _operation(elId, f);
  400. //todo this should call _elementProxy, and pass in the _operation(elId, f) call as a function. cos _toggleDraggable does that.
  401. };
  402. /**
  403. * updates the offset and size for a given element, and stores the values.
  404. * 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,
  405. * or if 'recalc' is true in order to force a recalculation, we use the offset, outerWidth and outerHeight methods to get
  406. * the current values.
  407. */
  408. var _updateOffset = function(elId, offset, recalc) {
  409. if (recalc || offset == null) { // if forced repaint or no offset available, we recalculate.
  410. // get the current size and offset, and store them
  411. var s = _getElementObject(elId);
  412. sizes[elId] = _getSize(s);
  413. offsets[elId] = _getOffset(s);
  414. } else {
  415. offsets[elId] = offset;
  416. }
  417. };
  418. /**
  419. * wraps one function with another, creating a placeholder for the wrapped function
  420. * if it was null. this is used to wrap the various drag/drop event functions - to allow
  421. * jsPlumb to be notified of important lifecycle events without imposing itself on the user's
  422. * drag/drop functionality.
  423. * TODO: determine whether or not we should support an error handler concept, if one of the functions fails.
  424. */
  425. var _wrap = function(wrappedFunction, newFunction) {
  426. wrappedFunction = wrappedFunction || function() { };
  427. newFunction = newFunction || function() { };
  428. return function() {
  429. var r = null;
  430. try { r = newFunction.apply(this, arguments); }
  431. catch (e) {
  432. _log('jsPlumb function failed : ' + e);
  433. }
  434. try { wrappedFunction.apply(this, arguments);
  435. }
  436. catch (e) {
  437. _log('wrapped function failed : ' + e);
  438. }
  439. return r;
  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. lastReturnValue = [ xy[0] + (self.x * wh[0]) + self.offsets[0], xy[1] + (self.y * wh[1]) + self.offsets[1]];
  471. var container = element? element.container : null;
  472. var containerAdjustment = {left:0, top:0 };
  473. if (container != null) {
  474. var eo = _getElementObject(container);
  475. var o = _getOffset(eo);
  476. var sl = jsPlumb.CurrentLibrary.getScrollLeft(eo);
  477. var st = jsPlumb.CurrentLibrary.getScrollTop(eo);
  478. containerAdjustment.left = o.left - sl;
  479. containerAdjustment.top = o.top - st;
  480. lastReturnValue[0] = lastReturnValue[0] - containerAdjustment.left;
  481. lastReturnValue[1] = lastReturnValue[1] - containerAdjustment.top;
  482. }
  483. return lastReturnValue;
  484. };
  485. this.getOrientation = function() { return orientation; };
  486. this.equals = function(anchor) {
  487. if (!anchor) return false;
  488. var ao = anchor.getOrientation();
  489. var o = this.getOrientation();
  490. 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];
  491. };
  492. };
  493. /**
  494. * An Anchor that floats. its orientation is computed dynamically from its position relative to the anchor it is floating relative to.
  495. *
  496. */
  497. var FloatingAnchor = function(params) {
  498. // this is the anchor that this floating anchor is referenced to for purposes of calculating the orientation.
  499. var ref = params.reference;
  500. // the canvas this refers to.
  501. var refCanvas = params.referenceCanvas;
  502. var size = _getSize(_getElementObject(refCanvas));
  503. // these are used to store the current relative position of our anchor wrt the reference anchor. they only indicate
  504. // direction, so have a value of 1 or -1 (or, very rarely, 0). these values are written by the compute method, and read
  505. // by the getOrientation method.
  506. var xDir = 0, yDir = 0;
  507. // temporary member used to store an orientation when the floating anchor is hovering over another anchor.
  508. var orientation = null;
  509. this.compute = function(xy, wh, txy, twh) {
  510. // set these for the getOrientation method to use.
  511. xDir = 0;//xy[0] < txy[0] ? -1 : xy[0] == txy[0] ? 0 : 1;
  512. yDir = 0;//xy[1] < txy[1] ? -1 : xy[1] == txy[1] ? 0 : 1;
  513. 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.
  514. };
  515. this.getOrientation = function() {
  516. if (orientation) return orientation;
  517. else {
  518. var o = ref.getOrientation();
  519. // here we take into account the orientation of the other anchor: if it declares zero for some direction, we declare zero too.
  520. // 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
  521. // like it makes sense. maybe this wont make sense.
  522. return [Math.abs(o[0]) * xDir * -1, Math.abs(o[1]) * yDir * -1];
  523. }
  524. };
  525. /**
  526. * notification the endpoint associated with this anchor is hovering over another anchor;
  527. * we want to assume that anchor's orientation for the duration of the hover.
  528. */
  529. this.over = function(anchor) {
  530. orientation = anchor.getOrientation();
  531. };
  532. /**
  533. * notification the endpoint associated with this anchor is no longer hovering
  534. * over another anchor; we should resume calculating orientation as we normally do.
  535. */
  536. this.out = function() {
  537. orientation = null;
  538. };
  539. };
  540. // ************** connection
  541. // ****************************************
  542. /**
  543. * allowed params:
  544. * source: source element (string or a jQuery element) (required)
  545. * target: target element (string or a jQuery element) (required)
  546. *
  547. * anchors: optional array of anchor placements. defaults to BottomCenter for source
  548. * and TopCenter for target.
  549. */
  550. var Connection = function(params) {
  551. // ************** get the source and target and register the connection. *******************
  552. var self = this;
  553. var id = new String('_jsplumb_c_' + (new Date()).getTime());
  554. this.getId = function() { return id; };
  555. this.container = params.container || _currentInstance.Defaults.Container; // may be null; we will append to the body if so.
  556. // get source and target as jQuery objects
  557. this.source = _getElementObject(params.source);
  558. this.target = _getElementObject(params.target);
  559. // sourceEndpoint and targetEndpoint override source/target, if they are present.
  560. if (params.sourceEndpoint)
  561. this.source = params.sourceEndpoint.getElement();
  562. if (params.targetEndpoint)
  563. this.target = params.targetEndpoint.getElement();
  564. this.sourceId = _getAttribute(this.source, "id");
  565. this.targetId = _getAttribute(this.target, "id");
  566. this.endpointsOnTop = params.endpointsOnTop != null ? params.endpointsOnTop : true;
  567. this.scope = params.scope; // scope may have been passed in to the connect call.
  568. // if it wasn't, we will pull it from the source endpoint,
  569. // after having initialised the endpoints.
  570. // init endpoints
  571. this.endpoints = [];
  572. this.endpointStyles = [];
  573. var prepareEndpoint = function(existing, index, params, element) {
  574. if (existing) {
  575. self.endpoints[index] = existing;
  576. existing.addConnection(self);
  577. }
  578. else {
  579. if(!params.endpoints) params.endpoints = [null,null];
  580. 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();
  581. if (!params.endpointStyles) params.endpointStyles = [null,null];
  582. var es = params.endpointStyles[index] || params.endpointStyle || _currentInstance.Defaults.EndpointStyles[index] || jsPlumb.Defaults.EndpointStyles[index] || _currentInstance.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle;
  583. var a = params.anchors ? params.anchors[index] : _currentInstance.Defaults.Anchors[index] || jsPlumb.Defaults.Anchors[index] || _currentInstance.Defaults.Anchor || jsPlumb.Defaults.Anchor || jsPlumb.Anchors.BottomCenter;
  584. var u = params.uuids ? params.uuids[index] : null;
  585. if(params.uuids)
  586. var e = new Endpoint({style:es, endpoint:ep, connections:[self], uuid:u, anchor:a, source:element, container:self.container });
  587. else
  588. var e = new Endpoint({style:es, endpoint:ep, connections:[self], anchor:a, source:element, container:self.container });
  589. self.endpoints[index] = e;
  590. return e;
  591. }
  592. };
  593. var eS = prepareEndpoint(params.sourceEndpoint, 0, params, self.source);
  594. if (eS) _addToList(endpointsByElement, this.sourceId, eS);
  595. var eT = prepareEndpoint(params.targetEndpoint, 1, params, self.target);
  596. if (eT) _addToList(endpointsByElement, this.targetId, eT);
  597. // if scope not set, set it to be the scope for the source endpoint.
  598. if (!this.scope) this.scope = this.endpoints[0].scope;
  599. this.connector = this.endpoints[0].connector || this.endpoints[1].connector || params.connector || _currentInstance.Defaults.Connector || jsPlumb.Defaults.Connector || new jsPlumb.Connectors.Bezier();
  600. this.paintStyle = this.endpoints[0].connectorStyle || this.endpoints[1].connectorStyle || params.paintStyle || _currentInstance.Defaults.PaintStyle || jsPlumb.Defaults.PaintStyle;
  601. _updateOffset(this.sourceId);
  602. _updateOffset(this.targetId);
  603. // paint the endpoints
  604. var myOffset = offsets[this.sourceId], myWH = sizes[this.sourceId];
  605. var anchorLoc = this.endpoints[0].anchor.compute([myOffset.left, myOffset.top], myWH, this.endpoints[0]);
  606. this.endpoints[0].paint(anchorLoc);
  607. myOffset = offsets[this.targetId]; myWH = sizes[this.targetId];
  608. anchorLoc = this.endpoints[1].anchor.compute([myOffset.left, myOffset.top], myWH, this.endpoints[1]);
  609. this.endpoints[1].paint(anchorLoc);
  610. // *************** create canvas on which the connection will be drawn ************
  611. var canvas = _newCanvas(jsPlumb.connectorClass, self.container);
  612. this.canvas = canvas;
  613. /**
  614. * paints the connection.
  615. * @param elId Id of the element that is in motion
  616. * @param ui current library's event system ui object (present if we came from a drag to get here)
  617. * @param recalc whether or not to recalculate element sizes. this is true if a repaint caused this to be painted.
  618. */
  619. this.paint = function(elId, ui, recalc, timestamp) {
  620. if (log) log.debug("Painting Connection; element in motion is " + elId + "; ui is [" + ui + "]; recalc is [" + recalc + "]");
  621. var fai = self.floatingAnchorIndex;
  622. // if the moving object is not the source we must transpose the two references.
  623. var swap = !(elId == this.sourceId);
  624. var tId = swap ? this.sourceId : this.targetId, sId = swap ? this.targetId : this.sourceId;
  625. var tIdx = swap ? 0 : 1, sIdx = swap ? 1 : 0;
  626. var el = swap ? this.target : this.source;
  627. if (this.canvas.getContext) {
  628. //if (recalc) {
  629. _updateOffset(elId, ui, recalc);
  630. _updateOffset(tId); // update the target if this is a forced repaint. otherwise, only the source has been moved.
  631. //}
  632. var myOffset = offsets[elId];
  633. var otherOffset = offsets[tId];
  634. var myWH = sizes[elId];
  635. var otherWH = sizes[tId];
  636. var ctx = canvas.getContext('2d');
  637. //TODO: why are these calculated again? they were calculated in the _draw function.
  638. var sAnchorP = this.endpoints[sIdx].anchor.compute([myOffset.left, myOffset.top], myWH, this.endpoints[sIdx]);
  639. var sAnchorO = this.endpoints[sIdx].anchor.getOrientation();
  640. var tAnchorP = this.endpoints[tIdx].anchor.compute([otherOffset.left, otherOffset.top], otherWH, this.endpoints[tIdx]);
  641. var tAnchorO = this.endpoints[tIdx].anchor.getOrientation();
  642. var dim = this.connector.compute(sAnchorP, tAnchorP, this.endpoints[sIdx].anchor, this.endpoints[tIdx].anchor, this.paintStyle.lineWidth);
  643. jsPlumb.sizeCanvas(canvas, dim[0], dim[1], dim[2], dim[3]);
  644. jsPlumb.extend(ctx, this.paintStyle);
  645. if (this.paintStyle.gradient && !ie) {
  646. var g = swap ? ctx.createLinearGradient(dim[4], dim[5], dim[6], dim[7]) : ctx.createLinearGradient(dim[6], dim[7], dim[4], dim[5]);
  647. for (var i = 0; i < this.paintStyle.gradient.stops.length; i++)
  648. g.addColorStop(this.paintStyle.gradient.stops[i][0],this.paintStyle.gradient.stops[i][1]);
  649. ctx.strokeStyle = g;
  650. }
  651. this.connector.paint(dim, ctx);
  652. }
  653. };
  654. //TODO: should this take a timestamp? probably. it reduces the amount of time
  655. // spent figuring out anchor locations.
  656. this.repaint = function() {
  657. this.paint(this.sourceId, null, true);
  658. };
  659. _initDraggableIfNecessary(self.source, params.draggable, params.dragOptions);
  660. _initDraggableIfNecessary(self.target, params.draggable, params.dragOptions);
  661. // resizing (using the jquery.ba-resize plugin). todo: decide whether to include or not.
  662. if (this.source.resize) {
  663. this.source.resize(function(e) {
  664. jsPlumb.repaint(self.sourceId);
  665. });
  666. }
  667. };
  668. /*
  669. Class: Endpoint
  670. Models an endpoint. Can have one to N connections emanating from it (although how to handle that in the UI is
  671. a very good question). also has a Canvas and paint style.
  672. */
  673. /*
  674. Function: Endpoint
  675. This is the Endpoint class constructor.
  676. Parameters:
  677. anchor - anchor for the endpoint, of type jsPlumb.Anchor. may be null.
  678. endpoint - endpoint object, of type jsPlumb.Endpoint. may be null.
  679. style - endpoint style, a js object. may be null.
  680. source - element the endpoint is attached to, of type jquery object. Required.
  681. canvas - canvas element to use. may be, and most often is, null.
  682. connections - optional list of connections to configure the endpoint with.
  683. isSource - boolean. indicates the endpoint can act as a source of new connections. optional.
  684. dragOptions - if isSource is set to true, you can supply arguments for the jquery draggable method. optional.
  685. connectionStyle - if isSource is set to true, this is the paint style for connections from this endpoint. optional.
  686. connector - optional connector type to use.
  687. isTarget - boolean. indicates the endpoint can act as a target of new connections. optional.
  688. dropOptions - if isTarget is set to true, you can supply arguments for the jquery droppable method. optional.
  689. reattach - optional boolean that determines whether or not the connections reattach after they
  690. have been dragged off an endpoint and left floating. defaults to false: connections
  691. dropped in this way will just be deleted.
  692. */
  693. var Endpoint = function(params) {
  694. params = params || {};
  695. // make a copy. then we can use the wrapper function.
  696. jsPlumb.extend({}, params);
  697. var self = this;
  698. var id = new String('_jsplumb_e_' + (new Date()).getTime());
  699. this.getId = function() { return id; };
  700. self.anchor = params.anchor || jsPlumb.Anchors.TopCenter;
  701. var _endpoint = params.endpoint || new jsPlumb.Endpoints.Dot();
  702. var _style = params.style || _currentInstance.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle;
  703. this.connectorStyle = params.connectorStyle;
  704. this.connector = params.connector;
  705. this.isSource = params.isSource || false;
  706. this.isTarget = params.isTarget || false;
  707. var _element = params.source;
  708. var _uuid = params.uuid;
  709. if (_uuid) endpointsByUUID[_uuid] = self;
  710. this.container = params.container || jsPlumb.Defaults.Container;
  711. var _elementId = _getAttribute(_element, "id");
  712. var _maxConnections = params.maxConnections || 1; // maximum number of connections this endpoint can be the source of.
  713. this.canvas = params.canvas || _newCanvas(jsPlumb.endpointClass, this.container, params.uuid);
  714. this.connections = params.connections || [];
  715. this.scope = params.scope || DEFAULT_SCOPE;
  716. var _reattach = params.reattach || false;
  717. var floatingEndpoint = null;
  718. var inPlaceCopy = null;
  719. this.dragAllowedWhenFull = params.dragAllowedWhenFull || true;
  720. this.addConnection = function(connection) {
  721. self.connections.push(connection);
  722. };
  723. this.detach = function(connection) {
  724. var idx = _findIndex(self.connections, connection);
  725. if (idx >= 0) {
  726. var t = connection.endpoints[0] == self ? connection.endpoints[1] : connection.endpoints[0];
  727. self.connections.splice(idx, 1);
  728. t.detach(connection);
  729. }
  730. };
  731. /**
  732. * detaches all connections this Endpoint has
  733. */
  734. this.detachAll = function() {
  735. while(self.connections.length > 0) {
  736. self.detach(self.connections[0]);
  737. }
  738. };
  739. /**
  740. * removes any connections from this Endpoint that are
  741. * connected to the given target endpoint.
  742. * @param targetEndpoint
  743. */
  744. this.detachFrom = function(targetEndpoint) {
  745. var c = [];
  746. for (var i = 0; i < self.connections.length; i++) {
  747. if (self.connections[i].endpoints[1] == targetEndpoint || self.connections[i].endpoints[0] == targetEndpoint) {
  748. c.push(self.connections[i]);
  749. }
  750. }
  751. for (var i = 0; i < c.length; i++) {
  752. targetEndpoint.detach(c[i]);
  753. self.detach(c[i]);
  754. }
  755. };
  756. /**
  757. * returns the DOM element this Endpoint is attached to.
  758. * @returns
  759. */
  760. this.getElement = function() { return _element; };
  761. /**
  762. * returns the UUID for this Endpoint, if there is one.
  763. * @returns
  764. */
  765. this.getUuid= function() { return _uuid; };
  766. /**
  767. * private but must be exposed.
  768. */
  769. this.makeInPlaceCopy = function() {
  770. var e = new Endpoint({anchor:self.anchor, source:_element, style:_style, endpoint:_endpoint});
  771. return e;
  772. };
  773. /**
  774. * returns whether or not this endpoint is connected to the given endpoint.
  775. * @param endpoint Endpoint to test.
  776. * @since 1.1.1
  777. *
  778. * todo: needs testing. will this work if the endpoint passed in is the source?
  779. */
  780. this.isConnectedTo = function(endpoint) {
  781. var found = false;
  782. if (endpoint) {
  783. for (var i = 0; i < self.connections.length; i++) {
  784. if (self.connections[i].endpoints[1] == endpoint) {
  785. found = true;
  786. break;
  787. }
  788. }
  789. }
  790. return found;
  791. };
  792. /**
  793. * private but needs to be exposed.
  794. * @returns {Boolean}
  795. */
  796. this.isFloating = function() { return floatingEndpoint != null; };
  797. /**
  798. * first pass at default ConnectorSelector: returns the first connection, if we have any.
  799. * modified a little, 5/10/2010: now it only returns a connector if we have not got equal to or more than _maxConnector connectors
  800. * 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
  801. * one. what if this could return a list? that implies everything should work with a list - dragging etc. could be nasty. could also
  802. * be cool.
  803. */
  804. var connectorSelector = function() {
  805. //return self.connections.length == 0 || self.connections.length < _maxConnections ? null : self.connections[0];
  806. if (self.connections.length < _maxConnections) return null;
  807. //else if (self.connections.length == _maxConnections) return false;
  808. else return self.connections[0];
  809. };
  810. /**
  811. * @returns whether or not the Endpoint can accept any more Connections.
  812. */
  813. this.isFull = function() { return _maxConnections < 1 ? false : (self.connections.length >= _maxConnections); };
  814. /**
  815. * sets whether or not connnctions can be dragged from this Endpoint once it is full. you would use
  816. * this in a UI in which you're going to provide some other way of breaking connections, if you need
  817. * to break them at all. this property is by default true; use it in conjunction with the 'reattach'
  818. * option on a connect call.
  819. */
  820. this.setDragAllowedWhenFull = function(allowed) {
  821. self.dragAllowedWhenFull = allowed;
  822. };
  823. /**
  824. * a deep equals check. everything must match, including the anchor, styles, everything.
  825. * TODO: finish Endpoint.equals
  826. */
  827. this.equals = function(endpoint) {
  828. return this.anchor.equals(endpoint.anchor) &&
  829. true;
  830. };
  831. /**
  832. * paints the Endpoint, recalculating offset and anchor positions if necessary.
  833. */
  834. this.paint = function(anchorPoint, connectorPaintStyle, canvas) {
  835. if (log) log.debug("Painting Endpoint with elementId [" + _elementId + "]; anchorPoint is [" + anchorPoint + "]");
  836. if (anchorPoint == null) {
  837. // do we always want to force a repaint here? i dont think so!
  838. var xy = offsets[_elementId];
  839. var wh = sizes[_elementId];
  840. if (xy == null || wh == null) {
  841. _updateOffset(_elementId);
  842. xy = offsets[_elementId];
  843. wh = sizes[_elementId];
  844. }
  845. anchorPoint = self.anchor.compute([xy.left, xy.top], wh, self);
  846. }
  847. _endpoint.paint(anchorPoint, self.anchor.getOrientation(), canvas || self.canvas, _style, connectorPaintStyle || _style);
  848. };
  849. /**
  850. * @deprecated
  851. */
  852. this.removeConnection = this.detach; // backwards compatibility
  853. // is this a connection source? we make it draggable and have the drag listener
  854. // maintain a connection with a floating endpoint.
  855. if (params.isSource && jsPlumb.CurrentLibrary.isDragSupported(_element)) {
  856. var n = null, id = null, jpc = null, existingJpc = false, existingJpcParams = null;
  857. // 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
  858. // to select one connection from the list, or drag them all. if we had a pluggable 'ConnectorSelector' interface we could probably
  859. // 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
  860. // information the interface needs from jsPlumb at execution time. if, however, we leave out the connector selection, and drag them all,
  861. // that wouldn't be too hard to organise. perhaps that behaviour would be on a switch for the endpoint, or perhaps the ConnectorSelector
  862. // interface returns a List, with the default implementation just returning everything. i think i like that.
  863. //
  864. // 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.
  865. // the major difference between that case and the case of a new endpoint is actually quite small - it's a question of where the
  866. // Connection comes from. for a new one, we create a new one. obviously. otherwise we just get the jpc from the Endpoint
  867. // (remember we're only assuming one connection right now). so all of the UI stuff we do to create the floating endpoint etc
  868. // 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
  869. // 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.
  870. var start = function() {
  871. jpc = connectorSelector();
  872. if (self.isFull() && !self.dragAllowedWhenFull) return false;
  873. inPlaceCopy = self.makeInPlaceCopy();
  874. inPlaceCopy.paint();
  875. n = document.createElement("div");
  876. var nE = _getElementObject(n);
  877. _appendElement(n); //
  878. // create and assign an id, and initialize the offset.
  879. //TODO can't this be replaced with a _getId call?
  880. var id = "" + new String(new Date().getTime());
  881. _setAttribute(nE, "id", id);
  882. _updateOffset(id);
  883. // store the id of the dragging div and the source element. the drop function
  884. // will pick these up.
  885. _setAttribute(_getElementObject(self.canvas), "dragId", id);
  886. _setAttribute(_getElementObject(self.canvas), "elId", _elementId);
  887. // create a floating anchor
  888. var floatingAnchor = new FloatingAnchor({reference:self.anchor, referenceCanvas:self.canvas});
  889. floatingEndpoint = new Endpoint({ style:{fillStyle:'rgba(0,0,0,0)'}, endpoint:_endpoint, anchor:floatingAnchor, source:nE });
  890. if (jpc == null) {
  891. // create a connection. one end is this endpoint, the other is a floating endpoint.
  892. jpc = new Connection({
  893. sourceEndpoint:self,
  894. targetEndpoint:floatingEndpoint,
  895. source:_getElementObject(_element),
  896. target:_getElementObject(n),
  897. anchors:[self.anchor, floatingAnchor],
  898. paintStyle : params.connectorStyle, // this can be null. Connection will use the default.
  899. connector: params.connector
  900. });
  901. } else {
  902. existingJpc = true;
  903. var anchorIdx = jpc.sourceId == _elementId ? 0 : 1;
  904. jpc.floatingAnchorIndex = anchorIdx;
  905. // probably we should remove the connection? and add it back if the user
  906. // does not drop it somewhere proper.
  907. self.removeConnection(jpc);
  908. if (anchorIdx == 0){
  909. existingJpcParams = [jpc.source, jpc.sourceId];
  910. jpc.source = _getElementObject(n);
  911. jpc.sourceId = id;
  912. }else {
  913. existingJpcParams = [jpc.target, jpc.targetId];
  914. jpc.target = _getElementObject(n);
  915. jpc.targetId = id;
  916. }
  917. jpc.suspendedEndpoint = jpc.endpoints[anchorIdx];
  918. jpc.endpoints[anchorIdx] = floatingEndpoint;
  919. }
  920. // register it.
  921. floatingConnections[id] = jpc;
  922. // todo unregister on stop
  923. floatingEndpoint.addConnection(jpc);
  924. // only register for the target endpoint; we will not be dragging the source at any time
  925. // before this connection is either discarded or made into a permanent connection.
  926. _addToList(endpointsByElement, id, floatingEndpoint);
  927. };
  928. var dragOptions = params.dragOptions || { };
  929. var defaultOpts = jsPlumb.extend({}, jsPlumb.CurrentLibrary.defaultDragOptions);
  930. dragOptions = jsPlumb.extend(defaultOpts, dragOptions);
  931. dragOptions.scope = dragOptions.scope || self.scope;
  932. var startEvent = jsPlumb.CurrentLibrary.dragEvents['start'];
  933. var stopEvent = jsPlumb.CurrentLibrary.dragEvents['stop'];
  934. var dragEvent = jsPlumb.CurrentLibrary.dragEvents['drag'];
  935. dragOptions[startEvent] = _wrap(dragOptions[startEvent], start);
  936. dragOptions[dragEvent] = _wrap(dragOptions[dragEvent], function() {
  937. var _ui = jsPlumb.CurrentLibrary.getUIPosition(arguments);
  938. _draw(_getElementObject(n), _ui);
  939. });
  940. dragOptions[stopEvent] = _wrap(dragOptions[stopEvent],
  941. function() {
  942. _removeFromList(endpointsByElement, id, floatingEndpoint);
  943. _removeElements([n, floatingEndpoint.canvas]); // TODO: clean up the connection canvas (if the user aborted)
  944. _removeElement(inPlaceCopy.canvas, _element);
  945. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  946. if (jpc.endpoints[idx] == floatingEndpoint) {
  947. // if the connection was an existing one:
  948. if (existingJpc && jpc.suspendedEndpoint) {
  949. if (_reattach) {
  950. jpc.floatingAnchorIndex = null;
  951. if (idx == 0) {
  952. jpc.source = existingJpcParams[0];
  953. jpc.sourceId = existingJpcParams[1];
  954. } else {
  955. jpc.target = existingJpcParams[0];
  956. jpc.targetId = existingJpcParams[1];
  957. }
  958. jpc.endpoints[idx] = jpc.suspendedEndpoint;
  959. jpc.suspendedEndpoint.addConnection(jpc);
  960. jsPlumb.repaint(existingJpcParams[1]);
  961. }
  962. else {
  963. jpc.endpoints[0].removeConnection(jpc);
  964. jpc.endpoints[1].removeConnection(jpc);
  965. _removeElement(jpc.canvas);
  966. _removeFromList(connectionsByScope, jpc.scope, jpc);
  967. _fireEvent("jsPlumbConnectionDetached", {
  968. source:jpc.source,
  969. target:jpc.target,
  970. sourceId:jpc.sourceId,
  971. targetId:jpc.targetId,
  972. sourceEndpoint:jpc.endpoints[0],
  973. targetEndpoint:jpc.endpoints[1]
  974. });
  975. }
  976. } else {
  977. _removeElement(jpc.canvas);
  978. //if (jpc.endpoints[1]) alert("target set");
  979. self.removeConnection(jpc);
  980. }
  981. }
  982. jpc = null;
  983. delete floatingEndpoint;
  984. delete inPlaceCopy;
  985. self.paint();
  986. }
  987. );
  988. var i = _getElementObject(self.canvas);
  989. jsPlumb.CurrentLibrary.initDraggable(i, dragOptions);
  990. };
  991. // connector target
  992. if (params.isTarget && jsPlumb.CurrentLibrary.isDropSupported(_element)) {
  993. var dropOptions = params.dropOptions || _currentInstance.Defaults.DropOptions || jsPlumb.Defaults.DropOptions;
  994. dropOptions = jsPlumb.extend({}, dropOptions);
  995. dropOptions.scope = dropOptions.scope || self.scope;
  996. var originalAnchor = null;
  997. var dropEvent = jsPlumb.CurrentLibrary.dragEvents['drop'];
  998. var overEvent = jsPlumb.CurrentLibrary.dragEvents['over'];
  999. var outEvent = jsPlumb.CurrentLibrary.dragEvents['out'];
  1000. dropOptions[dropEvent]= _wrap(dropOptions[dropEvent], function() {
  1001. var draggable = jsPlumb.CurrentLibrary.getDragObject(arguments);
  1002. var id = _getAttribute(_getElementObject(draggable), "dragId");
  1003. var elId = _getAttribute(_getElementObject(draggable),"elId");
  1004. var jpc = floatingConnections[id];
  1005. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  1006. var oidx = idx == 0 ? 1 : 0;
  1007. if (!self.isFull() && !(idx == 0 && !self.isSource) && !(idx == 1 && !self.isTarget)) {
  1008. if (idx == 0) {
  1009. jpc.source = _element;
  1010. jpc.sourceId = _elementId;
  1011. } else {
  1012. jpc.target = _element;
  1013. jpc.targetId = _elementId;
  1014. }
  1015. //todo test that the target is not full.
  1016. // remove this jpc from the current endpoint
  1017. jpc.endpoints[idx].removeConnection(jpc);
  1018. if (jpc.suspendedEndpoint)
  1019. jpc.suspendedEndpoint.removeConnection(jpc);
  1020. jpc.endpoints[idx] = self;
  1021. self.addConnection(jpc);
  1022. // add the jpc to the other endpoint too.
  1023. jpc.endpoints[oidx].addConnection(jpc);
  1024. _addToList(connectionsByScope, jpc.scope, jpc);
  1025. _initDraggableIfNecessary(_element, params.draggable, {});
  1026. jsPlumb.repaint(elId);
  1027. _fireEvent("jsPlumbConnection", {
  1028. source:jpc.source,
  1029. target:jpc.target,
  1030. sourceId:jpc.sourceId,
  1031. targetId:jpc.targetId,
  1032. sourceEndpoint:jpc.endpoints[0],
  1033. targetEndpoint:jpc.endpoints[1]
  1034. });
  1035. }
  1036. // else there must be some cleanup required.
  1037. delete floatingConnections[id];
  1038. });
  1039. dropOptions[overEvent]= _wrap(dropOptions[overEvent], function() {
  1040. var draggable = jsPlumb.CurrentLibrary.getDragObject(arguments);
  1041. var id = _getAttribute(_getElementObject(draggable),"dragId");
  1042. var jpc = floatingConnections[id];
  1043. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  1044. jpc.endpoints[idx].anchor.over(self.anchor);
  1045. });
  1046. dropOptions[outEvent] = _wrap(dropOptions[outEvent], function() {
  1047. var draggable = jsPlumb.CurrentLibrary.getDragObject(arguments);
  1048. var id = _getAttribute(_getElementObject(draggable),"dragId");
  1049. var jpc = floatingConnections[id];
  1050. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  1051. jpc.endpoints[idx].anchor.out();
  1052. });
  1053. jsPlumb.CurrentLibrary.initDroppable(_getElementObject(self.canvas), dropOptions);
  1054. }
  1055. return self;
  1056. };
  1057. /*
  1058. Property: Defaults
  1059. These are the default settings for jsPlumb, that is what will be used if you do not supply specific pieces of information
  1060. 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
  1061. somewhere after the jsPlumb include, but before you make any calls to jsPlumb, for instance in this example we set the PaintStyle to be
  1062. a blue line of 27 pixels:
  1063. > jsPlumb.Defaults.PaintStyle = { lineWidth:27, strokeStyle:'blue' }
  1064. */
  1065. this.Defaults = {
  1066. Anchor : null,
  1067. Anchors : [ null, null ],
  1068. Connector : null,
  1069. Container : null,
  1070. DragOptions: { },
  1071. DropOptions: { },
  1072. Endpoint : null,
  1073. Endpoints : [ null, null ],
  1074. EndpointStyle : { fillStyle : null },
  1075. EndpointStyles : [ null, null ],
  1076. MaxConnections : null,
  1077. PaintStyle : { lineWidth : 10, strokeStyle : 'red' },
  1078. Scope : "_jsPlumb_DefaultScope"
  1079. };
  1080. /*
  1081. Property: connectorClass
  1082. 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.
  1083. */
  1084. this.connectorClass = '_jsPlumb_connector';
  1085. /*
  1086. Property: endpointClass
  1087. 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.
  1088. */
  1089. this.endpointClass = '_jsPlumb_endpoint';
  1090. /*
  1091. Property: Anchors
  1092. Default jsPlumb Anchors. These are supplied in the file jsPlumb-defaults-x.x.x.js, which is merged in with the main jsPlumb script
  1093. 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:
  1094. > jsPlumb.Anchors.MyAnchor = { ....anchor code here. see the documentation. }
  1095. */
  1096. this.Anchors = {};
  1097. /*
  1098. Property: Connectors
  1099. Default jsPlumb Connectors. These are supplied in the file jsPlumb-defaults-x.x.x.js, which is merged in with the main jsPlumb script
  1100. 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:
  1101. > jsPlumb.Connectors.MyConnector = { ....connector code here. see the documentation. }
  1102. */
  1103. this.Connectors = {};
  1104. /*
  1105. Property: Endpoints
  1106. Default jsPlumb Endpoints. These are supplied in the file jsPlumb-defaults-x.x.x.js, which is merged in with the main jsPlumb script
  1107. 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:
  1108. > jsPlumb.Endpoints.MyEndpoint = { ....endpoint code here. see the documentation. }
  1109. */
  1110. this.Endpoints = {};
  1111. /*
  1112. Function: addEndpoint
  1113. Adds an Endpoint to a given element.
  1114. Parameters:
  1115. target - Element to add the endpoint to. either an element id, or a selector representing some element.
  1116. params - Object containing Endpoint options (more info required)
  1117. Returns:
  1118. The newly created Endpoint.
  1119. See Also:
  1120. <addEndpoints>
  1121. */
  1122. this.addEndpoint = function(target, params) {
  1123. params = jsPlumb.extend({}, params);
  1124. var el = _getElementObject(target);
  1125. var id = _getAttribute(el,"id");
  1126. params.source = el;
  1127. _updateOffset(id);
  1128. var e = new Endpoint(params);
  1129. _addToList(endpointsByElement, id, e);
  1130. var myOffset = offsets[id];
  1131. var myWH = sizes[id];
  1132. var anchorLoc = e.anchor.compute([myOffset.left, myOffset.top], myWH, e);
  1133. e.paint(anchorLoc);
  1134. return e;
  1135. };
  1136. /*
  1137. Function: addEndpoint
  1138. Adds a list of Endpoints to a given element.
  1139. Parameters:
  1140. target - element to add the endpoint to. either an element id, or a selector representing some element.
  1141. endpoints - List of objects containing Endpoint options. one Endpoint is created for each entry in this list.
  1142. Returns:
  1143. List of newly created Endpoints, one for each entry in the 'endpoints' argument.
  1144. See Also:
  1145. <addEndpoint>
  1146. */
  1147. this.addEndpoints = function(target, endpoints) {
  1148. var results = [];
  1149. for (var i = 0; i < endpoints.length; i++) {
  1150. results.push(jsPlumb.addEndpoint(target, endpoints[i]));
  1151. }
  1152. return results;
  1153. };
  1154. /*
  1155. Function: animate
  1156. Wrapper around supporting library's animate function; injects a call to jsPlumb in the 'step' function (creating it if necessary).
  1157. This only supports the two-arg version of the animate call in jQuery - the one that takes an 'options' object as the second arg.
  1158. MooTools has only one method - a two arg one. Which is handy.
  1159. Parameters:
  1160. el - Element to animate. Either an id, or a selector representing the element.
  1161. properties - The 'properties' argument you want passed to the standard jQuery animate call.
  1162. options - The 'options' argument you want passed to the standard jQuery animate call.
  1163. Returns:
  1164. void
  1165. */
  1166. this.animate = function(el, properties, options) {
  1167. var ele = _getElementObject(el);
  1168. var id = _getAttribute(el,"id");
  1169. //TODO this is not agnostic yet.
  1170. options = options || {};
  1171. var stepFunction = jsPlumb.CurrentLibrary.dragEvents['step'];
  1172. var completeFunction = jsPlumb.CurrentLibrary.dragEvents['complete'];
  1173. options[stepFunction] = _wrap(options[stepFunction], function()
  1174. {
  1175. _currentInstance.repaint(id);
  1176. });
  1177. // you know, probably onComplete should repaint too. that will help keep up
  1178. // with fast animations.
  1179. options[completeFunction] = _wrap(options[completeFunction], function()
  1180. {
  1181. _currentInstance.repaint(id);
  1182. });
  1183. jsPlumb.CurrentLibrary.animate(ele, properties, options);
  1184. };
  1185. /**
  1186. * clears the cache used to lookup elements by their id. if you remove any elements
  1187. * from the DOM you should call this to ensure that if you add them back in jsPlumb does not
  1188. * have a stale handle.
  1189. */
  1190. this.clearCache = function() {
  1191. delete cached;
  1192. cached = {};
  1193. };
  1194. /*
  1195. Function: connect
  1196. Establishes a connection between two elements.
  1197. Parameters:
  1198. params - Object containing setup for the connection. see documentation.
  1199. Returns:
  1200. The newly created Connection.
  1201. */
  1202. this.connect = function(params) {
  1203. var _p = jsPlumb.extend({}, params);
  1204. // test for endpoint uuids to connect
  1205. if (params.uuids) {
  1206. var _resolveByUuid = function(idx) {
  1207. var e = _getEndpoint(params.uuids[idx]);
  1208. if (!e) throw ("Endpoint with UUID " + params.uuids[idx] + " not found.");
  1209. return e;
  1210. };
  1211. _p.sourceEndpoint = _resolveByUuid(0);
  1212. _p.targetEndpoint = _resolveByUuid(1);
  1213. }
  1214. // now ensure that if we do have Endpoints already, they're not full.
  1215. if (_p.sourceEndpoint && _p.sourceEndpoint.isFull()) {
  1216. _log("could not add connection; source endpoint is full");
  1217. return;
  1218. }
  1219. if (_p.targetEndpoint && _p.targetEndpoint.isFull()) {
  1220. _log("could not add connection; target endpoint is full");
  1221. return;
  1222. }
  1223. var jpc = new Connection(_p);
  1224. // add to list of connections (by scope).
  1225. _addToList(connectionsByScope, jpc.scope, jpc);
  1226. // fire an event
  1227. _fireEvent("jsPlumbConnection", {
  1228. source:jpc.source,
  1229. target:jpc.target,
  1230. sourceId:jpc.sourceId,
  1231. targetId:jpc.targetId,
  1232. sourceEndpoint:jpc.endpoints[0],
  1233. targetEndpoint:jpc.endpoints[1]
  1234. });
  1235. // force a paint
  1236. _draw(jpc.source);
  1237. return jpc;
  1238. };
  1239. var fireDetachEvent = function(jpc) {
  1240. _fireEvent("jsPlumbConnectionDetached", {
  1241. source:jpc.source,
  1242. target:jpc.target,
  1243. sourceId:jpc.sourceId,
  1244. targetId:jpc.targetId,
  1245. sourceEndpoint:jpc.endpoints[0],
  1246. targetEndpoint:jpc.endpoints[1]
  1247. });
  1248. };
  1249. /*
  1250. Function: detach
  1251. Removes a connection.
  1252. Parameters:
  1253. sourceId - Id of the first element in the connection. A String.
  1254. targetId - iI of the second element in the connection. A String.
  1255. Returns:
  1256. true if successful, false if not.
  1257. */
  1258. this.detach = function(sourceId, targetId) {
  1259. if (arguments.length == 2) {
  1260. var f = function(jpc) {
  1261. if ((jpc.sourceId == sourceId && jpc.targetId == targetId) || (jpc.targetId == sourceId && jpc.sourceId == targetId)) {
  1262. _removeElement(jpc.canvas, jpc.container);
  1263. jpc.endpoints[0].removeConnection(jpc);
  1264. jpc.endpoints[1].removeConnection(jpc);
  1265. _removeFromList(connectionsByScope, jpc.scope, jpc);
  1266. fireDetachEvent(jpc);
  1267. return true;
  1268. }
  1269. };
  1270. // todo: how to cleanup the actual storage? a third arg to _operation?
  1271. _operation(sourceId, f);
  1272. }
  1273. // this is the new version of the method, taking a JS object like the connect method does.
  1274. else if (arguments.length == 1) {
  1275. var _p = jsPlumb.extend({}, sourceId); // a backwards compatibility hack: sourceId should be thought of as 'params' in this case.
  1276. // test for endpoint uuids to detach
  1277. if (_p.uuids) {
  1278. _getEndpoint(_p.uuids[0]).detachFrom(_getEndpoint(_p.uuids[1]));
  1279. }
  1280. else if (_p.sourceEndpoint && _p.targetEndpoint) {
  1281. _p.sourceEndpoint.detachFrom(_p.targetEndpoint);
  1282. }
  1283. else {
  1284. sourceId = _getId(_p.source);
  1285. targetId = _getId(_p.target);
  1286. var f = function(jpc) {
  1287. if ((jpc.sourceId == sourceId && jpc.targetId == targetId) || (jpc.targetId == sourceId && jpc.sourceId == targetId)) {
  1288. _removeElement(jpc.canvas, jpc.container);
  1289. jpc.endpoints[0].removeConnection(jpc);
  1290. jpc.endpoints[1].removeConnection(jpc);
  1291. _removeFromList(connectionsByScope, jpc.scope, jpc);
  1292. fireDetachEvent(jpc);
  1293. return true;
  1294. }
  1295. };
  1296. // todo: how to cleanup the actual storage? a third arg to _operation?
  1297. _operation(sourceId, f);
  1298. }
  1299. // fire an event
  1300. /*_fireEvent("jsPlumbDetach", {
  1301. source:jpc.source,
  1302. target:jpc.target,
  1303. sourceId:jpc.sourceId,
  1304. targetId:jpc.targetId,
  1305. sourceEndpoint:jpc.endpoints[0],
  1306. targetEndpoint:jpc.endpoints[1]
  1307. });*/
  1308. /*// force a paint
  1309. _draw(jpc.source);
  1310. return jpc;*/
  1311. }
  1312. };
  1313. /*
  1314. Function: detachAll
  1315. Removes all an element's connections.
  1316. Parameters:
  1317. el - either the id of the element, or a selector for the element.
  1318. Returns:
  1319. void
  1320. */
  1321. this.detachAll = function(el) {
  1322. var id = _getAttribute(el, "id");
  1323. var endpoints = endpointsByElement[id];
  1324. if (endpoints && endpoints.length) {
  1325. for (var i = 0; i < endpoints.length; i++) {
  1326. var c = endpoints[i].connections.length;
  1327. if (c > 0) {
  1328. for (var j = 0; j < c; j++) {
  1329. var jpc = endpoints[i].connections[0];
  1330. _removeElement(jpc.canvas, jpc.container);
  1331. jpc.endpoints[0].removeConnection(jpc);
  1332. jpc.endpoints[1].removeConnection(jpc);
  1333. _removeFromList(connectionsByScope, jpc.scope, jpc);
  1334. fireDetachEvent(jpc);
  1335. }
  1336. }
  1337. }
  1338. }
  1339. };
  1340. /*
  1341. Function: detachEverything
  1342. Remove all Connections from all elements, but leaves Endpoints in place.
  1343. Returns:
  1344. void
  1345. See Also:
  1346. <removeEveryEndpoint>
  1347. */
  1348. this.detachEverything = function() {
  1349. for(var id in endpointsByElement) {
  1350. var endpoints = endpointsByElement[id];
  1351. if (endpoints && endpoints.length) {
  1352. for (var i = 0; i < endpoints.length; i++) {
  1353. var c = endpoints[i].connections.length;
  1354. if (c > 0) {
  1355. for (var j = 0; j < c; j++) {
  1356. var jpc = endpoints[i].connections[0];
  1357. _removeElement(jpc.canvas, jpc.container);
  1358. jpc.endpoints[0].removeConnection(jpc);
  1359. jpc.endpoints[1].removeConnection(jpc);
  1360. fireDetachEvent(jpc);
  1361. }
  1362. }
  1363. }
  1364. }
  1365. }
  1366. delete connectionsByScope;
  1367. connectionsByScope = {};
  1368. };
  1369. /*
  1370. Function: draggable
  1371. initialises the draggability of some element or elements.
  1372. Parameters:
  1373. el - either an element id, a list of element ids, or a selector.
  1374. options - options to pass through to the underlying library
  1375. Returns:
  1376. void
  1377. */
  1378. this.draggable = function(el, options) {
  1379. if (typeof el == 'object' && el.length) {
  1380. for (var i = 0; i < el.length; i++)
  1381. {
  1382. var ele = _getElementObject(el[i]);
  1383. if (ele) _initDraggableIfNecessary(ele, true, options);
  1384. }
  1385. }
  1386. else {
  1387. var ele = _getElementObject(el);
  1388. if (ele)
  1389. _initDraggableIfNecessary(ele, true, options);
  1390. }
  1391. };
  1392. /*
  1393. Function: extend
  1394. Wraps the underlying library's extend functionality.
  1395. Parameters:
  1396. o1 - object to extend
  1397. o2 - object to extend o1 with
  1398. Returns:
  1399. o1, extended with all properties from o2.
  1400. */
  1401. this.extend = function(o1, o2) {
  1402. return jsPlumb.CurrentLibrary.extend(o1, o2);
  1403. };
  1404. /*
  1405. Function: getConnections
  1406. Gets all or a subset of connections currently managed by this jsPlumb instance.
  1407. Parameters:
  1408. options - a JS object that holds options defining what sort of connections you're looking for. Valid values are:
  1409. scope this may be a String or a list of Strings. jsPlumb will only return Connections whose scope matches what this option
  1410. defines. If you omit it, you will be given Connections of every scope.
  1411. source may be a string or a list of strings; constraints results to have only Connections whose source is this object.
  1412. target may be a string or a list of strings; constraints results to have only Connections whose target is this object.
  1413. The return value is a dictionary in this format:
  1414. {
  1415. 'scope1': [
  1416. {sourceId:'window1', targetId:'window2'},
  1417. {sourceId:'window3', targetId:'window4'},
  1418. {sourceId:'window1', targetId:'window3'}
  1419. ],
  1420. 'scope2': [
  1421. {sourceId:'window1', targetId:'window3'}
  1422. ]
  1423. }
  1424. */
  1425. this.getConnections = function(options) {
  1426. var r = {};
  1427. options = options || {};
  1428. var prepareList = function(input) {
  1429. var r = [];
  1430. if (input) {
  1431. if (typeof input == 'string') r.push(input);
  1432. else r = input;
  1433. }
  1434. return r;
  1435. };
  1436. var scopes = prepareList(options.scope);
  1437. var sources = prepareList(options.source);
  1438. var targets = prepareList(options.target);
  1439. var filter = function(list, value) {
  1440. return list.length > 0 ? _findIndex(list, value) != -1 : true;
  1441. };
  1442. for (var i in connectionsByScope) {
  1443. if (filter(scopes, i)) {
  1444. r[i] = [];
  1445. for (var j = 0; j < connectionsByScope[i].length; j++) {
  1446. var c = connectionsByScope[i][j];
  1447. if (filter(sources, c.sourceId) && filter(targets, c.targetId))
  1448. //r[i].push({sourceId:c.sourceId, targetId:c.targetId});
  1449. r[i].push({
  1450. sourceId:c.sourceId,
  1451. targetId:c.targetId,
  1452. source:c.source,
  1453. target:c.target,
  1454. sourceEndpoint:c.endpoints[0],
  1455. targetEndpoint:c.endpoints[1]
  1456. });
  1457. }
  1458. }
  1459. }
  1460. return r;
  1461. };
  1462. /*
  1463. Function: getDefaultScope
  1464. Gets the default scope for connections and endpoints. a scope defines a type of endpoint/connection; supplying a scope to an endpoint
  1465. 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,
  1466. you don't need to supply a scope. this method will probably be used by very few people; it's good for testing though.
  1467. */
  1468. this.getDefaultScope = function() {
  1469. return DEFAULT_SCOPE;
  1470. };
  1471. /*
  1472. Function: getEndpoint
  1473. gets an Endpoint by UUID
  1474. Parameters:
  1475. uuid - the UUID for the Endpoint
  1476. Returns:
  1477. Endpoint with the given UUID, null if nothing found.
  1478. */
  1479. this.getEndpoint = _getEndpoint;
  1480. /*
  1481. * gets an element's id, creating one if necessary. really only exposed for the lib-specific
  1482. * functionality to access; would be better to pass the current instance into the lib-specific
  1483. * code (even though this is a static call. i just don't want to expose it to the public API).
  1484. */
  1485. this.getId = _getId;
  1486. /*
  1487. Function: hide
  1488. Sets an element's connections to be hidden.
  1489. Parameters:
  1490. el - either the id of the element, or a selector for the element.
  1491. Returns:
  1492. void
  1493. */
  1494. this.hide = function(el) {
  1495. _setVisible(el, "none");
  1496. };
  1497. /*
  1498. Function: makeAnchor
  1499. Creates an anchor with the given params.
  1500. Parameters:
  1501. x - the x location of the anchor as a fraction of the total width.
  1502. y - the y location of the anchor as a fraction of the total height.
  1503. xOrientation - value indicating the general direction a connection from the anchor should go in, in the x direction.
  1504. yOrientation - value indicating the general direction a connection from the anchor should go in, in the y direction.
  1505. 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.
  1506. 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.
  1507. Returns:
  1508. The newly created Anchor.
  1509. */
  1510. this.makeAnchor = function(x, y, xOrientation, yOrientation, xOffset, yOffset) {
  1511. // backwards compatibility here. we used to require an object passed in but that makes the call very verbose. easier to use
  1512. // 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.
  1513. var params = {};
  1514. if (arguments.length == 1) jsPlumb.extend(params, x);
  1515. else {
  1516. params = {x:x, y:y};
  1517. if (arguments.length >= 4) {
  1518. params.orientation = [arguments[2], arguments[3]];
  1519. }
  1520. if (arguments.length == 6) params.offsets = [arguments[4], arguments[5]];
  1521. }
  1522. var a = new Anchor(params);
  1523. a.clone = function() { return new Anchor(params); };
  1524. return a;
  1525. };
  1526. /*
  1527. Function: repaint
  1528. Repaints an element and its connections. This method gets new sizes for the elements before painting anything.
  1529. Parameters:
  1530. el - either the id of the element or a selector representing the element.
  1531. Returns:
  1532. void
  1533. See Also:
  1534. <repaintEverything>
  1535. */
  1536. this.repaint = function(el) {
  1537. var _processElement = function(el) {
  1538. var ele = _getElementObject(el);
  1539. _draw(ele);
  1540. };
  1541. // TODO: support a jQuery result object too!
  1542. // support both lists...
  1543. if (typeof el =='object') {
  1544. for (var i = 0; i < el.length; i++)
  1545. _processElement(el[i]);
  1546. } // ...and single strings.
  1547. else _processElement(el);
  1548. };
  1549. /*
  1550. Function: repaintEverything
  1551. Repaints all connections.
  1552. Returns:
  1553. void
  1554. See Also:
  1555. <repaint>
  1556. */
  1557. this.repaintEverything = function() {
  1558. for (var elId in endpointsByElement) {
  1559. _draw(_getElementObject(elId));
  1560. }
  1561. };
  1562. /*
  1563. Function: removeAllEndpoints
  1564. Removes all Endpoints associated with a given element. Also removes all Connections associated with each Endpoint it removes.
  1565. Parameters:
  1566. el - either an element id, or a selector for an element.
  1567. Returns:
  1568. void
  1569. See Also:
  1570. <removeEndpoint>
  1571. */
  1572. this.removeAllEndpoints = function(el) {
  1573. var elId = _getAttribute(el, "id");
  1574. // first remove all Connections.
  1575. jsPlumb.detachAll(elId);
  1576. var ebe = endpointsByElement[elId];
  1577. for (var i in ebe) {
  1578. _removeElement(ebe[i].canvas);
  1579. }
  1580. endpointsByElement[elId] = [];
  1581. };
  1582. /*
  1583. Function: removeEveryEndpoint
  1584. Removes every Endpoint in this instance of jsPlumb.
  1585. Returns:
  1586. void
  1587. See Also:
  1588. <removeAllEndpoints> <removeEndpoint>
  1589. */
  1590. this.removeEveryEndpoint = function() {
  1591. for(var id in endpointsByElement) {
  1592. var endpoints = endpointsByElement[id];
  1593. if (endpoints && endpoints.length) {
  1594. for (var i = 0; i < endpoints.length; i++) {
  1595. _removeElement(endpoints[i].canvas, endpoints[i].container);
  1596. }
  1597. }
  1598. }
  1599. delete endpointsByElement;
  1600. endpointsByElement = {};
  1601. };
  1602. /*
  1603. Function: removeEndpoint
  1604. Removes the given Endpoint from the given element.
  1605. Parameters:
  1606. el - either an element id, or a selector for an element.
  1607. endpoint - Endpoint to remove. this is an Endpoint object, such as would have been returned from a call to addEndpoint.
  1608. Returns:
  1609. void
  1610. See Also:
  1611. <removeAllEndpoints> <removeEveryEndpoint>
  1612. */
  1613. this.removeEndpoint = function(el, endpoint) {
  1614. var elId = _getAttribute(el, "id");
  1615. var ebe = endpointsByElement[elId];
  1616. if (ebe) {
  1617. if(_removeFromList(endpointsByElement, elId, endpoint))
  1618. _removeElement(endpoint.canvas);
  1619. }
  1620. };
  1621. /*
  1622. Function:reset
  1623. removes all endpoints and connections and clears the element cache.
  1624. */
  1625. this.reset = function() {
  1626. this.detachEverything();
  1627. this.removeEveryEndpoint();
  1628. this.clearCache();
  1629. };
  1630. /*
  1631. Function: setAutomaticRepaint
  1632. Sets/unsets automatic repaint on window resize.
  1633. Parameters:
  1634. value - whether or not to automatically repaint when the window is resized.
  1635. Returns:
  1636. void
  1637. */
  1638. this.setAutomaticRepaint = function(value) {
  1639. automaticRepaint = value;
  1640. };
  1641. /*
  1642. Function: setDefaultNewCanvasSize
  1643. Sets the default size jsPlumb will use for a new canvas (we create a square canvas so one value is all that is required).
  1644. 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
  1645. it at initialisation time. The default value of this is 1200 pixels, which is quite large, but if for some reason you're
  1646. drawing connectors that are bigger, you should adjust this value appropriately.
  1647. Parameters:
  1648. size - The default size to use. jsPlumb will use a square canvas so you need only supply one value.
  1649. Returns:
  1650. void
  1651. */
  1652. this.setDefaultNewCanvasSize = function(size) {
  1653. DEFAULT_NEW_CANVAS_SIZE = size;
  1654. };
  1655. /*
  1656. Function: setDefaultScope
  1657. Sets the default scope for connections and endpoints. a scope defines a type of endpoint/connection; supplying a scope to an endpoint
  1658. 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,
  1659. 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
  1660. for the default scope.
  1661. */
  1662. this.setDefaultScope = function(scope) {
  1663. DEFAULT_SCOPE = scope;
  1664. };
  1665. /*
  1666. Function: setDraggable
  1667. Sets whether or not a given element is draggable, regardless of what any plumb command may request.
  1668. Parameters:
  1669. el - either the id for the element, or a selector representing the element.
  1670. Returns:
  1671. void
  1672. */
  1673. this.setDraggable = _setDraggable;
  1674. /*
  1675. Function: setDraggableByDefault
  1676. Sets whether or not elements are draggable by default. Default for this is true.
  1677. Parameters:
  1678. draggable - value to set
  1679. Returns:
  1680. void
  1681. */
  1682. this.setDraggableByDefault = function(draggable) {
  1683. _draggableByDefault = draggable;
  1684. };
  1685. this.setDebugLog = function(debugLog) {
  1686. log = debugLog;
  1687. };
  1688. /*
  1689. Function: setRepaintFunction
  1690. Sets the function to fire when the window size has changed and a repaint was fired.
  1691. Parameters:
  1692. f - Function to execute.
  1693. Returns:
  1694. void
  1695. */
  1696. this.setRepaintFunction = function(f) {
  1697. repaintFunction = f;
  1698. };
  1699. /*
  1700. Function: show
  1701. Sets an element's connections to be visible.
  1702. Parameters:
  1703. el - either the id of the element, or a selector for the element.
  1704. Returns:
  1705. void
  1706. */
  1707. this.show = function(el) {
  1708. _setVisible(el, "block");
  1709. };
  1710. /*
  1711. Function: sizeCanvas
  1712. Helper to size a canvas. You would typically use this when writing your own Connector or Endpoint implementation.
  1713. Parameters:
  1714. x - [int] x position for the Canvas origin
  1715. y - [int] y position for the Canvas origin
  1716. w - [int] width of the canvas
  1717. h - [int] height of the canvas
  1718. Returns:
  1719. void
  1720. */
  1721. this.sizeCanvas = function(canvas, x, y, w, h) {
  1722. if (canvas) {
  1723. canvas.style.height = h + "px"; canvas.height = h;
  1724. canvas.style.width = w + "px"; canvas.width = w;
  1725. canvas.style.left = x + "px"; canvas.style.top = y + "px";
  1726. // _currentInstance.CurrentLibrary.setPosition(canvas, x, y);
  1727. }
  1728. };
  1729. /**
  1730. * gets some test hooks. nothing writable.
  1731. */
  1732. this.getTestHarness = function() {
  1733. return {
  1734. endpointCount : function(elId) {
  1735. var e = endpointsByElement[elId];
  1736. return e ? e.length : 0;
  1737. },
  1738. connectionCount : function(scope) {
  1739. scope = scope || DEFAULT_SCOPE;
  1740. var c = connectionsByScope[scope];
  1741. return c ? c.length : 0;
  1742. },
  1743. findIndex : _findIndex,
  1744. getId : _getId
  1745. };
  1746. };
  1747. /**
  1748. * Toggles visibility of an element's connections. kept for backwards compatibility
  1749. */
  1750. this.toggle = _toggleVisible;
  1751. /*
  1752. Function: toggleVisible
  1753. Toggles visibility of an element's connections.
  1754. Parameters:
  1755. el - either the element's id, or a selector representing the element.
  1756. Returns:
  1757. void, but should be updated to return the current state
  1758. */
  1759. //TODO: update this method to return the current state.
  1760. this.toggleVisible = _toggleVisible;
  1761. /*
  1762. Function: toggleDraggable
  1763. Toggles draggability (sic) of an element's connections.
  1764. Parameters:
  1765. el - either the element's id, or a selector representing the element.
  1766. Returns:
  1767. The current draggable state.
  1768. */
  1769. this.toggleDraggable = _toggleDraggable;
  1770. /*
  1771. Function: unload
  1772. Unloads jsPlumb, deleting all storage. You should call this from an onunload attribute on the <body> element
  1773. Returns:
  1774. void
  1775. */
  1776. this.unload = function() {
  1777. delete endpointsByElement;
  1778. delete offsets;
  1779. delete sizes;
  1780. delete floatingConnections;
  1781. delete draggableStates;
  1782. };
  1783. /*
  1784. Function: wrap
  1785. Helper method to wrap an existing function with one of your own. This is used by the various
  1786. implementations to wrap event callbacks for drag/drop etc; it allows jsPlumb to be
  1787. transparent in its handling of these things. If a user supplies their own event callback,
  1788. for anything, it will always be called.
  1789. Parameters:
  1790. */
  1791. this.wrap = _wrap;
  1792. this.addListener = _addListener;
  1793. };
  1794. var jsPlumb = window.jsPlumb = new jsPlumbInstance();
  1795. jsPlumb.getInstance = function(_defaults) {
  1796. var j = new jsPlumbInstance();
  1797. if (_defaults)
  1798. jsPlumb.extend(j.Defaults, _defaults);
  1799. return j;
  1800. };
  1801. })();
  1802. /*
  1803. * jsPlumb-defaults-1.2.2
  1804. *
  1805. * This script contains the default Anchors, Endpoints and Connectors for jsPlumb. It should be used with jsPlumb 1.1.0 and above;
  1806. * prior to version 1.1.0 of jsPlumb the defaults were included inside the main script.
  1807. *
  1808. * Version 1.1.1 of this script adds the Triangle Endpoint, written by __________ and featured on this demo:
  1809. *
  1810. * http://www.mintdesign.ru/blog/javascript-jsplumb-making-graph
  1811. * http://www.mintdesign.ru/site/samples/jsplumb/jsPlumb-graph-sample.htm
  1812. *
  1813. * NOTE: for production usage you should use jsPlumb-all-x.x.x-min.js, which contains the main jsPlumb script and this script together,
  1814. * in a minified file.
  1815. *
  1816. * Dual licensed under MIT and GPL2.
  1817. */
  1818. (function() {
  1819. /**
  1820. * Places you can anchor a connection to. These are helpers for common locations; they all just return an instance
  1821. * of Anchor that has been configured appropriately.
  1822. *
  1823. * You can write your own one of these; you
  1824. * just need to provide a 'compute' method and an 'orientation'. so you'd say something like this:
  1825. *
  1826. * jsPlumb.Anchors.MY_ANCHOR = {
  1827. * compute : function(xy, wh, txy, twh) { return some mathematics on those variables; },
  1828. * getOrientation : function() { return [ox, oy]; }
  1829. * };
  1830. *
  1831. * compute takes the [x,y] position of the top left corner of the anchored element,
  1832. * and the element's [width,height] (all in pixels), as well as the location and dimension of the element it's plumbed to,
  1833. * and returns where the anchor should be located.
  1834. *
  1835. * the 'orientation' array (returned here as [ox,oy]) indicates the general direction a connection from the anchor
  1836. * should go in, if possible. it is an [x,y] matrix where a value of 0 means no preference,
  1837. * -1 means go in a negative direction for the given axis, and 1 means go in a positive
  1838. * direction. so consider a TopCenter anchor: the orientation matrix for it is [0,-1],
  1839. * meaning connections naturally want to go upwards on screen. in a Bezier implementation, for example,
  1840. * the curve would start out going in that direction, before bending towards the target anchor.
  1841. */
  1842. jsPlumb.Anchors.TopCenter = jsPlumb.makeAnchor(0.5, 0, 0,-1);
  1843. jsPlumb.Anchors.BottomCenter = jsPlumb.makeAnchor(0.5, 1, 0, 1);
  1844. jsPlumb.Anchors.LeftMiddle = jsPlumb.makeAnchor(0, 0.5, -1, 0);
  1845. jsPlumb.Anchors.RightMiddle = jsPlumb.makeAnchor(1, 0.5, 1, 0);
  1846. jsPlumb.Anchors.Center = jsPlumb.makeAnchor(0.5, 0.5, 0, 0);
  1847. jsPlumb.Anchors.TopRight = jsPlumb.makeAnchor(1, 0, 0,-1);
  1848. jsPlumb.Anchors.BottomRight = jsPlumb.makeAnchor(1, 1, 0, 1);
  1849. jsPlumb.Anchors.TopLeft = jsPlumb.makeAnchor(0, 0, 0, -1);
  1850. jsPlumb.Anchors.BottomLeft = jsPlumb.makeAnchor(0, 1, 0, 1);
  1851. /**
  1852. * The Straight connector draws a simple straight line between the two anchor points.
  1853. */
  1854. jsPlumb.Connectors.Straight = function() {
  1855. var self = this;
  1856. /**
  1857. * Computes the new size and position of the canvas.
  1858. * @param sourceAnchor Absolute position on screen of the source object's anchor.
  1859. * @param targetAnchor Absolute position on screen of the target object's anchor.
  1860. * @param positionMatrix Indicates the relative positions of the left,top of the
  1861. * two plumbed objects. so [0,0] indicates that the source is to the left of, and
  1862. * above, the target. [1,0] means the source is to the right and above. [0,1] means
  1863. * the source is to the left and below. [1,1] means the source is to the right
  1864. * and below. this is used to figure out which direction to draw the connector in.
  1865. * @returns an array of positioning information. the first two values are
  1866. * the [left, top] absolute position the canvas should be placed on screen. the
  1867. * next two values are the [width,height] the canvas should be. after that each
  1868. * Connector can put whatever it likes into the array:it will be passed back in
  1869. * to the paint call. This particular function stores the origin and destination of
  1870. * the line it is going to draw. a more involved implementation, like a Bezier curve,
  1871. * would store the control point info in this array too.
  1872. */
  1873. this.compute = function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth) {
  1874. var w = Math.abs(sourcePos[0] - targetPos[0]);
  1875. var h = Math.abs(sourcePos[1] - targetPos[1]);
  1876. var widthAdjusted = false, heightAdjusted = false;
  1877. // these are padding to ensure the whole connector line appears
  1878. var xo = 0.45 * w, yo = 0.45 * h;
  1879. // these are padding to ensure the whole connector line appears
  1880. w *= 1.9; h *=1.9;
  1881. var x = Math.min(sourcePos[0], targetPos[0]) - xo;
  1882. var y = Math.min(sourcePos[1], targetPos[1]) - yo;
  1883. if (w < 2 * lineWidth) {
  1884. w = 2 * lineWidth;
  1885. x = sourcePos[0] + ((targetPos[0] - sourcePos[0]) / 2) - lineWidth;
  1886. xo = (w - Math.abs(sourcePos[0]-targetPos[0])) / 2;
  1887. }
  1888. if (h < 2 * lineWidth) {
  1889. // minimum size is 2 * line Width
  1890. h = 2 * lineWidth;
  1891. y = sourcePos[1] + ((targetPos[1] - sourcePos[1]) / 2) - lineWidth;
  1892. yo = (h - Math.abs(sourcePos[1]-targetPos[1])) / 2;
  1893. }
  1894. var sx = sourcePos[0] < targetPos[0] ? w-xo : xo;
  1895. var sy = sourcePos[1] < targetPos[1] ? h-yo : yo;
  1896. var tx = sourcePos[0] < targetPos[0] ? xo : w-xo;
  1897. var ty = sourcePos[1] < targetPos[1] ? yo : h-yo;
  1898. var retVal = [ x, y, w, h, sx, sy, tx, ty ];
  1899. return retVal;
  1900. };
  1901. this.paint = function(dimensions, ctx)
  1902. {
  1903. ctx.beginPath();
  1904. ctx.moveTo(dimensions[4], dimensions[5]);
  1905. ctx.lineTo(dimensions[6], dimensions[7]);
  1906. ctx.stroke();
  1907. };
  1908. };
  1909. /**
  1910. * This Connector draws a Bezier curve with two control points.
  1911. * @param curviness How 'curvy' you want the curve to be! This is a directive for the
  1912. * placement of control points, not endpoints of the curve, so your curve does not
  1913. * actually touch the given point, but it has the tendency to lean towards it. the larger
  1914. * this value, the greater the curve is pulled from a straight line.
  1915. *
  1916. * a future implementation of this could take the control points as arguments, rather
  1917. * than fixing the curve to one basic shape.
  1918. */
  1919. jsPlumb.Connectors.Bezier = function(curviness) {
  1920. var self = this;
  1921. this.majorAnchor = curviness || 150;
  1922. this.minorAnchor = 10;
  1923. this._findControlPoint = function(point, sourceAnchorPosition, targetAnchorPosition, sourceAnchor, targetAnchor) {
  1924. // determine if the two anchors are perpendicular to each other in their orientation. we swap the control
  1925. // points around if so (code could be tightened up)
  1926. var soo = sourceAnchor.getOrientation(), too = targetAnchor.getOrientation();
  1927. var perpendicular = soo[0] != too[0] || soo[1] == too[1];
  1928. var p = [];
  1929. var ma = self.majorAnchor, mi = self.minorAnchor;
  1930. if (!perpendicular) {
  1931. if (soo[0] == 0) // X
  1932. p.push(sourceAnchorPosition[0] < targetAnchorPosition[0] ? point[0] + mi : point[0] - mi);
  1933. else p.push(point[0] - (ma * soo[0]));
  1934. if (soo[1] == 0) // Y
  1935. p.push(sourceAnchorPosition[1] < targetAnchorPosition[1] ? point[1] + mi : point[1] - mi);
  1936. else p.push(point[1] + (ma * too[1]));
  1937. }
  1938. else {
  1939. if (too[0] == 0) // X
  1940. p.push(targetAnchorPosition[0] < sourceAnchorPosition[0] ? point[0] + mi : point[0] - mi);
  1941. else p.push(point[0] + (ma * too[0]));
  1942. if (too[1] == 0) // Y
  1943. p.push(targetAnchorPosition[1] < sourceAnchorPosition[1] ? point[1] + mi : point[1] - mi);
  1944. else p.push(point[1] + (ma * soo[1]));
  1945. }
  1946. return p;
  1947. };
  1948. this.compute = function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth)
  1949. {
  1950. lineWidth = lineWidth || 0;
  1951. var w = Math.abs(sourcePos[0] - targetPos[0]) + lineWidth, h = Math.abs(sourcePos[1] - targetPos[1]) + lineWidth;
  1952. var canvasX = Math.min(sourcePos[0], targetPos[0])-(lineWidth/2), canvasY = Math.min(sourcePos[1], targetPos[1])-(lineWidth/2);
  1953. var sx = sourcePos[0] < targetPos[0] ? w - (lineWidth/2): (lineWidth/2), sy = sourcePos[1] < targetPos[1] ? h-(lineWidth/2) : (lineWidth/2);
  1954. var tx = sourcePos[0] < targetPos[0] ? (lineWidth/2) : w-(lineWidth/2), ty = sourcePos[1] < targetPos[1] ? (lineWidth/2) : h-(lineWidth/2);
  1955. var CP = self._findControlPoint([sx,sy], sourcePos, targetPos, sourceAnchor, targetAnchor);
  1956. var CP2 = self._findControlPoint([tx,ty], targetPos, sourcePos, targetAnchor, sourceAnchor);
  1957. var minx1 = Math.min(sx,tx); var minx2 = Math.min(CP[0], CP2[0]); var minx = Math.min(minx1,minx2);
  1958. var maxx1 = Math.max(sx,tx); var maxx2 = Math.max(CP[0], CP2[0]); var maxx = Math.max(maxx1,maxx2);
  1959. if (maxx > w) w = maxx;
  1960. if (minx < 0) {
  1961. canvasX += minx; var ox = Math.abs(minx);
  1962. w += ox; CP[0] += ox; sx += ox; tx +=ox; CP2[0] += ox;
  1963. }
  1964. var miny1 = Math.min(sy,ty); var miny2 = Math.min(CP[1], CP2[1]); var miny = Math.min(miny1,miny2);
  1965. var maxy1 = Math.max(sy,ty); var maxy2 = Math.max(CP[1], CP2[1]); var maxy = Math.max(maxy1,maxy2);
  1966. if (maxy > h) h = maxy;
  1967. if (miny < 0) {
  1968. canvasY += miny; var oy = Math.abs(miny);
  1969. h += oy; CP[1] += oy; sy += oy; ty +=oy; CP2[1] += oy;
  1970. }
  1971. // return [ canvasx, canvasy, canvasWidth, canvasHeight,
  1972. // sourceX, sourceY, targetX, targetY,
  1973. // controlPoint1_X, controlPoint1_Y, controlPoint2_X, controlPoint2_Y
  1974. return [canvasX, canvasY, w, h, sx, sy, tx, ty, CP[0], CP[1], CP2[0], CP2[1] ];
  1975. };
  1976. this.paint = function(d, ctx) {
  1977. /*var img = new Image();
  1978. img.src = "../img/pattern.jpg";
  1979. ctx.fillStyle = ctx.createPattern(img, 'repeat-y');*/
  1980. ctx.beginPath();
  1981. ctx.moveTo(d[4],d[5]);
  1982. ctx.bezierCurveTo(d[8],d[9],d[10],d[11],d[6],d[7]);
  1983. ctx.stroke();
  1984. //ctx.fill();
  1985. }
  1986. };
  1987. /**
  1988. * Types of endpoint UIs. we supply four - a circle of default radius 10px, a rectangle of
  1989. * default size 20x20, an image (with no default), and a Triangle, of default size 15.
  1990. * you can supply others of these if you want to - see the documentation for a howto.
  1991. */
  1992. /**
  1993. * a round endpoint, with default radius 10 pixels.
  1994. */
  1995. jsPlumb.Endpoints.Dot = function(params) {
  1996. params = params || { radius:10 };
  1997. var self = this;
  1998. this.radius = params.radius;
  1999. var defaultOffset = 0.5 * this.radius;
  2000. var defaultInnerRadius = this.radius / 3;
  2001. var parseValue = function(value) {
  2002. try {
  2003. return parseInt(value);
  2004. }
  2005. catch(e) {
  2006. if (value.substring(value.length - 1) == '%')
  2007. return parseInt(value.substring(0, value - 1));
  2008. }
  2009. }
  2010. var calculateAdjustments = function(gradient) {
  2011. var offsetAdjustment = defaultOffset;
  2012. var innerRadius = defaultInnerRadius;
  2013. if (gradient.offset) offsetAdjustment = parseValue(gradient.offset);
  2014. if(gradient.innerRadius) innerRadius = parseValue(gradient.innerRadius);
  2015. return [offsetAdjustment, innerRadius];
  2016. };
  2017. this.paint = function(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle) {
  2018. var radius = endpointStyle.radius || self.radius;
  2019. var x = anchorPoint[0] - radius;
  2020. var y = anchorPoint[1] - radius;
  2021. jsPlumb.sizeCanvas(canvas, x, y, radius * 2, radius * 2);
  2022. var ctx = canvas.getContext('2d');
  2023. var style = jsPlumb.extend({}, endpointStyle);
  2024. if (style.fillStyle == null) style.fillStyle = connectorPaintStyle.strokeStyle;
  2025. jsPlumb.extend(ctx, style);
  2026. var ie = (/MSIE/.test(navigator.userAgent) && !window.opera);
  2027. if (endpointStyle.gradient && !ie) {
  2028. var adjustments = calculateAdjustments(endpointStyle.gradient);
  2029. var yAdjust = orientation[1] == 1 ? adjustments[0] * -1 : adjustments[0];
  2030. var xAdjust = orientation[0] == 1 ? adjustments[0] * -1: adjustments[0];
  2031. var g = ctx.createRadialGradient(radius, radius, radius, radius + xAdjust, radius + yAdjust, adjustments[1]);
  2032. for (var i = 0; i < endpointStyle.gradient.stops.length; i++)
  2033. g.addColorStop(endpointStyle.gradient.stops[i][0], endpointStyle.gradient.stops[i][1]);
  2034. ctx.fillStyle = g;
  2035. }
  2036. ctx.beginPath();
  2037. ctx.arc(radius, radius, radius, 0, Math.PI*2, true);
  2038. ctx.closePath();
  2039. ctx.fill();
  2040. };
  2041. };
  2042. /**
  2043. * A Rectangular endpoint, with default size 20x20.
  2044. */
  2045. jsPlumb.Endpoints.Rectangle = function(params) {
  2046. params = params || { width:20, height:20 };
  2047. var self = this;
  2048. this.width = params.width;
  2049. this.height = params.height;
  2050. this.paint = function(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle) {
  2051. var width = endpointStyle.width || self.width;
  2052. var height = endpointStyle.height || self.height;
  2053. var x = anchorPoint[0] - (width/2);
  2054. var y = anchorPoint[1] - (height/2);
  2055. jsPlumb.sizeCanvas(canvas, x, y, width, height);
  2056. var ctx = canvas.getContext('2d');
  2057. //todo: the fillStyle needs some thought. we want to support a few options:
  2058. // 1. nothing supplied; use the stroke color or the default if no stroke color.
  2059. // 2. a fill color supplied - use it
  2060. // 3. a gradient supplied - use it
  2061. // 4. setting the endpoint to the same color as the bg of the element it is attached to.
  2062. var style = jsPlumb.extend({}, endpointStyle);
  2063. if (style.fillStyle == null) style.fillStyle = connectorPaintStyle.strokeStyle;
  2064. jsPlumb.extend(ctx, style);
  2065. var ie = (/MSIE/.test(navigator.userAgent) && !window.opera);
  2066. if (endpointStyle.gradient && !ie) {
  2067. // first figure out which direction to run the gradient in (it depends on the orientation of the anchors)
  2068. var y1 = orientation[1] == 1 ? height : orientation[1] == 0 ? height / 2 : 0;
  2069. var y2 = orientation[1] == -1 ? height : orientation[1] == 0 ? height / 2 : 0;
  2070. var x1 = orientation[0] == 1 ? width : orientation[0] == 0 ? width / 2 : 0;
  2071. var x2 = orientation[0] == -1 ? width : orientation[0] == 0 ? height / 2 : 0;
  2072. var g = ctx.createLinearGradient(x1,y1,x2,y2);
  2073. for (var i = 0; i < endpointStyle.gradient.stops.length; i++)
  2074. g.addColorStop(endpointStyle.gradient.stops[i][0], endpointStyle.gradient.stops[i][1]);
  2075. ctx.fillStyle = g;
  2076. }
  2077. ctx.beginPath();
  2078. ctx.rect(0, 0, width, height);
  2079. ctx.closePath();
  2080. ctx.fill();
  2081. };
  2082. };
  2083. jsPlumb.Endpoints.Triangle = function(params) {
  2084. params = params || { width:15, height:15 };
  2085. var self = this;
  2086. this.width = params.width;
  2087. this.height = params.height;
  2088. this.paint = function(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle)
  2089. {
  2090. var width = endpointStyle.width || self.width;
  2091. var height = endpointStyle.height || self.height;
  2092. var x = anchorPoint[0] - width/2;
  2093. var y = anchorPoint[1] - height/2;
  2094. jsPlumb.sizeCanvas(canvas, x, y, width, height);
  2095. var ctx = canvas.getContext('2d');
  2096. var
  2097. offsetX = 0,
  2098. offsetY = 0,
  2099. angle = 0;
  2100. if( orientation[0] == 1 )
  2101. {
  2102. offsetX = width;
  2103. offsetY = height;
  2104. angle = 180;
  2105. }
  2106. if( orientation[1] == -1 )
  2107. {
  2108. offsetX = width;
  2109. angle = 90;
  2110. }
  2111. if( orientation[1] == 1 )
  2112. {
  2113. offsetY = height;
  2114. angle = -90;
  2115. }
  2116. ctx.fillStyle = endpointStyle.fillStyle;
  2117. ctx.translate(offsetX, offsetY);
  2118. ctx.rotate(angle * Math.PI/180);
  2119. ctx.beginPath();
  2120. ctx.moveTo(0, 0);
  2121. ctx.lineTo(width/2, height/2);
  2122. ctx.lineTo(0, height);
  2123. ctx.closePath();
  2124. ctx.fill();
  2125. };
  2126. };
  2127. /**
  2128. * Image endpoint - draws an image as the endpoint. You must provide a 'url' property in the params object..
  2129. */
  2130. jsPlumb.Endpoints.Image = function(params) {
  2131. var self = this;
  2132. this.img = new Image();
  2133. var ready = false;
  2134. this.img.onload = function() {
  2135. self.ready = true;
  2136. };
  2137. this.img.src = params.url;
  2138. var actuallyPaint = function(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle) {
  2139. var width = self.img.width || endpointStyle.width;
  2140. var height = self.img.height || endpointStyle.height;
  2141. var x = anchorPoint[0] - (width/2);
  2142. var y = anchorPoint[1] - (height/2);
  2143. jsPlumb.sizeCanvas(canvas, x, y, width, height);
  2144. var ctx = canvas.getContext('2d');
  2145. ctx.drawImage(self.img,0,0);
  2146. };
  2147. this.paint = function(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle) {
  2148. if (self.ready) {
  2149. actuallyPaint(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle)
  2150. }
  2151. else
  2152. window.setTimeout(function() {
  2153. self.paint(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle);
  2154. }, 200);
  2155. };
  2156. };
  2157. })();
  2158. /*
  2159. * mootools.jsPlumb 1.2.2
  2160. *
  2161. * MooTools specific functionality for jsPlumb.
  2162. *
  2163. * http://morrisonpitt.com/jsPlumb/demo.html
  2164. * http://code.google.com/p/jsPlumb
  2165. *
  2166. * NOTE: for production usage you should use mootools.jsPlumb-all-x.x.x-min.js, which contains the main jsPlumb script and this script together,
  2167. * in a minified file.
  2168. *
  2169. * Dual licensed under MIT and GPL2.
  2170. *
  2171. */
  2172. (function() {
  2173. /*
  2174. * overrides the FX class to inject 'step' functionality, which MooTools does not
  2175. * offer, and which makes me sad. they don't seem keen to add it, either, despite
  2176. * the fact that it could be useful:
  2177. *
  2178. * https://mootools.lighthouseapp.com/projects/2706/tickets/668
  2179. *
  2180. */
  2181. var jsPlumbMorph = new Class({
  2182. Extends:Fx.Morph,
  2183. onStep : null,
  2184. initialize : function(el, options) {
  2185. this.parent(el, options);
  2186. if (options['onStep']) {
  2187. this.onStep = options['onStep'];
  2188. }
  2189. },
  2190. step : function() {
  2191. this.parent();
  2192. if (this.onStep) {
  2193. try { this.onStep(); }
  2194. catch(e) { }
  2195. }
  2196. }
  2197. });
  2198. var _droppables = {};
  2199. var _droppableOptions = {};
  2200. var _draggablesByScope = {};
  2201. var _draggablesById = {};
  2202. /*
  2203. *
  2204. */
  2205. var _executeDroppableOption = function(el, dr, event) {
  2206. if (dr) {
  2207. var id = dr.get("id");
  2208. if (id) {
  2209. var options = _droppableOptions[id];
  2210. if (options) {
  2211. if (options[event]) {
  2212. options[event](el, dr);
  2213. }
  2214. }
  2215. }
  2216. }
  2217. };
  2218. var _checkHover = function(el, entering) {
  2219. if (el) {
  2220. var id = el.get("id");
  2221. if (id) {
  2222. var options = _droppableOptions[id];
  2223. if (options) {
  2224. if (options['hoverClass']) {
  2225. if (entering) el.addClass(options['hoverClass']);
  2226. else el.removeClass(options['hoverClass']);
  2227. }
  2228. }
  2229. }
  2230. }
  2231. };
  2232. /**
  2233. * adds the given value to the given list, with the given scope. creates the scoped list
  2234. * if necessary.
  2235. * used by initDraggable and initDroppable.
  2236. */
  2237. var _add = function(list, scope, value) {
  2238. var l = list[scope];
  2239. if (!l) {
  2240. l = [];
  2241. list[scope] = l;
  2242. }
  2243. l.push(value);
  2244. };
  2245. jsPlumb.CurrentLibrary = {
  2246. dragEvents : {
  2247. 'start':'onStart', 'stop':'onComplete', 'drag':'onDrag', 'step':'onStep',
  2248. 'over':'onEnter', 'out':'onLeave','drop':'onDrop', 'complete':'onComplete'
  2249. },
  2250. appendElement : function(child, parent) {
  2251. jsPlumb.CurrentLibrary.getElementObject(parent).grab(child);
  2252. },
  2253. /*
  2254. * wrapper around the library's 'extend' functionality (which it hopefully has.
  2255. * otherwise you'll have to do it yourself). perhaps jsPlumb could do this for you
  2256. * instead. it's not like its hard.
  2257. */
  2258. extend : function(o1, o2) {
  2259. return $extend(o1, o2);
  2260. },
  2261. /*
  2262. * gets an "element object" from the given input. this means an object that is used by the
  2263. * underlying library on which jsPlumb is running. 'el' may already be one of these objects,
  2264. * in which case it is returned as-is. otherwise, 'el' is a String, the library's lookup
  2265. * function is used to find the element, using the given String as the element's id.
  2266. */
  2267. getElementObject : function(el) {
  2268. return $(el);
  2269. },
  2270. /*
  2271. gets the offset for the element object. this should return a js object like this:
  2272. { left:xxx, top: xxx}
  2273. */
  2274. getOffset : function(el) {
  2275. var p = el.getPosition();
  2276. return { left:p.x, top:p.y };
  2277. },
  2278. getSize : function(el) {
  2279. var s = el.getSize();
  2280. return [s.x, s.y];
  2281. },
  2282. /**
  2283. * gets the named attribute from the given element object.
  2284. */
  2285. getAttribute : function(el, attName) {
  2286. return el.get(attName);
  2287. },
  2288. /**
  2289. * sets the named attribute on the given element object.
  2290. */
  2291. setAttribute : function(el, attName, attValue) {
  2292. el.set(attName, attValue);
  2293. },
  2294. /**
  2295. * adds the given class to the element object.
  2296. */
  2297. addClass : function(el, clazz) {
  2298. el.addClass(clazz);
  2299. },
  2300. initDraggable : function(el, options) {
  2301. var id = jsPlumb.getId(el);
  2302. var drag = _draggablesById[id];
  2303. if (!drag) {
  2304. var originalZIndex = 0, originalCursor = null;
  2305. var dragZIndex = jsPlumb.Defaults.DragOptions.zIndex || 2000;
  2306. options['onStart'] = jsPlumb.wrap(options['onStart'], function()
  2307. {
  2308. originalZIndex = this.element.getStyle('z-index');
  2309. this.element.setStyle('z-index', dragZIndex);
  2310. if (jsPlumb.Defaults.DragOptions.cursor) {
  2311. originalCursor = this.element.getStyle('cursor');
  2312. this.element.setStyle('cursor', jsPlumb.Defaults.DragOptions.cursor);
  2313. }
  2314. });
  2315. options['onComplete'] = jsPlumb.wrap(options['onComplete'], function()
  2316. {
  2317. this.element.setStyle('z-index', originalZIndex);
  2318. if (originalCursor) {
  2319. this.element.setStyle('cursor', originalCursor);
  2320. }
  2321. });
  2322. // DROPPABLES:
  2323. var scope = options['scope'] || jsPlumb.Defaults.Scope;
  2324. var filterFunc = function(entry) {
  2325. return entry.get("id") != el.get("id");
  2326. };
  2327. var droppables = _droppables[scope] ? _droppables[scope].filter(filterFunc) : [];
  2328. options['droppables'] = droppables;
  2329. options['onLeave'] = jsPlumb.wrap(options['onLeave'], function(el, dr) {
  2330. if (dr) {
  2331. _checkHover(dr, false);
  2332. _executeDroppableOption(el, dr, 'onLeave');
  2333. }
  2334. });
  2335. options['onEnter'] = jsPlumb.wrap(options['onEnter'], function(el, dr) {
  2336. if (dr) {
  2337. _checkHover(dr, true);
  2338. _executeDroppableOption(el, dr, 'onEnter');
  2339. }
  2340. });
  2341. options['onDrop'] = function(el, dr) {
  2342. if (dr) {
  2343. _checkHover(dr, false);
  2344. _executeDroppableOption(el, dr, 'onDrop');
  2345. }
  2346. };
  2347. drag = new Drag.Move(el, options);
  2348. _add(_draggablesByScope, scope, drag);
  2349. _add(_draggablesById, el.get("id"), drag);
  2350. // test for disabled.
  2351. if (options.disabled) drag.detach();
  2352. }
  2353. return drag;
  2354. },
  2355. isDragSupported : function(el, options) {
  2356. return typeof Drag != 'undefined' ;
  2357. },
  2358. setDraggable : function(el, draggable) {
  2359. var draggables = _draggablesById[el.get("id")];
  2360. if (draggables) {
  2361. draggables.each(function(d) {
  2362. if (draggable) d.attach(); else d.detach();
  2363. });
  2364. }
  2365. },
  2366. initDroppable : function(el, options) {
  2367. var scope = options['scope'] || jsPlumb.Defaults.Scope;
  2368. _add(_droppables, scope, el);
  2369. var id = jsPlumb.getId(el);
  2370. _droppableOptions[id] = options;
  2371. var filterFunc = function(entry) {
  2372. return entry.element != el;
  2373. };
  2374. var draggables = _draggablesByScope[scope] ? _draggablesByScope[scope].filter(filterFunc) : [];
  2375. for (var i = 0; i < draggables.length; i++) {
  2376. draggables[i].droppables.push(el);
  2377. }
  2378. },
  2379. /*
  2380. * you need Drag.Move imported to make drop work.
  2381. */
  2382. isDropSupported : function(el, options) {
  2383. if (typeof Drag != undefined)
  2384. return typeof Drag.Move != undefined;
  2385. return false;
  2386. },
  2387. animate : function(el, properties, options) {
  2388. var m = new jsPlumbMorph(el, options);
  2389. m.start(properties);
  2390. },
  2391. /*
  2392. * takes the args passed to an event function and returns you an object that gives the
  2393. * position of the object being moved, as a js object with the same params as the result of
  2394. * getOffset, ie: { left: xxx, top: xxx }.
  2395. */
  2396. getUIPosition : function(eventArgs) {
  2397. var ui = eventArgs[0];
  2398. return { left: ui.offsetLeft, top: ui.offsetTop };
  2399. },
  2400. getDragObject : function(eventArgs) {
  2401. return eventArgs[0];
  2402. },
  2403. removeElement : function(element, parent) {
  2404. jsPlumb.CurrentLibrary.getElementObject(element).dispose(); // ??
  2405. },
  2406. getScrollLeft : function(el) {
  2407. return null;
  2408. },
  2409. getScrollTop : function(el) {
  2410. return null;
  2411. }
  2412. };
  2413. })();