/* * jQuery UI Nested Sortable * v 1.3.5 / 21 jun 2012 * http://mjsarfatti.com/code/nestedSortable * * Depends on: * jquery.ui.sortable.js 1.8+ * * Copyright (c) 2010-2012 Manuele J Sarfatti * Licensed under the MIT License * http://www.opensource.org/licenses/mit-license.php */!function(t){t.widget("mjs.nestedSortable",t.extend({},t.ui.sortable.prototype,{options:{tabSize:20,disableNesting:"mjs-nestedSortable-no-nesting",errorClass:"mjs-nestedSortable-error",doNotClear:!1,listType:"ol",maxLevels:0,protectRoot:!1,rootID:null,rtl:!1,isAllowed:function(){return!0}},_create:function(){if(this.element.data("sortable",this.element.data("nestedSortable")),!this.element.is(this.options.listType))throw new Error("nestedSortable: Please check the listType option is set to your actual list type");return t.ui.sortable.prototype._create.apply(this,arguments)},destroy:function(){return this.element.removeData("nestedSortable").unbind(".nestedSortable"),t.ui.sortable.prototype.destroy.apply(this,arguments)},_mouseDrag:function(e){this.position=this._generatePosition(e),this.positionAbs=this._convertPositionTo("absolute"),this.lastPositionAbs||(this.lastPositionAbs=this.positionAbs);var s=this.options;if(this.options.scroll){var i=!1;this.scrollParent[0]!=document&&"HTML"!=this.scrollParent[0].tagName?(this.overflowOffset.top+this.scrollParent[0].offsetHeight-e.pageY=0;r--){var l=this.items[r],n=l.item[0],h=this._intersectsWithPointer(l);if(h&&n!=this.currentItem[0]&&this.placeholder[1==h?"next":"prev"]()[0]!=n&&!t.contains(this.placeholder[0],n)&&("semi-dynamic"==this.options.type?!t.contains(this.element[0],n):!0)){if(t(n).mouseenter(),this.direction=1==h?"down":"up","pointer"!=this.options.tolerance&&!this._intersectsWithSides(l))break;t(n).mouseleave(),this._rearrange(e,l),this._clearEmpty(n),this._trigger("change",e,this._uiHash());break}}var a=this.placeholder[0].parentNode.parentNode&&t(this.placeholder[0].parentNode.parentNode).closest(".ui-sortable").length?t(this.placeholder[0].parentNode.parentNode):null,p=this._getLevel(this.placeholder),c=this._getChildLevels(this.helper),d=this.placeholder[0].previousSibling?t(this.placeholder[0].previousSibling):null;if(null!=d)for(;"li"!=d[0].nodeName.toLowerCase()||d[0]==this.currentItem[0]||d[0]==this.helper[0];){if(!d[0].previousSibling){d=null;break}d=t(d[0].previousSibling)}var u=this.placeholder[0].nextSibling?t(this.placeholder[0].nextSibling):null;if(null!=u)for(;"li"!=u[0].nodeName.toLowerCase()||u[0]==this.currentItem[0]||u[0]==this.helper[0];){if(!u[0].nextSibling){u=null;break}u=t(u[0].nextSibling)}var f=document.createElement(s.listType);return this.beyondMaxLevels=0,null!=a&&null==u&&(s.rtl&&this.positionAbs.left+this.helper.outerWidth()>a.offset().left+a.outerWidth()||!s.rtl&&this.positionAbs.leftd.offset().left+s.tabSize)?(this._isAllowed(d,p,p+c+1),d.children(s.listType).length||d[0].appendChild(f),o&&o<=d.offset().top?d.children(s.listType).prepend(this.placeholder):d.children(s.listType)[0].appendChild(this.placeholder[0]),this._trigger("change",e,this._uiHash())):this._isAllowed(a,p,p+c),this._contactContainers(e),t.ui.ddmanager&&t.ui.ddmanager.drag(this,e),this._trigger("sort",e,this._uiHash()),this.lastPositionAbs=this.positionAbs,!1},_mouseStop:function(e){this.beyondMaxLevels&&(this.placeholder.removeClass(this.options.errorClass),this.domPosition.prev?t(this.domPosition.prev).after(this.placeholder):t(this.domPosition.parent).prepend(this.placeholder),this._trigger("revert",e,this._uiHash()));for(var s=this.items.length-1;s>=0;s--){var i=this.items[s].item[0];this._clearEmpty(i)}t.ui.sortable.prototype._mouseStop.apply(this,arguments)},serialize:function(e){var s=t.extend({},this.options,e),i=this._getItemsAsjQuery(s&&s.connected),o=[];return t(i).each(function(){var e=(t(s.item||this).attr(s.attribute||"id")||"").match(s.expression||/(.+)[-=_](.+)/),i=(t(s.item||this).parent(s.listType).parent(s.items).attr(s.attribute||"id")||"").match(s.expression||/(.+)[-=_](.+)/);e&&o.push((s.key||e[1])+"["+(s.key&&s.expression?e[1]:e[2])+"]="+(i?s.key&&s.expression?i[1]:i[2]:s.rootID))}),!o.length&&s.key&&o.push(s.key+"="),o.join("&")},toHierarchy:function(e){function s(e){var o=(t(e).attr(i.attribute||"id")||"").match(i.expression||/(.+)[-=_](.+)/);if(o){var r={id:o[2]};return t(e).children(i.listType).children(i.items).length>0&&(r.children=[],t(e).children(i.listType).children(i.items).each(function(){var t=s(this);r.children.push(t)})),r}}var i=t.extend({},this.options,e),o=(i.startDepthCount||0,[]);return t(this.element).children(i.items).each(function(){var t=s(this);o.push(t)}),o},toArray:function(e){function s(e,l,n){var h,a,p=n+1;if(t(e).children(i.listType).children(i.items).length>0&&(l++,t(e).children(i.listType).children(i.items).each(function(){p=s(t(this),l,p)}),l--),h=t(e).attr(i.attribute||"id").match(i.expression||/(.+)[-=_](.+)/),l===o+1)a=i.rootID;else{var c=t(e).parent(i.listType).parent(i.items).attr(i.attribute||"id").match(i.expression||/(.+)[-=_](.+)/);a=c[2]}return h&&r.push({item_id:h[2],parent_id:a,depth:l,left:n,right:p}),n=p+1}var i=t.extend({},this.options,e),o=i.startDepthCount||0,r=[],l=2;return r.push({item_id:i.rootID,parent_id:"none",depth:o,left:"1",right:2*(t(i.items,this.element).length+1)}),t(this.element).children(i.items).each(function(){l=s(this,o+1,l)}),r=r.sort(function(t,e){return t.left-e.left})},_clearEmpty:function(e){var s=t(e).children(this.options.listType);!s.length||s.children().length||this.options.doNotClear||s.remove()},_getLevel:function(t){var e=1;if(this.options.listType)for(var s=t.closest(this.options.listType);s&&s.length>0&&!s.is(".ui-sortable");)e++,s=s.parent().closest(this.options.listType);return e},_getChildLevels:function(e,s){var i=this,o=this.options,r=0;return s=s||0,t(e).children(o.listType).children(o.items).each(function(t,e){r=Math.max(i._getChildLevels(e,s+1),r)}),s?r+1:r},_isAllowed:function(e,s,i){var o=this.options,r=t(this.domPosition.parent).hasClass("ui-sortable")?!0:!1,l=this.placeholder.closest(".ui-sortable").nestedSortable("option","maxLevels");!o.isAllowed(this.currentItem,e)||e&&e.hasClass(o.disableNesting)||o.protectRoot&&(null==e&&!r||r&&s>1)?(this.placeholder.addClass(o.errorClass),this.beyondMaxLevels=i>l&&0!=l?i-l:1):i>l&&0!=l?(this.placeholder.addClass(o.errorClass),this.beyondMaxLevels=i-l):(this.placeholder.removeClass(o.errorClass),this.beyondMaxLevels=0)}})),t.mjs.nestedSortable.prototype.options=t.extend({},t.ui.sortable.prototype.options,t.mjs.nestedSortable.prototype.options)}(jQuery);