JQuery Nested Sortable
Great little jquery addon to handle parent child relationships and sortable lists.
http://bitsinashortbit.wordpress.com/2007/07/22/nested-sortable-jquery-plugin/
This was written for jquery 1.1.x so if you have any issues with jquery 1.2.x or higher in IE which only allows dragging of objects once. Either downgrade jquery or download idrag_source.js from this post
I have customised it further to return more data formats when calling the serialize. My custom version can be found here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | /** * * Nested Sortable Plugin for jQuery/Interface. * * Version 1.0.1 * *Change Log: * 1.0 * Initial Release * 1.0.1 * Added noNestingClass option to prevent nesting in some elements. * * * Copyright (c) 2007 Bernardo de Padua dos Santos * Dual licensed under the MIT (MIT-LICENSE.txt) * and GPL (GPL-LICENSE.txt) licenses. * * http://code.google.com/p/nestedsortables/ * */ jQuery.iNestedSortable = { /* * Called when an item is being dragged. Puts the sort helper in the proper place. * The "e" argument passed in is the Sortable element. */ checkHover : function(e,o) { if(e.isNestedSortable){ //A brand new NestedSortable hovering check jQuery.iNestedSortable.scroll(e); return jQuery.iNestedSortable.newCheckHover(e); } else { //The legacy hovering check performed by plain Sortables //I don't think "o" is even used, but it is in the function declaration return jQuery.iNestedSortable.oldCheckHover(e,o); } }, oldCheckHover : jQuery.iSort.checkhover, newCheckHover : function (e) { if (!jQuery.iDrag.dragged) { return; } if ( !(e.dropCfg.el.size() > 0) ) { return; } //we need to recalculate the position of the sortables, //or there will be some mismatches if(!e.nestedSortCfg.remeasured) { jQuery.iSort.measure(e); e.nestedSortCfg.remeasured = true; } //finds item that is on top of the one being dragged, //and in a compatible nesting level var precItem = jQuery.iNestedSortable.findPrecedingItem(e); var shouldNest = jQuery.iNestedSortable.shouldNestItem(e, precItem); var touchingFirst = (!precItem) ? jQuery.iNestedSortable.isTouchingFirstItem(e) : false; var quit = false; //avoids doing things more than once if(precItem) { if(e.nestedSortCfg.lastPrecedingItem === precItem && e.nestedSortCfg.lastShouldNest === shouldNest) { quit = true; } } else if(e.nestedSortCfg.lastPrecedingItem === precItem && e.nestedSortCfg.lastTouchingFirst === touchingFirst) { quit = true; } e.nestedSortCfg.lastPrecedingItem = precItem; e.nestedSortCfg.lastShouldNest = shouldNest; e.nestedSortCfg.lastTouchingFirst = touchingFirst; if(quit) {return;} if (precItem !== null) { //there is an element on top of this one //on the same nesting, or smaller if (shouldNest) { jQuery.iNestedSortable.nestItem(e, precItem); } else { jQuery.iNestedSortable.appendItem(e, precItem); } } else if (touchingFirst) { //no element on top, but touches the first item on the list jQuery.iNestedSortable.insertOnTop(e); } }, /* * Auto scrolls the page when we are dragging an element. */ scroll: function(e) { if(!e.nestedSortCfg.autoScroll) { return false; } var sensitivity = e.nestedSortCfg.scrollSensitivity; var speed = e.nestedSortCfg.scrollSpeed; var pointer = jQuery.iDrag.dragged.dragCfg.currentPointer; var docDim = jQuery.iUtil.getScroll(); if((pointer.y - docDim.ih) - docDim.t > -sensitivity) { window.scrollBy(0,speed); } if(pointer.y - docDim.t < sensitivity) { window.scrollBy(0,-speed); } //The two lines bellow are for horizontal scrolling. It is not needed. //if((pointer.x - docDim.iw) - docDim.l > -sensitivity) {window.scrollBy(speed,0);} //if(pointer.x - docDim.l < sensitivity) {window.scrollBy(-speed,0);} }, /* * Called when the item is released after a drag. * The argument passed in is the dragged element released. */ check : function(dragged) { //A brand new NestedSortable release check jQuery.iNestedSortable.newCheck(dragged); //The legacy release check performed by plain Sortables return jQuery.iNestedSortable.oldCheck(dragged); }, oldCheck : jQuery.iSort.check, newCheck : function(dragged) { //removes nesting styling if (jQuery.iNestedSortable.latestNestingClass && jQuery.iNestedSortable.currentNesting) { jQuery.iNestedSortable.currentNesting .removeClass(jQuery.iNestedSortable.latestNestingClass); jQuery.iNestedSortable.currentNesting = null; jQuery.iNestedSortable.latestNestingClass = ""; } if(jQuery.iDrop.overzone.isNestedSortable) { jQuery.iDrop.overzone.nestedSortCfg.remeasured = false; } }, /* * Called when there is a need to serialize the * NestedSortable, to send back to the server. The * parameter is a string with the id of the NestedSortable * element, or an array of ids. If no parameter is passed, * all nested sortables in the page will be serialized. The * return value is an object with a 'hash' parameter, that * contains a string for use in GET or POST, and a 'o' parameter, * with contains a JavaScript object representation * of the item order. */ serialize: function(s) { if(jQuery('#' + s).get(0).isNestedSortable){ //A brand new NestedSortable serialization return jQuery.iNestedSortable.newSerialize(s); } else { //The legacy serialization return jQuery.iNestedSortable.oldSerialize(s); } }, oldSerialize: jQuery.iSort.serialize, newSerialize: function (s) { var i; var h = ''; //url get string that represents the element order var currentPath = ''; //used so the recursive function can build h var o = {}; //json object that represents the element order var e; //NestedSortable element being worked on var parentChildValues = ''; // a comma seperated list of items and their parents var depthValues = ''; // a comma seperated list of items and their depth in the tree var level = 0; var parentId = 0; /* * This recursive function will build the get string (h) * and return the object (o) for a given NestedSortable. * Added two more return parameters * depthValues : returns a list of IDs with their depth in the tree * parentChildValues : returns each item with it's parent value set. Zero for no parent */ var buildHierarchySer = function(context) { level++; var retVal = []; thisChildren = jQuery(context).children('.' + jQuery.iSort.collected[s]); thisChildren.each( function(i) { //Extracts part of the HTML element ID that //will be shown in the serialization, using a RegExp. var serId = jQuery.attr(this,'id'); if(serId && serId.match) { serId = serId.match(e.nestedSortCfg.serializeRegExp)[0]; } if (h.length > 0) { h += '&'; depthValues += ','; parentChildValues += ','; } h += s + currentPath + '['+i+'][id]=' + serId; depthValues += serId + "=" + level; parentChildValues += serId + "=" + parentId; retVal[i] = {id: serId}; var newContext = jQuery(this).children(e.nestedSortCfg.nestingTag + "." + e.nestedSortCfg.nestingTagClass.split(" ").join(".")).get(0); var oldPath = currentPath; var oldParentId = parentId; parentId = serId; currentPath += '['+i+'][children]'; var thisChildren = buildHierarchySer(newContext); if (thisChildren.length > 0) { retVal[i].children = thisChildren; } parentId = oldParentId; currentPath = oldPath; }); level --; return retVal; }; //analises the parameter passed in if (s) { if (jQuery.iSort.collected[s] ) { //when only one NestedSortable id was passed in e = jQuery('#' + s).get(0); o[s] = buildHierarchySer(e); } else { for ( a in s) { //when an array of NestedSortables ids was passed in if (jQuery.iSort.collected[s[a]] ) { e = jQuery('#' + s[a]).get(0); o[s[a]] = buildHierarchySer(e); } } } } else { //when nothing was passed in (we will serialize all) for ( i in jQuery.iSort.collected){ e = jQuery('#' + i).get(0); o[i] = buildHierarchySer(e); } } // alert(depthValues); return {hash:h, o:o, depthVals:depthValues, parentChild:parentChildValues}; }, /* * Finds the sortable item that is on top of the one being dragged, * and in a compatible nesting level. Returns null if none was found. */ findPrecedingItem : function (e) { var largestY = 0; var preceding = jQuery.grep(e.dropCfg.el, function(i) { //needs to be on top of the one being dragged var isOnTop = (i.pos.y < jQuery.iDrag.dragged.dragCfg.ny) && (i.pos.y > largestY); if(!isOnTop) { return false; } //needs to be on the same nesting level or a "child" level var isSameLevel; if(e.nestedSortCfg.rightToLeft) { isSameLevel = (i.pos.x + i.pos.wb + e.nestedSortCfg.snapTolerance > jQuery.iDrag.dragged.dragCfg.nx + jQuery.iDrag.dragged.dragCfg.oC.wb); } else { isSameLevel = (i.pos.x - e.nestedSortCfg.snapTolerance < jQuery.iDrag.dragged.dragCfg.nx); } if(!isSameLevel) { return false; } //can't be an element that is being dragged var isBeingDragged = jQuery.iNestedSortable.isBeingDragged(e, i); if(isBeingDragged) { return false; } //got here because it is a match largestY = i.pos.y; return true; }); if ( preceding.length > 0 ) { //the last one should be it return preceding[(preceding.length-1)]; } else { //nothing above it return null; } }, /* * Checks if the item being dragged is on top and touching the first item on the list. * Returns true or false. */ isTouchingFirstItem : function (e) { var lowestY; var firstItem = jQuery.grep(e.dropCfg.el, function(i) { //needs to be on top of all elements already looked up var isBefore = (lowestY === undefined || i.pos.y < lowestY); if (!isBefore) { return false; } //can't be an element that is being dragged var isBeingDragged = jQuery.iNestedSortable.isBeingDragged(e, i); if(isBeingDragged) { return false; } //got here because it is a match lowestY = i.pos.y; return true; }); if ( firstItem.length > 0 ) { //the last one should be it firstItem = firstItem[(firstItem.length-1)]; return firstItem.pos.y < jQuery.iDrag.dragged.dragCfg.ny + jQuery.iDrag.dragged.dragCfg.oC.hb && firstItem.pos.y > jQuery.iDrag.dragged.dragCfg.ny; } else { return false; } }, /* * Checks to see if an element is being dragged. * You may de dragging an element by itself or by one of his ancestors. * Returns true or false. */ isBeingDragged : function (e, elem) { var dragged = jQuery.iDrag.dragged; //nothing being dragged if(!dragged) { return false; } //trivial case if(elem == dragged) { return true; } //looks for its ancestors if ( jQuery(elem) .parents("." + e.sortCfg.accept.split(" ").join(".")) .filter(function() {return this == dragged;}).length !== 0 ) { return true; } else { return false; } }, shouldNestItem : function(e, precedingItem ) { //there should be a preceding item to be able to nest if(!precedingItem) {return false;} if(e.nestedSortCfg.noNestingClass && jQuery(precedingItem).filter("." + e.nestedSortCfg.noNestingClass).get(0) === precedingItem) {return false;} //This code is to limit the levels of nesting that can be achieved //Development of this is currently halted. /* if( e.nestedSortCfg.nestingLimit !== false) { //nesting level of the preceding item var nLevelPrec = jQuery(precedingItem) .parents("." + e.sortCfg.accept.split(" ").join(".")) .length; //don't allow nesting if (nLevelPrec >= e.nestedSortCfg.nestingLimit) return false; //will hold the maximum level of nesting in the element being dragged var nLevelDrag = 0; //for each leaf element inside the dragged element (that have no elements nested to it) jQuery(e.nestedSortCfg.nestingTag + "." + e.nestedSortCfg.nestingTagClass.split(" ").join(".") + " > ." + e.sortCfg.accept.split(" ").join(".") + ":first-child", jQuery.iDrag.dragged) .not("*["+e.nestedSortCfg.nestingTag + "." + e.nestedSortCfg.nestingTagClass.split(" ").join(".")+"]") .each( function(i) { var draggedIndex = 0; //finds out var parentLength = jQuery(this).parents("." + e.sortCfg.accept.split(" ").join(".")).each( function(i) { draggedIndex = i; return this == jQuery.iDrag.dragged; } ).length; //here dradraggedIndex holds the index //of the dragged element in the ancestors array of the leaf element var thisLevel = parentLength - draggedIndex; if (thisLevel > nLevelDrag ) nLevelDrag = thisLevel; } ); //don't allow nesting if (nLevelPrec + nLevelDrag > e.nestedSortCfg.nestingLimit) return false; }*/ if (e.nestedSortCfg.rightToLeft) { return precedingItem.pos.x + precedingItem.pos.wb - (e.nestedSortCfg.nestingPxSpace - e.nestedSortCfg.snapTolerance) > jQuery.iDrag.dragged.dragCfg.nx + jQuery.iDrag.dragged.dragCfg.oC.wb; } else { return precedingItem.pos.x + (e.nestedSortCfg.nestingPxSpace - e.nestedSortCfg.snapTolerance) < jQuery.iDrag.dragged.dragCfg.nx; } }, nestItem: function(e, parent) { //selects the nesting tag inside the parent var parentNesting = jQuery(parent).children(e.nestedSortCfg.nestingTag + "." + e.nestedSortCfg.nestingTagClass.split(" ").join(".") ); var helper = jQuery.iSort.helper; styleHelper = helper.get(0).style; styleHelper.width = 'auto'; //makes sure helper gets resized properly //if there is none, creates it if ( !parentNesting.size()) { var newUl = "<" + e.nestedSortCfg.nestingTag + " class='"+e.nestedSortCfg.nestingTagClass+"'></" + e.nestedSortCfg.nestingTag+">"; // Place new nesting tag and adds style parentNesting = jQuery(parent).append(newUl).children(e.nestedSortCfg.nestingTag).css(e.nestedSortCfg.styleToAttach); } //styles the nesting jQuery.iNestedSortable.updateCurrentNestingClass(e, parentNesting ); //does stuff before the helper is removed jQuery.iNestedSortable.beforeHelperRemove(e); //puts the helper in the proper place. parentNesting.prepend(helper.get(0)); //does stuff after the helper is inserted jQuery.iNestedSortable.afterHelperInsert(e); }, appendItem: function(e, itemBefore) { jQuery.iNestedSortable.updateCurrentNestingClass(e, jQuery(itemBefore).parent() ); jQuery.iNestedSortable.beforeHelperRemove(e); jQuery(itemBefore).after(jQuery.iSort.helper.get(0)); jQuery.iNestedSortable.afterHelperInsert(e); }, insertOnTop: function (e) { jQuery.iNestedSortable.updateCurrentNestingClass(e, e); jQuery.iNestedSortable.beforeHelperRemove(e); jQuery(e).prepend(jQuery.iSort.helper.get(0)); jQuery.iNestedSortable.afterHelperInsert(e); }, beforeHelperRemove : function (e) { //hides the nesting when it becomes empty var parent = jQuery.iSort.helper.parent(e.nestedSortCfg.nestingTag + "." + e.nestedSortCfg.nestingTagClass.split(" ").join(".")); var numSiblings = parent .children("." + e.sortCfg.accept.split(" ").join(".") + ":visible") .size(); if(numSiblings === 0 && parent.get(0) !== e) { parent.hide(); } }, afterHelperInsert : function (e) { //displays the nesting after something is inserted var parent = jQuery.iSort.helper.parent(); if(parent.get(0) !== e) { parent.show(); } e.nestedSortCfg.remeasured = false; }, updateCurrentNestingClass : function(e, nestingElem) { var nesting = jQuery(nestingElem); if ((e.nestedSortCfg.currentNestingClass) && //makes sure a special class is desired (!jQuery.iNestedSortable.currentNesting || nesting.get(0) != jQuery.iNestedSortable.currentNesting.get(0)) ){ //avoids doing it on the same elem //removes from last nesting if(jQuery.iNestedSortable.currentNesting) { jQuery.iNestedSortable.currentNesting .removeClass(e.nestedSortCfg.currentNestingClass); } if(nesting.get(0) != e) { //not root nesting jQuery.iNestedSortable.currentNesting = nesting; //adds to this one nesting.addClass(e.nestedSortCfg.currentNestingClass); //this is need to remove the styling on "check" jQuery.iNestedSortable.latestNestingClass = e.nestedSortCfg.currentNestingClass; } else { //don't style the "root nesting" //cleans up jQuery.iNestedSortable.currentNesting = null; jQuery.iNestedSortable.latestNestingClass = ""; } } }, destroy: function () { return this.each( function () { if(this.isNestedSortable) { this.nestedSortCfg = null; this.isNestedSortable = null; jQuery(this).SortableDestroy(); } } ); }, build: function(conf) { if (conf.accept && jQuery.iUtil && jQuery.iDrag && jQuery.iDrop && jQuery.iSort) { this.each( function() { this.isNestedSortable = true; this.nestedSortCfg = { noNestingClass : conf.noNestingClass ? conf.noNestingClass : false, rightToLeft : conf.rightToLeft ? true : false , nestingPxSpace : parseInt(conf.nestingPxSpace, 10) || 30 , currentNestingClass : conf.currentNestingClass ? conf.currentNestingClass : "", nestingLimit : conf.nestingLimit ? conf.nestingLimit : false, //not implemented yet autoScroll : conf.autoScroll !== undefined ? conf.autoScroll == true : true, scrollSensitivity: conf.scrollSensitivity ? conf.scrollSensitivity : 20, scrollSpeed: conf.scrollSpeed ? conf.scrollSpeed : 20, serializeRegExp : conf.serializeRegExp ? conf.serializeRegExp : /[^\-]*$/ }; //a "do nothing" tolerance when nesting/un-nesting, to stop things from jumping up too quickly this.nestedSortCfg.snapTolerance = parseInt(this.nestedSortCfg.nestingPxSpace * 0.4, 10); //the tag that will be used to nest items to parent items this.nestedSortCfg.nestingTag = this.tagName; this.nestedSortCfg.nestingTagClass = this.className; //style that makes nested elements be padded to the right or to the left this.nestedSortCfg.styleToAttach = (this.nestedSortCfg.rightToLeft) ? {"padding-left":0, "padding-right": this.nestedSortCfg.nestingPxSpace + 'px' } :{"padding-left": this.nestedSortCfg.nestingPxSpace + 'px', "padding-right": 0 }; jQuery(this.nestedSortCfg.nestingTag, this) .css(this.nestedSortCfg.styleToAttach); } ); //overides checkhover, check and serialize, without losing backwards compatilibity (eg. plain Sortables can stil be used) jQuery.iSort.checkhover = jQuery.iNestedSortable.checkHover; jQuery.iSort.check = jQuery.iNestedSortable.check; jQuery.iSort.serialize = jQuery.iNestedSortable.serialize; } return this.Sortable(conf); } }; //Extends jQuery to add the plugin. jQuery.fn.extend( { NestedSortable : jQuery.iNestedSortable.build, NestedSortableDestroy: jQuery.iNestedSortable.destroy } ); //Monkey patches interface with some corrections, which are not applied to //the 1.2 version yet. getScroll is needed by the autoScroll option. jQuery.iUtil.getScroll = function (e) { var t, l, w, h, iw, ih; if (e && e.nodeName.toLowerCase() != 'body') { t = e.scrollTop; l = e.scrollLeft; w = e.scrollWidth; h = e.scrollHeight; iw = 0; ih = 0; } else { if (document.documentElement && document.documentElement.scrollTop) { t = document.documentElement.scrollTop; l = document.documentElement.scrollLeft; w = document.documentElement.scrollWidth; h = document.documentElement.scrollHeight; } else if (document.body) { t = document.body.scrollTop; l = document.body.scrollLeft; w = document.body.scrollWidth; h = document.body.scrollHeight; } iw = self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0; ih = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0; } return { t: t, l: l, w: w, h: h, iw: iw, ih: ih }; }; |