/*
* jQuery UI Nested Sortable
* v 2.0 / 29 oct 2012
* http://mjsarfatti.com/sandbox/nestedSortable
*
* Depends on:
* jquery.ui.sortable.js 1.10+
*
* Copyright (c) 2010-2013 Manuele J Sarfatti
* Licensed under the MIT License
* http://www.opensource.org/licenses/mit-license.php
*/
!function(e){"function"==typeof define&&define.amd?define(["jquery","jquery-ui/sortable"],e):e(jQuery)}(function(e){function t(e,t,s){return e>t&&t+s>e}e.widget("mjs.nestedSortable",e.extend({},e.ui.sortable.prototype,{options:{disableParentChange:!1,doNotClear:!1,expandOnHover:700,isAllowed:function(){return!0},isTree:!1,listType:"ol",maxLevels:0,protectRoot:!1,rootID:null,rtl:!1,startCollapsed:!1,tabSize:20,branchClass:"mjs-nestedSortable-branch",collapsedClass:"mjs-nestedSortable-collapsed",disableNestingClass:"mjs-nestedSortable-no-nesting",errorClass:"mjs-nestedSortable-error",expandedClass:"mjs-nestedSortable-expanded",hoveringClass:"mjs-nestedSortable-hovering",leafClass:"mjs-nestedSortable-leaf",disabledClass:"mjs-nestedSortable-disabled"},_create:function(){if(this.element.data("ui-sortable",this.element.data("mjs-nestedSortable")),!this.element.is(this.options.listType))throw new Error("nestedSortable: Please check that the listType option is set to your actual list type");if(this.options.isTree&&this.options.expandOnHover&&(this.options.tolerance="intersect"),e.ui.sortable.prototype._create.apply(this,arguments),this.options.isTree){var t=this;e(this.items).each(function(){var e=this.item;e.children(t.options.listType).length?(e.addClass(t.options.branchClass),e.hasClass(t.options.collapsedClass)||e.hasClass(t.options.expandedClass)||e.addClass(t.options.startCollapsed?t.options.collapsedClass:t.options.expandedClass)):e.addClass(t.options.leafClass)})}},_destroy:function(){return this.element.removeData("mjs-nestedSortable").removeData("ui-sortable"),e.ui.sortable.prototype._destroy.apply(this,arguments)},_mouseDrag:function(t){var s,i,o,l,r=this.options,n=!1;this.position=this._generatePosition(t),this.positionAbs=this._convertPositionTo("absolute"),this.lastPositionAbs||(this.lastPositionAbs=this.positionAbs),this.options.scroll&&(this.scrollParent[0]!=document&&"HTML"!=this.scrollParent[0].tagName?(this.overflowOffset.top+this.scrollParent[0].offsetHeight-t.pageY<r.scrollSensitivity?this.scrollParent[0].scrollTop=n=this.scrollParent[0].scrollTop+r.scrollSpeed:t.pageY-this.overflowOffset.top<r.scrollSensitivity&&(this.scrollParent[0].scrollTop=n=this.scrollParent[0].scrollTop-r.scrollSpeed),this.overflowOffset.left+this.scrollParent[0].offsetWidth-t.pageX<r.scrollSensitivity?this.scrollParent[0].scrollLeft=n=this.scrollParent[0].scrollLeft+r.scrollSpeed:t.pageX-this.overflowOffset.left<r.scrollSensitivity&&(this.scrollParent[0].scrollLeft=n=this.scrollParent[0].scrollLeft-r.scrollSpeed)):(t.pageY-e(document).scrollTop()<r.scrollSensitivity?n=e(document).scrollTop(e(document).scrollTop()-r.scrollSpeed):e(window).height()-(t.pageY-e(document).scrollTop())<r.scrollSensitivity&&(n=e(document).scrollTop(e(document).scrollTop()+r.scrollSpeed)),t.pageX-e(document).scrollLeft()<r.scrollSensitivity?n=e(document).scrollLeft(e(document).scrollLeft()-r.scrollSpeed):e(window).width()-(t.pageX-e(document).scrollLeft())<r.scrollSensitivity&&(n=e(document).scrollLeft(e(document).scrollLeft()+r.scrollSpeed))),n!==!1&&e.ui.ddmanager&&!r.dropBehaviour&&e.ui.ddmanager.prepareOffsets(this,t)),this.positionAbs=this._convertPositionTo("absolute");var a=this.placeholder.offset().top;this.options.axis&&"y"===this.options.axis||(this.helper[0].style.left=this.position.left+"px"),this.options.axis&&"x"===this.options.axis||(this.helper[0].style.top=this.position.top+"px"),this.hovering=this.hovering?this.hovering:null,this.mouseentered=this.mouseentered?this.mouseentered:!1;var h=this.placeholder[0].parentNode.parentNode&&e(this.placeholder[0].parentNode.parentNode).closest(".ui-sortable").length?e(this.placeholder[0].parentNode.parentNode):null,p=this._getLevel(this.placeholder),d=this._getChildLevels(this.helper),c=document.createElement(r.listType);for(s=this.items.length-1;s>=0;s--)if(i=this.items[s],o=i.item[0],l=this._intersectsWithPointer(i),l&&i.instance===this.currentContainer){if(-1!==o.className.indexOf(r.disabledClass))if(2===l){var u=this.items[s+1];if(u&&-1!==u.item[0].className.indexOf(r.disabledClass))continue}else if(1===l){var m=this.items[s-1];if(m&&-1!==m.item[0].className.indexOf(r.disabledClass))continue}if(o!==this.currentItem[0]&&this.placeholder[1===l?"next":"prev"]()[0]!==o&&!e.contains(this.placeholder[0],o)&&("semi-dynamic"===this.options.type?!e.contains(this.element[0],o):!0)){if(this.mouseentered||(e(o).mouseenter(),this.mouseentered=!0),r.isTree&&e(o).hasClass(r.collapsedClass)&&r.expandOnHover&&!this.hovering){e(o).addClass(r.hoveringClass);var f=this;this.hovering=window.setTimeout(function(){e(o).removeClass(r.collapsedClass).addClass(r.expandedClass),f.refreshPositions(),f._trigger("expand",t,f._uiHash())},r.expandOnHover)}if(this.direction=1==l?"down":"up","pointer"!=this.options.tolerance&&!this._intersectsWithSides(i))break;if(e(o).mouseleave(),this.mouseentered=!1,e(o).removeClass(r.hoveringClass),this.hovering&&window.clearTimeout(this.hovering),this.hovering=null,!r.protectRoot||this.currentItem[0].parentNode==this.element[0]&&o.parentNode!=this.element[0])r.protectRoot||this._rearrange(t,i);else if(this.currentItem[0].parentNode!=this.element[0]&&o.parentNode==this.element[0]){e(o).children(r.listType).length||(o.appendChild(c),r.isTree&&e(o).removeClass(r.leafClass).addClass(r.branchClass+" "+r.expandedClass));var g="down"===this.direction?e(o).prev().children(r.listType):e(o).children(r.listType);void 0!==g[0]&&this._rearrange(t,null,g)}else this._rearrange(t,i);this._clearEmpty(o),this._trigger("change",t,this._uiHash());break}}var v=this.placeholder[0].previousSibling?e(this.placeholder[0].previousSibling):null;if(null!=v)for(;"li"!=v[0].nodeName.toLowerCase()||-1!==v[0].className.indexOf(r.disabledClass)||v[0]==this.currentItem[0]||v[0]==this.helper[0];){if(!v[0].previousSibling){v=null;break}v=e(v[0].previousSibling)}var C=this.placeholder[0].nextSibling?e(this.placeholder[0].nextSibling):null;if(null!=C)for(;"li"!=C[0].nodeName.toLowerCase()||-1!==C[0].className.indexOf(r.disabledClass)||C[0]==this.currentItem[0]||C[0]==this.helper[0];){if(!C[0].nextSibling){C=null;break}C=e(C[0].nextSibling)}return this.beyondMaxLevels=0,null==h||null!=C||r.protectRoot&&h[0].parentNode==this.element[0]||!(r.rtl&&this.positionAbs.left+this.helper.outerWidth()>h.offset().left+h.outerWidth()||!r.rtl&&this.positionAbs.left<h.offset().left)?null==v||v.hasClass(r.disableNestingClass)||!(v.children(r.listType).length&&v.children(r.listType).is(":visible")||!v.children(r.listType).length)||r.protectRoot&&this.currentItem[0].parentNode==this.element[0]||!(r.rtl&&this.positionAbs.left+this.helper.outerWidth()<v.offset().left+v.outerWidth()-r.tabSize||!r.rtl&&this.positionAbs.left>v.offset().left+r.tabSize)?this._isAllowed(h,p,p+d):(this._isAllowed(v,p,p+d+1),v.children(r.listType).length||(v[0].appendChild(c),r.isTree&&v.removeClass(r.leafClass).addClass(r.branchClass+" "+r.expandedClass)),a&&a<=v.offset().top?v.children(r.listType).prepend(this.placeholder):v.children(r.listType)[0].appendChild(this.placeholder[0]),this._trigger("change",t,this._uiHash())):(h.after(this.placeholder[0]),r.isTree&&h.children(r.listItem).children("li:visible:not(.ui-sortable-helper)").length<1&&h.removeClass(this.options.branchClass+" "+this.options.expandedClass).addClass(this.options.leafClass),this._clearEmpty(h[0]),this._trigger("change",t,this._uiHash())),this._contactContainers(t),e.ui.ddmanager&&e.ui.ddmanager.drag(this,t),this._trigger("sort",t,this._uiHash()),this.lastPositionAbs=this.positionAbs,!1},_mouseStop:function(t){this.beyondMaxLevels&&(this.placeholder.removeClass(this.options.errorClass),this.domPosition.prev?e(this.domPosition.prev).after(this.placeholder):e(this.domPosition.parent).prepend(this.placeholder),this._trigger("revert",t,this._uiHash())),e("."+this.options.hoveringClass).mouseleave().removeClass(this.options.hoveringClass),this.mouseentered=!1,this.hovering&&window.clearTimeout(this.hovering),this.hovering=null,e.ui.sortable.prototype._mouseStop.apply(this,arguments);var s=e(this.domPosition.parent).parent().attr("id"),i=this.domPosition.prev?e(this.domPosition.prev).next().index():0;(s!=this._uiHash().item.parent().parent().attr("id")||i!=this._uiHash().item.index())&&this._trigger("relocate",t,this._uiHash())},_intersectsWithSides:function(e){var s=this.options.isTree?.8:.5,i=t(this.positionAbs.top+this.offset.click.top,e.top+e.height*s,e.height),o=t(this.positionAbs.top+this.offset.click.top,e.top-e.height*s,e.height),l=t(this.positionAbs.left+this.offset.click.left,e.left+e.width/2,e.width),r=this._getDragVerticalDirection(),n=this._getDragHorizontalDirection();return this.floating&&n?"right"==n&&l||"left"==n&&!l:r&&("down"==r&&i||"up"==r&&o)},_contactContainers:function(){this.options.protectRoot&&this.currentItem[0].parentNode==this.element[0]||e.ui.sortable.prototype._contactContainers.apply(this,arguments)},_clear:function(){e.ui.sortable.prototype._clear.apply(this,arguments);for(var t=this.items.length-1;t>=0;t--){var s=this.items[t].item[0];this._clearEmpty(s)}},serialize:function(t){var s=e.extend({},this.options,t),i=this._getItemsAsjQuery(s&&s.connected),o=[];return e(i).each(function(){var t=(e(s.item||this).attr(s.attribute||"id")||"").match(s.expression||/(.+)[-=_](.+)/),i=(e(s.item||this).parent(s.listType).parent(s.items).attr(s.attribute||"id")||"").match(s.expression||/(.+)[-=_](.+)/);t&&o.push((s.key||t[1])+"["+(s.key&&s.expression?t[1]:t[2])+"]="+(i?s.key&&s.expression?i[1]:i[2]:s.rootID))}),!o.length&&s.key&&o.push(s.key+"="),o.join("&")},toHierarchy:function(t){function s(t){var o=(e(t).attr(i.attribute||"id")||"").match(i.expression||/(.+)[-=_](.+)/);if(o){var l={id:o[2]};return e(t).children(i.listType).children(i.items).length>0&&(l.children=[],e(t).children(i.listType).children(i.items).each(function(){var e=s(this);l.children.push(e)})),l}}var i=e.extend({},this.options,t),o=(i.startDepthCount||0,[]);return e(this.element).children(i.items).each(function(){var e=s(this);o.push(e)}),o},toArray:function(t){function s(t,r,n){var a,h,p=n+1;if(e(t).children(i.listType).children(i.items).length>0&&(r++,e(t).children(i.listType).children(i.items).each(function(){p=s(e(this),r,p)}),r--),a=e(t).attr(i.attribute||"id").match(i.expression||/(.+)[-=_](.+)/),r===o+1)h=i.rootID;else{var d=e(t).parent(i.listType).parent(i.items).attr(i.attribute||"id").match(i.expression||/(.+)[-=_](.+)/);h=d[2]}return a&&l.push({item_id:a[2],parent_id:h,depth:r,left:n,right:p}),n=p+1}var i=e.extend({},this.options,t),o=i.startDepthCount||0,l=[],r=1;return i.excludeRoot||(l.push({item_id:i.rootID,parent_id:null,depth:o,left:r,right:2*(e(i.items,this.element).length+1)}),r++),e(this.element).children(i.items).each(function(){r=s(this,o+1,r)}),l=l.sort(function(e,t){return e.left-t.left})},_clearEmpty:function(t){var s=this.options,i=e(t).children(s.listType);!i.length||i.children().length||s.doNotClear?s.isTree&&i.length&&i.children().length&&i.is(":visible")?e(t).removeClass(s.leafClass).addClass(s.branchClass+" "+s.expandedClass):s.isTree&&i.length&&i.children().length&&!i.is(":visible")&&e(t).removeClass(s.leafClass).addClass(s.branchClass+" "+s.collapsedClass):(s.isTree&&e(t).removeClass(s.branchClass+" "+s.expandedClass).addClass(s.leafClass),i.remove())},_getLevel:function(e){var t=1;if(this.options.listType)for(var s=e.closest(this.options.listType);s&&s.length>0&&!s.is(".ui-sortable");)t++,s=s.parent().closest(this.options.listType);return t},_getChildLevels:function(t,s){var i=this,o=this.options,l=0;return s=s||0,e(t).children(o.listType).children(o.items).each(function(e,t){l=Math.max(i._getChildLevels(t,s+1),l)}),s?l+1:l},_isAllowed:function(e,t,s){var i=this.options,o=this.placeholder.closest(".ui-sortable").nestedSortable("option","maxLevels"),l=this.currentItem.parent().parent(),r=i.disableParentChange&&(null!==e&&!l.is(e)||null===e&&l.is("li"));r||!i.isAllowed(this.placeholder,e,this.currentItem)?(this.placeholder.addClass(i.errorClass),this.beyondMaxLevels=s>o&&0!=o?s-o:1):s>o&&0!=o?(this.placeholder.addClass(i.errorClass),this.beyondMaxLevels=s-o):(this.placeholder.removeClass(i.errorClass),this.beyondMaxLevels=0)}})),e.mjs.nestedSortable.prototype.options=e.extend({},e.ui.sortable.prototype.options,e.mjs.nestedSortable.prototype.options)});