├── LICENSE.md ├── README.md ├── jquery.dragsort.js └── jquery.dragsort.min.js /LICENSE.md: -------------------------------------------------------------------------------- 1 | Both the original dragsort and this fork are released under Ms-PL. 2 | 3 | http://dragsort.codeplex.com/license 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmaish/DragSort/0199e6c9a61e41fedb8ddf62d0f022dea21404b0/README.md -------------------------------------------------------------------------------- /jquery.dragsort.js: -------------------------------------------------------------------------------- 1 | // jQuery List DragSort v0.5.1-cmaish 2 | // Website: http://dragsort.codeplex.com/ 3 | // License: http://dragsort.codeplex.com/license 4 | // This version from: https://github.com/cmaish/dragsort 5 | 6 | (function($) { 7 | 8 | $.fn.dragsort = function(options) { 9 | if (options == "destroy") { 10 | $(this.selector).trigger("dragsort-uninit"); 11 | return; 12 | } 13 | 14 | var opts = $.extend({}, $.fn.dragsort.defaults, options); 15 | var lists = []; 16 | var list = null, lastPos = null; 17 | 18 | this.each(function(i, cont) { 19 | 20 | //if list container is table, the browser automatically wraps rows in tbody if not specified so change list container to tbody so that children returns rows as user expected 21 | if ($(cont).is("table") && $(cont).children().size() == 1 && $(cont).children().is("tbody")) 22 | cont = $(cont).children().get(0); 23 | 24 | var newList = { 25 | draggedItem: null, 26 | placeHolderItem: null, 27 | pos: null, 28 | offset: null, 29 | offsetLimit: null, 30 | scroll: null, 31 | container: cont, 32 | 33 | init: function() { 34 | //set options to default values if not set 35 | opts.tagName = $(this.container).children().size() == 0 ? "li" : $(this.container).children().get(0).tagName.toLowerCase(); 36 | if (opts.itemSelector == "") 37 | opts.itemSelector = opts.tagName; 38 | if (opts.dragSelector == "") 39 | opts.dragSelector = opts.tagName; 40 | if (opts.placeHolderTemplate == "") 41 | opts.placeHolderTemplate = "<" + opts.tagName + "> "; 42 | 43 | //listidx allows reference back to correct list variable instance 44 | $(this.container).attr("data-listidx", i).mousedown(this.grabItem).bind("dragsort-uninit", this.uninit); 45 | this.styleDragHandlers(true); 46 | }, 47 | 48 | uninit: function() { 49 | var list = lists[$(this).attr("data-listidx")]; 50 | $(list.container).unbind("mousedown", list.grabItem).unbind("dragsort-uninit"); 51 | list.styleDragHandlers(false); 52 | }, 53 | 54 | getItems: function() { 55 | return $(this.container).children(opts.itemSelector); 56 | }, 57 | 58 | styleDragHandlers: function(cursor) { 59 | this.getItems().map(function() { return $(this).is(opts.dragSelector) ? this : $(this).find(opts.dragSelector).get(); }).css("cursor", cursor ? "pointer" : ""); 60 | }, 61 | 62 | grabItem: function(e) { 63 | var list = lists[$(this).attr("data-listidx")]; 64 | var item = $(e.target).closest("[data-listidx] > " + opts.tagName).get(0); 65 | var insideMoveableItem = list.getItems().filter(function() { return this == item; }).size() > 0; 66 | 67 | //if not left click or if clicked on excluded element (e.g. text box) or not a moveable list item return 68 | if (e.which != 1 || $(e.target).is(opts.dragSelectorExclude) || $(e.target).closest(opts.dragSelectorExclude).size() > 0 || !insideMoveableItem) 69 | return; 70 | 71 | //prevents selection, stops issue on Fx where dragging hyperlink doesn't work and on IE where it triggers mousemove even though mouse hasn't moved, 72 | //does also stop being able to click text boxes hence dragging on text boxes by default is disabled in dragSelectorExclude 73 | e.preventDefault(); 74 | 75 | //change cursor to move while dragging 76 | var dragHandle = e.target; 77 | while (!$(dragHandle).is(opts.dragSelector)) { 78 | if (dragHandle == this) return; 79 | dragHandle = dragHandle.parentNode; 80 | } 81 | $(dragHandle).attr("data-cursor", $(dragHandle).css("cursor")); 82 | $(dragHandle).css("cursor", "move"); 83 | 84 | //on mousedown wait for movement of mouse before triggering dragsort script (dragStart) to allow clicking of hyperlinks to work 85 | var listElem = this; 86 | var trigger = function() { 87 | list.dragStart.call(listElem, e); 88 | $(list.container).unbind("mousemove", trigger); 89 | }; 90 | $(list.container).mousemove(trigger).mouseup(function() { $(list.container).unbind("mousemove", trigger); $(dragHandle).css("cursor", $(dragHandle).attr("data-cursor")); }); 91 | }, 92 | 93 | dragStart: function(e) { 94 | if (list != null && list.draggedItem != null) 95 | list.dropItem(); 96 | 97 | list = lists[$(this).attr("data-listidx")]; 98 | list.draggedItem = $(e.target).closest("[data-listidx] > " + opts.tagName) 99 | 100 | //raise event 101 | if (opts.dragStart.apply(list.draggedItem) === false) { 102 | list.draggedItem = null; 103 | return; 104 | } 105 | 106 | //record current position so on dragend we know if the dragged item changed position or not, not using getItems to allow dragsort to restore dragged item to original location in relation to fixed items 107 | list.draggedItem.attr("data-origpos", $(this).attr("data-listidx") + "-" + $(list.container).children().index(list.draggedItem)); 108 | 109 | //calculate mouse offset relative to draggedItem 110 | var mt = parseInt(list.draggedItem.css("marginTop")); 111 | var ml = parseInt(list.draggedItem.css("marginLeft")); 112 | list.offset = list.draggedItem.offset(); 113 | list.offset.top = e.pageY - list.offset.top + (isNaN(mt) ? 0 : mt) - 1; 114 | list.offset.left = e.pageX - list.offset.left + (isNaN(ml) ? 0 : ml) - 1; 115 | 116 | //calculate box the dragged item can't be dragged outside of 117 | if (!opts.dragBetween) { 118 | var containerHeight = $(list.container).outerHeight() == 0 ? Math.max(1, Math.round(0.5 + list.getItems().size() * list.draggedItem.outerWidth() / $(list.container).outerWidth())) * list.draggedItem.outerHeight() : $(list.container).outerHeight(); 119 | list.offsetLimit = $(list.container).offset(); 120 | list.offsetLimit.right = list.offsetLimit.left + $(list.container).outerWidth() - list.draggedItem.outerWidth(); 121 | list.offsetLimit.bottom = list.offsetLimit.top + containerHeight - list.draggedItem.outerHeight(); 122 | } 123 | 124 | //create placeholder item 125 | var h = list.draggedItem.height(); 126 | var w = list.draggedItem.width(); 127 | if (opts.tagName == "tr") { 128 | list.draggedItem.children().each(function() { $(this).width($(this).width()); }); 129 | list.placeHolderItem = list.draggedItem.clone().attr("data-placeholder", true); 130 | list.draggedItem.after(list.placeHolderItem); 131 | list.placeHolderItem.children().each(function() { $(this).css({ borderWidth:0, width: $(this).width() + 1, height: $(this).height() + 1 }).html(" "); }); 132 | } else { 133 | list.draggedItem.after(opts.placeHolderTemplate); 134 | list.placeHolderItem = list.draggedItem.next().css({ height: h, width: w }).attr("data-placeholder", true); 135 | } 136 | 137 | if (opts.tagName == "td") { 138 | var listTable = list.draggedItem.closest("table").get(0); 139 | $("
").appendTo("body").children().append(list.draggedItem); 140 | } 141 | 142 | //style draggedItem while dragging 143 | var orig = list.draggedItem.attr("style"); 144 | list.draggedItem.attr("data-origstyle", orig ? orig : ""); 145 | list.draggedItem.css({ position: "absolute", opacity: 0.8, "z-index": 999, height: h, width: w }); 146 | 147 | //auto-scroll setup 148 | list.scroll = { moveX: 0, moveY: 0, maxX: $(document).width() - $(window).width(), maxY: $(document).height() - $(window).height() }; 149 | list.scroll.scrollY = window.setInterval(function() { 150 | if (opts.scrollContainer != window) { 151 | $(opts.scrollContainer).scrollTop($(opts.scrollContainer).scrollTop() + list.scroll.moveY); 152 | return; 153 | } 154 | var t = $(opts.scrollContainer).scrollTop(); 155 | if (list.scroll.moveY > 0 && t < list.scroll.maxY || list.scroll.moveY < 0 && t > 0) { 156 | $(opts.scrollContainer).scrollTop(t + list.scroll.moveY); 157 | list.draggedItem.css("top", list.draggedItem.offset().top + list.scroll.moveY + 1); 158 | } 159 | }, 10); 160 | list.scroll.scrollX = window.setInterval(function() { 161 | if (opts.scrollContainer != window) { 162 | $(opts.scrollContainer).scrollLeft($(opts.scrollContainer).scrollLeft() + list.scroll.moveX); 163 | return; 164 | } 165 | var l = $(opts.scrollContainer).scrollLeft(); 166 | if (list.scroll.moveX > 0 && l < list.scroll.maxX || list.scroll.moveX < 0 && l > 0) { 167 | $(opts.scrollContainer).scrollLeft(l + list.scroll.moveX); 168 | list.draggedItem.css("left", list.draggedItem.offset().left + list.scroll.moveX + 1); 169 | } 170 | }, 10); 171 | 172 | //misc 173 | $(lists).each(function(i, l) { l.createDropTargets(); l.buildPositionTable(); }); 174 | list.setPos(e.pageX, e.pageY); 175 | $(document).bind("mousemove", list.swapItems); 176 | $(document).bind("mouseup", list.dropItem); 177 | if (opts.scrollContainer != window) 178 | $(window).bind("DOMMouseScroll mousewheel", list.wheel); 179 | }, 180 | 181 | //set position of draggedItem 182 | setPos: function(x, y) { 183 | //remove mouse offset so mouse cursor remains in same place on draggedItem instead of top left corner 184 | var top = y - this.offset.top; 185 | var left = x - this.offset.left; 186 | 187 | //limit top, left to within box draggedItem can't be dragged outside of 188 | if (!opts.dragBetween) { 189 | top = Math.min(this.offsetLimit.bottom, Math.max(top, this.offsetLimit.top)); 190 | left = Math.min(this.offsetLimit.right, Math.max(left, this.offsetLimit.left)); 191 | } 192 | 193 | //adjust top & left calculations to parent offset 194 | var parent = this.draggedItem.offsetParent().not("body").offset(); //offsetParent returns body even when it's static, if not static offset is only factoring margin 195 | if (parent != null) { 196 | top -= parent.top; 197 | left -= parent.left; 198 | } 199 | 200 | //set x or y auto-scroll amount 201 | if (opts.scrollContainer == window) { 202 | y -= $(window).scrollTop(); 203 | x -= $(window).scrollLeft(); 204 | y = Math.max(0, y - $(window).height() + 5) + Math.min(0, y - 5); 205 | x = Math.max(0, x - $(window).width() + 5) + Math.min(0, x - 5); 206 | } else { 207 | var cont = $(opts.scrollContainer); 208 | var offset = cont.offset(); 209 | y = Math.max(0, y - cont.height() - offset.top) + Math.min(0, y - offset.top); 210 | x = Math.max(0, x - cont.width() - offset.left) + Math.min(0, x - offset.left); 211 | } 212 | 213 | list.scroll.moveX = x == 0 ? 0 : x * opts.scrollSpeed / Math.abs(x); 214 | list.scroll.moveY = y == 0 ? 0 : y * opts.scrollSpeed / Math.abs(y); 215 | 216 | //move draggedItem to new mouse cursor location 217 | this.draggedItem.css({ top: top, left: left }); 218 | }, 219 | 220 | //if scroll container is a div allow mouse wheel to scroll div instead of window when mouse is hovering over 221 | wheel: function(e) { 222 | if (($.browser.safari || $.browser.mozilla) && list && opts.scrollContainer != window) { 223 | var cont = $(opts.scrollContainer); 224 | var offset = cont.offset(); 225 | if (e.pageX > offset.left && e.pageX < offset.left + cont.width() && e.pageY > offset.top && e.pageY < offset.top + cont.height()) { 226 | var delta = e.detail ? e.detail * 5 : e.wheelDelta / -2; 227 | cont.scrollTop(cont.scrollTop() + delta); 228 | e.preventDefault(); 229 | } 230 | } 231 | }, 232 | 233 | //build a table recording all the positions of the moveable list items 234 | buildPositionTable: function() { 235 | var pos = []; 236 | this.getItems().not([list.draggedItem[0], list.placeHolderItem[0]]).each(function(i) { 237 | var loc = $(this).offset(); 238 | loc.right = loc.left + $(this).outerWidth(); 239 | loc.bottom = loc.top + $(this).outerHeight(); 240 | loc.elm = this; 241 | pos[i] = loc; 242 | }); 243 | this.pos = pos; 244 | }, 245 | 246 | dropItem: function() { 247 | if (list.draggedItem == null) 248 | return; 249 | 250 | //list.draggedItem.attr("style", "") doesn't work on IE8 and jQuery 1.5 or lower 251 | //list.draggedItem.removeAttr("style") doesn't work on chrome and jQuery 1.6 (works jQuery 1.5 or lower) 252 | var orig = list.draggedItem.attr("data-origstyle"); 253 | list.draggedItem.attr("style", orig); 254 | if (orig == "") 255 | list.draggedItem.removeAttr("style"); 256 | list.draggedItem.removeAttr("data-origstyle"); 257 | 258 | list.styleDragHandlers(true); 259 | 260 | list.placeHolderItem.before(list.draggedItem); 261 | list.placeHolderItem.remove(); 262 | 263 | $("[data-droptarget], .dragSortItem").remove(); 264 | 265 | window.clearInterval(list.scroll.scrollY); 266 | window.clearInterval(list.scroll.scrollX); 267 | 268 | //if position changed call dragEnd 269 | if (list.draggedItem.attr("data-origpos") != $(lists).index(list) + "-" + $(list.container).children().index(list.draggedItem)) 270 | if (opts.dragEnd.apply(list.draggedItem) == false) { //if dragEnd returns false revert order 271 | var pos = list.draggedItem.attr("data-origpos").split('-'); 272 | var nextItem = $(lists[pos[0]].container).children().not(list.draggedItem).eq(pos[1]); 273 | if (nextItem.size() > 0) 274 | nextItem.before(list.draggedItem); 275 | else if (pos[1] == 0) //was the only item in list 276 | $(lists[pos[0]].container).prepend(list.draggedItem); 277 | else //was the last item in list 278 | $(lists[pos[0]].container).append(list.draggedItem); 279 | } 280 | list.draggedItem.removeAttr("data-origpos"); 281 | 282 | list.draggedItem = null; 283 | $(document).unbind("mousemove", list.swapItems); 284 | $(document).unbind("mouseup", list.dropItem); 285 | if (opts.scrollContainer != window) 286 | $(window).unbind("DOMMouseScroll mousewheel", list.wheel); 287 | return false; 288 | }, 289 | 290 | //swap the draggedItem (represented visually by placeholder) with the list item the it has been dragged on top of 291 | swapItems: function(e) { 292 | if (list.draggedItem == null) 293 | return false; 294 | 295 | //move draggedItem to mouse location 296 | list.setPos(e.pageX, e.pageY); 297 | 298 | //retrieve list and item position mouse cursor is over 299 | var ei = list.findPos(e.pageX, e.pageY); 300 | var nlist = list; 301 | for (var i = 0; ei == -1 && opts.dragBetween && i < lists.length; i++) { 302 | ei = lists[i].findPos(e.pageX, e.pageY); 303 | nlist = lists[i]; 304 | } 305 | 306 | //if not over another moveable list item return 307 | if (ei == -1) 308 | return false; 309 | 310 | //save fixed items locations 311 | var children = function() { return $(nlist.container).children().not(nlist.draggedItem); }; 312 | var fixed = children().not(opts.itemSelector).each(function(i) { this.idx = children().index(this); }); 313 | 314 | //if moving draggedItem up or left place placeHolder before list item the dragged item is hovering over otherwise place it after 315 | if (lastPos == null || lastPos.top > list.draggedItem.offset().top || lastPos.left > list.draggedItem.offset().left) 316 | $(nlist.pos[ei].elm).before(list.placeHolderItem); 317 | else 318 | $(nlist.pos[ei].elm).after(list.placeHolderItem); 319 | 320 | //restore fixed items location 321 | fixed.each(function() { 322 | var elm = children().eq(this.idx).get(0); 323 | if (this != elm && children().index(this) < this.idx) 324 | $(this).insertAfter(elm); 325 | else if (this != elm) 326 | $(this).insertBefore(elm); 327 | }); 328 | 329 | //misc 330 | $(lists).each(function(i, l) { l.createDropTargets(); l.buildPositionTable(); }); 331 | lastPos = list.draggedItem.offset(); 332 | return false; 333 | }, 334 | 335 | //returns the index of the list item the mouse is over 336 | findPos: function(x, y) { 337 | for (var i = 0; i < this.pos.length; i++) { 338 | if (this.pos[i].left < x && this.pos[i].right > x && this.pos[i].top < y && this.pos[i].bottom > y) 339 | return i; 340 | } 341 | return -1; 342 | }, 343 | 344 | //create drop targets which are placeholders at the end of other lists to allow dragging straight to the last position 345 | createDropTargets: function() { 346 | if (!opts.dragBetween) 347 | return; 348 | 349 | $(lists).each(function() { 350 | var ph = $(this.container).find("[data-placeholder]"); 351 | var dt = $(this.container).find("[data-droptarget]"); 352 | if (ph.size() > 0 && dt.size() > 0) 353 | dt.remove(); 354 | else if (ph.size() == 0 && dt.size() == 0) { 355 | if (opts.tagName == "td") 356 | $(opts.placeHolderTemplate).attr("data-droptarget", true).appendTo(this.container); 357 | else 358 | //list.placeHolderItem.clone().removeAttr("data-placeholder") crashes in IE7 and jquery 1.5.1 (doesn't in jquery 1.4.2 or IE8) 359 | $(this.container).append(list.placeHolderItem.removeAttr("data-placeholder").clone().attr("data-droptarget", true)); 360 | 361 | list.placeHolderItem.attr("data-placeholder", true); 362 | } 363 | }); 364 | } 365 | }; 366 | 367 | newList.init(); 368 | lists.push(newList); 369 | }); 370 | 371 | return this; 372 | }; 373 | 374 | $.fn.dragsort.defaults = { 375 | itemSelector: "", 376 | dragSelector: "", 377 | dragSelectorExclude: "input, textarea", 378 | dragStart: function () { }, 379 | dragEnd: function() { }, 380 | dragBetween: false, 381 | placeHolderTemplate: "", 382 | scrollContainer: window, 383 | scrollSpeed: 5 384 | }; 385 | 386 | })(jQuery); -------------------------------------------------------------------------------- /jquery.dragsort.min.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmaish/DragSort/0199e6c9a61e41fedb8ddf62d0f022dea21404b0/jquery.dragsort.min.js --------------------------------------------------------------------------------