mootools.jsPlumb-1.2.5-RC1.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * mootools.jsPlumb 1.2.5-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. /*
  89. * gets an "element object" from the given input. this means an object that is used by the
  90. * underlying library on which jsPlumb is running. 'el' may already be one of these objects,
  91. * in which case it is returned as-is. otherwise, 'el' is a String, the library's lookup
  92. * function is used to find the element, using the given String as the element's id.
  93. */
  94. var _getElementObject = function(el) {
  95. return $(el);
  96. };
  97. jsPlumb.CurrentLibrary = {
  98. /**
  99. * adds the given class to the element object.
  100. */
  101. addClass : function(el, clazz) {
  102. el.addClass(clazz);
  103. },
  104. animate : function(el, properties, options) {
  105. var m = new jsPlumbMorph(el, options);
  106. m.start(properties);
  107. },
  108. appendElement : function(child, parent) {
  109. _getElementObject(parent).grab(child);
  110. },
  111. bind : function(el, event, callback) {
  112. el = _getElementObject(el);
  113. el.addEvent(event, callback);
  114. },
  115. dragEvents : {
  116. 'start':'onStart', 'stop':'onComplete', 'drag':'onDrag', 'step':'onStep',
  117. 'over':'onEnter', 'out':'onLeave','drop':'onDrop', 'complete':'onComplete'
  118. },
  119. /*
  120. * wrapper around the library's 'extend' functionality (which it hopefully has.
  121. * otherwise you'll have to do it yourself). perhaps jsPlumb could do this for you
  122. * instead. it's not like its hard.
  123. */
  124. extend : function(o1, o2) {
  125. return $extend(o1, o2);
  126. },
  127. /**
  128. * gets the named attribute from the given element object.
  129. */
  130. getAttribute : function(el, attName) {
  131. return el.get(attName);
  132. },
  133. getDragObject : function(eventArgs) {
  134. return eventArgs[0];
  135. },
  136. getElementObject : _getElementObject,
  137. /*
  138. gets the offset for the element object. this should return a js object like this:
  139. { left:xxx, top: xxx}
  140. */
  141. getOffset : function(el) {
  142. var p = el.getPosition();
  143. return { left:p.x, top:p.y };
  144. },
  145. getScrollLeft : function(el) {
  146. return null;
  147. },
  148. getScrollTop : function(el) {
  149. return null;
  150. },
  151. getSize : function(el) {
  152. var s = el.getSize();
  153. return [s.x, s.y];
  154. },
  155. /*
  156. * takes the args passed to an event function and returns you an object that gives the
  157. * position of the object being moved, as a js object with the same params as the result of
  158. * getOffset, ie: { left: xxx, top: xxx }.
  159. */
  160. getUIPosition : function(eventArgs) {
  161. var ui = eventArgs[0];
  162. return { left: ui.offsetLeft, top: ui.offsetTop };
  163. },
  164. initDraggable : function(el, options) {
  165. var id = jsPlumb.getId(el);
  166. var drag = _draggablesById[id];
  167. if (!drag) {
  168. var originalZIndex = 0, originalCursor = null;
  169. var dragZIndex = jsPlumb.Defaults.DragOptions.zIndex || 2000;
  170. options['onStart'] = jsPlumb.wrap(options['onStart'], function()
  171. {
  172. originalZIndex = this.element.getStyle('z-index');
  173. this.element.setStyle('z-index', dragZIndex);
  174. if (jsPlumb.Defaults.DragOptions.cursor) {
  175. originalCursor = this.element.getStyle('cursor');
  176. this.element.setStyle('cursor', jsPlumb.Defaults.DragOptions.cursor);
  177. }
  178. });
  179. options['onComplete'] = jsPlumb.wrap(options['onComplete'], function()
  180. {
  181. this.element.setStyle('z-index', originalZIndex);
  182. if (originalCursor) {
  183. this.element.setStyle('cursor', originalCursor);
  184. }
  185. });
  186. // DROPPABLES:
  187. var scope = options['scope'] || jsPlumb.Defaults.Scope;
  188. var filterFunc = function(entry) {
  189. return entry.get("id") != el.get("id");
  190. };
  191. var droppables = _droppables[scope] ? _droppables[scope].filter(filterFunc) : [];
  192. options['droppables'] = droppables;
  193. options['onLeave'] = jsPlumb.wrap(options['onLeave'], function(el, dr) {
  194. if (dr) {
  195. _checkHover(dr, false);
  196. _executeDroppableOption(el, dr, 'onLeave');
  197. }
  198. });
  199. options['onEnter'] = jsPlumb.wrap(options['onEnter'], function(el, dr) {
  200. if (dr) {
  201. _checkHover(dr, true);
  202. _executeDroppableOption(el, dr, 'onEnter');
  203. }
  204. });
  205. options['onDrop'] = function(el, dr) {
  206. if (dr) {
  207. _checkHover(dr, false);
  208. _executeDroppableOption(el, dr, 'onDrop');
  209. }
  210. };
  211. drag = new Drag.Move(el, options);
  212. _add(_draggablesByScope, scope, drag);
  213. _add(_draggablesById, el.get("id"), drag);
  214. // test for disabled.
  215. if (options.disabled) drag.detach();
  216. }
  217. return drag;
  218. },
  219. initDroppable : function(el, options) {
  220. var scope = options['scope'] || jsPlumb.Defaults.Scope;
  221. _add(_droppables, scope, el);
  222. var id = jsPlumb.getId(el);
  223. _droppableOptions[id] = options;
  224. var filterFunc = function(entry) { return entry.element != el; };
  225. var draggables = _draggablesByScope[scope] ? _draggablesByScope[scope].filter(filterFunc) : [];
  226. for (var i = 0; i < draggables.length; i++) {
  227. draggables[i].droppables.push(el);
  228. }
  229. },
  230. isAlreadyDraggable : function(el) {
  231. return _draggablesById[jsPlumb.getId(el)] != null;
  232. },
  233. isDragSupported : function(el, options) {
  234. return typeof Drag != 'undefined' ;
  235. },
  236. /*
  237. * you need Drag.Move imported to make drop work.
  238. */
  239. isDropSupported : function(el, options) {
  240. return (typeof Drag != undefined && typeof Drag.Move != undefined);
  241. },
  242. /**
  243. * removes the given class from the element object.
  244. */
  245. removeClass : function(el, clazz) {
  246. el.removeClass(clazz);
  247. },
  248. removeElement : function(element, parent) {
  249. _getElementObject(element).dispose(); // ??
  250. },
  251. /**
  252. * sets the named attribute on the given element object.
  253. */
  254. setAttribute : function(el, attName, attValue) {
  255. el.set(attName, attValue);
  256. },
  257. setDraggable : function(el, draggable) {
  258. var draggables = _draggablesById[el.get("id")];
  259. if (draggables) {
  260. draggables.each(function(d) {
  261. if (draggable) d.attach(); else d.detach();
  262. });
  263. }
  264. },
  265. setOffset : function(el, o) {
  266. _getElementObject(el).setPosition({x:o.left, y:o.top});
  267. }
  268. };
  269. })();