yui.jsPlumb-1.3.2-RC1.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * jsPlumb
  3. *
  4. * Title:jsPlumb 1.3.2
  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 YUI3 adapter.
  10. *
  11. * Copyright (c) 2010 - 2011 Simon Porritt (http://jsplumb.org)
  12. *
  13. * http://jsplumb.org
  14. * http://code.google.com/p/jsplumb
  15. *
  16. * Triple licensed under the MIT, GPL2 and Beer licenses.
  17. */
  18. /**
  19. * addClass adds a class to the given element
  20. * animate calls the underlying library's animate functionality
  21. * appendElement appends a child element to a parent element.
  22. * bind binds some event to an element
  23. * dragEvents a dictionary of event names
  24. * extend extend some js object with another. probably not overly necessary; jsPlumb could just do this internally.
  25. * getAttribute gets some attribute from an element
  26. * getDragObject gets the object that is being dragged, by extracting it from the arguments passed to a drag callback
  27. * getDragScope gets the drag scope for a given element.
  28. * getElementObject turns an id or dom element into an element object of the underlying library's type.
  29. * getOffset gets an element's offset
  30. * getScrollLeft gets an element's scroll left. TODO: is this actually used? will it be?
  31. * getScrollTop gets an element's scroll top. TODO: is this actually used? will it be?
  32. * getSize gets an element's size.
  33. * getUIPosition gets the position of some element that is currently being dragged, by extracting it from the arguments passed to a drag callback.
  34. * initDraggable initializes an element to be draggable
  35. * initDroppable initializes an element to be droppable
  36. * isDragSupported returns whether or not drag is supported for some element.
  37. * isDropSupported returns whether or not drop is supported for some element.
  38. * removeClass removes a class from a given element.
  39. * removeElement removes some element completely from the DOM.
  40. * setAttribute sets an attribute on some element.
  41. * setDraggable sets whether or not some element should be draggable.
  42. * setDragScope sets the drag scope for a given element.
  43. * setOffset sets the offset of some element.
  44. */
  45. (function() {
  46. if (!Array.prototype.indexOf) {
  47. Array.prototype.indexOf = function( v, b, s ) {
  48. for( var i = +b || 0, l = this.length; i < l; i++ ) {
  49. if( this[i]===v || s && this[i]==v ) { return i; }
  50. }
  51. return -1;
  52. };
  53. }
  54. var Y;
  55. YUI().use('node', 'dd', 'anim', function(_Y) {
  56. Y = _Y;
  57. Y.on("domready", function() { jsPlumb.init(); });
  58. });
  59. /**
  60. * adds the given value to the given list, with the given scope. creates the scoped list
  61. * if necessary.
  62. * used by initDraggable and initDroppable.
  63. */
  64. var _add = function(list, scope, value) {
  65. var l = list[scope];
  66. if (!l) {
  67. l = [];
  68. list[scope] = l;
  69. }
  70. l.push(value);
  71. },
  72. ddEvents = [ "drag:mouseDown", "drag:afterMouseDown", "drag:mouseup",
  73. "drag:align", "drag:removeHandle", "drag:addHandle", "drag:removeInvalid", "drag:addInvalid",
  74. "drag:start", "drag:end", "drag:drag", "drag:over", "drag:enter",
  75. "drag:exit", "drag:drophit", "drag:dropmiss", "drop:over", "drop:enter", "drop:exit", "drop:hit"
  76. ],
  77. animEvents = [ "tween" ],
  78. /**
  79. * helper function to curry callbacks for some element.
  80. */
  81. _wrapper = function(fn) {
  82. return function() {
  83. try {
  84. fn.apply(this, arguments);
  85. }
  86. catch (e) {
  87. console.log("wrap fail", e);
  88. }
  89. };
  90. },
  91. /**
  92. * extracts options from the given options object, leaving out event handlers.
  93. */
  94. _getDDOptions = function(options) {
  95. var o = {};
  96. for (var i in options) if (ddEvents.indexOf(i) == -1) o[i] = options[i];
  97. return o;
  98. },
  99. /**
  100. * attaches all event handlers found in options to the given dragdrop object, and registering
  101. * the given el as the element of interest.
  102. */
  103. _attachListeners = function(dd, options, eventList) {
  104. for (var ev in options) {
  105. if (eventList.indexOf(ev) != -1) {
  106. var w = _wrapper(options[ev]);
  107. dd.on(ev, w);
  108. }
  109. }
  110. },
  111. _droppables = {},
  112. _droppableOptions = {},
  113. _draggablesByScope = {},
  114. _draggablesById = {},
  115. _droppableScopesById = {},
  116. _checkHover = function(el, entering) {
  117. if (el) {
  118. var id = el.get("id");
  119. if (id) {
  120. var options = _droppableOptions[id];
  121. if (options) {
  122. if (options['hoverClass']) {
  123. if (entering) el.addClass(options['hoverClass']);
  124. else el.removeClass(options['hoverClass']);
  125. }
  126. }
  127. }
  128. }
  129. },
  130. _lastDragObject = null,
  131. _extend = function(o1, o2) {
  132. for (var i in o2)
  133. o1[i] = o2[i];
  134. return o1;
  135. },
  136. _getAttribute = function(el, attributeId) {
  137. return el.getAttribute(attributeId);
  138. },
  139. _getElementObject = function(el) {
  140. return typeof el == 'string' ? Y.one('#' + el) : Y.one(el);
  141. };
  142. jsPlumb.CurrentLibrary = {
  143. addClass : function(el, clazz) {
  144. el.addClass(clazz);
  145. },
  146. /**
  147. * animates the given element.
  148. */
  149. animate : function(el, properties, options) {
  150. var o = _extend({node:el, to:properties}, options);
  151. var id = _getAttribute(el, "id");
  152. o["tween"] = jsPlumb.wrap(properties["tween"], function() {
  153. jsPlumb.repaint(id);
  154. });
  155. var a = new Y.Anim(o);
  156. _attachListeners(a, o, animEvents);
  157. a.run();
  158. },
  159. appendElement : function(child, parent) {
  160. _getElementObject (parent).append(child);
  161. },
  162. /**
  163. * event binding wrapper.
  164. */
  165. bind : function(el, event, callback) {
  166. _getElementObject(el).on(event, callback);
  167. },
  168. dragEvents : {
  169. "start":"drag:start", "stop":"drag:end", "drag":"drag:drag", "step":"step",
  170. "over":"drop:enter", "out":"drop:exit", "drop":"drop:hit"
  171. },
  172. extend : _extend,
  173. getAttribute : _getAttribute,
  174. getClientXY : function(eventObject) {
  175. return [eventObject.clientX, eventObject.clientY];
  176. },
  177. /**
  178. * takes the args passed to an event function and returns you an object representing that which is being dragged.
  179. */
  180. getDragObject : function(eventArgs) {
  181. // this is a workaround for the unfortunate fact that in YUI3, the 'drop:exit' event does
  182. // not contain a reference to the drag that just exited. single-threaded js to the
  183. // rescue: we'll just keep it for ourselves.
  184. if (eventArgs[0].drag) _lastDragObject = eventArgs[0].drag.el;
  185. return _lastDragObject;
  186. },
  187. getDragScope : function(el) {
  188. var id = jsPlumb.getId(el);
  189. var dd = _draggablesById[id];
  190. return dd.scope;
  191. },
  192. getDropScope : function(el) {
  193. var id = jsPlumb.getId(el);
  194. return _droppableScopesById[id];
  195. },
  196. getElementObject : _getElementObject,
  197. getOffset : function(el) {
  198. var o = Y.DOM.getXY(el._node);
  199. return {left:o[0], top:o[1]};
  200. },
  201. getPageXY : function(eventObject) {
  202. return [eventObject.pageX, eventObject.pageY];
  203. },
  204. getParent : function(el) {
  205. return jsPlumb.CurrentLibrary.getElementObject(el).get("parentNode");
  206. },
  207. getScrollLeft : function(el) {
  208. return 0;
  209. },
  210. getScrollTop : function(el) {
  211. return 0;
  212. },
  213. getSize : function(el) {
  214. return [ el._node.offsetWidth, el._node.offsetHeight ];
  215. },
  216. getUIPosition : function(args) {
  217. var n = args[0].currentTarget.el._node,
  218. o = Y.DOM.getXY(n);
  219. return {left:o[0], top:o[1]};
  220. },
  221. hasClass : function(el, clazz) {
  222. return el.hasClass(clazz);
  223. },
  224. initDraggable : function(el, options) {
  225. var _opts = _getDDOptions(options);
  226. var id = jsPlumb.getId(el);
  227. _opts.node = "#" + id;
  228. var dd = new Y.DD.Drag(_opts);
  229. dd.el = el;
  230. var scope = options['scope'] || jsPlumb.Defaults.Scope;
  231. dd.scope = scope;
  232. _draggablesById[id] = dd;
  233. _add(_draggablesByScope, scope, dd);
  234. _attachListeners(dd, options, ddEvents);
  235. },
  236. initDroppable : function(el, options) {
  237. var _opts = _getDDOptions(options);
  238. var id = jsPlumb.getId(el);
  239. _opts.node = "#" + id;
  240. var dd = new Y.DD.Drop(_opts);
  241. _droppableOptions[id] = options;
  242. options = _extend({}, options);
  243. var scope = options['scope'] || jsPlumb.Defaults.Scope;
  244. _droppableScopesById[id] = scope;
  245. options["drop:enter"] = jsPlumb.wrap(options["drop:enter"], function(e) {
  246. if (e.drag.scope !== scope) return true;
  247. _checkHover(el, true);
  248. }, true);
  249. options["drop:exit"] = jsPlumb.wrap(options["drop:exit"], function(e) {
  250. _checkHover(el, false);
  251. });
  252. options["drop:hit"] = jsPlumb.wrap(options["drop:hit"], function(e) {
  253. if (e.drag.scope !== scope) return true;
  254. _checkHover(el, false);
  255. }, true);
  256. _attachListeners(dd, options, ddEvents);
  257. },
  258. isAlreadyDraggable : function(el) {
  259. el = _getElementObject(el);
  260. return el.hasClass("yui3-dd-draggable");
  261. },
  262. isDragSupported : function(el) { return true; },
  263. isDropSupported : function(el) { return true; },
  264. removeClass : function(el, clazz) { el.removeClass(clazz); },
  265. removeElement : function(el) { _getElementObject(el).remove(); },
  266. setAttribute : function(el, attributeName, attributeValue) {
  267. el.setAttribute(attributeName, attributeValue);
  268. },
  269. /**
  270. * sets the draggable state for the given element
  271. */
  272. setDraggable : function(el, draggable) {
  273. var id = jsPlumb.getId(el);
  274. var dd = _draggablesById[id];
  275. if (dd) dd.set("lock", !draggable);
  276. },
  277. setDragScope : function(el, scope) {
  278. var id = jsPlumb.getId(el);
  279. var dd = _draggablesById[id];
  280. if (dd) dd.scope = scope;
  281. },
  282. setOffset : function(el, o) {
  283. el = _getElementObject(el);
  284. el.set("top", o.top);
  285. el.set("left", o.left);
  286. }
  287. };
  288. })();