jquery.dragsort-0.5.1.min.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // jQuery List DragSort v0.5.1
  2. var dragsort_flag = false;
  3. (function(b) {
  4. b.fn.dragsort = function(k) {
  5. if ("destroy" == k) b(this.selector).trigger("dragsort-uninit");
  6. else {
  7. var f = b.extend({},
  8. b.fn.dragsort.defaults, k),
  9. h = [],
  10. a = null,
  11. l = null;
  12. this.each(function(k, j) {
  13. b(j).is("table") && 1 == b(j).children().size() && b(j).children().is("tbody") && (j = b(j).children().get(0));
  14. var m = {
  15. draggedItem: null,
  16. placeHolderItem: null,
  17. pos: null,
  18. offset: null,
  19. offsetLimit: null,
  20. scroll: null,
  21. container: j,
  22. init: function() {
  23. var a = 0 == b(this.container).children().size() ? "li": b(this.container).children(":first").get(0).tagName.toLowerCase();
  24. "" == f.itemSelector && (f.itemSelector = a);
  25. "" == f.dragSelector && (f.dragSelector = a);
  26. "" == f.placeHolderTemplate && (f.placeHolderTemplate = "<" + a + "> </" + a + ">");
  27. b(this.container).attr("data-listidx", k).mousedown(this.grabItem).bind("dragsort-uninit", this.uninit);
  28. this.styleDragHandlers(!0)
  29. },
  30. uninit: function() {
  31. var a = h[b(this).attr("data-listidx")];
  32. b(a.container).unbind("mousedown", a.grabItem).unbind("dragsort-uninit");
  33. a.styleDragHandlers(!1)
  34. },
  35. getItems: function() {
  36. dragsort_flag = true;
  37. setTimeout(function(){
  38. dragsort_flag = false;
  39. },1000);
  40. return b(this.container).children(f.itemSelector)
  41. },
  42. styleDragHandlers: function(a) {
  43. this.getItems().map(function() {
  44. return b(this).is(f.dragSelector) ? this: b(this).find(f.dragSelector).get()
  45. }).css("cursor", a ? "pointer": "")
  46. },
  47. grabItem: function(a) {
  48. if (! (1 != a.which || b(a.target).is(f.dragSelectorExclude) || 0 < b(a.target).closest(f.dragSelectorExclude).size() || 0 == b(a.target).closest(f.itemSelector).size())) {
  49. a.preventDefault();
  50. for (var c = a.target; ! b(c).is(f.dragSelector);) {
  51. if (c == this) return;
  52. c = c.parentNode
  53. }
  54. b(c).attr("data-cursor", b(c).css("cursor"));
  55. b(c).css("cursor", "move");
  56. var e = h[b(this).attr("data-listidx")],
  57. g = this,
  58. i = function() {
  59. e.dragStart.call(g, a);
  60. b(e.container).unbind("mousemove", i)
  61. };
  62. b(e.container).mousemove(i).mouseup(function() {
  63. b(e.container).unbind("mousemove", i);
  64. b(c).css("cursor", b(c).attr("data-cursor"))
  65. })
  66. }
  67. },
  68. dragStart: function(d) {
  69. null != a && null != a.draggedItem && a.dropItem();
  70. a = h[b(this).attr("data-listidx")];
  71. a.draggedItem = b(d.target).closest(f.itemSelector);
  72. a.draggedItem.attr("data-origpos", b(this).attr("data-listidx") + "-" + a.getItems().index(a.draggedItem));
  73. var c = parseInt(a.draggedItem.css("marginTop")),
  74. e = parseInt(a.draggedItem.css("marginLeft"));
  75. a.offset = a.draggedItem.offset();
  76. a.offset.top = d.pageY - a.offset.top + (isNaN(c) ? 0 : c) - 1;
  77. a.offset.left = d.pageX - a.offset.left + (isNaN(e) ? 0 : e) - 1;
  78. f.dragBetween || (c = 0 == b(a.container).outerHeight() ? Math.max(1, Math.round(0.5 + a.getItems().size() * a.draggedItem.outerWidth() / b(a.container).outerWidth())) * a.draggedItem.outerHeight() : b(a.container).outerHeight(), a.offsetLimit = b(a.container).offset(), a.offsetLimit.right = a.offsetLimit.left + b(a.container).outerWidth() - a.draggedItem.outerWidth(), a.offsetLimit.bottom = a.offsetLimit.top + c - a.draggedItem.outerHeight());
  79. c = a.draggedItem.height();
  80. e = a.draggedItem.width();
  81. "tr" == f.itemSelector ? (a.draggedItem.children().each(function() {
  82. b(this).width(b(this).width())
  83. }), a.placeHolderItem = a.draggedItem.clone().attr("data-placeholder", !0), a.draggedItem.after(a.placeHolderItem), a.placeHolderItem.children().each(function() {
  84. b(this).css({
  85. borderWidth: 0,
  86. width: b(this).width() + 1,
  87. height: b(this).height() + 1
  88. }).html(" ")
  89. })) : (a.draggedItem.after(f.placeHolderTemplate), a.placeHolderItem = a.draggedItem.next().css({
  90. height: c,
  91. width: e
  92. }).attr("data-placeholder", !0));
  93. if ("td" == f.itemSelector) {
  94. var g = a.draggedItem.closest("table").get(0);
  95. b("<table id='" + g.id + "' style='border-width: 0px;' class='dragSortItem " + g.className + "'><tr></tr></table>").appendTo("body").children().append(a.draggedItem)
  96. }
  97. g = a.draggedItem.attr("style");
  98. a.draggedItem.attr("data-origstyle", g ? g: "");
  99. a.draggedItem.css({
  100. position: "absolute",
  101. opacity: 0.8,
  102. "z-index": 999,
  103. height: c,
  104. width: e
  105. });
  106. a.scroll = {
  107. moveX: 0,
  108. moveY: 0,
  109. maxX: b(document).width() - b(window).width(),
  110. maxY: b(document).height() - b(window).height()
  111. };
  112. a.scroll.scrollY = window.setInterval(function() {
  113. if (f.scrollContainer != window){
  114. var c = b(f.scrollContainer).scrollTop();
  115. b(f.scrollContainer).scrollTop(c + a.scroll.moveY);
  116. } else {
  117. var c = b(f.scrollContainer).scrollTop();
  118. if ((0 < a.scroll.moveY && c < a.scroll.maxY) || (0 > a.scroll.moveY && 0 < c)){
  119. b(f.scrollContainer).scrollTop(c + a.scroll.moveY),a.draggedItem.css("top", a.draggedItem.offset().top + a.scroll.moveY + 1);
  120. }
  121. }
  122. },
  123. 10);
  124. a.scroll.scrollX = window.setInterval(function() {
  125. if (f.scrollContainer != window) b(f.scrollContainer).scrollLeft(b(f.scrollContainer).scrollLeft() + a.scroll.moveX);
  126. else {
  127. var c = b(f.scrollContainer).scrollLeft();
  128. if (0 < a.scroll.moveX && c < a.scroll.maxX || 0 > a.scroll.moveX && 0 < c) b(f.scrollContainer).scrollLeft(c + a.scroll.moveX),
  129. a.draggedItem.css("left", a.draggedItem.offset().left + a.scroll.moveX + 1)
  130. }
  131. },
  132. 10);
  133. b(h).each(function(a, b) {
  134. b.createDropTargets();
  135. b.buildPositionTable()
  136. });
  137. a.setPos(d.pageX, d.pageY);
  138. b(document).bind("mousemove", a.swapItems);
  139. b(document).bind("mouseup", a.dropItem);
  140. f.scrollContainer != window && b(window).bind("DOMMouseScroll mousewheel", a.wheel)
  141. },
  142. setPos: function(d, c) {
  143. var e = c - this.offset.top,
  144. g = d - this.offset.left;
  145. f.dragBetween || (e = Math.min(this.offsetLimit.bottom, Math.max(e, this.offsetLimit.top)), g = Math.min(this.offsetLimit.right, Math.max(g, this.offsetLimit.left)));
  146. this.draggedItem.parents().each(function() {
  147. if ("static" != b(this).css("position") && (!b.browser.mozilla || "table" != b(this).css("display"))) {
  148. var a = b(this).offset();
  149. e -= a.top;
  150. g -= a.left;
  151. return ! 1
  152. }
  153. });
  154. if (f.scrollContainer == window) c -= b(window).scrollTop(),
  155. d -= b(window).scrollLeft(),
  156. c = Math.max(0, c - b(window).height() + 5) + Math.min(0, c - 5),
  157. d = Math.max(0, d - b(window).width() + 5) + Math.min(0, d - 5);
  158. else var i = b(f.scrollContainer),
  159. h = i.offset(),
  160. c = Math.max(0, c - i.height() - h.top) + Math.min(0, c - h.top),
  161. d = Math.max(0, d - i.width() - h.left) + Math.min(0, d - h.left);
  162. a.scroll.moveX = 0 == d ? 0 : d * f.scrollSpeed / Math.abs(d);
  163. a.scroll.moveY = 0 == c ? 0 : c * f.scrollSpeed / Math.abs(c);
  164. this.draggedItem.css({
  165. top: e,
  166. left: g
  167. })
  168. },
  169. wheel: function(d) {
  170. if ((b.browser.safari || b.browser.mozilla) && a && f.scrollContainer != window) {
  171. var c = b(f.scrollContainer),
  172. e = c.offset();
  173. d.pageX > e.left && d.pageX < e.left + c.width() && d.pageY > e.top && d.pageY < e.top + c.height() && (e = d.detail ? 5 * d.detail: d.wheelDelta / -2, c.scrollTop(c.scrollTop() + e), d.preventDefault())
  174. }
  175. },
  176. buildPositionTable: function() {
  177. var d = [];
  178. this.getItems().not([a.draggedItem[0], a.placeHolderItem[0]]).each(function(a) {
  179. var e = b(this).offset();
  180. e.right = e.left + b(this).outerWidth();
  181. e.bottom = e.top + b(this).outerHeight();
  182. e.elm = this;
  183. d[a] = e
  184. });
  185. this.pos = d
  186. },
  187. dropItem: function() {
  188. if (null != a.draggedItem) {
  189. var d = a.draggedItem.attr("data-origstyle");
  190. a.draggedItem.attr("style", d);
  191. "" == d && a.draggedItem.removeAttr("style");
  192. a.draggedItem.removeAttr("data-origstyle");
  193. a.styleDragHandlers(!0);
  194. a.placeHolderItem.before(a.draggedItem);
  195. a.placeHolderItem.remove();
  196. b("[data-droptarget], .dragSortItem").remove();
  197. window.clearInterval(a.scroll.scrollY);
  198. window.clearInterval(a.scroll.scrollX);
  199. a.draggedItem.attr("data-origpos") != b(h).index(a) + "-" + a.getItems().index(a.draggedItem) && f.dragEnd.apply(a.draggedItem);
  200. a.draggedItem.removeAttr("data-origpos");
  201. a.draggedItem = null;
  202. b(document).unbind("mousemove", a.swapItems);
  203. b(document).unbind("mouseup", a.dropItem);
  204. f.scrollContainer != window && b(window).unbind("DOMMouseScroll mousewheel", a.wheel);
  205. return ! 1
  206. }
  207. },
  208. swapItems: function(d) {
  209. if (null == a.draggedItem) return ! 1;
  210. a.setPos(d.pageX, d.pageY);
  211. for (var c = a.findPos(d.pageX, d.pageY), e = a, g = 0; - 1 == c && f.dragBetween && g < h.length; g++) c = h[g].findPos(d.pageX, d.pageY),
  212. e = h[g];
  213. if ( - 1 == c) return ! 1;
  214. var i = function() {
  215. return b(e.container).children().not(e.draggedItem)
  216. },
  217. d = i().not(f.itemSelector).each(function() {
  218. this.idx = i().index(this)
  219. });
  220. null == l || l.top > a.draggedItem.offset().top || l.left > a.draggedItem.offset().left ? b(e.pos[c].elm).before(a.placeHolderItem) : b(e.pos[c].elm).after(a.placeHolderItem);
  221. d.each(function() {
  222. var a = i().eq(this.idx).get(0);
  223. this != a && i().index(this) < this.idx ? b(this).insertAfter(a) : this != a && b(this).insertBefore(a)
  224. });
  225. b(h).each(function(a, b) {
  226. b.createDropTargets();
  227. b.buildPositionTable()
  228. });
  229. l = a.draggedItem.offset();
  230. return ! 1
  231. },
  232. findPos: function(a, b) {
  233. for (var e = 0; e < this.pos.length; e++) if (this.pos[e].left < a && this.pos[e].right > a && this.pos[e].top < b && this.pos[e].bottom > b) return e;
  234. return - 1
  235. },
  236. createDropTargets: function() {
  237. f.dragBetween && b(h).each(function() {
  238. var d = b(this.container).find("[data-placeholder]"),
  239. c = b(this.container).find("[data-droptarget]");
  240. 0 < d.size() && 0 < c.size() ? c.remove() : 0 == d.size() && 0 == c.size() && ("td" == f.itemSelector ? b(f.placeHolderTemplate).attr("data-droptarget", !0).appendTo(this.container) : b(this.container).append(a.placeHolderItem.removeAttr("data-placeholder").clone().attr("data-droptarget", !0)), a.placeHolderItem.attr("data-placeholder", !0))
  241. })
  242. }
  243. };
  244. m.init();
  245. h.push(m)
  246. });
  247. return this
  248. }
  249. };
  250. b.fn.dragsort.defaults = {
  251. itemSelector: "",
  252. dragSelector: "",
  253. dragSelectorExclude: "input, textarea",
  254. dragEnd: function() {},
  255. dragBetween: !1,
  256. placeHolderTemplate: "",
  257. scrollContainer: window,
  258. scrollSpeed: 5
  259. }
  260. })(jQuery);