mootools.jsPlumb-1.2.4-RC1.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * mootools.jsPlumb 1.2.4-RC1
  3. *
  4. * MooTools specific functionality for jsPlumb.
  5. *
  6. * http://morrisonpitt.com/jsPlumb/demo.html
  7. * http://code.google.com/p/jsPlumb
  8. *
  9. * NOTE: for production usage you should use mootools.jsPlumb-all-x.x.x-min.js, which contains the main jsPlumb script and this script together,
  10. * in a minified file.
  11. *
  12. * Dual licensed under MIT and GPL2.
  13. *
  14. */
  15. (function() {
  16. /*
  17. * overrides the FX class to inject 'step' functionality, which MooTools does not
  18. * offer, and which makes me sad. they don't seem keen to add it, either, despite
  19. * the fact that it could be useful:
  20. *
  21. * https://mootools.lighthouseapp.com/projects/2706/tickets/668
  22. *
  23. */
  24. var jsPlumbMorph = new Class({
  25. Extends:Fx.Morph,
  26. onStep : null,
  27. initialize : function(el, options) {
  28. this.parent(el, options);
  29. if (options['onStep']) {
  30. this.onStep = options['onStep'];
  31. }
  32. },
  33. step : function() {
  34. this.parent();
  35. if (this.onStep) {
  36. try { this.onStep(); }
  37. catch(e) { }
  38. }
  39. }
  40. });
  41. var _droppables = {};
  42. var _droppableOptions = {};
  43. var _draggablesByScope = {};
  44. var _draggablesById = {};
  45. /*
  46. *
  47. */
  48. var _executeDroppableOption = function(el, dr, event) {
  49. if (dr) {
  50. var id = dr.get("id");
  51. if (id) {
  52. var options = _droppableOptions[id];
  53. if (options) {
  54. if (options[event]) {
  55. options[event](el, dr);
  56. }
  57. }
  58. }
  59. }
  60. };
  61. var _checkHover = function(el, entering) {
  62. if (el) {
  63. var id = el.get("id");
  64. if (id) {
  65. var options = _droppableOptions[id];
  66. if (options) {
  67. if (options['hoverClass']) {
  68. if (entering) el.addClass(options['hoverClass']);
  69. else el.removeClass(options['hoverClass']);
  70. }
  71. }
  72. }
  73. }
  74. };
  75. /**
  76. * adds the given value to the given list, with the given scope. creates the scoped list
  77. * if necessary.
  78. * used by initDraggable and initDroppable.
  79. */
  80. var _add = function(list, scope, value) {
  81. var l = list[scope];
  82. if (!l) {
  83. l = [];
  84. list[scope] = l;
  85. }
  86. l.push(value);
  87. };
  88. jsPlumb.CurrentLibrary = {
  89. dragEvents : {
  90. 'start':'onStart', 'stop':'onComplete', 'drag':'onDrag', 'step':'onStep',
  91. 'over':'onEnter', 'out':'onLeave','drop':'onDrop', 'complete':'onComplete'
  92. },
  93. appendElement : function(child, parent) {
  94. jsPlumb.CurrentLibrary.getElementObject(parent).grab(child);
  95. },
  96. bind : function(el, event, callback) {
  97. el = jsPlumb.CurrentLibrary.getElementObject(el);
  98. el.addEvent(event, callback);
  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 an "element object" from the given input. this means an object that is used by the
  110. * underlying library on which jsPlumb is running. 'el' may already be one of these objects,
  111. * in which case it is returned as-is. otherwise, 'el' is a String, the library's lookup
  112. * function is used to find the element, using the given String as the element's id.
  113. */
  114. getElementObject : function(el) {
  115. return $(el);
  116. },
  117. /*
  118. gets the offset for the element object. this should return a js object like this:
  119. { left:xxx, top: xxx}
  120. */
  121. getOffset : function(el) {
  122. var p = el.getPosition();
  123. return { left:p.x, top:p.y };
  124. },
  125. setOffset : function(el, o) {
  126. jsPlumb.CurrentLibrary.getElementObject(el).setPosition({x:o.left, y:o.top});
  127. },
  128. getSize : function(el) {
  129. var s = el.getSize();
  130. return [s.x, s.y];
  131. },
  132. /**
  133. * gets the named attribute from the given element object.
  134. */
  135. getAttribute : function(el, attName) {
  136. return el.get(attName);
  137. },
  138. /**
  139. * sets the named attribute on the given element object.
  140. */
  141. setAttribute : function(el, attName, attValue) {
  142. el.set(attName, attValue);
  143. },
  144. /**
  145. * adds the given class to the element object.
  146. */
  147. addClass : function(el, clazz) {
  148. el.addClass(clazz);
  149. },
  150. /**
  151. * removes the given class from the element object.
  152. */
  153. removeClass : function(el, clazz) {
  154. el.removeClass(clazz);
  155. },
  156. initDraggable : function(el, options) {
  157. var id = jsPlumb.getId(el);
  158. var drag = _draggablesById[id];
  159. if (!drag) {
  160. var originalZIndex = 0, originalCursor = null;
  161. var dragZIndex = jsPlumb.Defaults.DragOptions.zIndex || 2000;
  162. options['onStart'] = jsPlumb.wrap(options['onStart'], function()
  163. {
  164. originalZIndex = this.element.getStyle('z-index');
  165. this.element.setStyle('z-index', dragZIndex);
  166. if (jsPlumb.Defaults.DragOptions.cursor) {
  167. originalCursor = this.element.getStyle('cursor');
  168. this.element.setStyle('cursor', jsPlumb.Defaults.DragOptions.cursor);
  169. }
  170. });
  171. options['onComplete'] = jsPlumb.wrap(options['onComplete'], function()
  172. {
  173. this.element.setStyle('z-index', originalZIndex);
  174. if (originalCursor) {
  175. this.element.setStyle('cursor', originalCursor);
  176. }
  177. });
  178. // DROPPABLES:
  179. var scope = options['scope'] || jsPlumb.Defaults.Scope;
  180. var filterFunc = function(entry) {
  181. return entry.get("id") != el.get("id");
  182. };
  183. var droppables = _droppables[scope] ? _droppables[scope].filter(filterFunc) : [];
  184. options['droppables'] = droppables;
  185. options['onLeave'] = jsPlumb.wrap(options['onLeave'], function(el, dr) {
  186. if (dr) {
  187. _checkHover(dr, false);
  188. _executeDroppableOption(el, dr, 'onLeave');
  189. }
  190. });
  191. options['onEnter'] = jsPlumb.wrap(options['onEnter'], function(el, dr) {
  192. if (dr) {
  193. _checkHover(dr, true);
  194. _executeDroppableOption(el, dr, 'onEnter');
  195. }
  196. });
  197. options['onDrop'] = function(el, dr) {
  198. if (dr) {
  199. _checkHover(dr, false);
  200. _executeDroppableOption(el, dr, 'onDrop');
  201. }
  202. };
  203. drag = new Drag.Move(el, options);
  204. _add(_draggablesByScope, scope, drag);
  205. _add(_draggablesById, el.get("id"), drag);
  206. // test for disabled.
  207. if (options.disabled) drag.detach();
  208. }
  209. return drag;
  210. },
  211. isDragSupported : function(el, options) {
  212. return typeof Drag != 'undefined' ;
  213. },
  214. setDraggable : function(el, draggable) {
  215. var draggables = _draggablesById[el.get("id")];
  216. if (draggables) {
  217. draggables.each(function(d) {
  218. if (draggable) d.attach(); else d.detach();
  219. });
  220. }
  221. },
  222. initDroppable : function(el, options) {
  223. var scope = options['scope'] || jsPlumb.Defaults.Scope;
  224. _add(_droppables, scope, el);
  225. var id = jsPlumb.getId(el);
  226. _droppableOptions[id] = options;
  227. var filterFunc = function(entry) {
  228. return entry.element != el;
  229. };
  230. var draggables = _draggablesByScope[scope] ? _draggablesByScope[scope].filter(filterFunc) : [];
  231. for (var i = 0; i < draggables.length; i++) {
  232. draggables[i].droppables.push(el);
  233. }
  234. },
  235. /*
  236. * you need Drag.Move imported to make drop work.
  237. */
  238. isDropSupported : function(el, options) {
  239. if (typeof Drag != undefined)
  240. return typeof Drag.Move != undefined;
  241. return false;
  242. },
  243. animate : function(el, properties, options) {
  244. var m = new jsPlumbMorph(el, options);
  245. m.start(properties);
  246. },
  247. /*
  248. * takes the args passed to an event function and returns you an object that gives the
  249. * position of the object being moved, as a js object with the same params as the result of
  250. * getOffset, ie: { left: xxx, top: xxx }.
  251. */
  252. getUIPosition : function(eventArgs) {
  253. var ui = eventArgs[0];
  254. return { left: ui.offsetLeft, top: ui.offsetTop };
  255. },
  256. getDragObject : function(eventArgs) {
  257. return eventArgs[0];
  258. },
  259. removeElement : function(element, parent) {
  260. jsPlumb.CurrentLibrary.getElementObject(element).dispose(); // ??
  261. },
  262. getScrollLeft : function(el) {
  263. return null;
  264. },
  265. getScrollTop : function(el) {
  266. return null;
  267. }
  268. };
  269. })();