2 lines
8.1 KiB
JavaScript
2 lines
8.1 KiB
JavaScript
/*! UIkit 2.21.0 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
|
|
!function(t){var e;window.UIkit&&(e=t(UIkit)),"function"==typeof define&&define.amd&&define("uikit-sortable",["uikit"],function(){return e||t(UIkit)})}(function(t){"use strict";function e(e){e=t.$(e);do{if(e.data("sortable"))return e;e=t.$(e).parent()}while(e.length);return e}function o(t,e){var o=t.parentNode;if(e.parentNode!=o)return!1;for(var n=t.previousSibling;n&&9!==n.nodeType;){if(n===e)return!0;n=n.previousSibling}return!1}function n(t,e){var o=e;if(o==t)return null;for(;o;){if(o.parentNode===t)return o;if(o=o.parentNode,!o||!o.ownerDocument||11===o.nodeType)break}return null}function a(t){t.stopPropagation&&t.stopPropagation(),t.preventDefault&&t.preventDefault(),t.returnValue=!1}var s,r,i,l,d,h,u,p,c,g="ontouchstart"in window||window.DocumentTouch&&document instanceof DocumentTouch;return t.component("sortable",{defaults:{animation:150,threshold:10,childClass:"uk-sortable-item",placeholderClass:"uk-sortable-placeholder",overClass:"uk-sortable-over",draggingClass:"uk-sortable-dragged",dragMovingClass:"uk-sortable-moving",baseClass:"uk-sortable",noDragClass:"uk-sortable-nodrag",dragCustomClass:"",handleClass:!1,group:!1,stop:function(){},start:function(){},change:function(){}},boot:function(){t.ready(function(e){t.$("[data-uk-sortable]",e).each(function(){var e=t.$(this);if(!e.data("sortable")){t.sortable(e,t.Utils.options(e.attr("data-uk-sortable")))}})}),t.$html.on("mousemove touchmove",function(e){if(u){var o=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0]:e;(Math.abs(o.pageX-u.pos.x)>u.threshold||Math.abs(o.pageY-u.pos.y)>u.threshold)&&u.apply(o)}if(s){d||(d=!0,s.show(),s.$current.addClass(s.$sortable.options.placeholderClass),s.$sortable.element.children().addClass(s.$sortable.options.childClass),t.$html.addClass(s.$sortable.options.dragMovingClass));var n=s.data("mouse-offset"),a=parseInt(e.originalEvent.pageX,10)+n.left,r=parseInt(e.originalEvent.pageY,10)+n.top;s.css({left:a,top:r}),r<t.$win.scrollTop()?t.$win.scrollTop(t.$win.scrollTop()-Math.ceil(s.height()/2)):r+s.height()>window.innerHeight+t.$win.scrollTop()&&t.$win.scrollTop(t.$win.scrollTop()+Math.ceil(s.height()/2))}}),t.$html.on("mouseup touchend",function(t){if(u=h=!1,!r||!s)return r=s=null,void 0;var o=e(t.target),n=s.$sortable,a={type:t.type};o[0]&&n.dragDrop(a,n.element),n.dragEnd(a,n.element)})},init:function(){function e(){g?h.addEventListener("touchmove",v,!1):(h.addEventListener("mouseover",f,!1),h.addEventListener("mouseout",m,!1))}function o(){g?h.removeEventListener("touchmove",v,!1):(h.removeEventListener("mouseover",f,!1),h.removeEventListener("mouseout",m,!1))}function s(t){r&&d.dragMove(t,d)}function l(e){return function(o){var a,s,r;o&&(a=g&&o.touches&&o.touches[0]||{},s=a.target||o.target,g&&document.elementFromPoint&&(s=document.elementFromPoint(o.pageX-document.body.scrollLeft,o.pageY-document.body.scrollTop))),t.$(s).hasClass(d.options.childClass)?e.apply(s,[o]):s!==h&&(r=n(h,s),r&&e.apply(r,[o]))}}var d=this,h=this.element[0];p=[],0===this.element.children().length&&this.element.html(""),this.element.data("sortable-group",this.options.group?this.options.group:t.Utils.uid("sortable-group"));var u=l(function(e){var o=t.$(e.target),n=o.is("a[href]")?o:o.parents("a[href]");if(!o.is(":input"))return e.preventDefault(),!g&&n.length&&n.one("click",function(t){t.preventDefault()}).one("mouseup",function(){c||n.trigger("click")}),d.dragStart(e,this)}),f=(l(function(t){return r?(t.preventDefault&&t.preventDefault(),!1):!0}),l(t.Utils.debounce(function(t){return d.dragEnter(t,this)}),40)),m=l(function(){var e=d.dragenterData(this);d.dragenterData(this,e-1),d.dragenterData(this)||(t.$(this).removeClass(d.options.overClass),d.dragenterData(this,!1))}),v=l(function(t){return r&&r!==this&&i!==this?(d.element.children().removeClass(d.options.overClass),i=this,d.moveElementNextTo(r,this),a(t)):!0});this.addDragHandlers=e,this.removeDragHandlers=o,window.addEventListener(g?"touchmove":"mousemove",s,!1),h.addEventListener(g?"touchstart":"mousedown",u,!1)},dragStart:function(e,o){c=!1,d=!1,l=!1;{var n=this,a=t.$(e.target);n.element.children()}if(g||2!=e.button){if(n.options.handleClass){var i=a.hasClass(n.options.handleClass)?a:a.closest("."+n.options.handleClass,n.element);if(!i.length)return}if(!a.is("."+n.options.noDragClass)&&!a.closest("."+n.options._noDragClass).length&&!a.is(":input")){r=o,s&&s.remove();var h=t.$(r),p=h.offset();u={pos:{x:e.pageX,y:e.pageY},threshold:n.options.threshold,apply:function(e){s=t.$('<div class="'+[n.options.draggingClass,n.options.dragCustomClass].join(" ")+'"></div>').css({display:"none",top:p.top,left:p.left,width:h.width(),height:h.height(),padding:h.css("padding")}).data({"mouse-offset":{left:p.left-parseInt(e.pageX,10),top:p.top-parseInt(e.pageY,10)},origin:n.element,index:h.index()}).append(h.html()).appendTo("body"),s.$current=h,s.$sortable=n,h.data("sortable-group",n.options.group),n.addDragHandlers(),n.options.start(this,r),n.trigger("start.uk.sortable",[n,r]),c=!0,u=!1}}}}},dragMove:function(e){var o,n=t.$(document.elementFromPoint(e.pageX-document.body.scrollLeft,e.pageY-(window.pageYOffset||document.documentElement.scrollTop))),a=n.closest("."+this.options.baseClass),s=a.data("sortable-group"),i=t.$(r),l=i.parent(),d=i.data("sortable-group");a[0]!==l[0]&&void 0!==d&&s===d&&(a.data("sortable").addDragHandlers(),p.push(a),a.children().addClass(this.options.childClass),a.children().length>0?(o=n.closest("."+this.options.childClass),o.before(i)):n.append(i),0===l.children().length&&l.html(""),UIkit.$doc.trigger("mouseover"))},dragEnter:function(e,o){if(!r||r===o)return!0;var n=this.dragenterData(o);return this.dragenterData(o,n+1),0===n&&(t.$(o).addClass(this.options.overClass),this.moveElementNextTo(r,o)),!1},dragEnd:function(e,o){var n=this;r&&(this.options.stop(o),this.trigger("stop.uk.sortable",[this])),r=null,i=null,p.push(this.element),p.forEach(function(e){t.$(e).children().each(function(){1===this.nodeType&&(t.$(this).removeClass(n.options.overClass).removeClass(n.options.placeholderClass).removeClass(n.options.childClass),n.dragenterData(this,!1))})}),p=[],t.$html.removeClass(this.options.dragMovingClass),this.removeDragHandlers(),s&&(s.remove(),s=null)},dragDrop:function(t){"drop"===t.type&&(t.stopPropagation&&t.stopPropagation(),t.preventDefault&&t.preventDefault()),this.triggerChangeEvents()},triggerChangeEvents:function(){if(r){var e=t.$(r),o=s.data("origin"),n=e.closest("."+this.options.baseClass),a=[];o[0]===n[0]&&s.data("index")!=e.index()?a.push({el:this,mode:"moved"}):o[0]!=n[0]&&a.push({el:n,mode:"added"},{el:o,mode:"removed"}),a.forEach(function(t){t.el.trigger("change.uk.sortable",[t.el,r,t.mode])})}},dragenterData:function(e,o){return e=t.$(e),1==arguments.length?parseInt(e.data("child-dragenter"),10)||0:(o?e.data("child-dragenter",Math.max(0,o)):e.removeData("child-dragenter"),void 0)},moveElementNextTo:function(e,n){l=!0;var a=this,s=t.$(e).parent().css("min-height",""),r=o(e,n)?n:n.nextSibling,i=s.children(),d=i.length;return a.options.animation?(s.css("min-height",s.height()),i.stop().each(function(){var e=t.$(this),o=e.position();o.width=e.width(),e.data("offset-before",o)}),n.parentNode.insertBefore(e,r),t.Utils.checkDisplay(a.element.parent()),i=s.children().each(function(){var e=t.$(this);e.data("offset-after",e.position())}).each(function(){var e=t.$(this),o=e.data("offset-before");e.css({position:"absolute",top:o.top,left:o.left,"min-width":o.width})}),i.each(function(){var e=t.$(this),o=(e.data("offset-before"),e.data("offset-after"));e.css("pointer-events","none").width(),setTimeout(function(){e.animate({top:o.top,left:o.left},a.options.animation,function(){e.css({position:"",top:"",left:"","min-width":"","pointer-events":""}).removeClass(a.options.overClass).removeData("child-dragenter"),d--,d||(s.css("min-height",""),t.Utils.checkDisplay(a.element.parent()))})},0)}),void 0):(n.parentNode.insertBefore(e,r),t.Utils.checkDisplay(a.element.parent()),void 0)},serialize:function(){var e,o,n=[];return this.element.children().each(function(a,s){e={};for(var r=0;r<s.attributes.length;r++)o=s.attributes[r],0===o.name.indexOf("data-")&&(e[o.name.substr(5)]=t.Utils.str2json(o.value));n.push(e)}),n}}),t.sortable}); |