mootools.jsPlumb-1.2.6-RC1.js 8.6 KB

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