1 line
1.7 KiB
JavaScript
1 line
1.7 KiB
JavaScript
|
window.VT=window.VT||{},VT.AppSortable=function(a,b){function c(b,c){b!==r&&(r=b,a.dispatchEvent(new CustomEvent("sortableUpdate",{detail:f(c),bubbles:!0})))}function d(){i(),o=null,p=null,r=-1}function f(a){var b={data:a.detail.data,index:r,placeholder:o,setPlaceholder:function(a){g(a),b.placeholder=o},originalEvent:a};return b}function g(a){p===a||(p=a,i(),o=p.cloneNode(!0),o.classList.add("-placeholder"),o.removeAttribute("data-key"))}function h(b){o&&a.children[b]!==o&&(o.parentNode===a&&a.removeChild(o),a.insertBefore(o,a.children[b]))}function i(){o&&o.parentNode&&o.parentNode.removeChild(o)}function j(b){for(var c,d=0,e=a.children.length;d<e;++d)c=a.children[d],c&&c.dataset.key===b&&a.removeChild(c)}function k(b){if(0===a.children.length)return 0;for(var c,d=q?n:m,e=b.getBoundingClientRect(),f=0,g=0,h=a.children.length;g<h;++g){if(c=a.children[g],d(e,c.getBoundingClientRect()))return g-f;c===o&&(f=1)}return a.children.length-f}function m(a,b){return a.top+(a.bottom-a.top)/2<=b.top+(b.bottom-b.top)/2}function n(a,b){return a.left+(a.right-a.left)/2<=b.left+(b.right-b.left)/2}var o,p,q="horizontal"===b.direction,r=-1;a.addEventListener("draggableStart",function(a){a.detail.image.addEventListener("draggableCancel",d)}),a.addEventListener("draggableOver",function(a){c(k(a.detail.image),a)}),a.addEventListener("draggableLeave",function(a){c(-1,a)}),a.addEventListener("draggableDrop",function(b){a.dispatchEvent(new CustomEvent("sortableDrop",{detail:f(b),bubbles:!0}))}),a.addEventListener("sortableUpdate",function(a){o||a.detail.setPlaceholder(a.detail.originalEvent.detail.imageSource),0<=a.detail.index?h(a.detail.index):i(),j(a.detail.data.key)}),a.addEventListener("sortableDrop",d)};
|