forked from dbushell/Nestable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.nestable.min.js
1 lines (1 loc) · 9.29 KB
/
jquery.nestable.min.js
1
!function(t,e,s,i){"use strict";function a(e,i){this.w=t(s),this.el=t(e),l.rootClass=this.el.attr("class"),this.options=t.extend({},l,i,this.el.data()),this.init(),this.options.init(this.options)}var o="ontouchstart"in s,n=function(){var t=s.createElement("div"),i=s.documentElement;if(!("pointerEvents"in t.style))return!1;t.style.pointerEvents="auto",t.style.pointerEvents="x",i.appendChild(t);var a=e.getComputedStyle&&"auto"===e.getComputedStyle(t,"").pointerEvents;return i.removeChild(t),!!a}(),l={listNodeName:"ol",itemNodeName:"li",rootClass:"dd",listClass:"dd-list",itemClass:"dd-item",dragClass:"dd-dragel",handleClass:"dd-handle",collapsedClass:"dd-collapsed",placeClass:"dd-placeholder",noDragClass:"dd-nodrag",emptyClass:"dd-empty",expandBtnHTML:'<button data-action="expand" type="button">Expand</button>',collapseBtnHTML:'<button data-action="collapse" type="button">Collapse</button>',group:0,maxDepth:5,threshold:20,cursor:"move",flattenTree:!1,data:null,item:function(t,e,s){return t.id&&(e.attr("data-id",t.id),s.text(t.id)),e},list:t.noop,init:t.noop};a.prototype={init:function(){var s=this;s._data(),s.reset(),s.el.data("nestable-group",this.options.group),s.placeEl=t('<div class="'+s.options.placeClass+'"/>'),t.each(this.el.find(s.options.itemNodeName),function(){s.setParent(t(this))}),s.el.on("click","button",function(e){if(!s.dragEl){var i=t(e.currentTarget),a=i.data("action"),o=i.parent(s.options.itemNodeName);"collapse"===a&&s.collapseItem(o),"expand"===a&&s.expandItem(o)}});var i=function(e){var i=t(e.target).not("a,button");if(!i.hasClass(s.options.handleClass)){if(i.closest("."+s.options.noDragClass).length)return;i=i.closest("."+s.options.handleClass)}i.length&&!s.dragEl&&(s.isTouch=/^touch/.test(e.type),s.isTouch&&1!==e.touches.length||(e.preventDefault(),s.dragStart(e.touches?e.touches[0]:e)))},a=function(t){s.dragEl&&(t.preventDefault(),s.dragMove(t.touches?t.touches[0]:t))},n=function(t){s.dragEl&&(t.preventDefault(),s.dragStop(t.touches?t.touches[0]:t))};o&&(s.el[0].addEventListener("touchstart",i,!1),e.addEventListener("touchmove",a,!1),e.addEventListener("touchend",n,!1),e.addEventListener("touchcancel",n,!1)),s.el.on("mousedown",i),s.w.on("mousemove",a),s.w.on("mouseup",n)},add:function(e){var s,i,a=t("<li>").addClass(this.options.itemClass),o=t("<div>").addClass(this.options.handleClass).appendTo(a),n=e.parent_id?e.parent_id:0;this.options.item(e,a,o),n>0?(s=this.el.find('li[data-id="'+n+'"]'),i=s.find("> ."+this.options.listClass),0===i.length&&(i=this._createListEl().appendTo(s)),this.expandItem(s),this.setParent(s)):i=this._getRootList(),i.append(a),this.reset(),this.el.trigger("added",[e,a,i]).trigger("change")},remove:function(e){var s=t(e),i=s.parents("."+this.options.itemClass).eq(0);s.remove(),0===i.find("."+this.options.itemClass).length&&this.unsetParent(i),this.reset(),this.el.trigger("removed").trigger("change")},_data:function(){var e=this,s=this.options.data;if(s&&Object.keys(s).length>0){var i=this._getRootList();t.each(s,function(t,s){i.append(e._buildItem(s))}),this.el.append(i).attr("data-data",null),this.options.list(i)}},_buildItem:function(e){var s=this,i=t("<li>").addClass(s.options.itemClass),a=t("<div>").addClass(s.options.handleClass).appendTo(i);if(s.options.item(e,i,a),e.children&&Object.keys(e.children).length>0){var o=this._createListEl().appendTo(i);t.each(e.children,function(t,e){o.append(s._buildItem(e))}),s.options.list(o)}return i},_getRootList:function(){var t=this.el.find("."+this.options.listClass).eq(0);return 0===t.length&&(t=this._createListEl()),t},_createListEl:function(){return t("<ol>").addClass(this.options.listClass)},serialize:function(){var e=[],s=0,i=0,a=this,o=function(e,s,i){var n=[],l=e.children("."+a.options.itemClass);return l.each(function(){var e=t(this),l=null,d=a.options.flattenTree?{parent_id:i}:{},r=t.extend(d,e.data()),h=e.children(a.options.listNodeName);r=t.extend({position:e.index("."+a.options.itemClass)+1},r),n.push(r),h.length&&(l=o(h,s+1,e.data("id")),a.options.flattenTree?n=n.concat(l):r.children=l)}),n};return e=o(a.el.find(a.options.listNodeName).first(),s,i)},serialise:function(){return this.serialize()},reset:function(){this.mouse={offsetX:0,offsetY:0,startX:0,startY:0,lastX:0,lastY:0,nowX:0,nowY:0,distX:0,distY:0,dirAx:0,dirX:0,dirY:0,lastDirX:0,lastDirY:0,distAxX:0,distAxY:0},this.isTouch=!1,this.moving=!1,this.dragEl=null,this.dragRootEl=null,this.dragDepth=0,this.hasNewRoot=!1,this.pointEl=null},expandItem:function(t){t.removeClass(this.options.collapsedClass),t.children('[data-action="expand"]').hide(),t.children('[data-action="collapse"]').show(),t.children(this.options.listNodeName).show()},collapseItem:function(t){var e=t.children(this.options.listNodeName);e.length&&(t.addClass(this.options.collapsedClass),t.children('[data-action="collapse"]').hide(),t.children('[data-action="expand"]').show(),t.children(this.options.listNodeName).hide())},expandAll:function(){var e=this;e.el.find(e.options.itemNodeName).each(function(){e.expandItem(t(this))})},collapseAll:function(){var e=this;e.el.find(e.options.itemNodeName).each(function(){e.collapseItem(t(this))})},setParent:function(e){e.children(this.options.listNodeName).length&&(e.prepend(t(this.options.expandBtnHTML)),e.prepend(t(this.options.collapseBtnHTML))),e.children('[data-action="expand"]').hide()},unsetParent:function(t){t.removeClass(this.options.collapsedClass),t.children("[data-action]").remove(),t.children(this.options.listNodeName).remove()},dragStart:function(e){var a=this.mouse,o=t(e.target),n=t(s.body),l=o.closest(this.options.itemNodeName);this.placeEl.css("height",l.height()),a.offsetX=e.offsetX!==i?e.offsetX:e.pageX-o.offset().left,a.offsetY=e.offsetY!==i?e.offsetY:e.pageY-o.offset().top,a.startX=a.lastX=e.pageX,a.startY=a.lastY=e.pageY,this.dragRootEl=this.el,this.dragEl=t(s.createElement(this.options.listNodeName)).addClass(this.options.listClass+" "+this.options.dragClass),this.dragEl.css("width",l.width()),l.after(this.placeEl),l[0].parentNode.removeChild(l[0]),l.appendTo(this.dragEl),this._bodyCursor=n.css("cursor"),n.append(this.dragEl).css("cursor",this.options.cursor),this.dragEl.css({left:e.pageX-a.offsetX,top:e.pageY-a.offsetY});var d,r,h=this.dragEl.find(this.options.itemNodeName);for(d=0;d<h.length;d++)r=t(h[d]).parents(this.options.listNodeName).length,r>this.dragDepth&&(this.dragDepth=r)},dragStop:function(){var e=this.dragEl.children(this.options.itemNodeName).first();"undefined"!=typeof e[0]&&e[0].parentNode.removeChild(e[0]),this.placeEl.replaceWith(e),t(s.body).css("cursor",this._bodyCursor),delete this._bodyCursor,this.dragEl.remove(),this.el.trigger("change"),this.hasNewRoot&&this.dragRootEl.trigger("change"),this.reset()},dragMove:function(i){var a,o,l,d,r,h=this.options,p=this.mouse;this.dragEl.css({left:i.pageX-p.offsetX,top:i.pageY-p.offsetY}),p.lastX=p.nowX,p.lastY=p.nowY,p.nowX=i.pageX,p.nowY=i.pageY,p.distX=p.nowX-p.lastX,p.distY=p.nowY-p.lastY,p.lastDirX=p.dirX,p.lastDirY=p.dirY,p.dirX=0===p.distX?0:p.distX>0?1:-1,p.dirY=0===p.distY?0:p.distY>0?1:-1;var c=Math.abs(p.distX)>Math.abs(p.distY)?1:0;if(!p.moving)return p.dirAx=c,void(p.moving=!0);p.dirAx!==c?(p.distAxX=0,p.distAxY=0):(p.distAxX+=Math.abs(p.distX),0!==p.dirX&&p.dirX!==p.lastDirX&&(p.distAxX=0),p.distAxY+=Math.abs(p.distY),0!==p.dirY&&p.dirY!==p.lastDirY&&(p.distAxY=0)),p.dirAx=c,p.dirAx&&p.distAxX>=h.threshold&&(p.distAxX=0,l=this.placeEl.prev(h.itemNodeName),p.distX>0&&l.length&&!l.hasClass(h.collapsedClass)&&(a=l.find(h.listNodeName).last(),r=this.placeEl.parents(h.listNodeName).length,r+this.dragDepth<=h.maxDepth&&(a.length?(a=l.children(h.listNodeName).last(),a.append(this.placeEl)):(a=t("<"+h.listNodeName+"/>").addClass(h.listClass),a.append(this.placeEl),l.append(a),this.setParent(l)))),p.distX<0&&(d=this.placeEl.next(h.itemNodeName),d.length||(o=this.placeEl.parent(),this.placeEl.closest(h.itemNodeName).after(this.placeEl),o.children().length||this.unsetParent(o.parent()))));var u=!1;if(n||(this.dragEl[0].style.visibility="hidden"),this.pointEl=t(s.elementFromPoint(i.pageX-s.body.scrollLeft,i.pageY-(e.pageYOffset||s.documentElement.scrollTop))),n||(this.dragEl[0].style.visibility="visible"),this.pointEl.hasClass(h.handleClass)&&(this.pointEl=this.pointEl.parent(h.itemNodeName)),this.pointEl.hasClass(h.emptyClass))u=!0;else if(!this.pointEl.length||!this.pointEl.hasClass(h.itemClass))return;var g=this.pointEl.closest("."+h.rootClass),f=this.dragRootEl.data("nestable-id")!==g.data("nestable-id");if(!p.dirAx||f||u){if(f&&h.group!==g.data("nestable-group"))return;if(r=this.dragDepth-1+this.pointEl.parents(h.listNodeName).length,r>h.maxDepth)return;var m=i.pageY<this.pointEl.offset().top+this.pointEl.height()/2;o=this.placeEl.parent(),u?(a=t(s.createElement(h.listNodeName)).addClass(h.listClass),a.append(this.placeEl),this.pointEl.replaceWith(a)):m?this.pointEl.before(this.placeEl):this.pointEl.after(this.placeEl),o.children().length||this.unsetParent(o.parent()),this.dragRootEl.find(h.itemNodeName).length||this.dragRootEl.find(h.emptyClass).length||this.dragRootEl.append('<div class="'+h.emptyClass+'"/>'),f&&(this.dragRootEl=g,this.hasNewRoot=this.el[0]!==this.dragRootEl[0])}}},t.fn.nestable=function(e,s){var i=this,o=this;return i.each(function(){var i=t(this).data("nestable");i?"string"==typeof e&&"function"==typeof i[e]&&(o=i[e](s)):(t(this).data("nestable",new a(this,e)),t(this).data("nestable-id",(new Date).getTime()))}),o||i}}(window.jQuery||window.Zepto,window,document);