You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SiteWriter/static/js/html5sortable.min.js

3 lines
14 KiB
JavaScript

var sortable=function(){"use strict";function e(e,t,n){if(void 0===n)return e&&e.h5s&&e.h5s.data&&e.h5s.data[t];e.h5s=e.h5s||{},e.h5s.data=e.h5s.data||{},e.h5s.data[t]=n}function t(e,t){if(!(e instanceof NodeList||e instanceof HTMLCollection||e instanceof Array))throw new Error("You must provide a nodeList/HTMLCollection/Array of elements to be filtered.");return"string"!=typeof t?Array.from(e):Array.from(e).filter(function(e){return 1===e.nodeType&&e.matches(t)})}var n=new Map,r=function(){function e(){this._config=new Map,this._placeholder=void 0,this._data=new Map}return Object.defineProperty(e.prototype,"config",{get:function(){var e={};return this._config.forEach(function(t,n){e[n]=t}),e},set:function(e){if("object"!=typeof e)throw new Error("You must provide a valid configuration object to the config setter.");var t=Object.assign({},e);this._config=new Map(Object.entries(t))},enumerable:!0,configurable:!0}),e.prototype.setConfig=function(e,t){if(!this._config.has(e))throw new Error("Trying to set invalid configuration item: "+e);this._config.set(e,t)},e.prototype.getConfig=function(e){if(!this._config.has(e))throw new Error("Invalid configuration item requested: "+e);return this._config.get(e)},Object.defineProperty(e.prototype,"placeholder",{get:function(){return this._placeholder},set:function(e){if(!(e instanceof HTMLElement)&&null!==e)throw new Error("A placeholder must be an html element or null.");this._placeholder=e},enumerable:!0,configurable:!0}),e.prototype.setData=function(e,t){if("string"!=typeof e)throw new Error("The key must be a string.");this._data.set(e,t)},e.prototype.getData=function(e){if("string"!=typeof e)throw new Error("The key must be a string.");return this._data.get(e)},e.prototype.deleteData=function(e){if("string"!=typeof e)throw new Error("The key must be a string.");return this._data.delete(e)},e}();function o(e){if(!(e instanceof HTMLElement))throw new Error("Please provide a sortable to the store function.");return n.has(e)||n.set(e,new r),n.get(e)}function i(e,t,n){if(e instanceof Array)for(var r=0;r<e.length;++r)i(e[r],t,n);else e.addEventListener(t,n),o(e).setData("event"+t,n)}function a(e,t){if(e instanceof Array)for(var n=0;n<e.length;++n)a(e[n],t);else e.removeEventListener(t,o(e).getData("event"+t)),o(e).deleteData("event"+t)}function l(e,t,n){if(e instanceof Array)for(var r=0;r<e.length;++r)l(e[r],t,n);else e.setAttribute(t,n)}function s(e,t){if(e instanceof Array)for(var n=0;n<e.length;++n)s(e[n],t);else e.removeAttribute(t)}function c(e){if(!e.parentElement)throw new Error("target element must be part of the dom");var t=e.getClientRects()[0];return{left:t.left+window.scrollX,right:t.right+window.scrollX,top:t.top+window.scrollY,bottom:t.bottom+window.scrollY}}function f(e,t){if(!(e instanceof HTMLElement&&(t instanceof NodeList||t instanceof HTMLCollection||t instanceof Array)))throw new Error("You must provide an element and a list of elements.");return Array.from(t).indexOf(e)}function d(e){if(!(e instanceof HTMLElement))throw new Error("Element is not a node element.");return null!==e.parentNode}var u=function(e,t,n){if(!(e instanceof HTMLElement&&e.parentElement instanceof HTMLElement))throw new Error("target and element must be a node");e.parentElement.insertBefore(t,"before"===n?e:e.nextElementSibling)},p=function(e,t){return u(e,t,"before")},m=function(e,t){return u(e,t,"after")};function g(e){if(!(e instanceof HTMLElement))throw new Error("You must provide a valid dom element");var t=window.getComputedStyle(e);return["height","padding-top","padding-bottom"].map(function(e){var n=parseInt(t.getPropertyValue(e),10);return isNaN(n)?0:n}).reduce(function(e,t){return e+t})}function h(e,t){if(!(e instanceof Array))throw new Error("You must provide a Array of HTMLElements to be filtered.");return"string"!=typeof t?e:e.filter(function(e){return e.querySelector(t)instanceof HTMLElement}).map(function(e){return e.querySelector(t)})}var v=function(e,t,n){return{element:e,posX:n.pageX-t.left,posY:n.pageY-t.top}};function y(e,t){if(!0===e.isSortable){var n=o(e).getConfig("acceptFrom");if(null!==n&&!1!==n&&"string"!=typeof n)throw new Error('HTML5Sortable: Wrong argument, "acceptFrom" must be "null", "false", or a valid selector string.');if(null!==n)return!1!==n&&n.split(",").filter(function(e){return e.length>0&&t.matches(e)}).length>0;if(e===t)return!0;if(void 0!==o(e).getConfig("connectWith")&&null!==o(e).getConfig("connectWith"))return o(e).getConfig("connectWith")===o(t).getConfig("connectWith")}return!1}var E,b,w,T,L,C,M,x={items:null,connectWith:null,disableIEFix:null,acceptFrom:null,copy:!1,placeholder:null,placeholderClass:"sortable-placeholder",draggingClass:"sortable-dragging",hoverClass:!1,debounce:0,throttleTime:100,maxItems:0,itemSerializer:void 0,containerSerializer:void 0,customDragImage:null};function D(e,n){if("string"==typeof o(e).getConfig("hoverClass")){var r=o(e).getConfig("hoverClass").split(" ");!0===n?(i(e,"mousemove",function(e,t){var n=this;if(void 0===t&&(t=250),"function"!=typeof e)throw new Error("You must provide a function as the first argument for throttle.");if("number"!=typeof t)throw new Error("You must provide a number as the second argument for throttle.");var r=null;return function(){for(var o=[],i=0;i<arguments.length;i++)o[i-0]=arguments[i];var a=Date.now();(null===r||a-r>=t)&&(r=a,e.apply(n,o))}}(function(n){0===n.buttons&&t(e.children,o(e).getConfig("items")).forEach(function(e){var t,o;e!==n.target?(t=e.classList).remove.apply(t,r):(o=e.classList).add.apply(o,r)})},o(e).getConfig("throttleTime"))),i(e,"mouseleave",function(){t(e.children,o(e).getConfig("items")).forEach(function(e){var t;(t=e.classList).remove.apply(t,r)})})):(a(e,"mousemove"),a(e,"mouseleave"))}}var A=function(e){a(e,"dragstart"),a(e,"dragend"),a(e,"dragover"),a(e,"dragenter"),a(e,"drop"),a(e,"mouseenter"),a(e,"mouseleave")},I=function(e,t){var n=e;return!0===o(t).getConfig("copy")&&(l(n=e.cloneNode(!0),"aria-copied","true"),e.parentElement.appendChild(n),n.style.display="none",n.oldDisplay=e.style.display),n};function H(e){for(;!0!==e.isSortable;)e=e.parentElement;return e}function S(n,r){var o=e(n,"opts"),i=t(n.children,o.items).filter(function(e){return e.contains(r)});return i.length>0?i[0]:r}var Y=function(n){var r,o,i=e(n,"opts")||{},l=t(n.children,i.items),c=h(l,i.handle);a(n,"dragover"),a(n,"dragenter"),a(n,"drop"),(o=r=n).h5s&&delete o.h5s.data,s(r,"aria-dropeffect"),a(c,"mousedown"),A(l),function(e){s(e,"aria-grabbed"),s(e,"aria-copied"),s(e,"draggable"),s(e,"role")}(l)},_=function(n){var r=e(n,"opts"),o=t(n.children,r.items),a=h(o,r.handle);(l(n,"aria-dropeffect","move"),e(n,"_disabled","false"),l(a,"draggable","true"),!1===r.disableIEFix)&&("function"==typeof(document||window.document).createElement("span").dragDrop&&i(a,"mousedown",function(){if(-1!==o.indexOf(this))this.dragDrop();else{for(var e=this.parentElement;-1===o.indexOf(e);)e=e.parentElement;e.dragDrop()}}))},O=function(n){var r=e(n,"opts"),o=t(n.children,r.items),i=h(o,r.handle);e(n,"_disabled","false"),A(o),a(i,"mousedown"),a(n,"dragover"),a(n,"dragenter"),a(n,"drop")};function F(r,a){var s=String(a);return a=Object.assign({connectWith:null,acceptFrom:null,copy:!1,placeholder:null,disableIEFix:null,placeholderClass:"sortable-placeholder",draggingClass:"sortable-dragging",hoverClass:!1,debounce:0,maxItems:0,itemSerializer:void 0,containerSerializer:void 0,customDragImage:null,items:null},"object"==typeof a?a:{}),"string"==typeof r&&(r=document.querySelectorAll(r)),r instanceof HTMLElement&&(r=[r]),r=Array.prototype.slice.call(r),/serialize/.test(s)?r.map(function(n){var r=e(n,"opts");return function(n,r,o){if(void 0===r&&(r=function(e,t){return e}),void 0===o&&(o=function(e){return e}),!(n instanceof HTMLElement)||1==!n.isSortable)throw new Error("You need to provide a sortableContainer to be serialized.");if("function"!=typeof r||"function"!=typeof o)throw new Error("You need to provide a valid serializer for items and the container.");var i=e(n,"opts").items,a=t(n.children,i),l=a.map(function(e){return{parent:n,node:e,html:e.outerHTML,index:f(e,a)}});return{container:o({node:n,itemCount:l.length}),items:l.map(function(e){return r(e,n)})}}(n,r.itemSerializer,r.containerSerializer)}):(r.forEach(function(r){if(/enable|disable|destroy/.test(s))return F[s](r);["connectWith","disableIEFix"].forEach(function(e){a.hasOwnProperty(e)&&null!==a[e]&&console.warn('HTML5Sortable: You are using the deprecated configuration "'+e+'". This will be removed in an upcoming version, make sure to migrate to the new options when updating.')}),a=Object.assign({},x,a),o(r).config=a,a=e(r,"opts")||a,e(r,"opts",a),r.isSortable=!0,O(r);var u,h=t(r.children,a.items);if(null!==a.placeholder&&void 0!==a.placeholder){var A=document.createElement(r.tagName);A.innerHTML=a.placeholder,u=A.children[0]}o(r).placeholder=function(e,t,n){if(void 0===n&&(n="sortable-placeholder"),!(e instanceof HTMLElement))throw new Error("You must provide a valid element as a sortable.");if(!(t instanceof HTMLElement)&&void 0!==t)throw new Error("You must provide a valid element as a placeholder or set ot to undefined.");return void 0===t&&(["UL","OL"].includes(e.tagName)?t=document.createElement("li"):["TABLE","TBODY"].includes(e.tagName)?(t=document.createElement("tr")).innerHTML='<td colspan="100"></td>':t=document.createElement("div")),"string"==typeof n&&(r=t.classList).add.apply(r,n.split(" ")),t;var r}(r,u,a.placeholderClass),e(r,"items",a.items),a.acceptFrom?e(r,"acceptFrom",a.acceptFrom):a.connectWith&&e(r,"connectWith",a.connectWith),_(r),l(h,"role","option"),l(h,"aria-grabbed","false"),D(r,!0),i(r,"dragstart",function(e){if(!0!==e.target.isSortable&&(e.stopImmediatePropagation(),(!a.handle||e.target.matches(a.handle))&&"false"!==e.target.getAttribute("draggable"))){var n=H(e.target),r=S(n,e.target);C=t(n.children,a.items),T=C.indexOf(r),L=f(r,n.children),w=n,function(e,t,n){if(!(e instanceof Event))throw new Error("setDragImage requires a DragEvent as the first argument.");if(!(t instanceof HTMLElement))throw new Error("setDragImage requires the dragged element as the second argument.");if(n||(n=v),e.dataTransfer&&e.dataTransfer.setDragImage){var r=n(t,c(t),e);if(!(r.element instanceof HTMLElement)||"number"!=typeof r.posX||"number"!=typeof r.posY)throw new Error("The customDragImage function you provided must return and object with the properties element[string], posX[integer], posY[integer].");e.dataTransfer.effectAllowed="copyMove",e.dataTransfer.setData("text/plain","arbitrary"),e.dataTransfer.setDragImage(r.element,r.posX,r.posY)}}(e,r,a.customDragImage),b=g(r),r.classList.add(a.draggingClass),l(E=I(r,n),"aria-grabbed","true"),n.dispatchEvent(new CustomEvent("sortstart",{detail:{origin:{elementIndex:L,index:T,container:w},item:E}}))}}),i(r,"dragenter",function(n){if(!0!==n.target.isSortable){var i=H(n.target);M=t(i.children,e(i,"items")).filter(function(e){return e!==o(r).placeholder})}}),i(r,"dragend",function(t){if(E){E.classList.remove(a.draggingClass),l(E,"aria-grabbed","false"),"true"===E.getAttribute("aria-copied")&&"true"!==e(E,"dropped")&&E.remove(),E.style.display=E.oldDisplay,delete E.oldDisplay;var o=Array.from(n.values()).map(function(e){return e.placeholder}).filter(function(e){return e instanceof HTMLElement}).filter(d)[0];o&&o.remove(),r.dispatchEvent(new CustomEvent("sortstop",{detail:{origin:{elementIndex:L,index:T,container:w},item:E}})),E=null,b=null}}),i(r,"drop",function(i){if(y(r,E.parentElement)){i.preventDefault(),i.stopPropagation(),e(E,"dropped","true");var l=Array.from(n.values()).map(function(e){return e.placeholder}).filter(function(e){return e instanceof HTMLElement}).filter(d)[0];m(l,E),l.remove(),r.dispatchEvent(new CustomEvent("sortstop",{detail:{origin:{elementIndex:L,index:T,container:w},item:E}}));var s=o(r).placeholder,c=t(w.children,a.items).filter(function(e){return e!==s}),u=!0===this.isSortable?this:this.parentElement,p=t(u.children,e(u,"items")).filter(function(e){return e!==s}),g=f(E,Array.from(E.parentElement.children).filter(function(e){return e!==s})),h=f(E,p);L===g&&w===u||r.dispatchEvent(new CustomEvent("sortupdate",{detail:{origin:{elementIndex:L,index:T,container:w,itemsBeforeUpdate:C,items:c},destination:{index:h,elementIndex:g,container:u,itemsBeforeUpdate:M,items:p},item:E}}))}});var Y,W,j,z=(Y=function(e,r,i){if(E)if(a.forcePlaceholderSize&&(o(e).placeholder.style.height=b+"px"),Array.from(e.children).indexOf(r)>-1){var l=g(r),s=f(o(e).placeholder,r.parentElement.children),d=f(r,r.parentElement.children);if(l>b){var u=l-b,h=c(r).top;if(s<d&&i<h)return;if(s>d&&i>h+l-u)return}void 0===E.oldDisplay&&(E.oldDisplay=E.style.display),"none"!==E.style.display&&(E.style.display="none"),s<d?m(r,o(e).placeholder):p(r,o(e).placeholder),Array.from(n.values()).filter(function(e){return void 0!==e.placeholder}).forEach(function(t){t.placeholder!==o(e).placeholder&&t.placeholder.remove()})}else{var v=Array.from(n.values()).filter(function(e){return void 0!==e.placeholder}).map(function(e){return e.placeholder});-1!==v.indexOf(r)||e!==r||t(r.children,a.items).length||(v.forEach(function(e){return e.remove()}),r.appendChild(o(e).placeholder))}},void 0===(W=a.debounce)&&(W=0),function(){for(var e=[],t=0;t<arguments.length;t++)e[t-0]=arguments[t];clearTimeout(j),j=setTimeout(function(){Y.apply(void 0,e)},W)}),N=function(n){var r=n.target,i=!0===r.isSortable?r:H(r);if(r=S(i,r),E&&y(i,E.parentElement)&&"true"!==e(i,"_disabled")){var a=e(i,"opts");parseInt(a.maxItems)&&t(i.children,e(i,"items")).length>=parseInt(a.maxItems)&&E.parentElement!==i||(n.preventDefault(),n.stopPropagation(),n.dataTransfer.dropEffect=!0===o(i).getConfig("copy")?"copy":"move",z(i,r,n.pageY))}};i(h.concat(r),"dragover",N),i(h.concat(r),"dragenter",N)}),r)}return F.destroy=function(e){Y(e)},F.enable=function(e){_(e)},F.disable=function(n){!function(n){var r=e(n,"opts"),o=h(t(n.children,r.items),r.handle);l(n,"aria-dropeffect","none"),e(n,"_disabled","true"),l(o,"draggable","false"),a(o,"mousedown")}(n)},F}();
//# sourceMappingURL=html5sortable.min.js.map