UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

1 lines 2.81 kB
(()=>{"use strict";const e={d:(t,r)=>{if(Array.isArray(r))for(var n=0;n<r.length;){var a=r[n++],s=r[n++];e.o(t,a)?0===s&&n++:0===s?Object.defineProperty(t,a,{enumerable:!0,value:r[n++]}):Object.defineProperty(t,a,{enumerable:!0,get:s})}else for(var a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};let t={};e.d(t,{TinyDragDropDetector:()=>r});const r=class{#e;#t;#r;#n;#a;#s;#i;constructor(e,t){const{hoverClass:r="dnd-hover",onDrop:n,onEnter:a,onLeave:s}=t;if(!(e instanceof HTMLElement))throw new TypeError('The "target" option must be an instance of HTMLElement.');if("string"!=typeof r)throw new TypeError('The "hoverClass" option must be a string.');if("function"!=typeof n)throw new TypeError('The "onDrop" option must be a function.');if("function"!=typeof a)throw new TypeError('The "onEnter" option must be a function.');if("function"!=typeof s)throw new TypeError('The "onLeave" option must be a function.');this.#e=e,this.#t=r,this.#r=n||(()=>{}),this.#n=a,this.#a=s,this.#s=!1,this.#i=!1,this._handleDragEnter=this._handleDragEnter.bind(this),this._handleDragOver=this._handleDragOver.bind(this),this._handleDragLeave=this._handleDragLeave.bind(this),this._handleDrop=this._handleDrop.bind(this),this.#o()}getTarget(){return this.#e}getHoverClass(){return this.#t}isDragging(){return this.#s}bound(){return this.#i}#o(){if(this.#i)return;const e=this.getTarget();e.addEventListener("dragenter",this._handleDragEnter),e.addEventListener("dragover",this._handleDragOver),e.addEventListener("dragleave",this._handleDragLeave),e.addEventListener("drop",this._handleDrop),this.#i=!0}#h(){if(!this.#i)return;const e=this.getTarget();e.removeEventListener("dragenter",this._handleDragEnter),e.removeEventListener("dragover",this._handleDragOver),e.removeEventListener("dragleave",this._handleDragLeave),e.removeEventListener("drop",this._handleDrop),this.#i=!1}_handleDragEnter(e){if(e.preventDefault(),!this.#s){const t=this.getTarget();this.#s=!0,t.classList.add(this.#t),this.#n(e)}}_handleDragOver(e){e.preventDefault(),e.dataTransfer?e.dataTransfer.dropEffect="copy":console.warn("[TinyDragDropDetector] [handleDragOver] DragOver event missing dataTransfer.")}_handleDragLeave(e){e.preventDefault();const t=this.getTarget();null!==e.relatedTarget&&t.contains(e.relatedTarget)||(this.#s=!1,t.classList.remove(this.#t),this.#a(e))}_handleDrop(e){if(e.preventDefault(),!e.dataTransfer)return void console.warn("[TinyDragDropDetector] [handleDrop] DragOver event missing dataTransfer.");const t=this.getTarget();this.#s=!1,t.classList.remove(this.#t);const r=e.dataTransfer.files;r.length>0&&this.#r(r,e)}destroy(){this.#h(),this.getTarget().classList.remove(this.#t)}};window.TinyDragDropDetector=t.TinyDragDropDetector})();