jquery.jsPlumb-1.3.0-RC1.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * jsPlumb
  3. *
  4. * jquery.jsPlumb 1.3.0-RC1
  5. *
  6. * jQuery specific functionality for jsPlumb.
  7. *
  8. * http://jsplumb.org
  9. * http://code.google.com/p/jsplumb
  10. *
  11. * Triple licensed under MIT, GPL2 and Beer licenses.
  12. *
  13. */
  14. /*
  15. * the library specific functions, such as find offset, get id, get attribute, extend etc.
  16. * the full list is:
  17. *
  18. * addClass adds a class to the given element
  19. * animate calls the underlying library's animate functionality
  20. * appendElement appends a child element to a parent element.
  21. * bind binds some event to an element
  22. * dragEvents a dictionary of event names
  23. * extend extend some js object with another. probably not overly necessary; jsPlumb could just do this internally.
  24. * getAttribute gets some attribute from an element
  25. * getDragObject gets the object that is being dragged, by extracting it from the arguments passed to a drag callback
  26. * getDragScope gets the drag scope for a given element.
  27. * getElementObject turns an id or dom element into an element object of the underlying library's type.
  28. * getOffset gets an element's offset
  29. * getPageXY gets the page event's xy location.
  30. * getParent gets the parent of some element.
  31. * getScrollLeft gets an element's scroll left. TODO: is this actually used? will it be?
  32. * getScrollTop gets an element's scroll top. TODO: is this actually used? will it be?
  33. * getSize gets an element's size.
  34. * getUIPosition gets the position of some element that is currently being dragged, by extracting it from the arguments passed to a drag callback.
  35. * hasClass returns whether or not the given element has the given class.
  36. * initDraggable initializes an element to be draggable
  37. * initDroppable initializes an element to be droppable
  38. * isDragSupported returns whether or not drag is supported for some element.
  39. * isDropSupported returns whether or not drop is supported for some element.
  40. * removeClass removes a class from a given element.
  41. * removeElement removes some element completely from the DOM.
  42. * setAttribute sets an attribute on some element.
  43. * setDraggable sets whether or not some element should be draggable.
  44. * setDragScope sets the drag scope for a given element.
  45. * setOffset sets the offset of some element.
  46. */
  47. (function($) {
  48. //var getBoundingClientRectSupported = "getBoundingClientRect" in document.documentElement;
  49. jsPlumb.CurrentLibrary = {
  50. /**
  51. * adds the given class to the element object.
  52. */
  53. addClass : function(el, clazz) {
  54. el.addClass(clazz);
  55. },
  56. /**
  57. * animates the given element.
  58. */
  59. animate : function(el, properties, options) {
  60. el.animate(properties, options);
  61. },
  62. /**
  63. * appends the given child to the given parent.
  64. */
  65. appendElement : function(child, parent) {
  66. jsPlumb.CurrentLibrary.getElementObject(parent).append(child);
  67. },
  68. /**
  69. * event binding wrapper. it just so happens that jQuery uses 'bind' also. yui3, for example,
  70. * uses 'on'.
  71. */
  72. bind : function(el, event, callback) {
  73. el = jsPlumb.CurrentLibrary.getElementObject(el);
  74. el.bind(event, callback);
  75. },
  76. /**
  77. * mapping of drag events for jQuery
  78. */
  79. dragEvents : {
  80. 'start':'start', 'stop':'stop', 'drag':'drag', 'step':'step',
  81. 'over':'over', 'out':'out', 'drop':'drop', 'complete':'complete'
  82. },
  83. /**
  84. * wrapper around the library's 'extend' functionality (which it hopefully has.
  85. * otherwise you'll have to do it yourself). perhaps jsPlumb could do this for you
  86. * instead. it's not like its hard.
  87. */
  88. extend : function(o1, o2) {
  89. return $.extend(o1, o2);
  90. },
  91. /**
  92. * gets the named attribute from the given element object.
  93. */
  94. getAttribute : function(el, attName) {
  95. return el.attr(attName);
  96. },
  97. getClientXY : function(eventObject) {
  98. return [eventObject.clientX, eventObject.clientY];
  99. },
  100. getDocumentElement : function() { return document; },
  101. /**
  102. * takes the args passed to an event function and returns you an object representing that which is being dragged.
  103. */
  104. getDragObject : function(eventArgs) {
  105. return eventArgs[1].draggable;
  106. },
  107. getDragScope : function(el) {
  108. return el.draggable("option", "scope");
  109. },
  110. /**
  111. * gets an "element object" from the given input. this means an object that is used by the
  112. * underlying library on which jsPlumb is running. 'el' may already be one of these objects,
  113. * in which case it is returned as-is. otherwise, 'el' is a String, the library's lookup
  114. * function is used to find the element, using the given String as the element's id.
  115. *
  116. */
  117. getElementObject : function(el) {
  118. return typeof(el)=='string' ? $("#" + el) : $(el);
  119. },
  120. /**
  121. * gets the offset for the element object. this should return a js object like this:
  122. *
  123. * { left:xxx, top: xxx }
  124. */
  125. getOffset : function(el) {
  126. return el.offset();
  127. },
  128. getPageXY : function(eventObject) {
  129. return [eventObject.pageX, eventObject.pageY];
  130. },
  131. getParent : function(el) {
  132. return jsPlumb.CurrentLibrary.getElementObject(el).parent();
  133. },
  134. getScrollLeft : function(el) {
  135. return el.scrollLeft();
  136. },
  137. getScrollTop : function(el) {
  138. return el.scrollTop();
  139. },
  140. /**
  141. * gets the size for the element object, in an array : [ width, height ].
  142. */
  143. getSize : function(el) {
  144. return [el.outerWidth(), el.outerHeight()];
  145. },
  146. /**
  147. * takes the args passed to an event function and returns you an object that gives the
  148. * position of the object being moved, as a js object with the same params as the result of
  149. * getOffset, ie: { left: xxx, top: xxx }.
  150. *
  151. * different libraries have different signatures for their event callbacks.
  152. * see getDragObject as well
  153. */
  154. getUIPosition : function(eventArgs) {
  155. // this code is a workaround for the case that the element being dragged has a margin set on it. jquery UI passes
  156. // in the wrong offset if the element has a margin (it doesn't take the margin into account). the getBoundingClientRect
  157. // method, which is in pretty much all browsers now, reports the right numbers. but it introduces a noticeable lag, which
  158. // i don't like.
  159. /*if ( getBoundingClientRectSupported ) {
  160. var r = eventArgs[1].helper[0].getBoundingClientRect();
  161. return { left : r.left, top: r.top };
  162. } else {*/
  163. var ui = eventArgs[1], _offset = ui.offset;
  164. return _offset || ui.absolutePosition;
  165. //}
  166. },
  167. hasClass : function(el, clazz) {
  168. return el.hasClass(clazz);
  169. },
  170. /**
  171. * initialises the given element to be draggable.
  172. */
  173. initDraggable : function(el, options) {
  174. // remove helper directive if present.
  175. options.helper = null;
  176. options['scope'] = options['scope'] || jsPlumb.Defaults.Scope;
  177. el.draggable(options);
  178. },
  179. /**
  180. * initialises the given element to be droppable.
  181. */
  182. initDroppable : function(el, options) {
  183. options['scope'] = options['scope'] || jsPlumb.Defaults.Scope;
  184. el.droppable(options);
  185. },
  186. isAlreadyDraggable : function(el) {
  187. el = jsPlumb.CurrentLibrary.getElementObject(el);
  188. return el.hasClass("ui-draggable");
  189. },
  190. /**
  191. * returns whether or not drag is supported (by the library, not whether or not it is disabled) for the given element.
  192. */
  193. isDragSupported : function(el, options) {
  194. return el.draggable;
  195. },
  196. /**
  197. * returns whether or not drop is supported (by the library, not whether or not it is disabled) for the given element.
  198. */
  199. isDropSupported : function(el, options) {
  200. return el.droppable;
  201. },
  202. /**
  203. * removes the given class from the element object.
  204. */
  205. removeClass : function(el, clazz) {
  206. el.removeClass(clazz);
  207. },
  208. removeElement : function(element, parent) {
  209. jsPlumb.CurrentLibrary.getElementObject(element).remove();
  210. },
  211. /**
  212. * sets the named attribute on the given element object.
  213. */
  214. setAttribute : function(el, attName, attValue) {
  215. el.attr(attName, attValue);
  216. },
  217. /**
  218. * sets the draggable state for the given element
  219. */
  220. setDraggable : function(el, draggable) {
  221. el.draggable("option", "disabled", !draggable);
  222. },
  223. /**
  224. * sets the drag scope. probably time for a setDragOption method (roll this and the one above together)
  225. * @param el
  226. * @param scope
  227. */
  228. setDragScope : function(el, scope) {
  229. el.draggable("option", "scope", scope);
  230. },
  231. setOffset : function(el, o) {
  232. jsPlumb.CurrentLibrary.getElementObject(el).offset(o);
  233. }
  234. };
  235. $(document).ready(jsPlumb.init);
  236. })(jQuery);