content.html 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  1. <html>
  2. <head>
  3. <title>jsPlumb 1.3.4 - documentation</title>
  4. <link rel="stylesheet" href="/mp.css">
  5. <link rel="stylesheet" href="jsPlumbDoc.css">
  6. </head>
  7. <body>
  8. <div class="menu">
  9. <a href="http://jsplumb.org" class="mplink" target="_top">view demo</a>
  10. &nbsp;|&nbsp;<a href="mailto:simon.porritt@gmail.com" class="mplink">contact me</a>
  11. &nbsp;|&nbsp;<a href="http://jsplumb.tumblr.com" class="mplink">jsPlumb on Tumblr</a>
  12. </div>
  13. <!--
  14. GWT
  15. http://fishfilosophy.blogspot.com/2011/12/how-to-connect-gwt-widgets-using.html?spref=tw
  16. -->
  17. <div class="content">
  18. <div class="section">
  19. <h3><a id="summary">Summary</a></h3>
  20. jsPlumb allows you to connect elements on the screen using SVG, Canvas or VML, depending on the capabilities of the browser.
  21. <p>
  22. It can be used with jQuery, MooTools or YUI3 (or another library of your choice if you feel like implementing an adapter for it).
  23. </p>
  24. <h5>Browser Compatibility</h5>
  25. <p>jsPlumb 1.3.4 has been tested on the following browsers:
  26. <ul>
  27. <li class="bullet">IE 6 on Windows XP</li>
  28. <li class="bullet">IE 7 on Windows XP</li>
  29. <li class="bullet">IE 8 on Windows XP</li>
  30. <li class="bullet">Firefox 3.5.8 on Windows XP</li>
  31. <li class="bullet">IE 9 on Windows 7</li>
  32. <li class="bullet">Chrome 12 on Windows 7</li>
  33. <li class="bullet">Firefox 3.5.8 on Windows 7</li>
  34. <li class="bullet">Firefox 3.6.3 on Ubuntu 10.04</li>
  35. <li class="bullet">Chrome on Ubuntu 11.04</li>
  36. <li class="bullet">Safari 4 on Mac Leopard</li>
  37. <li class="bullet">Chrome on Mac Leopard</li>
  38. <li class="bullet">Safari 4 on Windows Vista</li>
  39. <li class="bullet">Safari 5.0.5 on Windows 7</li>
  40. <li class="bullet">Opera 10.54 on Windows XP</li>
  41. </ul>
  42. </p>
  43. </div>
  44. <div class="section">
  45. <h3><a id="changes">Changes since version 1.3.3</a></h3>
  46. <p>
  47. Version 1.3.4 is a fairly significant release, containing several bugfixes relating to positioning
  48. when elements with scrollbars or 'overflow:auto' are involved, a new type of connector, "Continuous"
  49. anchors, which are like Dynamic anchors on steroids, beforeDrag/beforeDrop interceptors, and a bunch
  50. of other bits and pieces such as the ability to make an entire div a connection source or target.
  51. </p>
  52. <h5>Backwards Compatibility</h5>
  53. <ul>
  54. <li class="bullet">Starting with this release, jsPlumb <strong>no longer automatically initialises any elements to be
  55. draggable</strong>.
  56. <p>I thought about this long and hard and decided that it was too invasive for jsPlumb to assume that
  57. you wanted every connected element to be draggable. There were other complications, too, such as the
  58. fact that dragging was not initialised until you had at least one connection to an element - you could
  59. start out with a UI in which some elements were draggable, and others were not, and there did not seem,
  60. to the user, to be a decent reason for that.</p>
  61. <p>You should use the <strong>jsPlumb.draggable(some selector)</strong> method to initialise elements
  62. as draggable now. It is still imperative that you use this method and not your underlying library's
  63. drag method, because otherwise jsPlumb will not know what's going on. Valid values for that
  64. selector are whatever your underlying library can support: $("someSelector") for jQuery, $$("someSelector") for MooTools, and
  65. Y.all("someSelector") for YUI3.</p>
  66. </li>
  67. <li class="bullet"><strong>The default renderer is now SVG</strong>, where supported, and VML elsewhere.
  68. <p>I'm of the opinion that SVG is a better option for almost every application using jsPlumb: it
  69. uses far less memory, has native mouse event support so is faster, and can be styled using CSS.</p>
  70. <p>If you really want to use Canvas, call <strong>jsPlumb.setRenderMode(jsPlumb.CANVAS);</strong>.</p>
  71. <p>Be aware that there is a jQuery bug with SVG in IE9 for all jQuery version from 1.6.x to 1.7.2: the hover events
  72. are not properly reported.
  73. </p>
  74. </li>
  75. <li class="bullet">The <strong>detachEverything</strong> method, which was deprecated in 1.3.2, has been removed from jsPlumb. Use detachEveryConnection instead.
  76. It does the same thing.</li>
  77. <li class="bullet">The <strong>detachAll</strong> method, which was deprecated in 1.3.2, has been removed. Use detachAllConnections instead. It does the same thing.
  78. </li>
  79. <li class="bullet">The <strong>labelStyle</strong> parameter has been removed from the Label overlay. You should use the <strong>cssClass</strong> parameter to set a class, and then control the appearance in your stylesheets.</li>
  80. </ul>
  81. <h5>New Features</h5>
  82. <ul>
  83. <li class="bullet">
  84. <p>StateMachine Connectors<p>
  85. These connectors look like the state machine connectors you might have seen in
  86. software like GraphViz. Support for "loopback" connections is included.
  87. </li>
  88. <li class="bullet">
  89. <p>Continuous Anchors<p>
  90. Continuous anchors are anchors whose position is dynamically dependent on every connection
  91. that is attached to some element. If you're familiar with the existing Dynamic
  92. anchors in jsPlumb, these operate in pretty much the same way, with the exception that
  93. you do not need to nominate any specific locations yourself. They choose their position
  94. based on an element's orientation to the other element in a connection, and everything
  95. is ordered to keep the number of crossed connections to a minimum.
  96. <p>For more information, see the <a href="#anchors">anchors</a> section.</p>
  97. </li>
  98. <li class="bullet">
  99. <p>beforeDrag/beforeDrop interceptors<p>
  100. These interceptors allow you to decide whether or not to allow a new connection to be
  101. dragged or dropped before jsPlumb goes ahead with the action. They are documented in
  102. full in the <a href="#interceptors">interceptors</a> section of the documentation.
  103. </li>
  104. <li class="bullet">
  105. <p>makeSource/makeTarget methods</p>
  106. makeSource and makeTarget provide a means for you to nominate some entire element as
  107. a connection source or target. There are many applications in which this functionality
  108. makes more sense than having to register a dedicated Endpoint. Read more about these
  109. in the <a href="#connections">connections</a> section below.
  110. </li>
  111. <li class="bullet"><p>'transparent' is now supported as a fillStyle or strokeStyle.</p></li>
  112. <li class="bullet"><p>Endpoint and Connector definitions now support a <strong>hoverClass</strong> parameter.
  113. This is the hover equivalent of the existing <strong>cssClass</strong> parameter.</p>
  114. </li>
  115. <li class="bullet"><p>The Image Endpoint now has an <strong>onload</strong> parameter, allowing you to supply a callback to run once the image has loaded</p></li>
  116. <li class="bullet"><p>The Image Endpoint now has a <strong>setImage</strong> method, which also takes an optional onload parameter. See the API docs.</p></li>
  117. <li class="bullet"><p>The Image Endpoint now has optional <strong>width</strong> and <strong>height</strong> parameters, allowing you to override the displayed size of the endpoint.</p></li>
  118. <li class="bullet"><p>You can supply a <strong>parameters</strong> object to connect, addEndpoint, makeSource and makeTarget calls. This specifies an object literal of parameter values which will be present
  119. on resulting connections, available via the <strong>getParameter(String)</strong>method</p></li>
  120. </ul>
  121. <h5>Bugfixes for which there were not issues raised</h5>
  122. <ul>
  123. <li class="bullet">A mouse event bug with the SVG renderer was fixed; it was preventing mouse events from
  124. reaching SVG elements that were underneath other SVG elements</li>
  125. </ul>
  126. <h5>Issues Resolved</h5>
  127. <ul>
  128. <!--li><strong>154</strong> - reinstate the 'tooltip' and 'connectorTooltip' parameters</li-->
  129. <li><strong>159</strong> - do not add methods to the Array prototype</li>
  130. <li><strong>158</strong> - use enclosing table element if an Endpoint is added to a table cell.</li>
  131. <li><strong>150</strong> - hover over one Endpoint does not send the event to the other Endpoint in a Connection</li>
  132. <li><strong>145</strong> - add getLabel/setLabel methods to Overlays</li>
  133. <li><strong>142</strong> - document jsPlumb.connectionDetached event properly</li>
  134. <li><strong>139</strong> - duplicate ids are sometimes given to Endpoints and Connections</li>
  135. <li><strong>132</strong> - moving a Label Overlay does not remove its associated element</li>
  136. <li><strong>130</strong> - positioning issues when in div with overflow:scroll</li>
  137. </ul>
  138. </div>
  139. <div class="section">
  140. <h3><a id="init">Setup</a></h3>
  141. <h4><a id="imports">Required Imports</a></h4>
  142. <h4>jQuery</h4>
  143. <ul>
  144. <li class="bullet">jQuery 1.3.x or higher.</li>
  145. <li class="bullet">jQuery UI 1.7.x or 1.8.x (if you wish to support drag and drop).</li>
  146. </ul>
  147. <div class="code">
  148. <pre>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"&gt;&lt;/script&gt;
  149. &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"&gt;&lt;/script&gt;
  150. &lt;script type="text/javascript" src="PATH_TO/jquery.jsPlumb-1.3.4-all-min.js "&gt;&lt;/script&gt;</pre>
  151. </div>
  152. <h4>MooTools</h4>
  153. <ul>
  154. <li class="bullet">MooTools Core 1.2.4 or higher. jsPlumb has been tested with 1.2.4 and 1.3.3.</li>
  155. <li class="bullet">Drag.Move from MooTools More 1.2.4.4 or higher (if you wish to support drag and drop).</li>
  156. </ul>
  157. <div class="code">
  158. <pre>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.3.3/mootools-yui-compressed.js"&gt;&lt;/script&gt;
  159. &lt;script type="text/javascript" src="PATH_TO_MOO_TOOLS_MORE_1_3_2_1"&gt;&lt;/script&gt;
  160. &lt;script type="text/javascript" src="PATH_TO/mootools.jsPlumb-1.3.4-all-min.js "&gt;&lt;/script&gt;</pre>
  161. </div>
  162. <h4>YUI3</h4>
  163. <ul>
  164. <li class="bullet">YUI 3.3.x. jsPlumb has been tested on 3.3.0 only; it is possible other 3.x.x versions will work.</li>
  165. </ul>
  166. <div class="code">
  167. <pre>&lt;script type="text/javascript" src="http://yui.yahooapis.com/3.3.0/build/simpleyui/simpleyui-min.js"&gt;&lt;/script&gt;
  168. &lt;script type="text/javascript" src="PATH_TO/yui.jsPlumb-1.3.4-all-min.js "&gt;&lt;/script&gt;</pre>
  169. </div>
  170. <h4><a id="renderMode">Render Mode</a></h4>
  171. jsPlumb can use SVG, HTML5 Canvas elements or VML to render the various objects it adds to the display. Most modern browsers support
  172. Canvas and SVG; IE&lt;9 browsers support neither.
  173. <p>
  174. By default, jsPlumb attempts to use <strong>jsPlumb.SVG</strong> as the render mode (prior to 1.3.4, the default renderer was Canvas), falling back to <strong>jsPlumb.VML</strong> if the
  175. browser is IE&lt;9. You can change the render mode by making this call:
  176. </p>
  177. <div class="code">
  178. <pre>jsPlumb.setRenderMode(jsPlumb.CANVAS);</pre>
  179. </div>
  180. if, for some reason, you set the render mode to be <strong>jsPlumb.VML</strong> but you're in any browser other than IE&lt;9, jsPlumb
  181. will use SVG.
  182. <p>
  183. The <strong>jsPlumb.setRenderMode</strong> method returns you the render mode that jsPlumb actually ended up setting. Valid values for setRenderMode are:
  184. </p>
  185. <ul>
  186. <li class="bullet">jsPlumb.CANVAS</li>
  187. <li class="bullet">jsPlumb.SVG</li>
  188. <li class="bullet">jsPlumb.VML</li>
  189. </ul>
  190. <h5>Performance Considerations</h5>
  191. <p>
  192. SVG uses less memory than Canvas and has support for mouse events built in, so I recommend using
  193. SVG unless you have a really good reason for wanting Canvas.
  194. </p>
  195. <h4><a id="initializing">Initializing jsPlumb</a></h4>
  196. You should not start making calls to jsPlumb until the DOM has been initialized - perhaps no surprises there. With YUI, though, the asynchronous
  197. nature of the script loading process means that you are not guaranteed that jsPlumb is ready as soon as the DOM is. To handle this,
  198. you should bind to the "ready" event on jsPlumb (or the instance of jsPlumb you are working with):
  199. <div class="code">
  200. <pre>jsPlumb.bind("ready", function() {
  201. ...
  202. // your jsPlumb related init code goes here
  203. ...
  204. });
  205. </pre>
  206. </div>
  207. There's a helper method that can save you approximately 9 (count them!) characters:
  208. <div class="code">
  209. <pre>jsPlumb.ready(function() {
  210. ...
  211. // your jsPlumb related init code goes here
  212. ...
  213. });
  214. </pre>
  215. </div>
  216. <p>
  217. For jQuery and MooTools you do not actually need to do this; by the time the DOM ready event is fired in each of those libraries you can be sure
  218. all the JS we need has been loaded. But in terms of explicitly calling out that you are waiting for jsPlumb, it seems like a good practice to bind to the "ready" event.
  219. </p>
  220. <p>
  221. If you bind to the "ready" event after jsPlumb has already been initialized, your callback will be executed immediately.
  222. </p>
  223. <h4><a id="unload">Unloading jsPlumb</a></h4>
  224. jsPlumb offers a method you can call when your page is unloading. You should do this to insure
  225. against memory leaks. You configure it like this:
  226. <div class="code">
  227. <pre>
  228. &lt;body onunload="jsPlumb.unload();"&gt;
  229. ...
  230. &lt;/body&gt;
  231. </pre>
  232. </div>
  233. <h4><a id="multipleInstances">Multiple jsPlumb instances</a></h4>
  234. jsPlumb is registered on the browser's Window by default, providing one static instance
  235. for the whole page to use. Should you need to, though, you can instantiate independent
  236. instances of jsPlumb, using the <strong>getInstance</strong> method, for example:
  237. <div class="code">
  238. <pre>
  239. var firstInstance = jsPlumb.getInstance();
  240. </pre>
  241. </div>
  242. The variable 'firstInstance' can now be treated exactly as you would treat the 'jsPlumb' variable - you can set
  243. defaults, call the connect method, whatever:
  244. <div class="code">
  245. <pre>
  246. firstInstance.Defaults.Connector = [ "Bezier", { curviness: 150 } ];
  247. firstInstance.Defaults.Anchors = [ "TopCenter", "BottomCenter" ];
  248. firstInstance.connect({source:"element1", target:"element2", scope:"someScope" });
  249. </pre>
  250. </div>
  251. <strong>getInstance</strong> optionally takes an object that provides the defaults:
  252. <div class="code">
  253. <pre>
  254. var secondInstance = jsPlumb.getInstance({
  255. PaintStyle:{ lineWidth:6, strokeStyle:"#567567", outlineColor:"black", outlineWidth:1 },
  256. Connector:[ "Bezier", { curviness: 30 } ],
  257. Endpoint:[ "Dot", { radius:5 } ],
  258. EndpointStyle : { fillStyle: "#567567" },
  259. Anchor : [ 0.5, 0.5, 1, 1 ]
  260. });
  261. secondInstance.connect({ source:"element4", target:"element3", scope:"someScope" });
  262. </pre>
  263. </div>
  264. <h4><a id="zIndex">Z-index Considerations</a></h4>
  265. <p>
  266. You need to pay attention to the z-indices of the various parts of your UI when
  267. using jsPlumb, in particular to ensure that the elements that jsPlumb adds to
  268. the DOM do not overlay other parts of the interface.
  269. </p>
  270. <p>
  271. jsPlumb adds an element to the DOM for each Endpoint, Connector and Overlay. So
  272. for a connection having visible Endpoints at each end and a label in the middle,
  273. jsPlumb adds four elements to the DOM. The actual elements it adds depend on the
  274. renderer in use (Canvas/SVG/VML).
  275. </p>
  276. <p>
  277. To help you organise z-indices correctly, jsPlumb adds a CSS class to each type of
  278. element it adds. They are as follows:
  279. </p>
  280. <table style="color:black;width:70%;font-size:90%;">
  281. <tr><td><strong>type of element</strong></td><td><strong>class added</strong></td></tr>
  282. <tr><td>Endpoint</td><td>_jsPlumb_endpoint</td></tr>
  283. <tr><td>Connector</td><td>_jsPlumb_connector</td></tr>
  284. <tr><td>Overlay</td><td>_jsPlumb_overlay</td></tr>
  285. </table>
  286. <h4><a id="containerDefault">Where does jsPlumb add elements?</a></h4>
  287. <p>
  288. It's important to understand where in the DOM jsPlumb will add any elements it creates,
  289. as it has a bearing on the markup you can use with jsPlumb.
  290. </p>
  291. <p>
  292. Prior to 1.3.0, jsPlumb added everything to the end of the <em>body</em> element. This
  293. has the advantage of being the most flexible arrangement in terms of supporting what
  294. elements can be connected, but in certain use cases produced unexpected results. Consider
  295. the arrangement where you have some connected elements in a tab: you would
  296. expect jsPlumb to add elements inside the tab, so that when the user switches tabs and
  297. the current one is hidden, all the jsPlumb stuff is hidden too. But when the elements are
  298. on the body, this does not happen!
  299. </p>
  300. <p>
  301. For this reason, from 1.3.0 onwards, jsPlumb's default behaviour is to attach Endpoint elements to the parent
  302. of the element the Endpoint is attached to (<strong>not</strong> the actual element
  303. the Endpoint is attached to), and to attach Connector elements to the parent of
  304. the source Endpoint in the Connection. This results in all the elements of the
  305. Connection being siblings inside the same parent node, which jsPlumb requires, for
  306. various reasons arising from the trade off between doing lots of maths and the
  307. performance of the whole thing.
  308. </p>
  309. <p>
  310. In general, the default behaviour means that you cannot do things like this:
  311. </p>
  312. <div class="code">
  313. <pre>
  314. &lt;body&gt;
  315. &lt;div id="container0"&gt;
  316. &lt;div id="node0"&gt;&lt;/div&gt;
  317. &lt;/div&gt;
  318. &lt;div id="container1"&gt;
  319. &lt;div id="node1"&gt;&lt;/div&gt;
  320. &lt;/div&gt;
  321. &lt;/body&gt;
  322. &lt;script type="text/javascript"&gt;
  323. var e0 = jsPlumb.addEndpoint("node0"),
  324. e1 = jsPlumb.addEndpoint("node1");
  325. jsPlumb.connect({ source:e0, target:e1 });
  326. &lt;/script&gt;
  327. </pre>
  328. </div>
  329. <p>
  330. because the elements created for 'e0' and 'e1' would have different parent nodes, and an attempt
  331. to make a Connection between them would not work. For the example just given, you would need to
  332. register the Endpoints on the 'container' divs (and then quite possibly throw away the 'node' divs):
  333. </p>
  334. <div class="code">
  335. <pre>
  336. &lt;body&gt;
  337. &lt;div id="container0"&gt;
  338. &lt;/div&gt;
  339. &lt;div id="container1"&gt;
  340. &lt;/div&gt;
  341. &lt;/body&gt;
  342. &lt;script type="text/javascript"&gt;
  343. var e0 = jsPlumb.addEndpoint("container0"),
  344. e1 = jsPlumb.addEndpoint("container1");
  345. jsPlumb.connect({ source:e0, target:e1 });
  346. &lt;/script&gt;
  347. </pre>
  348. </div>
  349. now 'e0' and 'e1' have the same parent - the <em>body</em> element.
  350. <p>Remember that you can use the <strong>anchor</strong> parameter to an <strong>addEndpoint</strong>
  351. call to specify where on an element you want an Endpoint to appear. Used in conjunction with
  352. CSS classes (discussed below), you can have an Endpoint on top of some element wherever you like.
  353. </p>
  354. <p><strong>Note regarding the drawEndpoints option on jsPlumb.connect</strong>: with the default behaviour, jsPlumb uses the offsetParent of
  355. the source endpoint in a connection to make final adjustments to the position of a connector. When drawEndpoints is set to false, there is no
  356. offsetParent of the source endpoint because it is not visible. If your connection lies inside some container other than the document body,
  357. the connector will not be able to take that container's offset into account, and will most likely not be in the right place. You should either
  358. use the "Blank" endpoint when you don't want to see one, or instruct jsPlumb to attach everything to the document body (see below).
  359. </p>
  360. <h5>Overriding the default behaviour</h5>
  361. <p>
  362. From version 1.3.3, the <strong>container</strong> concept was reintroduced into jsPlumb, because there are use cases
  363. in which the default behaviour makes it difficult or impossible to build the UI you want. You can instruct jsPlumb to use some
  364. element as the parent of everything jsPlumb adds to the UI through usage of the <strong>jsPlumb.Defaults.Container</strong> property,
  365. or you can set a <strong>container</strong> parameter on calls to <strong>addEndpoint</strong> or <strong>connect</strong> (if this is set
  366. and a default is also set, this default value is actually used. This may seem counter-intuitive, and could of course be changed if
  367. there is feedback from the community that this would be a good idea). Some examples:
  368. </p>
  369. Set a container to use as the default container, using a jQuery selector (you can supply MooTools/YUI selectors if that's the library you're using),
  370. and then add an Endpoint. The canvas (or SVG/VML element) created will be a child of the document body:
  371. <div class="code">
  372. <pre>
  373. jsPlumb.Defaults.Container = $("body");
  374. ...
  375. jsPlumb.addEndpoint(someDiv, { endpoint options });
  376. </pre>
  377. </div>
  378. Set a container to use as the default container, using an element id,
  379. and then connect two elements. The elements created in this example will be children of the element with id "containerId":
  380. <div class="code">
  381. <pre>
  382. jsPlumb.Defaults.Container = "containerId";
  383. ...
  384. jsPlumb.connect({ source:someDiv, target:someOtherDiv });
  385. </pre>
  386. </div>
  387. Pass a container to use into a jsPlumb.connect call, using a selector. The elements created in this example will be children of the element with id "containerId":
  388. <div class="code">
  389. <pre>
  390. jsPlumb.connect({ source:someDiv, target:someOtherDiv, container:$("#containerId") });
  391. </pre>
  392. </div>
  393. Pass a container to use into a jsPlumb.addEndpoint call, using an element id. The element created in this example will be a child of the element with id "containerId":
  394. <div class="code">
  395. <pre>
  396. jsPlumb.addEndpoint(someDiv, { ..., container:"containerId" });
  397. </pre>
  398. </div>
  399. </div>
  400. <div class="section">
  401. <h3><a id="jsPlumbBasics">Basic Concepts</a></h3>
  402. jsPlumb is all about connecting things together, so the core abstraction in jsPlumb is the <strong>Connection</strong> object, which is itself
  403. broken down into these four concepts:
  404. <ul>
  405. <li><strong>Anchor</strong> - a location, relative to an element's origin, at which an Endpoint can exist. You do not create these yourself; you
  406. supply hints to the various jsPlumb functions, which create them as needed. They have no visual representation; they are a logical position only.
  407. Anchors can be referenced by name, for the Anchors that jsPlumb ships with, or a four element array describing [ x, y, x orientation, y orientation ].
  408. See the <a href="#anchors">anchors</a> section for more detail.<br/><br/></li>
  409. <li><strong>Endpoint</strong> - the visual representation of one end of a Connection. You can create and attach these to elements
  410. yourself, which you are required to do to support drag and drop, or have jsPlumb create them when creating a Connection
  411. programmatically using jsPlumb.connect(...). You can also join two Endpoints programmatically, by passing them as arguments to
  412. jsPlumb.connect(...).<br/><br/></li>
  413. <li><strong>Connector</strong> - the visual representation of the line connecting two elements in the page. jsPlumb has four types of these
  414. available as defaults - a Bezier curve, a straight line, 'flowchart' connectors and 'state machine connectors. You do not interact with Connectors;
  415. you just specify definitions of them when you need to - see <a href="#definitions">Connector, Endpoint &amp; Overlay Definitions</a> <br/><br/></li>
  416. <li><strong>Overlay</strong> - a UI component that is used to decorate a Connector, such as a Label, Arrow, etc.<br/><br/></li>
  417. </ul>
  418. <p>One Connection is made up of two Endpoints, a Connector, and zero or more Overlays working together to join two elements.</p>
  419. <p>jsPlumb's public API exposes only Connection and Endpoint, handling the creation and configuration of everything else internally.
  420. But you still need to be across the concepts encapsulated by Anchor, Connector and Overlay.
  421. </p>
  422. </div>
  423. <div class="section">
  424. <h3><a id="anchors">Anchors</a></h3>
  425. An Anchor models the notion of where on an element a Connector should connect. There are three main types of Anchors:
  426. <ul>
  427. <li class="bullet">'Static' anchors - these are fixed to some point on an element and do not move. They can be specified using a string to
  428. identify one of the defaults that jsPlumb ships with, or an array describing the location (see below)</li>
  429. <li class="bullet">'Dynamic' anchors - these are lists of Static anchors from which jsPlumb picks the most appropriate one each time a Connection
  430. is painted. The algorithm used to determine the most appropriate anchor picks the one that is closest to the center of the other element in
  431. the Connection. A future version of jsPlumb might support a pluggable algorithm to make this decision.</li>
  432. <li class="bullet">'Continuous' anchors - These anchors, introduced in 1.3.4, are not fixed to any specific loction; they are assigned to one of
  433. the four faces of an element depending on that element's orientation to the other element in the associated Connection. Continuous anchors are
  434. slightly more computationally intensive than Static or Dynamic anchors because jsPlumb is required to calculate the position of every Connection
  435. during a paint cycle, rather than just Connections belonging to the element in motion. These anchors were designed for use with the StateMachine
  436. connector that was also introduced in 1.3.4, but will work with any combination of Connector and Endpoint.</li>
  437. </ul>
  438. <h4><a id="staticAnchors">Static Anchors</a></h4>
  439. jsPlumb has nine default anchor locations you can use to specify where the Connectors connect to elements: these are the four corners of an element,
  440. the center of the element, and the midpoint of each edge of the element:
  441. <ul>
  442. <li class="bullet">TopCenter</li>
  443. <li class="bullet">TopRight</li>
  444. <li class="bullet">RightMiddle</li>
  445. <li class="bullet">BottomRight</li>
  446. <li class="bullet">BottomCenter</li>
  447. <li class="bullet">BottomLeft</li>
  448. <li class="bullet">LeftMiddle</li>
  449. <li class="bullet">TopLeft</li>
  450. </ul>
  451. Each of these string representations is just a wrapper around the underlying array-based syntax [x, y, dx, dy], where x and y are
  452. coordinates in the interval [0,1] specifying the position of the
  453. anchor, and dx and dy are coordinates in the interval [-1, 1]
  454. specifying the orientation of the curve incident to the anchor. For
  455. example, [0, 0.5, -1, 0] defines a "LeftCenter" anchor with a
  456. connector curve that emanates leftward from the anchor. Similarly,
  457. [0.5, 0, 0, -1] defines a "CenterTop" anchor with a connector curve
  458. emanating upwards.
  459. <div class="code">
  460. <pre>
  461. jsPlumb.connect({...., anchor:"BottomCenter", ... });
  462. </pre>
  463. </div>
  464. is identical to:
  465. <div class="code">
  466. <pre>
  467. jsPlumb.connect({...., anchor:[ 0.5, 1, 0, 1 ], ... });
  468. </pre>
  469. </div>
  470. <h4><a id="dynamicAnchors">Dynamic Anchors</a></h4>
  471. These are Anchors that can be positioned in one of a number of locations, choosing the one that is most appropriate each time something moves or is painted in the UI.
  472. <p>
  473. There is no special syntax to creating a DynamicAnchor; you just provide an array of individual Static Anchor specifications, eg:
  474. </p>
  475. <div class="code">
  476. <pre>
  477. var dynamicAnchors = [ [ 0.2, 0, 0, -1 ], [ 1, 0.2, 1, 0 ],
  478. [ 0.8, 1, 0, 1 ], [ 0, 0.8, -1, 0 ] ];
  479. </pre>
  480. </div>
  481. <h4> Default Dynamic Anchor</h4>
  482. jsPlumb provides a dynamic anchor called "AutoDefault" that chooses from TopCenter, RightMiddle, BottomCenter and LeftMiddle:
  483. <div class="code">
  484. <pre>jsPlumb.connect({...., anchor:"AutoDefault", ... });</pre>
  485. </div>
  486. <h4> Location Selection</h4>
  487. The initial implementation of the algorithm that decides which location to choose just calculates which location is closest to the center of the
  488. other element in the connection. It is possible that future versions of jsPlumb could support more sophisticated choice algorithms, if the need arose.
  489. <h4>Draggable Connections</h4>
  490. Dynamic Anchors and Draggable Connections can interoperate: jsPlumb locks the position of a dynamic anchor when you start to drag a connection from it,
  491. and unlocks it once the connection is either established or abandoned. At that point you may see the position of the dynamic anchor change, as jsPlumb
  492. optimises the connection. <p>You can see this behaviour in the <a href="../html/jquery/draggableConnectorsDemo.html" target="_blank">draggable connections</a> demonstration, when
  493. you drag a connection from the blue endpoint on window 1 to the blue endpoint on window 3 - the connection is established and then window 1's blue
  494. endpoint jumps down to a location that is closer to window 3.</p>
  495. <h4><a id="continuousAnchors">Continuous Anchors</a></h4>
  496. As discussed above, these are anchors whose positions are calculated by jsPlumb according to the orientation between elements in a Connection, and also how
  497. many other Continuous anchors happen to be sharing the element. You specify that you want to use Continuous anchors using the string syntax you would use
  498. to specify one of the default Static Anchors, for example:
  499. <div class="code">
  500. <pre>
  501. jsPlumb.connect({
  502. source:someDiv,
  503. target:someOtherDiv,
  504. anchor:"Continuous"
  505. });
  506. </pre>
  507. </div>
  508. Note in this example I specified only "anchor", rather than "anchors" - jsPlumb will use the same spec for both anchors. But I could have said this:
  509. <div class="code">
  510. <pre>
  511. jsPlumb.connect({
  512. source:someDiv,
  513. target:someOtherDiv,
  514. anchors:["BottomCenter", "Continuous"]
  515. });
  516. </pre>
  517. </div>
  518. ..which would have resulted in the source element having a Static Anchor at BottomCenter. In practise, though, it seems the Continuous Anchors work best if both
  519. elements in a Connection are using them.
  520. <p>Note also that Continuous Anchors can be specified on <strong>addEndpoint</strong> calls:</p>
  521. <div class="code">
  522. <pre>
  523. jsPlumb.addEndpoint(someDiv, {
  524. anchor:"Continuous",
  525. paintStyle:{ fillStyle:"red" }
  526. });
  527. </pre>
  528. </div>
  529. ... and in the jsPlumb defaults:
  530. <div class="code">
  531. <pre>
  532. jsPlumb.Defaults.Anchor = "Continuous";
  533. </pre>
  534. </div>
  535. </div>
  536. <div class="section">
  537. <h4><a id="definitions">Connector, Endpoint &amp; Overlay Definitions</a></h4>
  538. Before we discuss Connector, Endpoints and Overlays, a quick word on definitions: whenever you need to define a Connector, Endpoint or Overlay, you must use a "definition" of it, rather than constructing one
  539. directly. This definition can be either a string that nominates the artifact you want to create:
  540. <div class="code">
  541. <pre>jsPlumb.connect({
  542. source:"someDiv",
  543. target:"someOtherDiv",
  544. endpoint:"Rectangle"
  545. });
  546. </pre>
  547. </div>
  548. or an array consisting of both the artifact's name and the arguments you want to pass to its constructor:
  549. <div class="code">
  550. <pre>jsPlumb.connect({
  551. source:"someDiv",
  552. target:"someOtherDiv",
  553. endpoint:[ "Rectangle", { cssClass:"myEndpoint", width:30, height:10 } ]
  554. });
  555. </pre>
  556. </div>
  557. there is also a three-argument method that allows you to specify two sets of parameters, which jsPlumb will merge together for you. The idea
  558. behind this is that you will often want to define common characteristics somewhere and reuse them across a bunch of different calls:
  559. <div class="code">
  560. <pre>var common = {
  561. cssClass : "myCssClass",
  562. hoverClass : "myHoverClass"
  563. };
  564. jsPlumb.connect({
  565. source:"someDiv",
  566. target:"someOtherDiv",
  567. endpoint:[ "Rectangle", { width:30, height:10 }, common ]
  568. });
  569. </pre>
  570. </div>
  571. This syntax is supported for all Endpoint, Connector and Overlay definitions. Here's an example using definitions for all three:
  572. <div class="code">
  573. <pre>var common = {
  574. cssClass:"myCssClass"
  575. };
  576. jsPlumb.connect({
  577. source:"someDiv",
  578. target:"someOtherDiv",
  579. endpoint:[ "Dot", { radius:5, hoverClass:"myEndpointHover" }, common ],
  580. connector:[ "Bezier", { curviness:100 }, common ],
  581. overlays: [
  582. [ "Arrow", { foldback:0.2 }, common ],
  583. [ "Label", { cssClass:"labelClass" } ]
  584. ]
  585. });
  586. </pre>
  587. </div>
  588. The allowed constructor parameters are different for each artifact you create, but every artifact takes a single JS object as argument,
  589. with the parameters as [key,value] pairs in that object. See the relevant sections on <a href="#connectors">Connectors</a>,
  590. <a href="#endpoints">Endpoints</a> and <a href="#overlays">Overlays for information on supported parameters.</a>
  591. </div>
  592. <div class="section">
  593. <h3><a id="connectors">Connectors</a></h3>
  594. <p>Connectors are the lines that actually join elements of the UI. jsPlumb has four connector implementations - a straight line, a Bezier curve,
  595. "flowchart", and "state machine". The default connector is the Bezier curve. </p>
  596. <p>You optionally specify a Connector by setting the 'connector' property on a call to jsPlumb.connect, jsPlumb.addEndpoint(s), jsPlumb.makeSource or
  597. jsPlumb.makeTarget. If you do not supply a value for 'connector', the default will be used (see <a href="#defaults">defaults</a>).</p>
  598. <p>You specify Connectors using the syntax described above in <a href="#definitions">Connector, Endpoint &amp; Overlay Definitions.</a>Allowed
  599. constructor values for each Connector type are described below:</p>
  600. <h4><a id="bezierConnector">Bezier Connector</a></h4>
  601. <p>The Bezier Connector provides a cubic Bezier path between the two Endpoints. It supports a single constructor argument:</p>
  602. <p>
  603. <strong>curviness</strong> - Optional; defaults to 150. This defines the distance in pixels that
  604. the Bezier's control points are situated from the anchor points. This does not mean that your
  605. connector will pass through a point at this distance from your curve. It is a hint to how you want the
  606. curve to travel. Rather than discuss Bezier curves at length here, because they are a complex topic,
  607. we refer you to <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve" target="_blank">Wikipedia.</a>
  608. </p>
  609. <h4><a id="straightConnector">Straight Connector</a></h4>
  610. <p>The Straight Connector draws a straight line between the two endpoints. No constructor arguments are supported; use the <strong>endpointStyle</strong>
  611. argument to a connect or addEndpoint call to control the appearance of one of these Connectors.
  612. </p>
  613. <h4><a id="flowchartConnector">Flowchart Connector</a></h4>
  614. <p>This type of Connector draws a connection that consists of a series of vertical or horizontal segments - the classic flowchart look. A single constructor argument is supported:</p>
  615. <strong>stub</strong> - this is the minimum length, in pixels, of the initial stub that emanates from an Endpoint. This parameter is optional, and defaults to 30 pixels.
  616. <h4><a id="stateMachineConnector">State Machine Connector</a></h4>
  617. <p>This Connector draws slightly curved lines (they are actually quadratic Bezier curves), similar to the State Machine connectors you may have
  618. seen in software like GraphViz. Connections in which some element is both the source and the target (so-called 'loopback') are supported by these
  619. Connectors, in which case you get a circle. These are the only Connectors for which the loopback case is supported. Supported parameters are:
  620. </p>
  621. <p>
  622. <strong>margin</strong> - Optional; defaults to 5. Defines the distance from the element that the connector begins/ends.
  623. </p>
  624. <p>
  625. <strong>curviness</strong> - Optional, defaults to 10. This has a similar effect to the curviness parameter on Bezier curves.
  626. </p>
  627. <p>
  628. <strong>proximityLimit</strong> - Optional, defaults to 80. The minimum distance between the two ends of the Connector before it paints itself as
  629. a straight line rather than a quadratic Bezier curve.
  630. </p>
  631. </div>
  632. <div class="section">
  633. <h3><a id="endpoints">Endpoint Types</a></h3>
  634. <p>An Endpoint is the UI component that marks the location of an Anchor, ie. the place where a Connector joins an
  635. element. jsPlumb comes with four Endpoint implementations - Dot, Rectangle, Blank and Image. You optionally specify Endpoint properties using the
  636. <strong>endpoint</strong> parameter in a call to jsPlumb.connect, jsPlumb.addEndpoint, jsPlumb.makeSource or jsPlumb.makeTarget.
  637. </p>
  638. <p>As with Connectors and Overlays, you specify Endpoints using the syntax described above in <a href="#definitions">Connector, Endpoint &amp; Overlay Definitions.</a></p>
  639. The four available Endpoint types, and their constructor arguments, are as follows:
  640. <h4><a id="dotEndpoint">Dot Endpoint</a></h4>
  641. This Endpoint draw a dot on the screen. It supports three constructor parameters:
  642. <p>
  643. <strong>radius</strong> - Optional; defaults to 10 pixels. Defines the radius of the dot.
  644. </p>
  645. <p>
  646. <strong>cssClass</strong> - Optional. A CSS class to attach to the element the Endpoint creates.
  647. </p>
  648. <p>
  649. <strong>hoverClass</strong> - Optional. A CSS class to attach to the element the Endpoint creates whenever the mouse is hovering over the
  650. element or an attached Connection.
  651. </p>
  652. <h4><a id="rectangleEndpoint">Rectangle Endpoint</a></h4>
  653. Draws a rectangle. Supported constructor parameters are:
  654. <p>
  655. <strong>width</strong> - Optional; defaults to 20 pixels. Defines the width of the rectangle.
  656. </p>
  657. <p>
  658. <strong>height</strong> - Optional; defaults to 20 pixels. Defines the height of the rectangle.
  659. </p>
  660. <p>
  661. <strong>cssClass</strong> - Optional. A CSS class to attach to the element the Endpoint creates.
  662. </p>
  663. <p>
  664. <strong>hoverClass</strong> - Optional. A CSS class to attach to the element the Endpoint creates whenever the mouse is hovering over the
  665. element or an attached Connection.
  666. </p>
  667. <h4><a id="imageEndpoint">Image Endpoint</a></h4>
  668. Draws an image from a given URL. This Endpoint supports three constructor parameters:
  669. <p>
  670. <strong>src</strong> - Required. Specifies the URL of the image to use.
  671. </p>
  672. <p>
  673. <strong>cssClass</strong> - Optional. A CSS class to attach to the element the Endpoint creates.
  674. </p>
  675. <p>
  676. <strong>hoverClass</strong> - Optional. A CSS class to attach to the element the Endpoint creates whenever the mouse is hovering over the
  677. element or an attached Connection.
  678. </p>
  679. <h4><a id="blankEndpoint">Blank Endpoint</a></h4>
  680. Does not draw anything visible to the user. However, a div element is added to the UI and so this Endpoint also support the css parameters:
  681. <p>
  682. <strong>cssClass</strong> - Optional. A CSS class to attach to the element the Endpoint creates.
  683. </p>
  684. <p>
  685. <strong>hoverClass</strong> - Optional. A CSS class to attach to the element the Endpoint creates whenever the mouse is hovering over the
  686. element or an attached Connection.
  687. </p>
  688. </div>
  689. <div class="section">
  690. <h3><a id="overlays">Overlay Types</a></h3>
  691. <p>
  692. Overlays are UI elements that are painted onto connections, such as labels or arrows.
  693. jsPlumb comes with four defaults:
  694. </p>
  695. <ul>
  696. <li><strong>Arrow</strong> - a configurable arrow that is painted at some point along the connector. You can control the length and width of the Arrow, the 'foldback' point - a point the tail points fold back into, and the direction (allowed values are 1 and -1; 1 is the default and means point in the direction of the connection)</li>
  697. <li><strong>Label</strong> - a configurable label that is painted at some point along the connector.</li>
  698. <li><strong>PlainArrow</strong> - an Arrow shaped as a triangle, with no foldback.</li>
  699. <li><strong>Diamond</strong> - as the name suggests, a diamond.</li>
  700. </ul>
  701. The last two are actually just configured instances of the generic Arrow overlay (see examples).
  702. <p>All Overlays support these two methods for getting/setting their location (location is a number between 0 and 1 inclusive, indicating some point along the path
  703. inscribed by the associated Connector):</p>
  704. <ul>
  705. <li class="bullet"><strong>getLocation</strong> - returns the current location</li>
  706. <li class="bullet"><strong>setLocation</strong> - sets the current location; valid values are decimals in the range 0-1 inclusive</li>
  707. </ul>
  708. <p>You can specify one or more overlays when making a call to jsPlumb.connect, jsPlumb.addEndpoint or jsPlumb.makeSource (but not jsPlumb.makeTarget; overlays
  709. are always derived from what the source of a Connection defines) The three cases are discussed below:</p>
  710. 1. Specifying one or more overlays on a jsPlumb.connect call. In this example we'll create an Arrow with the default options for an Arrow, and
  711. a label with the text "foo":
  712. <div class="code">
  713. <pre>jsPlumb.connect({
  714. ...
  715. overlays:[
  716. "Arrow",
  717. [ "Label", { label:"foo", location:0.25 }, id:"myLabel" ]
  718. ],
  719. ...
  720. });
  721. </pre>
  722. </div>
  723. This connection will have an arrow located halfway along it, and the label "foo" one quarter of the way along. Notice the <strong>id</strong>
  724. parameter; it can be used later if you wish to remove the Overlay or change its visibility (see below).<br/><br/>
  725. 2. Specifying one or more overlays on a jsPlumb.addEndpoint call. Note in this example that we use the parameter 'connectorOverlays' and not
  726. 'overlays' as in the last example. This is because 'overlays' will one day be used to support Endpoint Overlays:
  727. <div class="code">
  728. <pre>jsPlumb.addEndpoint("someDiv", {
  729. ...
  730. connectorOverlays:[
  731. [ "Arrow", { width:10, length:30, location:1, id:"arrow" } ],
  732. [ "Label", { label:"foo", id:"label" } ]
  733. ],
  734. ...
  735. });</pre>
  736. </div>
  737. This connection will have a 10x30 Arrow located right at the head of the connection, and the label "foo" located at the halfway point. Again,
  738. note that <strong>id</strong> parameter passed in to each Overlay spec.<br/><br/>
  739. 3. Specifying one or more overlays on a jsPlumb.makeSource call. Note in this example that we again use the parameter 'connectorOverlays' and not
  740. 'overlays'. The 'endpoint' parameter to jsPlumb.makeSource supports everything you might pass to the second argument of a jsPlumb.addEndpoint call:
  741. <div class="code">
  742. <pre>jsPlumb.makeSource("someDiv", {
  743. ...
  744. endpoint:{
  745. connectorOverlays:[
  746. [ "Arrow", { width:10, length:30, location:1, id:"arrow" } ],
  747. [ "Label", { label:"foo", id:"label" } ]
  748. ]
  749. }
  750. ...
  751. });</pre>
  752. </div>
  753. This connection will have a 10x30 Arrow located right at the head of the connection, and the label "foo" located at the halfway point. Again,
  754. note that <strong>id</strong> parameter passed in to each Overlay spec.<br/><br/>
  755. <h4><a id="arrowOverlay">Arrow Overlay</a></h4>
  756. <p>This overlay draws an arrow, using four points: the head and two tail points, and a 'foldback' point, which permits the tail
  757. of the arrow to be indented. Available constructor arguments for this Overlay are </p>
  758. <ul>
  759. <li><strong>width</strong> - width of the tail of the arrow</li>
  760. <li><strong>length</strong> - distance from the tail of the arrow to the head</li>
  761. <li><strong>location</strong> - where, as a proportional value from 0 to 1 inclusive, the Arrow should appear on the Connector</li>
  762. <li><strong>direction</strong> - which way to point. Allowed values are 1 (the default, meaning forwards) and -1, meaning backwards </li>
  763. <li><strong>foldback</strong> - how far along the axis of the arrow the tail points foldback in to. Default is 0.623.</li>
  764. <li><strong>paintStyle</strong> - a style object in the form used for paintStyle values for Endpoints and Connectors</li>
  765. </ul>
  766. <h4><a id="plainArrowOverlay">PlainArrow Overlay</a></h4>
  767. <p>This is just a specialized instance of Arrow in which jsPlumb hardcodes 'foldback' to 1, meaning the tail of the Arrow is a flat edge. All of the constructor parameters from Arrow apply for PlainArrow.</p>
  768. <h4><a id="diamondOverlay">Diamond Overlay</a></h4>
  769. <p>This is a specialized instance of Arrow in which jsPlumb hardcodes 'foldback' to 2, meaning the Arrow turns into a Diamond. All of the constructor parameters from Arrow apply for PlainArrow.</p>
  770. <h4><a id="labelOverlay">Label Overlay</a></h4>
  771. This provides a text label to decorate Connectors with. The available constructor arguments are:
  772. <ul>
  773. <li><strong>label</strong> - The text to display. You can provide a function here instead of plain text: it is passed the Connection as an argument, and it should return a String.</li>
  774. <li><strong>cssClass</strong> - Optional css class to use for the Label. This is now preferred over using the 'labelStyle' parameter.</li>
  775. <li><strong>labelStyle</strong> - (deprecated, use cssClass instead) Optional arguments for the label's appearance. Valid entries in this JS object are:
  776. <ul>
  777. <li><em>font</em> - a font string in a format suitable for the Canvas element</li>
  778. <li><em>fillStyle</em> - the color to fill the label's background with. Optional.</li>
  779. <li><em>color</em> - the color of the label's text. Optional.</li>
  780. <li><em>padding</em> - optional padding for the label. This is expressed as a proportion of the width of the label, not in pixels or ems.</li>
  781. <li><em>borderWidth</em> - optional width in pixels for the label's border. Defaults to 0.</li>
  782. <li><em>borderStyle</em> - optional. The color to paint the border, if there is one.</li>
  783. </ul>
  784. </li>
  785. <li><strong>location</strong> - As for Arrow Overlay. Where, proportionally from 0 to 1 inclusive, the label should appear.</li>
  786. </ul>
  787. <h4><a id="hideshowOverlay">Hiding/Showing Overlays</a></h4>
  788. <p>You can control the visibility of Overlays using the <strong>setVisible</strong> method of Overlays themselves, or with
  789. <strong>showOverlay(id)</strong> or <strong>hideOverlay(id)</strong> on a Connection.</p>
  790. <p>Remember the <strong>id</strong> parameter that we specified in the examples above? This can be used to retrieve the Overlay
  791. from a Connection:</p>
  792. <div class="code">
  793. <pre>var connection = jsPlumb.connect({
  794. ...
  795. overlays:[
  796. "Arrow",
  797. [ "Label", { label:"foo", location:0.25 }, id:"myLabel" ]
  798. ],
  799. ...
  800. });
  801. // time passes
  802. var overlay = connection.getOverlay("myLabel");
  803. // now you can hide this Overlay:
  804. overlay.setVisible(false);
  805. // there are also hide/show methods:
  806. overlay.show();
  807. overlay.hide();
  808. </pre>
  809. </div>
  810. <p>However, Connection also has two convenience methods you could use instead:
  811. <div class="code">
  812. <pre>var connection = jsPlumb.connect({
  813. ...
  814. overlays:[
  815. "Arrow",
  816. [ "Label", { label:"foo", location:0.25 }, id:"myLabel" ]
  817. ],
  818. ...
  819. });
  820. // time passes
  821. connection.hideOverlay("myLabel");
  822. // more time passes
  823. connection.showOverlay("myLabel");
  824. </pre>
  825. </div>
  826. <h4><a id="hideshowOverlay">Removing Overlays</a></h4>
  827. <p>Connection also has a <strong>removeOverlay</strong> method, that does what you might expect:
  828. <div class="code">
  829. <pre>var connection = jsPlumb.connect({
  830. ...
  831. overlays:[
  832. "Arrow",
  833. [ "Label", { label:"foo", location:0.25 }, id:"myLabel" ]
  834. ],
  835. ...
  836. });
  837. // time passes
  838. connection.removeOverlay("myLabel");
  839. </pre>
  840. </div>
  841. </div>
  842. <div class="section">
  843. <h3><a id="defaults">Defaults</a></h3>
  844. The easiest way to set a look and feel for your plumbing is to override the defaults that jsPlumb uses. If you
  845. do not do this you are forced to provide your overridden values on every call. Every argument to the connect and addEndpoint methods has an
  846. associated default value in jsPlumb.<br/><br/>
  847. The defaults that ship with jsPlumb are stored in <em>jsPlumb.Defaults</em>, which is a Javascript object. Valid entries, and their initial values, are:
  848. <div class="code">
  849. <pre>
  850. Anchor : "BottomCenter",
  851. Anchors : [ null, null ],
  852. Connector : "Bezier",
  853. DragOptions : { },
  854. DropOptions : { },
  855. Endpoint : "Dot",
  856. Endpoints : [ null, null ],
  857. EndpointStyle : { fillStyle : null },
  858. EndpointStyles : [ null, null ],
  859. EndpointHoverStyle : null,
  860. EndpointHoverStyles : [ null, null ],
  861. HoverPaintStyle : null,
  862. LabelStyle : { color : "black" },
  863. LogEnabled : false,
  864. Overlays : [ ],
  865. MaxConnections : 1,
  866. MouseEventsEnabled : true,
  867. PaintStyle : { lineWidth : 8, strokeStyle : "#456" },
  868. RenderMode : "svg",
  869. Scope : "_jsPlumb_DefaultScope"
  870. </pre>
  871. </div>
  872. <p>
  873. Note that in EndpointStyle, the default fillStyle is 'null'. This instructs jsPlumb to use the strokeStyle
  874. from the attached connector to fill the endpoint.</p>
  875. <p>Note also that you can specify either or both (or neither) of 'EndpointStyle' and 'EndpointStyles'. This allows you to specify a different
  876. end point for each end of a connection. 'Endpoint' and 'Endpoints' use the same concept. jsPlumb will look first in the
  877. individual endpoint/endpoint style arrays, and then fall back to the single default version.</p>
  878. you can override these defaults by including this in a script somewhere:
  879. <div class="code">
  880. <pre>
  881. jsPlumb.Defaults.PaintStyle = {
  882. lineWidth:13,
  883. strokeStyle: 'rgba(200,0,0,100)'
  884. }
  885. jsPlumb.Defaults.DragOptions = { cursor: "crosshair" };
  886. jsPlumb.Default.Endpoints = [ [ "Dot", 7 ], [ "Dot", 11 ] ];
  887. jsPlumb.Defaults.EndpointStyles = [{ fillStyle:"#225588" }, { fillStyle:"#558822" }];</pre>
  888. </div>
  889. after the jsPlumb script has been loaded of course! Here we have specified the following default behaviour:
  890. <ul>
  891. <li>- connectors are 13 pixels wide and painted with a semi-transparent red line</li>
  892. <li>- when dragging an element the crosshair cursor is used</li>
  893. <li>- the source endpoint is a dot of radius 7; the target endpoint is a dot of radius 11</li>
  894. <li>- the source endpoint is blue; the target endpoint is green</li>
  895. </ul>
  896. </div>
  897. <div class="section">
  898. <h3><a id="simpleConnections">Connections</a></h3>
  899. <h4><a id="simpleConnections">Programmatic Connections</a></h4>
  900. The most simple connection you can make with jsPlumb looks like this:
  901. <div class="code">
  902. <pre>
  903. jsPlumb.connect({source:"element1", target:"element2"});</pre>
  904. </div>
  905. In this example we have created a Connection from 'element1' to 'element2'. Remember that a Connection in jsPlumb consists of two Endpoints,
  906. a Connector, and zero or more Overlays. But this call to 'connect' supplied none of those things, so jsPlumb uses the default values wherever it needs to.
  907. In this case, default values have been used for the following:
  908. <ul>
  909. <li>- The type and appearance of each Endpoint in the Connection. jsPlumb's default for this is the "Dot" endpoint, of radius 10, with fill color "#456".</li>
  910. <li>- The Anchors that define where the connection's Endpoints appear on each element. The jsPlumb default is "BottomCenter"</li>
  911. <li>- Whether or not each Endpoint can be a source or target for new Connections. The default is false.</li>
  912. <li>- The type and appearance of the Connection's Connector. The default is a "Bezier" connector of line width 8, and color "#456".</li>
  913. </ul>
  914. <p>
  915. So this call will result in an 8px Bezier, colored "#456", from the bottom center of 'element1' to the bottom center of 'element2', and each
  916. Endpoint will be a 10px radius Dot Endpoint, colored "#456".
  917. </p>
  918. <p>
  919. Let's beef up this call a little and tell jsPlumb what sort of Endpoints we want, and where we want them:
  920. </p>
  921. <div class="code">
  922. <pre>
  923. jsPlumb.connect({
  924. source:"element1",
  925. target:"element2",
  926. anchors:["RightMiddle", "LeftMiddle" ],
  927. endpoint:"Rectangle",
  928. endpointStyle:{ fillStyle: "yellow" }
  929. });
  930. </pre>
  931. </div>
  932. <p>This is what we have told jsPlumb we want this time:</p>
  933. <ul>
  934. <li><strong>anchors</strong> - this array tells jsPlumb where the source and target Endpoints should be located on their parent elements. In this case,
  935. we use the shorthand syntax to name one of jsPlumb's default anchors; you can also specify custom locations (see <a href="#anchors">anchors</a>).
  936. Instead of <strong>anchors</strong> you can use <strong>anchor</strong>, if you want the source and target Endpoints to be located at the
  937. same place on their parent elements. <br/>
  938. </li>
  939. <li>
  940. <strong>endpoint</strong> - this tells jsPlumb to use the "Rectangle" Endpoint for both the source and target of the Connection. As with anchors,
  941. <strong>endpoint</strong> has a plural version that allows you to specify a different Endpoint for each end of the Connection.<br/>
  942. </li>
  943. <li>
  944. <strong>endpointStyle</strong> - this is the definition of the appearance of the Endpoint you specified above. Again, there is a
  945. plural equivalent of this that allows you to specify a different style for each end of the Connection. For more information about
  946. allowed values for this value, see <a href="#paintstyles">Connector, Endpoint & Overlay Styles</a>.
  947. </li>
  948. </ul>
  949. <h5>Reusing common settings between jsPlumb.connect calls</h5>
  950. <p>
  951. A fairly common situation you will find yourself in is wanting to create a bunch of Connections that have only minor differences
  952. between them. To support that, <strong>jsPlumb.connect</strong> takes an optional second argument. For example:
  953. </p>
  954. <div class="code">
  955. <pre>
  956. var common = {
  957. anchors:[ "BottomCenter", "TopCenter" ],
  958. endpoints:["Dot", "Blank" ]
  959. };
  960. jsPlumb.connect({ source:"someElement", target:"someOtherElement" }, common);
  961. jsPlumb.connect({ source:"aThirdElement", target:"yetAnotherElement" }, common);
  962. </pre>
  963. </div>
  964. <h5>Endpoints created by jsPlumb.connect</h5>
  965. <p>If you supply an element id or selector for either the source or target , jsPlumb.connect will automatically create an Endpoint on the
  966. given element. These automatically created Endpoints are not marked as drag source or targets, and cannot
  967. be interacted with. For some situations this behaviour is perfectly fine, but for more interactive UIs you should set things up using the drag and drop
  968. method discussed below.</p>
  969. <p>Given that jsPlumb.connect creates its own Endpoints in some circumstances, in order to avoid leaving orphaned Endpoints around the place, if the Connection is subsequently
  970. deleted, these created Endpoints are deleted too. Should you want to, you can override this behaviour by setting <strong>deleteEndpointsOnDetach</strong> to
  971. false in the connect call:</p>
  972. <div class="code">
  973. <pre>
  974. jsPlumb.connect({
  975. source:"aThirdElement",
  976. target:"yetAnotherElement",
  977. deleteEndpointsOnDetach:false
  978. });
  979. </pre>
  980. </div>
  981. <h4><a id="draggableConnections">Connections using Drag and Drop</a></h4>
  982. To support drag and drop connections, you first need to set a few things up. Every drag and drop connection needs at least a source
  983. Endpoint that the user can drag a connection from. Here's a simple example of how to create an Endpoint:
  984. <div class="code">
  985. <pre>
  986. var endpointOptions = { isSource:true };
  987. var endpoint = jsPlumb.addEndpoint('elementId', endpointOptions);
  988. </pre>
  989. </div>
  990. This Endpoint will act as a source for new Connections, and will use the jsPlumb defaults for its own appearance and that of any Connections that are drawn from it.
  991. <h5>Tip: use the three-argument addEndpoint method for common data </h5>
  992. One thing that happens quite often is that you have an Endpoint whose appearance and behaviour is largely the
  993. same between usages on different elements, with just a few differences.
  994. <div class="code">
  995. <pre>
  996. var exampleGreyEndpointOptions = {
  997. endpoint:"Rectangle",
  998. paintStyle:{ width:25, height:21, fillStyle:'#666' },
  999. isSource:true,
  1000. connectorStyle : { strokeStyle:"#666" },
  1001. isTarget:true
  1002. };
  1003. </pre>
  1004. </div>
  1005. Notice there is no 'anchor' set. Here we apply it to two elements, at a different location in each:
  1006. <div class="code">
  1007. <pre>
  1008. jsPlumb.addEndpoint('element1', { anchor:"BottomCenter" }, exampleGreyEndpointOptions));
  1009. jsPlumb.addEndpoint('element2', { anchor:"TopCenter" }, exampleGreyEndpointOptions));
  1010. </pre>
  1011. </div>
  1012. <p>
  1013. Now that you have a source Endpoint, you need to either create a target Endpoint on some element, or notify jsPlumb that you wish to make an
  1014. entire element a drop target. Let's look at how to attach a target Endpoint first:
  1015. </p>
  1016. <div class="code">
  1017. <pre>var endpointOptions = { isTarget:true, endpoint:"Rectangle", paintStyle:{ fillStyle:"gray" } };
  1018. var endpoint = jsPlumb.addEndpoint("otherElementId", endpointOptions);
  1019. </pre>
  1020. </div>
  1021. <p>
  1022. This Endpoint, a gray rectangle, has declared that it can act as a drop target for Connections.
  1023. </p>
  1024. <p>
  1025. jsPlumb also supports marking entire elements as drag sources or drag targets, using the <strong>makeSource</strong> and <strong>makeTarget</strong> methods. This
  1026. is a useful function for certain applications. Each of these methods takes an Endpoint declaration which defines the Endpoint that jsPlumb will create after a Connection
  1027. has been attached. Let's take a look at makeTarget first:
  1028. </p>
  1029. <h5>jsPlumb.makeTarget</h5>
  1030. <p>
  1031. This method takes two arguments, the first of which specifies some element (or list of elements); the second specifies the Endpoint you wish to create on
  1032. that element whenever a Connection is established on it. In this example we will use the exact same target Endpoint we used before - the gray
  1033. rectangle - but we will tell jsPlumb that the element "aTargetDiv" will be the drop target:
  1034. </p>
  1035. <div class="code">
  1036. <pre>var endpointOptions = {
  1037. isTarget:true,
  1038. endpoint:"Rectangle",
  1039. paintStyle:{ fillStyle:"gray" }
  1040. };
  1041. jsPlumb.makeTarget("aTargetDiv", endpointOptions);
  1042. </pre>
  1043. </div>
  1044. <p>
  1045. The allowed values in 'endpointOptions' are identical for both the <strong>jsPlumb.addEndpoint</strong> and <strong>jsPlumb.makeTarget</strong> methods, but makeTarget supports an extended
  1046. anchor syntax that allows you more control over the location of the target endpoint. This is discussed below.
  1047. </p>
  1048. <h5>Target Anchors positions with makeTarget</h5>
  1049. <p>When using the makeTarget method, jsPlumb allows you to provide a callback function to be used to determine
  1050. the appropriate location of a target anchor for every new Connection dropped on the given target. It may be the
  1051. case that you want to take some special action rather than just relying on one of the standard anchor mechanisms.
  1052. </p>
  1053. <p>This is achieved through an extended anchor syntax (note that this syntax is <strong>not supported</strong> in
  1054. the <strong>jsPlumb.addEndpoint</strong> method) that supplies a "positionFinder" to the anchor specification. jsPlumb
  1055. provides two of these by default; you can register your own on jsPlumb and refer to them by name, or just
  1056. supply a function. Here's a few examples:</p>
  1057. Instruct jsPlumb to place the target anchor at the exact location at which the mouse button was released on the
  1058. target element. Note that you tell jsPlumb the anchor is of type "Assign", and you then provide a "position"
  1059. parameter, which can be the name of some position finder, or a position finder function itself:
  1060. <div class="code">
  1061. <pre>
  1062. jsPlumb.makeTarget("someElement", {
  1063. endpoint:{
  1064. anchor:[ "Assign", {
  1065. position:"Fixed"
  1066. }]
  1067. }
  1068. });
  1069. </pre>
  1070. </div>
  1071. <p><strong>Fixed</strong> is one of the two default position finders provided by jsPlumb. The other is <strong>Grid</strong>:
  1072. <div class="code">
  1073. <pre>
  1074. jsPlumb.makeTarget("someElement", {
  1075. endpoint:{
  1076. anchor:[ "Assign", {
  1077. position:"Grid",
  1078. grid:[3,3]
  1079. }]
  1080. }
  1081. });
  1082. </pre>
  1083. </div>
  1084. The Grid position finder takes a "grid" parameter that defines the size of the grid required. [3,3] means
  1085. 3 rows and 3 columns.
  1086. <p>To supply your own position finder to jsPlumb you first need to create the callback function. It takes
  1087. N arguments and does stuff. This paragraph obviously needs to be finished.</p>
  1088. <h5>jsPlumb.makeSource</h5>
  1089. There are two use cases supported by this method. The first is the case that you want to drag a Connection from the element itself and have an Endpoint attached to
  1090. the element when a Connection is established. The second is a more specialised case: you want to drag a Connection from the element, but once the Connection is
  1091. established you want jsPlumb to move it so that its source is on some other element. For an example of this second case, check out the <a href="">WHERE IS THIS State Machine Connectors Demo</a>
  1092. <p>Here's an example code snippet for the basic use case of makeSource:</p>
  1093. <div class="code">
  1094. <pre>
  1095. jsPlumb.makeSource(someDiv, {
  1096. paintStyle:{ fillStyle:"yellow" },
  1097. endpoint:"Blank",
  1098. anchor:"BottomCenter"
  1099. });
  1100. </pre>
  1101. </div>
  1102. <p>
  1103. Notice again that the second argument is the same as the second argument to an addEndpoint call. makeSource is, essentially, a type of addEndpoint call. In this example
  1104. we have told jsPlumb that we will support dragging Connections directly from 'someDiv'. Whenever a Connection is established between 'someDiv' and some other element,
  1105. jsPlumb assigns an Endpoint at BottomCenter of 'someDiv', fills it yellow, and sets that Endpoint as the newly created Connection's source.
  1106. </p>
  1107. <p>Configuring an element to be an entire Connection source using makeSource means that the element cannnot itself be draggable. There would be no way for jsPlumb to distinguish
  1108. between the user attempting to drag the element and attempting to drag a Connection from the element. If your UI works in such a way that that is acceptable then you'll
  1109. be fine to use this method. But if you want to drag Connections from some element but also have the element be draggable itself, you might want to consider the second
  1110. use case. First, a code snippet:</p>
  1111. <div class="code">
  1112. <pre>
  1113. jsPlumb.makeSource("someConnectionSourceDiv", {
  1114. paintStyle:{ fillStyle:"yellow" },
  1115. endpoint:"Blank",
  1116. anchor:"BottomCenter",
  1117. parent:"someDiv"
  1118. });
  1119. </pre>
  1120. </div>
  1121. <p>The only difference here is the inclusion of the 'parent' parameter. It instructs jsPlumb to configure the source Endpoint on the element specified - in this case,
  1122. "someDiv" (the value of this parameter may be a String id or a selector).</p>
  1123. <p>Again, I'd suggest taking a look at the <a href="../demo/jquery/stateMachineDemo.html">State Machine Connectors demonstration</a> for an example of this.</p>
  1124. <p>
  1125. There are many things you can set in an Endpoint options object; for a thorough list see the API documentation for Endpoint.
  1126. </p>
  1127. <p>Here's an example of specifying that you want an Arrow overlay halfway along any Connection dragged from this Endpoint:</p>
  1128. <div class="code">
  1129. <pre>
  1130. var exampleGreyEndpointOptions = {
  1131. endpoint:"Rectangle",
  1132. paintStyle:{ width:25, height:21, fillStyle:'#666' },
  1133. isSource:true,
  1134. connectorStyle : { strokeStyle:"#666" },
  1135. isTarget:true,
  1136. connectorOverlays: [ [ "Arrow", { location:0.5 } ] ]
  1137. };
  1138. </pre>
  1139. </div>
  1140. This is an Endpoint that moves around the element it is attached to dependent on the location of other elements in the connections it is attached to
  1141. (a 'dynamic' anchor):
  1142. <div class="code">
  1143. <pre>
  1144. var exampleGreyEndpointOptions = {
  1145. endpoint:"Rectangle",
  1146. paintStyle:{ width:25, height:21, fillStyle:'#666' },
  1147. isSource:true,
  1148. connectorStyle : { strokeStyle:"#666" },
  1149. isTarget:true,
  1150. connectorOverlays: [ [ "Arrow", { location:0.5 } ] ]
  1151. anchor:[ "TopCenter","RightMiddle","BottomCenter","LeftMiddle" ]
  1152. };
  1153. </pre>
  1154. </div>
  1155. <h5><a id="dragOptions">Drag Options</a></h5>
  1156. These are options that will be passed through to the supporting library's drag API. jsPlumb passes everything you supply here through, inserting
  1157. wrapping functions if necessary for the various lifecycle events that jsPlumb needs to know about. So if, for example, you pass a function to be
  1158. called when dragging starts, jsPlumb will wrap that function with a function that does what jsPlumb needs to do, then call yours.
  1159. <p>
  1160. At the time of writing, jsPlumb supports jQuery, MooTools and YUI3, and each of those libraries uses different terminology. In addition, jQuery's API is
  1161. more fully featured, providing easy support for setting the zIndex and opacity of elements being dragged, as well as the 'scope' for a drag/drop (allowing
  1162. you to specify more than one type of drag/drop pair), and hover classes for when a draggable is on the move or
  1163. over a droppable. If you're using jQuery you can of course just supply these values on the dragOptions; to make it easier, jsPlumb's MooTools and YUI3 adapters recognize
  1164. these options and add appropriate callbacks for you.
  1165. </p>
  1166. <p>
  1167. Given that the options here are library-specific, and they are all well-documented, we're going to discuss just the three drag options
  1168. that behave the same way in all (see below for hoverClass):
  1169. <ul>
  1170. <li><strong>opacity</strong> - the opacity of an element that is being dragged. Should be a fraction between 0 and 1 inclusive.</li>
  1171. <li><strong>zIndex</strong> - the zIndex of an element that is being dragged.</li>
  1172. <li><strong>scope</strong> - the scope of the draggable. can only be dropped on a droppable with the same scope. this is discussed below.</li>
  1173. </ul>
  1174. </p>
  1175. <p>For more information about drag options, take a look at the <a href="http://docs.jquery.com/UI/Draggable" target="_blank">jQuery</a>, <a href="http://mootools.net/docs/more/Drag/Drag.Move" target="_blank">MooTools</a>, or <a href="http://developer.yahoo.com/yui/3/dd/" target="_blank">YUI3</a> docs.
  1176. </p>
  1177. <p><strong>NOTE: there is an issue in Chrome that affects the 'cursor' argument to drag options in jQuery. See these links:</strong>
  1178. </p><p>
  1179. <a href="http://forum.jquery.com/topic/draggable-cursor-option-does-not-work" target="_blank">http://forum.jquery.com/topic/draggable-cursor-option-does-not-work</a><br/>
  1180. <a href="http://forum.jquery.com/topic/chrome-text-select-cursor-on-drag" target="_blank">http://forum.jquery.com/topic/chrome-text-select-cursor-on-drag</a>
  1181. </p>
  1182. You should put something like this at the top of your JS to avoid it (perhaps not something so drastic as a document-wide override):
  1183. <div class="code">
  1184. <pre>document.onselectstart = function () { return false; };</pre>
  1185. </div>
  1186. <h5><a id="dropOptions">Drop Options</a></h5>
  1187. Drop options are treated by jsPlumb in the same way as drag options - they are passed through to the underlying library. MooTools does not have drop options like jQuery and YUI3 do; droppable functionality in MooTools
  1188. is actually implemented by the Drag.Move class - the one used to initialise a draggable. But when you setup an Endpoint in jsPlumb you should ignore
  1189. that fact, and treat droppables like you would in jQuery or YUI3. jsPlumb wires everything up for you under the hood.
  1190. <p>There are three jQuery droppable options that jsPlumb treats as shortcuts in MooTools and YUI3, for the sake of consistency:</p>
  1191. <ul>
  1192. <li><strong>hoverClass</strong> - the CSS class to attach to the droppable when a draggable is hovering over it.</li>
  1193. <li><strong>activeClass</strong> - the CSS class to attach to the droppable when a draggable is, um, being dragged.</li>
  1194. <li><strong>scope</strong> - the scope of the draggable. The draggable can only be dropped on a droppable with the same scope. This is discussed below.</li>
  1195. </ul>
  1196. <p>For more information about drop options when using jQuery, see <a href="http://docs.jquery.com/UI/Droppable" target="_blank">here</a>.</p>
  1197. <h5><a id="dragAndDropScope">Drag and Drop Scope</a></h5>
  1198. jsPlumb borrowed the concept of 'scope' from jQuery's drag/drop implementation: the notion of
  1199. which draggables can be dropped on which droppables. In jsPlumb you can provide a 'scope' entry when
  1200. creating an Endpoint. Here's the example grey Endpoint example with 'scope' added:
  1201. <div class="code">
  1202. <pre>
  1203. var exampleGreyEndpointOptions = {
  1204. endpoint:"Rectangle",
  1205. paintStyle:{ width:25, height:21, fillStyle:"#666" },
  1206. isSource:true,
  1207. connectionStyle : { strokeStyle:"#666" },
  1208. isTarget:true,
  1209. scope:"exampleGreyConnection"
  1210. };</pre>
  1211. </div>
  1212. If you do not provide a 'scope' entry, jsPlumb uses a default scope. Its value is accessible through this method:
  1213. <div class="code">
  1214. <pre>
  1215. jsPlumb.getDefaultScope();
  1216. </pre>
  1217. </div>
  1218. If you want to change it for some reason you can do so with this method:
  1219. <div class="code">
  1220. <pre>
  1221. jsPlumb.setDefaultScope("mySpecialDefaultScope");
  1222. </pre>
  1223. </div>
  1224. You can also, should you want to, provide the scope value separately on the drag/drop options, like this:
  1225. <div class="code">
  1226. <pre>
  1227. var exampleGreyEndpointOptions = {
  1228. endpoint:"Rectangle",
  1229. painStyle:{ width:25, height:21, fillStyle:'#666' },
  1230. isSource:true,
  1231. connectionStyle : { strokeStyle:"#666" },
  1232. isTarget:true,
  1233. dragOptions:{ scope:"dragScope" },
  1234. dropOptions:{ scope:"dropScope" }
  1235. };</pre>
  1236. </div>
  1237. </div>
  1238. <div class="section">
  1239. <h3><a id="beforeDrop">Interceptors</a></h3>
  1240. <p>
  1241. Interceptors are new to jsPlumb 1.3.4. They are basically event handlers from which you can return a value that tells jsPlumb to abort what it is that
  1242. it was doing. There are currently two interceptors supported - <strong>beforeDrop</strong>, which is called when the user has dropped a Connection onto some target,
  1243. and <strong>beforeDetach</strong>, which is called when the user is attempting to detach a Connection.
  1244. </p>
  1245. <p>Interceptors can be registered via the <strong>bind</strong> method on jsPlumb just like any other event listeners, and they can also be passed in to
  1246. the <strong>jsPlumb.addEndpoint</strong>, <strong>jsPlumb.makeSource</strong> and <strong>jsPlumb.makeTarget</strong>
  1247. methods. A future version of jsPlumb will support registering interceptors on Endpoints using the <strong>bind</strong> method as you can now with the jsPlumb object.
  1248. Note that binding 'beforeDrop' (as an example) on jsPlumb itself is like a catch-all: it will be called every time a Connection is dropped on _any_
  1249. Endpoint. But passing a 'beforeDrop' callback into an Endpoint (and in the future, registering one using 'bind') constrains that callback to just the
  1250. Endpoint in question.
  1251. <h4><a id="beforeDrop">Before Drop</a></h4>
  1252. This is called when the user has released the mouse button over a Connection target. Your callback is passed the
  1253. a JS object containing these fields:
  1254. <ul>
  1255. <li>sourceId</li>
  1256. <li>targetId</li>
  1257. <li>scope</li>
  1258. </ul>
  1259. <p>Returning false - or nothing - from this callback will cause the drag to be abandoned, and the new Connection removed from the UI.</p>
  1260. <h4><a id="beforeDetach">Before Detach</a></h4>
  1261. This is called when the user has detached a Connection, which can happen for a number of reasons: by default, jsPlumb allows users to drag Connections
  1262. off of target Endpoints, but this can also result from a programmatic 'detach' call. Every case is treated the same by jsPlumb, so in fact it is possible
  1263. for you to write code that attempts to detach a Connection but then denies itself! You might want to be careful with that.
  1264. <p>Note that this interceptor is passed the actual Connection object; this is different from the beforeDrop
  1265. interceptor discussed above: in this case, we've already got a Connection, but with beforeDrop we are yet
  1266. to confirm that a Connection should be created.</p>
  1267. <p>Returning false - or nothing - from this callback will cause the detach to be abandoned, and the Connection will be reinstated/left on its current target.</p>
  1268. </div>
  1269. <div class="section">
  1270. <h3><a id="paintstyles">Paint Styles</a></h3>
  1271. Defining the appearance of Connectors, Endpoints (and Overlays, but this is deprecated in favour of using CSS classes) is achieved through a 'paintStyle' (or a quite similar name...keep reading)
  1272. object passed as a parameter to one of jsPlumb.connect, jsPlumb.addEndpoint, jsPlumb.makeSource or jsPlumb.makeTarget. Depending on the method you are
  1273. calling, the parameter names vary.
  1274. <h5>Connector Paint Styles</h5>
  1275. These are specified in a <em>paintStyle</em> parameter on a call to <strong>jsPlumb.connect</strong>:
  1276. <div class="code">
  1277. <pre>jsPlumb.connect({
  1278. source:"el1",
  1279. target:"el2",
  1280. paintStyle:{ strokeStyle:"blue", lineWidth:10 }
  1281. });
  1282. </pre>
  1283. </div>
  1284. or in the <em>connectorPaintStyle</em> parameter on a call to <strong>jsPlumb.addEndpoint</strong> or <strong>jsPlumb.makeSource</strong>:
  1285. <div class="code">
  1286. <pre>jsPlumb.addEndpoint("el1", {
  1287. paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1288. connectorPaintStyle:{ strokeStyle:"blue", lineWidth:10 }
  1289. });
  1290. </pre>
  1291. </div>
  1292. <div class="code">
  1293. <pre>jsPlumb.makeSource("el1", {
  1294. paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1295. connectorPaintStyle:{ strokeStyle:"blue", lineWidth:10 }
  1296. });
  1297. </pre>
  1298. </div>
  1299. Notice the <em>paintStyle</em> parameter in those examples: it is the paint style for the Endpoint, which we'll discuss below.
  1300. <h5>Endpoint Paint Styles</h5>
  1301. These are specified in a <em>paintStyle</em> parameter on a call to <strong>jsPlumb.addEndpoint</strong>. This is the example from
  1302. just above:
  1303. <div class="code">
  1304. <pre>jsPlumb.addEndpoint("el1", {
  1305. paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1306. connectorPaintStyle:{ strokeStyle:"blue", lineWidth:10 }
  1307. });
  1308. </pre>
  1309. </div>
  1310. ...or as the <em>endpointStyle</em> parameter to a <strong>jsPlumb.connect</strong> call:
  1311. <div class="code">
  1312. <pre>jsPlumb.connect({
  1313. source:"el1",
  1314. target:"el2",
  1315. endpointStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1316. paintStyle:{ strokeStyle:"blue", lineWidth:10 }
  1317. });
  1318. </pre>
  1319. </div>
  1320. ... or as an entry in the <em>endpointStyles</em> array passed to a <strong>jsPlumb.connect</strong> call:
  1321. <div class="code">
  1322. <pre>jsPlumb.connect({
  1323. source:"el1",
  1324. target:"el2",
  1325. endpointStyles:[
  1326. { fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1327. { fillStyle:"green" }
  1328. ],
  1329. paintStyle:{ strokeStyle:"blue", lineWidth:10 }
  1330. });
  1331. </pre>
  1332. </div>
  1333. or as the <em>paintStyle</em> entry in an <em>endpoint</em> parameter passed to a <strong>jsPlumb.makeTarget</strong> or <strong>jsPlumb.makeSource</strong> call:
  1334. <div class="code">
  1335. <pre>jsPlumb.makeTarget("el1", {
  1336. endpoint: {
  1337. paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }
  1338. }
  1339. });
  1340. </pre>
  1341. </div>
  1342. <div class="code">
  1343. <pre>jsPlumb.makeSource("el1", {
  1344. endpoint: {
  1345. paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }
  1346. },
  1347. parent:"someOtherDivIJustPutThisHereToRemindYouYouCanDoThis"
  1348. });
  1349. </pre>
  1350. </div>
  1351. In the first example we made "el1" into a drop target, and defined a paint style for the Endpoint jsPlumb will create when a Connection is
  1352. established. In the second we made "el1" a connection source and assigned the same values for the Endpoint jsPlumb will create when a Connection
  1353. is dragged from that element.
  1354. <h5>Overlay Paint Styles</h5>
  1355. The preferred way to set paint styles for Overlays is to use the <em>cssClass</em> parameter in the constructor arguments of an Overlay definition.
  1356. <h4>Paint Style Parameters</h4>
  1357. This is the full list of parameters you can set in a paintStyle object, but note that <strong>fillStyle</strong> is ignored by Connectors,
  1358. and <strong>strokeStyle</strong> is ignored by Endpoints. Also, if you create a Connection using jsPlumb.connect and do not specify any
  1359. Endpoint styles, the Endpoints will derive their fillStyle from the Connector's strokeStyle.
  1360. <p>
  1361. fillStyle, strokeStyle and outlineColor can be specified using any valid CSS3 syntax.
  1362. </p>
  1363. <ul>
  1364. <li><strong>fillStyle</strong> - color for an Endpoint, eg. rgba(100,100,100,50), "blue", "#456", "#993355", rgb(34, 56, 78).</li>
  1365. <li><strong>strokeStyle</strong> - color for a Connector. see fillStyle examples.</li>
  1366. <li><strong>lineWidth</strong> - width of a Connector's line. An integer.</li>
  1367. <li><strong>outlineWidth</strong> - width of the outline for an Endpoint or Connector. An integer.</li>
  1368. <li><strong>outlineColor</strong> - color of the outline for an Endpoint or Connector. see fillStyle examples.</li>
  1369. <li><strong>dashstyle</strong> - VML and SVG only. This comes from VML, and allows you to create dashed or dotted lines. It has a
  1370. better syntax than the equivalent attribute in SVG (<em>stroke-dasharray</em>, discussed below), so jsPlumb supports this for both renderers.
  1371. The <em>dashstyle</em> attribute is specified as an array of strokes and spaces, where each value is some multiple of <em>the width of
  1372. the Connector</em>, and that's where it's better than SVG, which uses pixels.
  1373. <p>
  1374. <a href="http://www.w3.org/TR/NOTE-VML" target="_blank">The VML spec</a> is a good place to find valid values for dashstyle. Note that
  1375. jsPlumb does not support the string values for this attribute ("solid", "dashdot", etc).
  1376. </p>
  1377. <p>
  1378. In SVG render mode, jsPlumb uses the <em>lineWidth</em> parameter in conjunction with the values in a <em>dashstyle</em> attribute
  1379. to create an appropriate value for <em>stroke-dasharray</em>.
  1380. </p>
  1381. </li>
  1382. <li><strong>stroke-dasharray</strong> - SVG only. This is the SVG equivalent of <em>dashstyle</em>.
  1383. <a href="http://www.w3.org/TR/SVG/painting.html" target="_blank">The SVG spec</a> discusses valid values for this parameter. But be aware
  1384. that jsPlumb does not convert this into a valid <em>dashstyle</em> attribute when using the VML renderer. Better to use <em>dashstyle</em>.
  1385. </li>
  1386. <li><strong>stroke-dashoffset</strong> - SVG only. This is used in SVG to specify how far into the dash pattern to start painting. For more information,
  1387. see <a href="http://www.w3.org/TR/SVG/painting.html" target="_blank">the SVG spec.</a></li>
  1388. <li><strong>joinstyle</strong> - VML and SVG only. As with <em>dashstyle</em>, this is a VML attribute that jsPlumb supports for both VML
  1389. and SVG - jsPlumb turns this into a <em>stroke-linejoin</em> attribute when rendering with SVG. This attribute specifies how you want individual
  1390. segments of connectors to be joined; the VML and SVG specs both have examples of this, of which many are the same between the two, which is
  1391. why jsPlumb will automatically convert this attribute into the SVG equivalent.
  1392. </li>
  1393. <li><strong>stroke-linejoin</strong> - SVG only. This is the equivalent of VML's <em>joinstyle</em> attribute, but as with <em>stroke-dasharray</em>,
  1394. jsPlumb does not convert this into something approriate for VML. So, using <em>joinstyle</em> will enable you to support more browsers with
  1395. less effort.
  1396. </li>
  1397. </ul>
  1398. <h3><a id="hoverpaintstyles">Hover Paint Styles</a></h3>
  1399. Connectors and Endpoints both support the concept of a "hover" paint style - a paint style to use when the mouse is hovering over
  1400. the component. These are specified in the exact same format as paint styles discussed above, but hover paint styles also inherit
  1401. any values from the main paint style. This is because you will typically want to just change the color, or perhaps outline color, of
  1402. a Connector or Endpoint when the mouse is hovering, but leave everything else the same. So having hover paint styles inherit their
  1403. values precludes you from having to define things in more than one place.
  1404. <p>
  1405. The naming convention adopted for hover paint styles is pretty much to insert the word 'hover' into the corresponding main paint
  1406. style parameters. Here are a couple of examples:
  1407. </p>
  1408. <div class="code">
  1409. <pre>jsPlumb.connect({
  1410. source:"el1",
  1411. target:"el2",
  1412. paintStyle:{ strokeStyle:"blue", lineWidth:10 },
  1413. hoverPaintStyle:{ strokeStyle:"red" },
  1414. endpointStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1415. endpointHoverStyle:{ fillStyle:"red" }
  1416. });
  1417. </pre>
  1418. </div>
  1419. In this example we specified a hover style for both the Connector, and each of its Endpoints. Here's the same thing, but using the plural
  1420. version, to specify a different hover style for each Endpoint:
  1421. <div class="code">
  1422. <pre>jsPlumb.connect({
  1423. source:"el1",
  1424. target:"el2",
  1425. paintStyle:{ strokeStyle:"blue", lineWidth:10 },
  1426. hoverPaintStyle:{ strokeStyle:"red" },
  1427. endpointStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1428. endpointHoverStyles:[ { fillStyle:"red" }, { fillStyle:"yellow" } ]
  1429. });
  1430. </pre>
  1431. </div>
  1432. Calls to <strong>jsPlumb.addEndpoint</strong> and <strong>jsPlumb.makeTarget</strong> can also specify various hover paint styles:
  1433. <div class="code">
  1434. <pre>jsPlumb.addEndpoint("el1", {
  1435. paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1436. hoverPaintStyle:{ fillStyle:"red" },
  1437. connectorPaintStyle:{ strokeStyle:"blue", lineWidth:10 },
  1438. connectorHoverPaintStyle:{ strokeStyle:"red", outlineColor:"yellow", outlineWidth:1 }
  1439. });
  1440. </pre>
  1441. </div>
  1442. Here we specified a hover paint style for both the Endpoint we are adding, and any Connections that are made from the Endpoint.
  1443. <p><strong>jsPlumb.makeTarget</strong> also supports hover paint styles - here's the example from before, with one applied:</p>
  1444. <div class="code">
  1445. <pre>jsPlumb.makeTarget("el1", {
  1446. endpoint: {
  1447. paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 },
  1448. hoverPaintStyle:{ fillStyle:"red" }
  1449. }
  1450. });
  1451. </pre>
  1452. </div>
  1453. Note that <strong>jsPlumb.makeTarget</strong> does not support Connector parameters. It is for creating targets only; Connector parameters
  1454. will be set by the source Endpoint in any Connections that are made to the element that you turned into a target by using this method.
  1455. <h4><a id="gradients">Gradients</a></h4>
  1456. The Canvas and SVG renderers both support gradients. The VML renderer does not. jsPlumb uses its own syntax to define
  1457. gradients, to abstract out the differences between the syntax required by canvas and that required by SVG.
  1458. <p>There are two types of gradients available - a 'linear' gradient, which consists of colored lines all
  1459. going in one direction, and a 'radial' gradient, which consists of colored circles emanating from one circle to another.
  1460. Because of their basic shape, jsPlumb supports only <em>linear</em> gradients for
  1461. Connectors. But for Endpoints, jsPlumb supports both <em>linear</em> and <em>radial</em> gradients.</p>
  1462. <h5>Connector gradients</h5>
  1463. To specify a linear gradient to use in a Connector, you must add a <em>gradient</em> object to your
  1464. Connector's <em>paintStyle</em>, for instance:
  1465. <div class="code">
  1466. <pre>jsPlumb.connect({
  1467. source : "window2",
  1468. target : "window3",
  1469. paintStyle:{
  1470. gradient:{
  1471. stops:[[0,"green"], [1,"red"]]
  1472. },
  1473. lineWidth:15
  1474. }
  1475. });
  1476. </pre>
  1477. </div>
  1478. Here we have connected window2 to window3 with a 15 pixel wide connector that has a gradient from green to red.</p><p> Notice the <strong>gradient</strong> object and the
  1479. <strong>stops</strong> list inside it - the gradient consists of an arbitrary number of these "color stops". Each color stop is comprised
  1480. of two values - [position, color]. Position must be a decimal value between 0 and 1 (inclusive), and indicates where the color
  1481. stop is situated as a fraction of the length of the entire gradient. Valid values for
  1482. the colors in the <strong>stops</strong> list are the same as those that are valid for <strong>strokeStyle</strong> when describing a color.
  1483. </p>
  1484. As mentioned, the <strong>stops</strong> list can hold an arbitrary number of entries. Here's an example of a gradient that goes from red to blue to green, and back again through blue to red:
  1485. <div class="code">
  1486. <pre>jsPlumb.connect({
  1487. source : 'window2',
  1488. target : 'window3',
  1489. paintStyle : {
  1490. gradient:{
  1491. stops:[[0,'red'], [0.33,'blue'], [0.66,'green'], [0.33,'blue'], [1,'red']]
  1492. },
  1493. lineWidth : 15
  1494. }
  1495. });
  1496. </pre>
  1497. </div>
  1498. <strong>Note:</strong> when using the VML renderer, jsPlumb will simply ignore the gradient directive
  1499. so it is best to ensure you also supply a <em>strokeStyle</em> in your paintStyle object, to give jsPlumb something to fall back on. If you do not supply
  1500. a <em>strokeStyle</em> your Connectors will be painted black. The previous example might look like this, for instance:
  1501. <div class="code">
  1502. <pre>jsPlumb.connect({
  1503. source : 'window2',
  1504. target : 'window3',
  1505. paintStyle:{
  1506. strokeStyle:'red',
  1507. gradient:{
  1508. stops:[[0,'red'], [0.33,'blue'], [0.66,'green'], [0.33,'blue'], [1,'red']]
  1509. },
  1510. lineWidth:15
  1511. }
  1512. });
  1513. </pre>
  1514. </div>
  1515. Notice the <strong>strokeStyle:'red'</strong> directive at the beginning of the parameter list in <strong>paintStyle</strong>.
  1516. <h5>Endpoint gradients</h5>
  1517. Endpoint gradients are specified using the same syntax as Connector gradients. You put the gradient specifier either in the
  1518. <strong>endpoint</strong> member, or if you are specifying different Endpoints for each end of the Connector, in one or both of
  1519. the values in the <strong>endpoints</strong> array. Also, this information applies to the case that you are creating standalone
  1520. Endpoints that you will be configuring for drag and drop creation of new Connections.
  1521. <p>
  1522. This is an example of an Endpoint gradient that is different for each Endpoint in the Connector. This comes from the main demo; it is
  1523. the Connector joining Window 2 to Window 3:
  1524. </p>
  1525. <div class="code">
  1526. <pre>var w23Stroke = 'rgb(189,11,11)';
  1527. jsPlumb.connect({
  1528. source : 'window2',
  1529. target : 'window3',
  1530. paintStyle:{
  1531. lineWidth:8,
  1532. strokeStyle:w23Stroke
  1533. },
  1534. anchors:[ [0.3,1,0,1], "TopCenter" ],
  1535. endpoint:"Rectangle",
  1536. endpointStyles:[
  1537. { gradient : {stops:[[0, w23Stroke], [1, '#558822']] } },
  1538. { gradient : {stops:[[0, w23Stroke], [1, '#882255']] } }
  1539. ]
  1540. });
  1541. </pre>
  1542. </div>
  1543. The first entry in the gradient will be the one that is on the Connector end of the Endpoint. You can of course have as many color stops as
  1544. you want in this gradient, just like with Connector gradients.
  1545. <h5>Applying the gradient in Endpoints</h5>
  1546. Only the Dot and Rectangle endpoints honour the presence of a gradient (and, remember, not in VML). The Image endpoint of course ignores a gradient
  1547. as it does no painting of its own.
  1548. <p>The type of gradient you will see depends on the Endpoint type:</p>
  1549. <ul>
  1550. <li><strong>Dot</strong> - renders a radial endpoint, with color stop 0 on the outside, progressing inwards as we move through color stops.
  1551. <p>Radial gradients actually require more data than linear gradients - in a linear gradient we just move from one point to another, whereas
  1552. in a radial gradient we move from one <em>circle</em> to another. By default, jsPlumb will render a radial gradient using a source
  1553. circle of the same radius as the Endpoint itself, and a target circle of 1/3 of the radius of the Endpoint (both circles share the
  1554. same center as the Endpoint itself). This circle will be offset by radius/2 in each direction.</p>
  1555. <p>You can supply your own values for these inside the gradient descriptor:</p>
  1556. <div class="code">
  1557. <pre>var w34Stroke = 'rgba(50, 50, 200, 1)';
  1558. var w34HlStroke = 'rgba(180, 180, 200, 1)';
  1559. jsPlumb.connect({
  1560. source : 'window3',
  1561. target : 'window4',
  1562. paintStyle:{
  1563. lineWidth:10,
  1564. strokeStyle:w34Stroke
  1565. },
  1566. anchors:[ "RightMiddle", "LeftMiddle" ],
  1567. endpointStyle:{
  1568. gradient : {
  1569. stops:[ [0, w34Stroke], [1, w34HlStroke] ],
  1570. offset:37.5,
  1571. innerRadius:40
  1572. },
  1573. radius:55
  1574. }
  1575. });
  1576. </pre>
  1577. </div>
  1578. Here we have instructed jsPlumb to make the gradient's inner radius 10px instead of the default 25/3 = 8 ish pixels, and the offset in each direction
  1579. will be 5px, instead of the default radius / 2 = 12.5 pixels.
  1580. <p>It is also possible to specify the offset and inner radius as percentages - enter the values as strings with a '%' symbol on the end:</p>
  1581. <div class="code">
  1582. <pre>var w34Stroke = 'rgba(50, 50, 200, 1)';
  1583. var w34HlStroke = 'rgba(180, 180, 200, 1)';
  1584. jsPlumb.connect({
  1585. source : 'window3',
  1586. target : 'window4',
  1587. paintStyle:{
  1588. lineWidth:10,
  1589. strokeStyle:w34Stroke
  1590. },
  1591. anchors:[ "RightMiddle", "LeftMiddle" ],
  1592. endpointStyle:{
  1593. gradient : {
  1594. stops:[ [0, w34Stroke], [1, w34HlStroke] ],
  1595. offset:'68%',
  1596. innerRadius:'73%'
  1597. },
  1598. radius:25
  1599. }
  1600. });
  1601. </pre>
  1602. </div>
  1603. This will give roughly the same output as the example above (the percentages are not entirely exact).<br/><br/>
  1604. </li>
  1605. <li><strong>Rectangle</strong> - renders a linear endpoint, with color stop 0 closest to the end of the Connector</li>
  1606. </ul>
  1607. <h4><a id="svgvmlcss">Styling SVG/VML using CSS</a></h4>
  1608. A nice way of controlling your UI's appearance is to make use of the fact that both SVG and VML can be styled using CSS.
  1609. <p>This section will be expanded in the next few weeks to give some decent examples of how to do so</p>
  1610. </div>
  1611. <div class="section">
  1612. <h3><a id="cssclasses">CSS Class Reference</a></h3>
  1613. jsPlumb attaches classes to each of the UI components it creates.
  1614. <p>These class names are exposed on the jsPlumb object and can be overridden if you need to do so (see the third column in the table) </p>
  1615. <table width="70%" class="table">
  1616. <tr><th>Component</th><th>CSS Class</th><th>jsPlumb Member</th></tr>
  1617. <tr><td>Connector</td><td>_jsPlumb_connector</td><td>connectorClass</td></tr>
  1618. <tr><td>Endpoint</td><td>_jsPlumb_endpoint</td><td>endpointClass</td></tr>
  1619. <tr><td>Overlay</td><td>_jsPlumb_overlay</td><td>overlayClass</td></tr>
  1620. </table>
  1621. <p>You would typically use these to establish appropriate z-indices for your UI.</p>
  1622. </div>
  1623. <div class="section">
  1624. <h3><a id="animation">Animation</a></h3>
  1625. jsPlumb offers an 'animate' function, which wraps the underlying animation engine for whichever library you happen to be using and
  1626. inserts a callback for jsPlumb to repaint whatever it needs to at each step. You could of course do this yourself; it's a convenience
  1627. method really.
  1628. <p>The method signature is:</p>
  1629. <div class="code">
  1630. <pre>
  1631. jsPlumb.animate : function(el, properties, options)
  1632. </pre>
  1633. </div>
  1634. The arguments are as follows:
  1635. <ul>
  1636. <li><strong>el</strong> - element id, or element object from the library you're using.</li>
  1637. <li><strong>properties</strong> - properties for the animation, such as duration etc.</li>
  1638. <li><strong>options</strong> - options for the animation, such as callbacks etc.</li>
  1639. </ul>
  1640. <h4>jQuery and the 'revert' option</h4>
  1641. jQuery offers a 'revert' option that you can use to instruct it to revert a drag under some condition. This is
  1642. rendered in the UI as an animation that returns the drag object from its current location to wherever it started
  1643. out. It's a nice feature. Unfortunately, the animation that runs the revert does not offer any lifecycle
  1644. callback methods - no 'step', no 'complete' - so its not possible for jsPlumb to know that the revert
  1645. animation is taking place.
  1646. </div>
  1647. <div class="section">
  1648. <h3><a id="connectionInfo">Retrieving Connection Information</a></h3>
  1649. jsPlumb offers one fairly versatile method - <strong>getConnections</strong> - to retrieve information about the
  1650. currently managed connections.
  1651. <p>Before you use this method you should understand jsPlumb's notion of 'scope' - documentation
  1652. is <a href="#dragAndDropScope">here</a> </p>
  1653. <h4>Retrieving connections for a single scope</h4>
  1654. <p>To do this, you call getConnections with either no arguments, in which case jsPlumb uses the default scope, or with a string
  1655. specifying one scope</p>
  1656. <div class="code">
  1657. <pre>var connectionList = jsPlumb.getConnections(); // you get a list of Connection objects that are in the default scope.
  1658. </pre>
  1659. </div>
  1660. Compare this with:
  1661. <div class="code">
  1662. <pre>var connectionList = jsPlumb.getConnections("myScope"); // you get a list of Connection objects that are in "myScope".
  1663. </pre>
  1664. </div>
  1665. <h4>More advanced filtering</h4>
  1666. getConnections optionally takes a JS object specifying filter parameters, of which there are three:
  1667. <ul>
  1668. <li>scope - the scope(s) of the connection type(s) you wish to retrieve</li>
  1669. <li>source - limits the returned connections to those that have this source id</li>
  1670. <li>target - limits the returned connections to those that have this target id</li>
  1671. </ul>
  1672. Each of these three parameters may be supplied as a string, or a list of strings - see the examples below.
  1673. <p>
  1674. The return value of a call to getConnection using a JS object as parameter varies on how many scopes you defined. If you defined only
  1675. a single scope then jsPlumb returns you a list of Connections in that scope. Otherwise the return value is a dictionary whose keys are
  1676. scope names, and whose values are lists of Connections. For example, the following call:
  1677. </p>
  1678. <div class="code">
  1679. <pre>
  1680. jsPlumb.getConnections({scope:["someScope", "someCustomScope"]});
  1681. </pre>
  1682. </div>
  1683. would result in this output:
  1684. <div class="code">
  1685. <pre>
  1686. {
  1687. "someScope" : [ 1..n Connections ],
  1688. "someCustomScope": [ 1..m Connections ]
  1689. }
  1690. </pre>
  1691. </div>
  1692. The following examples show the various ways you can get connection information:
  1693. <ul>
  1694. <li>Get all connections:
  1695. <div class="code">
  1696. <pre>
  1697. var c = jsPlumb.getAllConnections();
  1698. </pre>
  1699. </div>
  1700. </li>
  1701. <li>Get all connections for the default scope only (return value is a list):
  1702. <div class="code">
  1703. <pre>
  1704. var c = jsPlumb.getConnections();
  1705. </pre>
  1706. </div>
  1707. </li>
  1708. <li>Get all connections for the given scope (return value is a list):
  1709. <div class="code">
  1710. <pre>
  1711. var c = jsPlumb.getConnections({scope:"myTestScope"});
  1712. </pre>
  1713. </div>
  1714. </li>
  1715. <li>Get all connections for the given scopes (return value is a map of scope names to connection lists):
  1716. <div class="code">
  1717. <pre>
  1718. var c = jsPlumb.getConnections({scope:["myTestScope", "yourTestScope"]});
  1719. </pre>
  1720. </div>
  1721. </li>
  1722. <li>Get all connections for the given source (return value is a map of scope names to connection lists):
  1723. <div class="code">
  1724. <pre>
  1725. var c = jsPlumb.getConnections({source:"mySourceElement"});
  1726. </pre>
  1727. </div>
  1728. </li>
  1729. <li>Get all connections for the given sources (return value is a map of scope names to connection lists):
  1730. <div class="code">
  1731. <pre>
  1732. var c = jsPlumb.getConnections({source:["mySourceElement", "yourSourceElement"]});
  1733. </pre>
  1734. </div>
  1735. </li>
  1736. <li>Get all connections for the given target (return value is a map of scope names to connection lists):
  1737. <div class="code">
  1738. <pre>
  1739. var c = jsPlumb.getConnections({target:"myTargetElement"});
  1740. </pre>
  1741. </div>
  1742. </li>
  1743. <li>Get all connections for the given source and targets (return value is a map of scope names to connection lists):
  1744. <div class="code">
  1745. <pre>
  1746. var c = jsPlumb.getConnections({source:"mySourceElement", target:["target1", "target2"]});
  1747. </pre>
  1748. </div>
  1749. </li>
  1750. <li>Get all connections for the given scope, with the given source and target (return value is a list of connections):
  1751. <div class="code">
  1752. <pre>
  1753. var c = jsPlumb.getConnections({scope:'myScope", source:"mySourceElement", target:"myTargetElement"});
  1754. </pre>
  1755. </div>
  1756. </li>
  1757. </ul>
  1758. </div>
  1759. <div class="section">
  1760. <h3><a id="events">Events</a></h3>
  1761. jsPlumb supports binding to several different events on Connections, Endpoints and Overlays, and also on the jsPlumb
  1762. object itself.
  1763. <h4><a id="jsPlumbEvents">jsPlumb Events</a></h4>
  1764. <p>To bind an event to jsPlumb itself, use <strong>jsPlumb.bind(event, callback)</strong>:</p>
  1765. <div class="code">
  1766. <pre>
  1767. jsPlumb.bind("jsPlumbConnection", function(info) {
  1768. .. update your model in here, maybe.
  1769. });
  1770. </pre>
  1771. </div>
  1772. <p>These are the events you can bind to on the jsPlumb class:</p>
  1773. <ul>
  1774. <li class="bullet"><strong>jsPlumbConnection</strong> - notification a Connection was established.
  1775. <p>
  1776. The callback is passed an object with the following properties:
  1777. </p>
  1778. <div class="code">
  1779. <pre>
  1780. {
  1781. connection : the new Connection. you can register listeners on this etc.
  1782. sourceId : id of the source element in the Connection
  1783. targetId : id of the target element in the Connection
  1784. source : the source element in the Connection
  1785. target : the target element in the Connection
  1786. sourceEndpoint : the source Endpoint in the Connection
  1787. targetEndpoint : the targetEndpoint in the Connection
  1788. }
  1789. </pre>
  1790. </div>
  1791. <p>
  1792. All of the source/target properties are actually available inside the Connection object, but - for one of those rubbish historical
  1793. reasons - are provided separately because of a vagary of the <em>jsPlumbConnectionDetached</em> callback, which is discussed below.
  1794. </p>
  1795. </li>
  1796. <li class="bullet"><strong>jsPlumbConnectionDetached</strong> - notification a Connection was detached.
  1797. <p>
  1798. As with <em>jsPlumbConnection</em>, the callback is passed an object with the following properties:
  1799. </p>
  1800. <div class="code">
  1801. <pre>
  1802. {
  1803. connection : the Connection that was detached.
  1804. sourceId : id of the source element in the Connection <em>before it was detached</em>
  1805. targetId : id of the target element in the Connection before it was detached
  1806. source : the source element in the Connection before it was detached
  1807. target : the target element in the Connection before it was detached
  1808. sourceEndpoint : the source Endpoint in the Connection before it was detached
  1809. targetEndpoint : the targetEndpoint in the Connection before it was detached
  1810. }
  1811. </pre>
  1812. </div>
  1813. <p>
  1814. As mentioned above, the source/target properties are provided separately from the Connection, because this event is fired whenever
  1815. a Connection is either detached and abandoned, or detached from some Endpoint and attached to another. In the latter case, the
  1816. Connection that is passed to this callback is in an indeterminate state (that is, the Endpoints are still in the state they are
  1817. in when dragging, and do not reflect static reality), and so the source/target properties give you the real story.
  1818. </p>
  1819. <p> Like I said above, this is really just for rubbish historical reasons. I'm attempting to document my way out of it!</p>
  1820. </li>
  1821. <li class="bullet"><strong>click</strong> - notification a Connection was clicked. The callback is passed the Connection and the original mouse event.</li>
  1822. <li class="bullet"><strong>dblclick</strong> - notification a Connection was double-clicked. The callback is passed the Connection and the original mouse event.</li>
  1823. <li class="bullet"><strong>endpointClick</strong> - notification an Endpoint was clicked. The callback is passed the Endpoint and the original mouse event.</li>
  1824. <li class="bullet"><strong>endpointDblClick</strong> - notification an Endpoint was double-clicked. The callback is passed the Endpoint and the original mouse event.</li>
  1825. <li class="bullet"><strong>beforeDrag</strong>
  1826. This event is fired when a new connection is about to be dragged. Your callback function
  1827. is passed the Connection that jsPlumb has just created. If you return false (or nothing)
  1828. from this callback, the new Connection is aborted.
  1829. </li>
  1830. <li class="bullet"><strong>beforeDrop</strong>
  1831. This event is fired when a new connection has been dropped. Your callback function
  1832. is passed the Connection that jsPlumb has just created. If you return false (or nothing)
  1833. from this callback, the new Connection is aborted and removed from the UI.
  1834. </li>
  1835. </ul>
  1836. <h4><a id="connectionEvents">Connection Events</a></h4>
  1837. <p>To bind to an event on a Connection, you also use the <strong>bind</strong> method:</p>
  1838. <div class="code">
  1839. <pre>
  1840. var connection = jsPlumb.connect({source:"d1", target:"d2"});
  1841. connection.bind("click", function(conn) {
  1842. console.log("you clicked on ", conn);
  1843. });
  1844. </pre>
  1845. </div>
  1846. <p>These are the Connection events to which you can bind a listener:</p>
  1847. <ul>
  1848. <li class="bullet"><strong>click</strong> - notification a Connection was clicked. The callback is passed the Connection and the original mouse event.</li>
  1849. <li class="bullet"><strong>dblclick</strong> - notification a Connection was double-clicked. The callback is passed the Connection and the original mouse event.</li>
  1850. <li class="bullet"><strong>mouseenter</strong> - notification the mouse entered the Connection's path. The callback is passed the Connection and the original mouse event.</li>
  1851. <li class="bullet"><strong>mouseexit</strong> - notification the mouse exited the Connection's. The callback is passed the Connection and the original mouse event.</li>
  1852. </ul>
  1853. <h4><a id="endpointEvents">Endpoint Events</a></h4>
  1854. <p>To bind to an event on a Endpoint, you again use the <strong>bind</strong> method:</p>
  1855. <div class="code">
  1856. <pre>
  1857. var endpoint = jsPlumb.addEndpoint("d1", { someOptions } );
  1858. endpoint.bind("click", function(endpoint) {
  1859. console.log("you clicked on ", endpoint);
  1860. });
  1861. </pre>
  1862. </div>
  1863. <p>These are the Endpoint events to which you can bind a listener:</p>
  1864. <ul>
  1865. <li class="bullet"><strong>click</strong> - notification an Endpoint was clicked. The callback is passed the Endpoint and the original mouse event.</li>
  1866. <li class="bullet"><strong>dblclick</strong> - notification an Endpoint was double-clicked. The callback is passed the Endpoint and the original mouse event.</li>
  1867. <li class="bullet"><strong>mouseenter</strong> - notification the mouse entered the Endpoint. The callback is passed the Endpoint and the original mouse event.</li>
  1868. <li class="bullet"><strong>mouseexit</strong> - notification the mouse exited the Endpoint. The callback is passed the Endpoint and the original mouse event.</li>
  1869. </ul>
  1870. <h4><a id="overlayEvents">Overlay Events</a></h4>
  1871. Registering event listeners on an Overlay is a slightly different procedure - you provide them as arguments to the Overlay's
  1872. constructor. This is because you never actually act on an 'Overlay' object. Here's how to register a click listener on an Overlay:
  1873. <div class="code">
  1874. <pre>jsPlumb.connect({
  1875. source:"el1",
  1876. target:"el2",
  1877. overlays:[
  1878. [ "Label", {
  1879. events:{
  1880. click:function(labelOverlay, originalEvent) { alert("you clicked on the label overlay for this connection :" + labelOverlay.connection) }
  1881. }
  1882. }]
  1883. ]
  1884. });
  1885. </pre>
  1886. </div>
  1887. </div>
  1888. <div class="section">
  1889. <a id="examples"><h3>jsPlumb.connect Examples</h3></a>
  1890. This section provides examples of how to use the programmatic API to establish Connections.
  1891. <p>
  1892. The basic syntax of a call is that you execute 'connect', providing a source and a target, and optionally a paintStyle and preferences for where you
  1893. want the plumbing to be anchored on each element, as well as the type of connector to use.
  1894. </p>
  1895. <ul>
  1896. <li>Connect window1 to window2 with the default settings:
  1897. <div class="code">
  1898. <pre>jsPlumb.connect({source:"window1", target:"window2"});</pre>
  1899. </div>
  1900. This connects 'window1' to 'window2' using the jsPlumb defaults - a red Bezier curve Connector and black Dot Endpoints.
  1901. </li>
  1902. <li>Connect window1 to window2 with a 15 pixel wide yellow Connector, and a slightly brighter endpoint (remember the default Endpoint is a Dot):
  1903. <div class="code">
  1904. <pre>jsPlumb.connect({
  1905. source:'window1',
  1906. target:'window2',
  1907. paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'},
  1908. endpointStyle:{fillStyle:'rgb(243,229,0)'}
  1909. });</pre>
  1910. </div>
  1911. </li>
  1912. <li>Connect window1 to window2 with a 15 pixel wide yellow Connector, and a slightly brighter endpoint:
  1913. <div class="code">
  1914. <pre>jsPlumb.connect({
  1915. source:'window1',
  1916. target:'window2',
  1917. paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'},
  1918. endpointStyle:{fillStyle:'rgb(243,229,0)'}
  1919. });</pre>
  1920. </div>
  1921. </li>
  1922. <li>Connect window3 to 'window4' with a 10 pixel wide, semi opaque blue Connector, anchored to the left middle of window3, and the right middle of window4, with a Rectangle endpoint of width 10 and height 8:
  1923. <div class="code">
  1924. <pre>jsPlumb.connect({
  1925. source:'window3',
  1926. target:'window4',
  1927. paintStyle:{ lineWidth:10, strokeStyle:'rgba(0, 0, 200, 0.5)' },
  1928. anchors:["RightMiddle", "LeftMiddle"],
  1929. endpoint:[ "Rectangle", { width:10, height:8 } ]
  1930. });</pre>
  1931. </div>
  1932. </li>
  1933. <li>Connect window2 to window3 with a default Connector from the top center of window2 to the bottom center of window3, and rectangular endpoints:
  1934. <div class="code">
  1935. <pre>jsPlumb.connect({
  1936. source:'window2',
  1937. target:'window3',
  1938. paintStyle:{lineWidth:8, strokeStyle:'rgb(189,11,11)'},
  1939. anchors:["BottomCenter", "TopCenter"],
  1940. endpoint:"Rectangle"
  1941. });</pre>
  1942. </div>
  1943. </li>
  1944. <li>Connect window1 to window2 with a 15 px wide yellow Bezier. endpoints are a slightly lighter shade of yellow.
  1945. <div class="code">
  1946. <pre>jsPlumb.connect({
  1947. source:'window1',
  1948. target:'window2',
  1949. anchors:["BottomCenter", [0.75,0,0,-1]],
  1950. paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'},
  1951. endpointStyle:{fillStyle:'rgb(243,229,0)'}
  1952. });</pre>
  1953. </div></li>
  1954. <li>Connect window3 to window4 with a 10px wide blue-ish half transparent Bezier. put endpoints underneath the element they attach to.
  1955. the endpoints have a radial gradient. both ways of specifying gradient positioning are shown here.
  1956. <div class="code">
  1957. <pre>var w34Stroke = 'rgba(50, 50, 200, 1)';
  1958. var w34HlStroke = 'rgba(180, 180, 200, 1)';
  1959. jsPlumb.connect( {
  1960. source:'window3',
  1961. target:'window4',
  1962. paintStyle:{lineWidth:10, strokeStyle:w34Stroke},
  1963. anchors:["RightMiddle", "LeftMiddle"],
  1964. endpointStyle:{ gradient : {stops:[[0, w34Stroke], [1, w34HlStroke]], offset:17.5, innerRadius:15 }, radius:35},
  1965. //endpointStyle:{ gradient : {stops:[[0, w34Stroke], [1, w34HlStroke]], offset:'78%', innerRadius:'73%'}, radius:35 },
  1966. endpointsOnTop:false
  1967. }
  1968. );</pre>
  1969. </div></li>
  1970. <li>Connect window2 to window3 with an 8px red Bezier and default rectangular endpoints. see also how the first anchor is
  1971. specified here - this is how you create anchors in locations jsPlumb does not offer shortcuts for.
  1972. the endpoints in this example have linear gradients applied.
  1973. <div class="code">
  1974. <pre>var w23Stroke = 'rgb(189,11,11)';
  1975. jsPlumb.connect({
  1976. source:'window2',
  1977. target:'window3',
  1978. paintStyle:{lineWidth:8,strokeStyle:w23Stroke},
  1979. anchors:[[0.3,1,0,1], "TopCenter"],
  1980. endpoint:"Rectangle",
  1981. endpointStyles:[{ gradient : {stops:[[0, w23Stroke], [1, '#558822']] }},
  1982. { gradient : {stops:[[0, w23Stroke], [1, '#882255']] }}]
  1983. });</pre>
  1984. </div></li>
  1985. <li>Connect window5 to window6 from center to center, 5px wide line that is green and half transparent. the endpoints are
  1986. 125px in radius and spill out from underneath their elements.
  1987. <div class="code">
  1988. <pre>jsPlumb.connect({
  1989. source:'window5',
  1990. target:'window6',
  1991. anchors:["Center", "Center"],
  1992. paintStyle:{lineWidth:5,strokeStyle:'rgba(0,255,0,0.5)'},
  1993. endpointStyle:{radius:125}
  1994. });</pre>
  1995. </div></li>
  1996. <li>Connect window4 to window5 from bottom right to top left, with a 7px straight line purple connector, and an image as the endpoint,
  1997. placed on top of the element it is connected to.
  1998. <div class="code">
  1999. <pre>jsPlumb.connect({
  2000. source:"window4",
  2001. target:"window5",
  2002. anchors:[ "BottomRight","TopLeft" ],
  2003. paintStyle:{ lineWidth:7, strokeStyle:"rgb(131,8,135)" },
  2004. endpoint:[ "Image", { src:"http://morrisonpitt.com/jsPlumb/img/endpointTest1.png" } ],
  2005. connector:"Straight"
  2006. });</pre>
  2007. </div></li>
  2008. <li>Connect window5 to window6 between their center points with a semi-opaque connector, and 125px endpoints:
  2009. <div class="code">
  2010. <pre>jsPlumb.connect({
  2011. source:"window5",
  2012. target:"window6",
  2013. anchors:[ "Center", "Center" ],
  2014. paintStyle:{ lineWidth:5, strokeStyle:"rgba(0,255,0,0.5)" },
  2015. endpointStyle:{ radius:125 }
  2016. });</pre>
  2017. </div>
  2018. </li>
  2019. <li>Connect window7 to window8 with a 10 pixel wide blue Connector, anchored on the top left of window7 and the bottom right of window8:
  2020. <div class="code">
  2021. <pre>jsPlumb.connect({
  2022. source:"window7",
  2023. target:"window8",
  2024. paintStyle:{ lineWidth:10, strokeStyle:"blue" },
  2025. anchors:[ "TopLeft", "BottomRight" ]
  2026. });
  2027. </pre>
  2028. </div>
  2029. </li>
  2030. <li>Connect the bottom right corner of window4 to the top left corner of window5, with rectangular endpoints of size 40x40 and a hover color of light blue:
  2031. <div class="code">
  2032. <pre>jsPlumb.connect({
  2033. source:"window4",
  2034. target:"window5",
  2035. anchors:["BottomRight","TopLeft"],
  2036. paintStyle:{lineWidth:7,strokeStyle:'rgb(131,8,135)'},
  2037. hoverPaintStyle:{ strokeStyle:"rgb(0, 0, 135)" },
  2038. endpointStyle:{ width:40, height:40 },
  2039. endpoint:"Rectangle",
  2040. connector:"Straight"
  2041. });</pre>
  2042. </div>
  2043. </li>
  2044. <li>Connect window1 to window2 with the default paint settings but provide some drag options (which are passed through to the underlying library's draggable call):
  2045. <div class="code">
  2046. <pre>jsPlumb.connect({source:'window1', target:'window2', dragOptions:{cursor:'crosshair'}});</pre>
  2047. </div>
  2048. </li>
  2049. </div>
  2050. <div class="section">
  2051. <a id="dragAndDropExamples"><h3>Draggable Connections Examples</h3></a>
  2052. This is a list of examples of how to use jsPlumb to create Connections using drag and drop.
  2053. <p>
  2054. The basic procedure is:
  2055. <ol>
  2056. <li>1a. Create Endpoints and register them on elements in your UI</li>
  2057. <li>1b. alternatively, create a source endpoint and then make some element a drop target </li>
  2058. <li>2. Drag and Drop</li>
  2059. </ol>
  2060. That's all there is to it. Of course there are plenty of options you can set when doing this...it will be easier
  2061. to show you some examples:
  2062. </p>
  2063. <ul>
  2064. <li>Define an Endpoint with default appearance, that is both a source and target of new Connections:
  2065. <div class="code">
  2066. <pre>var endpointOptions = { isSource:true, isTarget:true };
  2067. </pre>
  2068. </div>
  2069. </li>
  2070. <li>Register that Endpoint on window3, specifying that it should be located in the top center of the element:
  2071. <div class="code">
  2072. <pre>var window3Endpoint = jsPlumb.addEndpoint('window3', { anchor:"TopCenter" }, endpointOptions );
  2073. </pre>
  2074. </div>
  2075. Notice here the usage of the three-argument addEndpoint - we can reuse 'endpointOptions' with a different Anchor for another element. This is a useful
  2076. practice to get into.<br/><br/>
  2077. </li>
  2078. <li>Now register that Endpoint on window4, specifying that it should be located in the bottom center of the element:
  2079. <div class="code">
  2080. <pre>var window4Endpoint = jsPlumb.addEndpoint('window4', { anchor:"BottomCenter" }, endpointOptions );
  2081. </pre>
  2082. </div>
  2083. Now we have two Endpoints, both of which support drag and drop of new Connections. We can use these to make a programmatic Connection, too, though:<br/><br/>
  2084. </li>
  2085. <li>Connect window3 to window4 with a 25px wide yellow Bezier that has a 'curviness' of 175:
  2086. <div class="code">
  2087. <pre>jsPlumb.connect({
  2088. source:window3Endpoint,
  2089. target:window4Endpoint,
  2090. connector: [ "Bezier", 175 ],
  2091. paintStyle:{ lineWidth:25, strokeStyle:'yellow' }
  2092. });
  2093. </pre>
  2094. </div>
  2095. </li>
  2096. <li>Define an Endpoint that creates Connections that are 20px wide straight lines, that is both a source and target of new Connections,
  2097. and that has a 'scope' of 'blueline'. Also, this Endpoint mandates that once it is full, Connections can
  2098. no longer be dragged from it (even if 'reattach' is specified on a Connection):
  2099. <div class="code">
  2100. <pre>var endpointOptions = {
  2101. isSource:true,
  2102. isTarget:true,
  2103. connector : "Straight",
  2104. connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  2105. scope:"blueline",
  2106. dragAllowedWhenFull:false
  2107. };
  2108. </pre>
  2109. </div>
  2110. </li>
  2111. <li>Define an Endpoint that will be anchored to "TopCenter". It creates Connections that are 20px wide straight lines, that is both a source and target of new Connections,
  2112. and that has a 'scope' of 'blueline'. Also, this Endpoint mandates that once it is full, Connections can
  2113. no longer be dragged from it (even if 'reattach' is specified on a Connection):
  2114. <div class="code">
  2115. <pre>var endpointOptions = {
  2116. anchor:"TopCenter",
  2117. isSource:true,
  2118. isTarget:true,
  2119. connector : "Straight",
  2120. connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  2121. scope:"blueline",
  2122. dragAllowedWhenFull:false
  2123. };
  2124. </pre>
  2125. </div>
  2126. </li>
  2127. <li>Define an Endpoint that will create a dynamic anchor which can be positioned at "TopCenter" or "BottomCenter". It creates Connections that are 20px wide straight lines, it is both a source and target of new Connections,
  2128. and it has a 'scope' of 'blueline'. Also, this Endpoint mandates that once it is full, Connections can
  2129. no longer be dragged from it (even if 'reattach' is specified on a Connection):
  2130. <div class="code">
  2131. <pre>var endpointOptions = {
  2132. anchor:[ "TopCenter", "BottomCenter" ],
  2133. isSource:true,
  2134. isTarget:true,
  2135. connector : "Straight",
  2136. connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  2137. scope:"blueline",
  2138. dragAllowedWhenFull:false
  2139. };
  2140. </pre>
  2141. </div>
  2142. </li>
  2143. <li>Exactly the same as before, but shows how you can use "anchors" instead of "anchor", if that makes you feel happier:
  2144. <div class="code">
  2145. <pre>var endpointOptions = {
  2146. anchors:[ "TopCenter", "BottomCenter" ],
  2147. isSource:true,
  2148. isTarget:true,
  2149. connector : "Straight",
  2150. connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  2151. scope:"blueline",
  2152. dragAllowedWhenFull:false
  2153. };
  2154. </pre>
  2155. </div>
  2156. </li>
  2157. <li>Define an Endpoint that is a 30px blue dot, creates Connections that are 20px wide straight lines, is both a source and target of new Connections,
  2158. has a 'scope' of 'blueline', and has an event handler that pops up an alert (note: the event handler name means this example is jQuery - MooTools
  2159. and YUI3 use different event handler names):
  2160. <div class="code">
  2161. <pre>var endpointOptions = {
  2162. isSource:true,
  2163. isTarget:true,
  2164. endpoint: [ "Dot", { radius:30 } ],
  2165. style:{fillStyle:'blue'},
  2166. connector : "Straight",
  2167. connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  2168. scope:"blueline",
  2169. dropOptions:{ drop:function(e, ui) { alert('drop!'); } }
  2170. };
  2171. </pre>
  2172. </div>
  2173. </li>
  2174. <li>Same example as before, but this is for MooTools, and the Endpoint can support up to 5 connections (the default is 1):
  2175. <div class="code">
  2176. <pre>var endpointOptions = {
  2177. isSource:true,
  2178. isTarget:true,
  2179. endpoint: [ "Dot", { radius:30 } ],
  2180. style:{ fillStyle:'blue' },
  2181. maxConnections:5,
  2182. connector : "Straight",
  2183. connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  2184. scope:"blueline",
  2185. dropOptions:{ onDrop:function(e, ui) { alert('drop!'); } }
  2186. };
  2187. </pre>
  2188. </div>
  2189. </li>
  2190. <li>Same example again, but maxConnections being set to -1 means that the Endpoint has no maximum limit of Connections:
  2191. <div class="code">
  2192. <pre>var endpointOptions = {
  2193. isSource:true,
  2194. isTarget:true,
  2195. endpoint: [ "Dot", {radius:30} ],
  2196. style:{ fillStyle:'blue' },
  2197. maxConnections:-1,
  2198. connector : "Straight",
  2199. connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  2200. scope:"blueline",
  2201. dropOptions:{ onDrop:function(e, ui) { alert('drop!'); } }
  2202. };
  2203. </pre>
  2204. </div>
  2205. </li>
  2206. <li>Same example again, but for YUI3. Note the drop callback is "drop:hit":
  2207. <div class="code">
  2208. <pre>var endpointOptions = {
  2209. isSource:true,
  2210. isTarget:true,
  2211. endpoint: [ "Dot", { radius:30 } ],
  2212. style:{fillStyle:'blue'},
  2213. maxConnections:-1,
  2214. connector : "Straight",
  2215. connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  2216. scope:"blueline",
  2217. dropOptions:{ "drop:hit":function(e, ui) { alert('drop!'); } }
  2218. };
  2219. </pre>
  2220. </div>
  2221. </li>
  2222. <li>Assign a UUID to the endpoint options created above, and add as Endpoints to "window1" and "window2":
  2223. <div class="code">
  2224. <pre>
  2225. jsPlumb.addEndpoint("window1", { uuid:"abcdefg" }, endpointOptions );
  2226. jsPlumb.addEndpoint("window2", { uuid:"hijklmn" }, endpointOptions );
  2227. </pre>
  2228. </div>
  2229. </li>
  2230. <li>Connect the two Endpoints we just registered on "window1" and "window2":
  2231. <div class="code">
  2232. <pre>
  2233. jsPlumb.connect({uuids:["abcdefg", "hijklmn"]});
  2234. </pre>
  2235. </div>
  2236. </li>
  2237. <li>Create a source Endpoint, register it on some element, then make some other element a Connection target:
  2238. <div class="code">
  2239. <pre>
  2240. var sourceEndpoint = { isSource:true, endpoint:[ "Dot", { radius:50 } ] };
  2241. var targetEndpoint = { endpoint:[ "Rectangle", { width:10, height:10 } ] };
  2242. jsPlumb.addEndpoint( "window1", sourceEndpoint );
  2243. jsPlumb.makeTarget( "window2", targetEndpoint );
  2244. </pre>
  2245. </div>
  2246. Notice that the endpoint definition we use on the target window does not include the "isTarget:true" directive. jsPlumb ignores that flag when
  2247. creating a connection using an element as the target; but if you then tried to drag another connection to the Endpoint that was created, you would
  2248. not be able to. To permit that, you would set 'isTarget:true' on the targetEndpoint options defined above.
  2249. </li>
  2250. </ul>
  2251. </div>
  2252. <div class="section">
  2253. <h3><a id="utilityFunctions">Utility Functions</a></h3>
  2254. <ul>
  2255. <li>Detach window5 from all connections
  2256. <div class="code">
  2257. <pre>jsPlumb.detachAll("window5");</pre>
  2258. </div>
  2259. </li>
  2260. <li>Hide all window5's connections
  2261. <div class="code">
  2262. <pre>jsPlumb.hide("window5");</pre>
  2263. </div>
  2264. </li>
  2265. <li>Hide all window5's connections endpoints
  2266. <div class="code">
  2267. <pre>jsPlumb.hide("window5", true);</pre>
  2268. </div>
  2269. </li>
  2270. <li>Show all window5's connections
  2271. <div class="code">
  2272. <pre>jsPlumb.show("window5");</pre>
  2273. </div>
  2274. </li>
  2275. <li>Show all window5's connections and endpoints. Note that in the case that you call jsPlumb.show with two arguments, jsPlumb
  2276. will also not make a connection visible if it determines that the other endpoint in the connection is not visible.
  2277. <div class="code">
  2278. <pre>jsPlumb.show("window5", hide);</pre>
  2279. </div>
  2280. </li>
  2281. <li>Toggle the visibility of window5's connections
  2282. <div class="code">
  2283. <pre>jsPlumb.toggle("window5");</pre>
  2284. </div>
  2285. </li>
  2286. <li>Force repaint of all of window5's connections
  2287. <div class="code">
  2288. <pre>jsPlumb.repaint("window5");</pre>
  2289. </div>
  2290. </li>
  2291. <li>Force repaint of all of window5, window6 and window11's connections
  2292. <div class="code">
  2293. <pre>jsPlumb.repaint( [ "window5", "window6", "window11" ] );</pre>
  2294. </div>
  2295. </li>
  2296. <li>Force repaint of every connection
  2297. <div class="code">
  2298. <pre>jsPlumb.repaintEverything();</pre>
  2299. </div>
  2300. </li>
  2301. <li>Detach every connection
  2302. <div class="code">
  2303. <pre>jsPlumb.detachEverything();</pre>
  2304. </div>
  2305. </li>
  2306. <li>Remove the given Endpoint from element "window1", deleting its Connections.
  2307. <div class="code">
  2308. <pre>jsPlumb.removeEndpoint("window1", someEndpoint);</pre>
  2309. </div>
  2310. </li>
  2311. <li>Remove all Endpoints for the element 'window1', deleting their Connections.
  2312. <div class="code">
  2313. <pre>jsPlumb.removeAllEndpoints("window1");</pre>
  2314. </div>
  2315. </li>
  2316. <li>Removes every Endpoint managed by this instance of jsPlumb, deleting all Connections.
  2317. This is the same as jsPlumb.reset(), effectively, but it does not clear out the event listeners list.
  2318. <div class="code">
  2319. <pre>jsPlumb.removeEveryEndpoint();</pre>
  2320. </div>
  2321. </li>
  2322. <li>Deletes the given Endpoint and all its Connections.
  2323. <div class="code">
  2324. <pre>jsPlumb.deleteEndpoint(endpoint);</pre>
  2325. </div>
  2326. </li>
  2327. <li>Removes every endpoint, detaches every connection, and clears the event listeners list. Returns jsPlumb instance to its initial state.
  2328. <div class="code">
  2329. <pre>jsPlumb.reset();</pre>
  2330. </div>
  2331. </li>
  2332. <li>Set window1 to be not draggable, no matter what some jsPlumb command may request.
  2333. <div class="code">
  2334. <pre>jsPlumb.setDraggable("window1", false);</pre>
  2335. </div>
  2336. </li>
  2337. <li>Set window1 and window2 to be not draggable, no matter what some jsPlumb command may request.
  2338. <div class="code">
  2339. <pre>jsPlumb.setDraggable(["window1","window2"], false);</pre>
  2340. </div>
  2341. </li>
  2342. <li>Sets whether or not elements that are connected are draggable by default.
  2343. The default for this is true.
  2344. <div class="code">
  2345. <pre>jsPlumb.setDraggableByDefault(false);</pre>
  2346. </div>
  2347. </li>
  2348. <li>Initialises window1 as a draggable element (all libraries)
  2349. <div class="code">
  2350. <pre>jsPlumb.draggable("window1");</pre>
  2351. </div>
  2352. </li>
  2353. <li>Initialises window1 and window2 as draggable elements (all libraries)
  2354. <div class="code">
  2355. <pre>jsPlumb.draggable(["window1","window2"]);</pre>
  2356. </div>
  2357. </li>
  2358. <li>Initialises window1 as a draggable element (all libraries)
  2359. <div class="code">
  2360. <pre>jsPlumb.draggable("window1");</pre>
  2361. </div>
  2362. </li>
  2363. <li>Initialises all elements with class 'window' as draggable elements (jQuery)
  2364. <div class="code">
  2365. <pre>jsPlumb.draggable($(".window"));</pre>
  2366. </div>
  2367. </li>
  2368. <li>Initialises all elements with class 'window' as draggable elements (MooTools)
  2369. <div class="code">
  2370. <pre>jsPlumb.draggable($$(".window"));</pre>
  2371. </div>
  2372. </li>
  2373. <li>Initialises all elements with class 'window' as draggable elements (YIU3)
  2374. <div class="code">
  2375. <pre>jsPlumb.draggable(Y.all(".window"));</pre>
  2376. </div>
  2377. </li>
  2378. <li>Initialises window1 as a draggable element (jQuery)
  2379. <div class="code">
  2380. <pre>jsPlumb.draggable($("#window1"));</pre>
  2381. </div>
  2382. </li>
  2383. <li>Initialises window1 as a draggable element (MooTools)
  2384. <div class="code">
  2385. <pre>jsPlumb.draggable($("window1"));</pre>
  2386. </div>
  2387. </li>
  2388. <li>Initialises window1 as a draggable element (YUI3)
  2389. <div class="code">
  2390. <pre>jsPlumb.draggable(Y.one("window1"));</pre>
  2391. </div>
  2392. </li>
  2393. </ul>
  2394. </div>
  2395. <div class="section">
  2396. <h3><a id="developingJsPlumb">Advanced Topics</a></h3>
  2397. <h4><a id="which">Which files are which?</a></h4>
  2398. In development, jsPlumb is broken up into seven scripts:
  2399. <ul>
  2400. <li>- jsPlumb-x.x.x.js
  2401. <p>This is the main jsPlumb engine.</p>
  2402. </li>
  2403. <li>- jsPlumb-defaults-x.x.x.js
  2404. <p>This contains the default Anchor, Endpoint, Connector and Overlay implementations.</p>
  2405. </li>
  2406. <li>- jsPlumb-renderers-canvas-x.x.x.js
  2407. <p>This contains the HTML5 canvas render code.</p>
  2408. </li>
  2409. <li>- jsPlumb-renderers-svg-x.x.x.js
  2410. <p>This contains the SVG render code.</p>
  2411. </li>
  2412. <li>- jsPlumb-renderers-vml-x.x.x.js
  2413. <p>This contains the VML render code.</p>
  2414. </li>
  2415. <li>- jsPlumb-connectors-statemachine-x.x.x.js
  2416. <p>This State Machine connector.</p>
  2417. </li>
  2418. <li>- &lt;LIBRARY_PREFIX&gt;.jsPlumb-x.x.x.js
  2419. <p>This contains library-specific helper methods. jsPlumb ships with three of these - one each for jQuery, MooTools and YUI3. See below for information on how to create a new library implementation.</p>
  2420. </li>
  2421. <li>- jsBezier-0.3-min.js
  2422. <p>These are the Bezier curve functions; they are maintained in a separate project called <a href="http://code.google.com/p/jsbezier/">jsBezier</a></p>
  2423. </li>
  2424. </ul>
  2425. These seven files are packaged together to form the scripts that people use, for example:
  2426. <ul>
  2427. <li>jquery.jsPlumb-1.3.4-all.js
  2428. <p>Contains jsPlumb-1.3.4.js, jsPlumb-defaults-1.3.4.js, jsPlumb-renderers-canvas-1.3.4.js, jsPlumb-renderers-svg-1.3.4.js, jsPlumb-renderers-vml-1.3.4.js, jsPlumb-connectors-statemachine-1.3.4.js, jquery.jsPlumb-1.3.4.js and jsBezier-0.3-min.js</p>
  2429. </li>
  2430. <li>jquery.jsPlumb-1.3.4-all-min.js
  2431. <p>A minified version of the script above (minified using the YUI Compressor)</p>
  2432. </li>
  2433. </ul>
  2434. <h4><a id="pluggableLibrarySupport">Pluggable Library Support</a></h4>
  2435. Out of the box, jsPlumb can be run on top of jQuery, MooTools or YUI3. This is achieved by
  2436. delegating several core methods - tasks such as finding an element by id, finding an element's
  2437. position or dimensions, initialising a draggable, etc - to the library in question.
  2438. <p>To develop one of these, your test page should include the first two scripts discussed above, and
  2439. then your own script containing your library specific functionality. The existing implementations
  2440. may be documented well enough for you to create your own, but contact me if you need assistance. If you do this, it would be
  2441. great to share it with everyone...</p>
  2442. <p>There is a group of people working on an ExtJS adapter currently. There is no schedule for when this will be completed though.</p>
  2443. <!-- h4><a id="customConnectors">Custom Connectors</a></h4>
  2444. You can provide your own connectors if you need to. A Connector consists of two functions, which work as a pair. First a call is made to the <em>compute</em> function:
  2445. <div class="code">
  2446. <pre>
  2447. this.compute = function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth) {
  2448. ...
  2449. return dimensions;
  2450. }
  2451. </pre>
  2452. </div>
  2453. which is expected to return a list that the <em>paint</em> function can make sense of. The first four entries in the
  2454. list <strong>must be</strong> the [x,y,width,height] values for the canvas that the connector will be drawn on; jsPlumb will
  2455. use this information to size the canvas prior to calling the Connector's <em>paint</em> function. Therefore it
  2456. is the Connector's responsibility to ensure that the returned dimensions describe a large enough space for
  2457. the line that will be drawn on it.</p><p>The next four elements <strong>must be</strong> the coordinates of the two endpoints of the line
  2458. you are going to draw.</p><p>The remainder of the items in the returned list are arbitrary, and will
  2459. vary between Connector implementations; this list is passed in to a Connector's <em>paint</em> function, so each
  2460. implementation will put into the list whatever it needs to paint itself. For instance, the straight line
  2461. connector only needs the [x,y] location of each end of the line it will paint, and that is one of the required entries, so
  2462. it does not have to do anything extra, whereas the Bezier connector adds the location of the two control points. Other types of
  2463. Connectors will do whatever is appropriate for their particular situation.
  2464. </p>
  2465. This is the method signature for the <em>paint</em> function:
  2466. <div class="code">
  2467. <pre>this.paint = function(dimensions, ctx) { .. }</pre>
  2468. </div>
  2469. here, the 'dimensions' argument to the 'paint' function is the return value of the 'compute' function. The 'ctx' argument is the Canvas context; you
  2470. will do all your drawing on this.
  2471. </p>
  2472. To change the connector from the default, specify it in your connect call:
  2473. <div class="code">
  2474. <pre>jsPlumb.connect({source:'someWindow', target:'otherWindow', connector:new jsPlumb.Connectors.Straight()});</pre>
  2475. </div>
  2476. note that you can use shorthand for connectors if you don't need to specify parameters to it:
  2477. <div class="code">
  2478. <pre>jsPlumb.connect({source:'someWindow', target:'otherWindow', connector:"Straight"});</pre>
  2479. </div>
  2480. This works in the same way as specifying Anchors as string does.
  2481. <p>
  2482. The section below this discusses Overlays, which allow you to decorate Connectors with
  2483. things such as labels or arrows or whatever else you like. Overlays can only work with Connectors that implement a few helper functions.
  2484. <h4>The concept of <strong>location</strong></h4>
  2485. Before discussing the helper functions you first must be across the concept of 'location' as used by this mechanism. The <em>location</em>
  2486. on a connector can be a decimal value between 0 and 1 inclusive, which marks a proportionate amount of travel <em>along the path
  2487. inscribed by the Connector</em>. For a straight line connector this is a simple piece of maths, but for Bezier connectors it's a little
  2488. bit more involved.
  2489. <h4>Required Helper Methods</h4>
  2490. <ul>
  2491. <li><strong>pointOnPath(location)</strong> - returns an [x,y] point corresponding to the given location</li>
  2492. <li><strong>pointAlongPathFrom(location, distance)</strong> - returns an [x,y] point corresponding to travelling 'distance' pixels along the connector from 'location'.</li>
  2493. <li><strong>gradientAtPoint(location)</strong> - returns the connector's gradient at the given location. For linear connectors such as Straight this is constant, but for Bezier connectors the gradient changes continually.</li>
  2494. <li><strong>perpendicularToPathAt(location, distance, length)</strong> - returns a line that is perpendicular to (and centered on) the connector at 'distance' from the given location, with length 'length'.</li>
  2495. </ul>
  2496. </p>
  2497. <h4><a id="customEndpoints">Custom Endpoints</a></h4>
  2498. <p>To create your own Endpoint implementation, you need to implement a single method:
  2499. <div class="code"><pre>paint : function(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle) { ... }</pre></div>
  2500. The arguments to this method are as follows:
  2501. <ul>
  2502. <li>anchorPoint - [x,y] location of the anchor point on screen</li>
  2503. <li>orientation - [x,y] hints for the general direction the anchor points to</li>
  2504. <li>canvas - the canvas to draw into</li>
  2505. <li>endpointStyle - Javascript object containing style directives as discussed above. The contents of this are arbitrary, so if you write a new Endpoint that needs some extra settings, you can add them no hassle.</li>
  2506. <li>connectorPaintStyle - the style being used to paint the associated Connector.</li>
  2507. </ul>
  2508. </p>
  2509. <p>
  2510. It is your responsibility to size and locate the canvas to suit your needs. jsPlumb provides the following
  2511. helper method to assist you:
  2512. <div class="code"><pre>jsPlumb.sizeCanvas(canvas, x, y, width, height);</pre></div>
  2513. Allows you to locate the canvas on screen and to size it.
  2514. </p>
  2515. <h4><a id="customOverlays">Custom Overlays</a></h4>
  2516. Overlays can only work with Connectors that implement the methods <strong>pointOnPath</strong>, <strong>pointAlongPathFrom</strong>,
  2517. <strong>gradientAtPoint</strong>, and <strong>perpendicularToPathAt</strong>. These are discussed in more detail in the Custom Connectors section above. Both of the Connectors
  2518. that come with jsPlumb - Straight and Bezier -implement these methods; if you write a custom connector, or have written a custom connector, you will need to supply
  2519. them.
  2520. <h4>Overlay Interface</h4>
  2521. An Overlay is required to implement two methods in order to be usable by jsPlumb:
  2522. <ul>
  2523. <li><strong>computeMaxSize(connector, context)</strong> - returns an integer value indicating the larger of this overlay's width and height. It is used by jsPlumb to ensure that the canvas is large enough to accomodate the overlay. The examples below should help to clarify this.</li>
  2524. <li><strong>draw(connector, ctx)</strong> - draws the overlay. What happens in this method is up to the given implementation.</li>
  2525. </ul>
  2526. <h4>computeMaxSize methods</h4>
  2527. This is the Arrow overlay's computeMaxSize method:
  2528. <div class="code">
  2529. <pre>
  2530. this.computeMaxSize = function() { return width; }
  2531. </pre>
  2532. </div>
  2533. Here, <em>width</em> is a private member of Arrow that indicates the width of the arrow's tail. So the Arrow overlay reports that figure as the width it needs.
  2534. Contrast this with the Label Overlay's computeMaxSize method:
  2535. <div class="code">
  2536. <pre>
  2537. this.computeMaxSize = function(connector, ctx) {
  2538. if (labelText) {
  2539. ctx.save();
  2540. if (self.labelStyle.font) ctx.font = self.labelStyle.font;
  2541. var t = ctx.measureText(labelText).width;
  2542. // a fake text height measurement: use the width of upper case M
  2543. var h = ctx.measureText("M").width;
  2544. labelPadding = self.labelStyle.padding || 0.25;
  2545. labelWidth = t + (2 * t * labelPadding);
  2546. labelHeight = h + (2 * h * labelPadding);
  2547. ctx.restore();
  2548. return Math.max(labelWidth, labelHeight);
  2549. }
  2550. return 0;
  2551. };
  2552. </pre>
  2553. </div>
  2554. The Label overlay has to use the context to determine how big it will be on screen.
  2555. <h4>draw methods</h4>
  2556. To give you a taste for how you can interact with a connector, consider the first few lines of the Arrow overlay's draw method:
  2557. <div class="code">
  2558. <pre>
  2559. this.draw = function(connector, ctx) {
  2560. // this is the arrow head position
  2561. var hxy = connector.pointAlongPathFrom(self.loc, length / 2);
  2562. // this is the center of the tail
  2563. var txy = connector.pointAlongPathFrom(self.loc, -length / 2), tx = txy[0], ty = txy[1];
  2564. // this is the tail vector
  2565. var tail = connector.perpendicularToPathAt(self.loc, -length / 2, width);
  2566. ...
  2567. </pre>
  2568. </div>
  2569. <p>The first two calls to the connector use the <strong>pointAlongPathFrom</strong> method, which passes <em>self.loc</em> as the location of the
  2570. point on the path, and <em>length / 2</em> as the number of pixels along the path to traverse.</p>
  2571. <p><em>self.loc</em> is Arrow's internal member describing the location of the overlay, and it is a decimal (between 0 and 1 inclusive) that points to
  2572. a location at some distance along the path inscribed by the connector. So these first two calls get us [x,y] locations of points on
  2573. the connector that mark the head and tail points for the arrow.
  2574. </p>
  2575. <p>
  2576. The <strong>connector.perpendicularToPathAt(self.loc, -length / 2, width)</strong> call returns a line description of a line that is perpendicular to, and centered on, the connector
  2577. at "-length / 2" pixels from the given point, with the given width (width is an internal member of Arrow). At this point, the Arrow object has the three main points it
  2578. needs in order to draw itself.
  2579. </p>
  2580. </div-->
  2581. </div>
  2582. </body>
  2583. </html>