ligerDrag.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * jQuery ligerUI 1.1.9
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2012 [ gd_star@163.com ]
  7. *
  8. */
  9. (function ($)
  10. {
  11. var l = $.ligerui;
  12. $.fn.ligerDrag = function (options)
  13. {
  14. return l.run.call(this, "ligerDrag", arguments,
  15. {
  16. idAttrName: 'ligeruidragid', hasElement: false, propertyToElemnt: 'target'
  17. }
  18. );
  19. };
  20. $.fn.ligerGetDragManager = function ()
  21. {
  22. return l.run.call(this, "ligerGetDragManager", arguments,
  23. {
  24. idAttrName: 'ligeruidragid', hasElement: false, propertyToElemnt: 'target'
  25. });
  26. };
  27. $.ligerDefaults.Drag = {
  28. onStartDrag: false,
  29. onDrag: false,
  30. onStopDrag: false,
  31. handler: null,
  32. //代理 拖动时的主体,可以是'clone'或者是函数,放回jQuery 对象
  33. proxy: true,
  34. revert: false,
  35. animate: true,
  36. onRevert: null,
  37. onEndRevert: null,
  38. //接收区域 jQuery对象或者jQuery选择字符
  39. receive: null,
  40. //进入区域
  41. onDragEnter: null,
  42. //在区域移动
  43. onDragOver: null,
  44. //离开区域
  45. onDragLeave: null,
  46. //在区域释放
  47. onDrop: null,
  48. disabled: false,
  49. proxyX: null, //代理相对鼠标指针的位置,如果不设置则对应target的left
  50. proxyY: null
  51. };
  52. l.controls.Drag = function (options)
  53. {
  54. l.controls.Drag.base.constructor.call(this, null, options);
  55. };
  56. l.controls.Drag.ligerExtend(l.core.UIComponent, {
  57. __getType: function ()
  58. {
  59. return 'Drag';
  60. },
  61. __idPrev: function ()
  62. {
  63. return 'Drag';
  64. },
  65. _render: function ()
  66. {
  67. var g = this, p = this.options;
  68. this.set(p);
  69. g.cursor = "move";
  70. g.handler.css('cursor', g.cursor);
  71. g.handler.bind('mousedown.drag', function (e)
  72. {
  73. if (p.disabled) return;
  74. if (e.button == 2) return;
  75. g._start.call(g, e);
  76. }).bind('mousemove.drag', function ()
  77. {
  78. if (p.disabled) return;
  79. g.handler.css('cursor', g.cursor);
  80. });
  81. },
  82. _rendered: function ()
  83. {
  84. this.options.target.ligeruidragid = this.id;
  85. },
  86. _start: function (e)
  87. {
  88. var g = this, p = this.options;
  89. if (g.reverting) return;
  90. if (p.disabled) return;
  91. g.current = {
  92. target: g.target,
  93. left: g.target.offset().left,
  94. top: g.target.offset().top,
  95. startX: e.pageX || e.screenX,
  96. startY: e.pageY || e.clientY
  97. };
  98. if (g.trigger('startDrag', [g.current, e]) == false) return false;
  99. g.cursor = "move";
  100. g._createProxy(p.proxy, e);
  101. //代理没有创建成功
  102. if (p.proxy && !g.proxy) return false;
  103. (g.proxy || g.handler).css('cursor', g.cursor);
  104. $(document).bind("selectstart.drag", function () { return false; });
  105. $(document).bind('mousemove.drag', function ()
  106. {
  107. g._drag.apply(g, arguments);
  108. });
  109. l.draggable.dragging = true;
  110. $(document).bind('mouseup.drag', function ()
  111. {
  112. l.draggable.dragging = false;
  113. g._stop.apply(g, arguments);
  114. });
  115. },
  116. _drag: function (e)
  117. {
  118. var g = this, p = this.options;
  119. if (!g.current) return;
  120. var pageX = e.pageX || e.screenX;
  121. var pageY = e.pageY || e.screenY;
  122. g.current.diffX = pageX - g.current.startX;
  123. g.current.diffY = pageY - g.current.startY;
  124. (g.proxy || g.handler).css('cursor', g.cursor);
  125. if (g.receive)
  126. {
  127. g.receive.each(function (i, obj)
  128. {
  129. var receive = $(obj);
  130. var xy = receive.offset();
  131. if (pageX > xy.left && pageX < xy.left + receive.width()
  132. && pageY > xy.top && pageY < xy.top + receive.height())
  133. {
  134. if (!g.receiveEntered[i])
  135. {
  136. g.receiveEntered[i] = true;
  137. g.trigger('dragEnter', [obj, g.proxy || g.target, e]);
  138. }
  139. else
  140. {
  141. g.trigger('dragOver', [obj, g.proxy || g.target, e]);
  142. }
  143. }
  144. else if (g.receiveEntered[i])
  145. {
  146. g.receiveEntered[i] = false;
  147. g.trigger('dragLeave', [obj, g.proxy || g.target, e]);
  148. }
  149. });
  150. }
  151. if (g.hasBind('drag'))
  152. {
  153. if (g.trigger('drag', [g.current, e]) != false)
  154. {
  155. g._applyDrag();
  156. }
  157. else
  158. {
  159. g._removeProxy();
  160. }
  161. }
  162. else
  163. {
  164. g._applyDrag();
  165. }
  166. },
  167. _stop: function (e)
  168. {
  169. var g = this, p = this.options;
  170. $(document).unbind('mousemove.drag');
  171. $(document).unbind('mouseup.drag');
  172. $(document).unbind("selectstart.drag");
  173. if (g.receive)
  174. {
  175. g.receive.each(function (i, obj)
  176. {
  177. if (g.receiveEntered[i])
  178. {
  179. g.trigger('drop', [obj, g.proxy || g.target, e]);
  180. }
  181. });
  182. }
  183. if (g.proxy)
  184. {
  185. if (p.revert)
  186. {
  187. if (g.hasBind('revert'))
  188. {
  189. if (g.trigger('revert', [g.current, e]) != false)
  190. g._revert(e);
  191. else
  192. g._removeProxy();
  193. }
  194. else
  195. {
  196. g._revert(e);
  197. }
  198. }
  199. else
  200. {
  201. g._applyDrag(g.target);
  202. g._removeProxy();
  203. }
  204. }
  205. g.cursor = 'move';
  206. g.trigger('stopDrag', [g.current, e]);
  207. g.current = null;
  208. g.handler.css('cursor', g.cursor);
  209. },
  210. _revert: function (e)
  211. {
  212. var g = this;
  213. g.reverting = true;
  214. g.proxy.animate({
  215. left: g.current.left,
  216. top: g.current.top
  217. }, function ()
  218. {
  219. g.reverting = false;
  220. g._removeProxy();
  221. g.trigger('endRevert', [g.current, e]);
  222. g.current = null;
  223. });
  224. },
  225. _applyDrag: function (applyResultBody)
  226. {
  227. var g = this, p = this.options;
  228. applyResultBody = applyResultBody || g.proxy || g.target;
  229. var cur = {}, changed = false;
  230. var noproxy = applyResultBody == g.target;
  231. if (g.current.diffX)
  232. {
  233. if (noproxy || p.proxyX == null)
  234. cur.left = g.current.left + g.current.diffX;
  235. else
  236. cur.left = g.current.startX + p.proxyX + g.current.diffX;
  237. changed = true;
  238. }
  239. if (g.current.diffY)
  240. {
  241. if (noproxy || p.proxyY == null)
  242. cur.top = g.current.top + g.current.diffY;
  243. else
  244. cur.top = g.current.startY + p.proxyY + g.current.diffY;
  245. changed = true;
  246. }
  247. if (applyResultBody == g.target && g.proxy && p.animate)
  248. {
  249. g.reverting = true;
  250. applyResultBody.animate(cur, function ()
  251. {
  252. g.reverting = false;
  253. });
  254. }
  255. else
  256. {
  257. applyResultBody.css(cur);
  258. }
  259. },
  260. _setReceive: function (receive)
  261. {
  262. this.receiveEntered = {};
  263. if (!receive) return;
  264. if (typeof receive == 'string')
  265. this.receive = $(receive);
  266. else
  267. this.receive = receive;
  268. },
  269. _setHandler: function (handler)
  270. {
  271. var g = this, p = this.options;
  272. if (!handler)
  273. g.handler = $(p.target);
  274. else
  275. g.handler = (typeof handler == 'string' ? $(handler, p.target) : handler);
  276. },
  277. _setTarget: function (target)
  278. {
  279. this.target = $(target);
  280. },
  281. _setCursor: function (cursor)
  282. {
  283. this.cursor = cursor;
  284. (this.proxy || this.handler).css('cursor', cursor);
  285. },
  286. _createProxy: function (proxy, e)
  287. {
  288. if (!proxy) return;
  289. var g = this, p = this.options;
  290. if (typeof proxy == 'function')
  291. {
  292. g.proxy = proxy.call(this.options.target, g, e);
  293. }
  294. else if (proxy == 'clone')
  295. {
  296. g.proxy = g.target.clone().css('position', 'absolute');
  297. g.proxy.appendTo('body');
  298. }
  299. else
  300. {
  301. g.proxy = $("<div class='l-draggable'></div>");
  302. g.proxy.width(g.target.width()).height(g.target.height())
  303. g.proxy.attr("dragid", g.id).appendTo('body');
  304. }
  305. g.proxy.css({
  306. left: p.proxyX == null ? g.current.left : g.current.startX + p.proxyX,
  307. top: p.proxyY == null ? g.current.top : g.current.startY + p.proxyY
  308. }).show();
  309. },
  310. _removeProxy: function ()
  311. {
  312. var g = this;
  313. if (g.proxy)
  314. {
  315. g.proxy.remove();
  316. g.proxy = null;
  317. }
  318. }
  319. });
  320. })(jQuery);