jquery.jsPlumb-1.3.6-RC1.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * jsPlumb
  3. *
  4. * Title:jsPlumb 1.3.6
  5. *
  6. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  7. * elements, or VML.
  8. *
  9. * This file contains the jQuery adapter.
  10. *
  11. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  12. *
  13. * http://jsplumb.org
  14. * http://github.com/sporritt/jsplumb
  15. * http://code.google.com/p/jsplumb
  16. *
  17. * Dual licensed under the MIT and GPL2 licenses.
  18. */
  19. /*
  20. * the library specific functions, such as find offset, get id, get attribute, extend etc.
  21. * the full list is:
  22. *
  23. * addClass adds a class to the given element
  24. * animate calls the underlying library's animate functionality
  25. * appendElement appends a child element to a parent element.
  26. * bind binds some event to an element
  27. * dragEvents a dictionary of event names
  28. * extend extend some js object with another. probably not overly necessary; jsPlumb could just do this internally.
  29. * getAttribute gets some attribute from an element
  30. * getDragObject gets the object that is being dragged, by extracting it from the arguments passed to a drag callback
  31. * getDragScope gets the drag scope for a given element.
  32. * getDropScope gets the drop scope for a given element.
  33. * getElementObject turns an id or dom element into an element object of the underlying library's type.
  34. * getOffset gets an element's offset
  35. * getPageXY gets the page event's xy location.
  36. * getParent gets the parent of some element.
  37. * getScrollLeft gets an element's scroll left. TODO: is this actually used? will it be?
  38. * getScrollTop gets an element's scroll top. TODO: is this actually used? will it be?
  39. * getSize gets an element's size.
  40. * getUIPosition gets the position of some element that is currently being dragged, by extracting it from the arguments passed to a drag callback.
  41. * hasClass returns whether or not the given element has the given class.
  42. * initDraggable initializes an element to be draggable
  43. * initDroppable initializes an element to be droppable
  44. * isDragSupported returns whether or not drag is supported for some element.
  45. * isDropSupported returns whether or not drop is supported for some element.
  46. * removeClass removes a class from a given element.
  47. * removeElement removes some element completely from the DOM.
  48. * setAttribute sets an attribute on some element.
  49. * setDraggable sets whether or not some element should be draggable.
  50. * setDragScope sets the drag scope for a given element.
  51. * setOffset sets the offset of some element.
  52. * trigger triggers some event on an element.
  53. * unbind unbinds some listener from some element.
  54. */
  55. (function($) {
  56. //var getBoundingClientRectSupported = "getBoundingClientRect" in document.documentElement;
  57. jsPlumb.CurrentLibrary = {
  58. /**
  59. * adds the given class to the element object.
  60. */
  61. addClass : function(el, clazz) {
  62. el = jsPlumb.CurrentLibrary.getElementObject(el);
  63. try {
  64. if (el[0].className.constructor == SVGAnimatedString) {
  65. jsPlumb.util.svg.addClass(el[0], clazz);
  66. }
  67. }
  68. catch (e) {
  69. // SVGAnimatedString not supported; no problem.
  70. }
  71. el.addClass(clazz);
  72. },
  73. /**
  74. * animates the given element.
  75. */
  76. animate : function(el, properties, options) {
  77. el.animate(properties, options);
  78. },
  79. /**
  80. * appends the given child to the given parent.
  81. */
  82. appendElement : function(child, parent) {
  83. jsPlumb.CurrentLibrary.getElementObject(parent).append(child);
  84. },
  85. /**
  86. * event binding wrapper. it just so happens that jQuery uses 'bind' also. yui3, for example,
  87. * uses 'on'.
  88. */
  89. bind : function(el, event, callback) {
  90. el = jsPlumb.CurrentLibrary.getElementObject(el);
  91. el.bind(event, callback);
  92. },
  93. /**
  94. * mapping of drag events for jQuery
  95. */
  96. dragEvents : {
  97. 'start':'start', 'stop':'stop', 'drag':'drag', 'step':'step',
  98. 'over':'over', 'out':'out', 'drop':'drop', 'complete':'complete'
  99. },
  100. /**
  101. * wrapper around the library's 'extend' functionality (which it hopefully has.
  102. * otherwise you'll have to do it yourself). perhaps jsPlumb could do this for you
  103. * instead. it's not like its hard.
  104. */
  105. extend : function(o1, o2) {
  106. return $.extend(o1, o2);
  107. },
  108. /**
  109. * gets the named attribute from the given element object.
  110. */
  111. getAttribute : function(el, attName) {
  112. return el.attr(attName);
  113. },
  114. getClientXY : function(eventObject) {
  115. return [eventObject.clientX, eventObject.clientY];
  116. },
  117. getDocumentElement : function() { return document; },
  118. /**
  119. * takes the args passed to an event function and returns you an object representing that which is being dragged.
  120. */
  121. getDragObject : function(eventArgs) {
  122. return eventArgs[1].draggable;
  123. },
  124. getDragScope : function(el) {
  125. return el.draggable("option", "scope");
  126. },
  127. getDropScope : function(el) {
  128. return el.droppable("option", "scope");
  129. },
  130. /**
  131. * gets a DOM element from the given input, which might be a string (in which case we just do document.getElementById),
  132. * a selector (in which case we return el[0]), or a DOM element already (we assume this if it's not either of the other
  133. * two cases). this is the opposite of getElementObject below.
  134. */
  135. getDOMElement : function(el) {
  136. if (typeof(el) == "string") return document.getElementById(el);
  137. else if (el.context) return el[0];
  138. else return el;
  139. },
  140. /**
  141. * gets an "element object" from the given input. this means an object that is used by the
  142. * underlying library on which jsPlumb is running. 'el' may already be one of these objects,
  143. * in which case it is returned as-is. otherwise, 'el' is a String, the library's lookup
  144. * function is used to find the element, using the given String as the element's id.
  145. *
  146. */
  147. getElementObject : function(el) {
  148. return typeof(el) == "string" ? $("#" + el) : $(el);
  149. },
  150. /**
  151. * gets the offset for the element object. this should return a js object like this:
  152. *
  153. * { left:xxx, top: xxx }
  154. */
  155. getOffset : function(el) {
  156. return el.offset();
  157. },
  158. getPageXY : function(eventObject) {
  159. return [eventObject.pageX, eventObject.pageY];
  160. },
  161. getParent : function(el) {
  162. return jsPlumb.CurrentLibrary.getElementObject(el).parent();
  163. },
  164. getScrollLeft : function(el) {
  165. return el.scrollLeft();
  166. },
  167. getScrollTop : function(el) {
  168. return el.scrollTop();
  169. },
  170. getSelector : function(spec) {
  171. return $(spec);
  172. },
  173. /**
  174. * gets the size for the element object, in an array : [ width, height ].
  175. */
  176. getSize : function(el) {
  177. return [el.outerWidth(), el.outerHeight()];
  178. },
  179. getTagName : function(el) {
  180. var e = jsPlumb.CurrentLibrary.getElementObject(el);
  181. return e.length > 0 ? e[0].tagName : null;
  182. },
  183. /**
  184. * takes the args passed to an event function and returns you an object that gives the
  185. * position of the object being moved, as a js object with the same params as the result of
  186. * getOffset, ie: { left: xxx, top: xxx }.
  187. *
  188. * different libraries have different signatures for their event callbacks.
  189. * see getDragObject as well
  190. */
  191. getUIPosition : function(eventArgs) {
  192. // this code is a workaround for the case that the element being dragged has a margin set on it. jquery UI passes
  193. // in the wrong offset if the element has a margin (it doesn't take the margin into account). the getBoundingClientRect
  194. // method, which is in pretty much all browsers now, reports the right numbers. but it introduces a noticeable lag, which
  195. // i don't like.
  196. /*if ( getBoundingClientRectSupported ) {
  197. var r = eventArgs[1].helper[0].getBoundingClientRect();
  198. return { left : r.left, top: r.top };
  199. } else {*/
  200. if (eventArgs.length == 1) {
  201. ret = { left: eventArgs[0].pageX, top:eventArgs[0].pageY };
  202. }
  203. else {
  204. var ui = eventArgs[1], _offset = ui.offset;
  205. ret = _offset || ui.absolutePosition;
  206. }
  207. return ret;
  208. },
  209. hasClass : function(el, clazz) {
  210. return el.hasClass(clazz);
  211. },
  212. /**
  213. * initialises the given element to be draggable.
  214. */
  215. initDraggable : function(el, options) {
  216. options = options || {};
  217. // remove helper directive if present.
  218. options.helper = null;
  219. options['scope'] = options['scope'] || jsPlumb.Defaults.Scope;
  220. el.draggable(options);
  221. },
  222. /**
  223. * initialises the given element to be droppable.
  224. */
  225. initDroppable : function(el, options) {
  226. options['scope'] = options['scope'] || jsPlumb.Defaults.Scope;
  227. el.droppable(options);
  228. },
  229. isAlreadyDraggable : function(el) {
  230. el = jsPlumb.CurrentLibrary.getElementObject(el);
  231. return el.hasClass("ui-draggable");
  232. },
  233. /**
  234. * returns whether or not drag is supported (by the library, not whether or not it is disabled) for the given element.
  235. */
  236. isDragSupported : function(el, options) {
  237. return el.draggable;
  238. },
  239. /**
  240. * returns whether or not drop is supported (by the library, not whether or not it is disabled) for the given element.
  241. */
  242. isDropSupported : function(el, options) {
  243. return el.droppable;
  244. },
  245. /**
  246. * removes the given class from the element object.
  247. */
  248. removeClass : function(el, clazz) {
  249. el = jsPlumb.CurrentLibrary.getElementObject(el);
  250. try {
  251. if (el[0].className.constructor == SVGAnimatedString) {
  252. jsPlumb.util.svg.removeClass(el[0], clazz);
  253. }
  254. }
  255. catch (e) {
  256. // SVGAnimatedString not supported; no problem.
  257. }
  258. el.removeClass(clazz);
  259. },
  260. removeElement : function(element, parent) {
  261. jsPlumb.CurrentLibrary.getElementObject(element).remove();
  262. },
  263. /**
  264. * sets the named attribute on the given element object.
  265. */
  266. setAttribute : function(el, attName, attValue) {
  267. el.attr(attName, attValue);
  268. },
  269. /**
  270. * sets the draggable state for the given element
  271. */
  272. setDraggable : function(el, draggable) {
  273. el.draggable("option", "disabled", !draggable);
  274. },
  275. /**
  276. * sets the drag scope. probably time for a setDragOption method (roll this and the one above together)
  277. * @param el
  278. * @param scope
  279. */
  280. setDragScope : function(el, scope) {
  281. el.draggable("option", "scope", scope);
  282. },
  283. setOffset : function(el, o) {
  284. jsPlumb.CurrentLibrary.getElementObject(el).offset(o);
  285. },
  286. /**
  287. * note that jquery ignores the name of the event you wanted to trigger, and figures it out for itself.
  288. * the other libraries do not. yui, in fact, cannot even pass an original event. we have to pull out stuff
  289. * from the originalEvent to put in an options object for YUI.
  290. * @param el
  291. * @param event
  292. * @param originalEvent
  293. */
  294. trigger : function(el, event, originalEvent) {
  295. //originalEvent.stopPropagation();
  296. //jsPlumb.CurrentLibrary.getElementObject(el).trigger(originalEvent);
  297. var h = jQuery._data(jsPlumb.CurrentLibrary.getElementObject(el)[0], "handle");
  298. h(originalEvent);
  299. //originalEvent.stopPropagation();
  300. },
  301. /**
  302. * event unbinding wrapper. it just so happens that jQuery uses 'unbind' also. yui3, for example,
  303. * uses..something else.
  304. */
  305. unbind : function(el, event, callback) {
  306. el = jsPlumb.CurrentLibrary.getElementObject(el);
  307. el.unbind(event, callback);
  308. }
  309. };
  310. $(document).ready(jsPlumb.init);
  311. })(jQuery);