yui.jsPlumb-1.3.4-RC1.js 10 KB

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