jquery.jsPlumb-1.3.4-RC1.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * jsPlumb
  3. *
  4. * Title:jsPlumb 1.3.5
  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 an "element object" from the given input. this means an object that is used by the
  132. * underlying library on which jsPlumb is running. 'el' may already be one of these objects,
  133. * in which case it is returned as-is. otherwise, 'el' is a String, the library's lookup
  134. * function is used to find the element, using the given String as the element's id.
  135. *
  136. */
  137. getElementObject : function(el) {
  138. return typeof(el)=='string' ? $("#" + el) : $(el);
  139. },
  140. /**
  141. * gets the offset for the element object. this should return a js object like this:
  142. *
  143. * { left:xxx, top: xxx }
  144. */
  145. getOffset : function(el) {
  146. return el.offset();
  147. },
  148. getPageXY : function(eventObject) {
  149. return [eventObject.pageX, eventObject.pageY];
  150. },
  151. getParent : function(el) {
  152. return jsPlumb.CurrentLibrary.getElementObject(el).parent();
  153. },
  154. getScrollLeft : function(el) {
  155. return el.scrollLeft();
  156. },
  157. getScrollTop : function(el) {
  158. return el.scrollTop();
  159. },
  160. getSelector : function(spec) {
  161. return $(spec);
  162. },
  163. /**
  164. * gets the size for the element object, in an array : [ width, height ].
  165. */
  166. getSize : function(el) {
  167. return [el.outerWidth(), el.outerHeight()];
  168. },
  169. getTagName : function(el) {
  170. var e = jsPlumb.CurrentLibrary.getElementObject(el);
  171. return e.length > 0 ? e[0].tagName : null;
  172. },
  173. /**
  174. * takes the args passed to an event function and returns you an object that gives the
  175. * position of the object being moved, as a js object with the same params as the result of
  176. * getOffset, ie: { left: xxx, top: xxx }.
  177. *
  178. * different libraries have different signatures for their event callbacks.
  179. * see getDragObject as well
  180. */
  181. getUIPosition : function(eventArgs) {
  182. // this code is a workaround for the case that the element being dragged has a margin set on it. jquery UI passes
  183. // in the wrong offset if the element has a margin (it doesn't take the margin into account). the getBoundingClientRect
  184. // method, which is in pretty much all browsers now, reports the right numbers. but it introduces a noticeable lag, which
  185. // i don't like.
  186. /*if ( getBoundingClientRectSupported ) {
  187. var r = eventArgs[1].helper[0].getBoundingClientRect();
  188. return { left : r.left, top: r.top };
  189. } else {*/
  190. if (eventArgs.length == 1) {
  191. return { left: eventArgs[0].pageX, top:eventArgs[0].pageY };
  192. }
  193. else {
  194. var ui = eventArgs[1], _offset = ui.offset;
  195. return _offset || ui.absolutePosition;
  196. }
  197. },
  198. hasClass : function(el, clazz) {
  199. return el.hasClass(clazz);
  200. },
  201. /**
  202. * initialises the given element to be draggable.
  203. */
  204. initDraggable : function(el, options) {
  205. options = options || {};
  206. // remove helper directive if present.
  207. options.helper = null;
  208. options['scope'] = options['scope'] || jsPlumb.Defaults.Scope;
  209. el.draggable(options);
  210. },
  211. /**
  212. * initialises the given element to be droppable.
  213. */
  214. initDroppable : function(el, options) {
  215. options['scope'] = options['scope'] || jsPlumb.Defaults.Scope;
  216. el.droppable(options);
  217. },
  218. isAlreadyDraggable : function(el) {
  219. el = jsPlumb.CurrentLibrary.getElementObject(el);
  220. return el.hasClass("ui-draggable");
  221. },
  222. /**
  223. * returns whether or not drag is supported (by the library, not whether or not it is disabled) for the given element.
  224. */
  225. isDragSupported : function(el, options) {
  226. return el.draggable;
  227. },
  228. /**
  229. * returns whether or not drop is supported (by the library, not whether or not it is disabled) for the given element.
  230. */
  231. isDropSupported : function(el, options) {
  232. return el.droppable;
  233. },
  234. /**
  235. * removes the given class from the element object.
  236. */
  237. removeClass : function(el, clazz) {
  238. el = jsPlumb.CurrentLibrary.getElementObject(el);
  239. try {
  240. if (el[0].className.constructor == SVGAnimatedString) {
  241. jsPlumb.util.svg.removeClass(el[0], clazz);
  242. }
  243. }
  244. catch (e) {
  245. // SVGAnimatedString not supported; no problem.
  246. }
  247. el.removeClass(clazz);
  248. },
  249. removeElement : function(element, parent) {
  250. jsPlumb.CurrentLibrary.getElementObject(element).remove();
  251. },
  252. /**
  253. * sets the named attribute on the given element object.
  254. */
  255. setAttribute : function(el, attName, attValue) {
  256. el.attr(attName, attValue);
  257. },
  258. /**
  259. * sets the draggable state for the given element
  260. */
  261. setDraggable : function(el, draggable) {
  262. el.draggable("option", "disabled", !draggable);
  263. },
  264. /**
  265. * sets the drag scope. probably time for a setDragOption method (roll this and the one above together)
  266. * @param el
  267. * @param scope
  268. */
  269. setDragScope : function(el, scope) {
  270. el.draggable("option", "scope", scope);
  271. },
  272. setOffset : function(el, o) {
  273. jsPlumb.CurrentLibrary.getElementObject(el).offset(o);
  274. },
  275. /**
  276. * note that jquery ignores the name of the event you wanted to trigger, and figures it out for itself.
  277. * the other libraries do not. yui, in fact, cannot even pass an original event. we have to pull out stuff
  278. * from the originalEvent to put in an options object for YUI.
  279. * @param el
  280. * @param event
  281. * @param originalEvent
  282. */
  283. trigger : function(el, event, originalEvent) {
  284. //originalEvent.stopPropagation();
  285. //jsPlumb.CurrentLibrary.getElementObject(el).trigger(originalEvent);
  286. var h = jQuery._data(jsPlumb.CurrentLibrary.getElementObject(el)[0], "handle");
  287. h(originalEvent);
  288. //originalEvent.stopPropagation();
  289. },
  290. /**
  291. * event unbinding wrapper. it just so happens that jQuery uses 'unbind' also. yui3, for example,
  292. * uses..something else.
  293. */
  294. unbind : function(el, event, callback) {
  295. el = jsPlumb.CurrentLibrary.getElementObject(el);
  296. el.unbind(event, callback);
  297. }
  298. };
  299. $(document).ready(jsPlumb.init);
  300. })(jQuery);