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
JavaScript
(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{TinyDragDropDetector:()=>r});const r=class{#e;#t;#r;#n;#a;#s;#o;#i;constructor(e={}){const{target:t,fullscreen:r=!0,hoverClass:n="dnd-hover",onDrop:a,onEnter:s,onLeave:o}=e;if("boolean"!=typeof r)throw new TypeError('The "fullscreen" option must be a boolean.');const i=r?document.body:t||document.body;if(!(i instanceof HTMLElement))throw new TypeError('The "target" option must be an instance of HTMLElement.');if("string"!=typeof n)throw new TypeError('The "hoverClass" option must be a string.');if("function"!=typeof a)throw new TypeError('The "onDrop" option must be a function.');if("function"!=typeof s)throw new TypeError('The "onEnter" option must be a function.');if("function"!=typeof o)throw new TypeError('The "onLeave" option must be a function.');this.#e=i,this.#t=r,this.#r=n,this.#n=a||(()=>{}),this.#a=s,this.#s=o,this.#o=!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.#h()}getTarget(){return this.#e}getHoverClass(){return this.#r}isFullScreen(){return this.#t}isDragging(){return this.#o}bound(){return this.#i}#h(){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}#l(){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.#o){const t=this.getTarget();this.#o=!0,t.classList.add(this.#r),this.#a(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.#o=!1,t.classList.remove(this.#r),this.#s(e))}_handleDrop(e){if(e.preventDefault(),!e.dataTransfer)return void console.warn("[TinyDragDropDetector] [handleDrop] DragOver event missing dataTransfer.");const t=this.getTarget();this.#o=!1,t.classList.remove(this.#r);const r=e.dataTransfer.files;r.length>0&&this.#n(r,e)}destroy(){this.#l(),this.getTarget().classList.remove(this.#r)}};window.TinyDragDropDetector=t.TinyDragDropDetector})();