UNPKG

vue-sortablejs

Version:
2 lines (1 loc) 3.86 kB
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n():"function"==typeof define&&define.amd?define(n):n()}(0,function(){"use strict";function t(t){1===t.parentNode.nodeType&&t.parentNode.removeChild(t)}function n(t,n,e){e<t.children.length?t.insertBefore(n,t.children[e]):t.appendChild(n)}function e(t,n){return t.map(function(t){return t.elm}).indexOf(n)}function o(t,n){return t?Array.prototype.map.call(n,function(n){return e(t,n)}):[]}function i(t,n){this.$emit(t.toLowerCase(),n)}function r(t){var n=this;return function(e){null!==n.realList&&n["onDrag"+t](e),i.call(n,t,e)}}function s(s){var a=null,u=["Add","Start","Update","End","Remove"],l=["Choose","Sort","Filter","Clone"],c=["Move"].concat(u,l).map(function(t){return"on"+t});return{props:{options:Object,list:{type:Array,required:!1,default:null},move:{type:Function,default:null},clone:{type:Function,default:function(t){return t}},element:{type:String,default:"div"}},data:function(){return{transitionFlag:!1}},render:function(t){if(this.$slots.default&&1===this.$slots.default.length){var n=this.$slots.default[0];n.componentOptions&&"transition-group"===n.componentOptions.tag&&(this.transitionFlag=!0)}return t(this.element,null,this.$slots.default)},mounted:function(){var t=this,n={};u.forEach(function(e){n["on"+e]=r.call(t,e)}),l.forEach(function(e){n["on"+e]=i.bind(t,e)});var e=Object.assign({},this.options,n,{onMove:function(n){return t.onDragMove(n)}});this._sortable=new s(this.rootContainer,e),this.computeIndexes()},beforeDestroy:function(){this._sortable.destroy()},computed:{rootContainer:function(){return this.transitionFlag?this.$el.children[0]:this.$el},realList:function(){return this.list}},watch:{options:function(t){var n=this;for(var e in t)-1===c.indexOf(e)&&n._sortable.option(e,t[e])},realList:function(){this.computeIndexes()}},methods:{getChildren:function(){var t=this.$slots.default;return this.transitionFlag?t[0].child.$slots.default:t},computeIndexes:function(){var t=this;this.$nextTick(function(){t.vIndices=o(t.getChildren(),t.rootContainer.children)})},getDomElement:function(t){var n=e(this.getChildren(),t);return{index:n,element:this.realList[n]}},getDragElementComponent:function(t){var n=t.__vue__;return n&&n.$options&&"transition-group"===n.$options._componentTag?n.$parent:n},emitChanges:function(t){var n=this;this.$nextTick(function(){n.$emit("change",t)})},changeList:function(t){t(this.list)},updateLocation:function(t,n){var e=function(e){return e.splice(n,0,e.splice(t,1)[0])};this.changeList(e)},getRelatedContext:function(t){var n=t.to,e=t.related,o=this.getDragElementComponent(n);if(!o)return{component:o};var i=o.realList,r={realList:i,component:o};if(n!==e&&i&&o.getDomElement){var s=o.getDomElement(e);return Object.assign(s,r)}return r},getDomIndex:function(t){var n=this.vIndices,e=n.length;return t>e-1?e:n[t]},onDragStart:function(t){this.context=this.getDomElement(t.item),t.item._underlying_vm_=this.clone(this.context.element),a=t.item},onDragUpdate:function(e){t(e.item),n(e.from,e.item,e.oldIndex);var o=this.context.index,i=this.getDomIndex(e.newIndex);this.updateLocation(o,i);var r={element:this.context.element,oldIndex:o,newIndex:i};this.emitChanges({moved:r})},computeDestinationIndex:function(t,n){if(!t.element)return!1;var e=[].concat(n.to.children),o=e.indexOf(n.related),i=t.component.getDomIndex(o);return-1!=e.indexOf(a)?i:i+1},onDragMove:function(t){var n=this.move;if(!n||!this.realList)return!0;var e=this.getRelatedContext(t),o=this.context,i=this.computeDestinationIndex(e,t);return Object.assign(o,{destinationIndex:i}),Object.assign(t,{relatedContext:e,draggedContext:o}),n(t)},onDragEnd:function(){a=null}}}}if(window&&window.Vue&&window.Sortable){var a=s(window.Sortable);Vue.component("draggable",a)}else if("object"==typeof exports){var u=require("sortablejs");module.exports=s(u)}});