-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnd-zone.min.js
9 lines (8 loc) · 2.45 KB
/
dnd-zone.min.js
1
2
3
4
5
6
7
8
9
/*!
* DnD Zone
* Author: Mohammad Zahed Kamal
* Author Url: https://zahedkamal.com
* Git: https://github.com/zahedkamal87/dnd-zone
* License: MIT
*/
!function(e,n){"function"==typeof define&&define.amd?define([],n("DnDZone")):"object"==typeof exports?module.exports=n("DnDZone"):e.DnDZone=n("DnDZone")}(window||module||{},function(e){"use strict";const n={};let t=null;const i={beforeFilesAdded:e=>e,afterFilesAdded:e=>{},accept:"",triggerChange:!1};function r(r,d){return t=this,n.name=e,n.element=r,n.defaults=i,n.options=d,n.settings=Object.assign({},i,d),t.initialize(),n}const d=()=>{["drag","dragstart","dragend","dragover","dragenter","dragleave","drop"].forEach(function(e){n.dndzone.addEventListener(e,function(e){e.preventDefault(),e.stopPropagation()})}),["dragover","dragenter"].forEach(function(e){n.dndzone.addEventListener(e,function(e){this.classList.add("dndpicker-dragging")})}),["dragleave","dragend","drop"].forEach(function(e){n.dndzone.addEventListener(e,function(e){this.classList.remove("dndpicker-dragging")})}),["drop"].forEach(function(e){n.dndzone.addEventListener(e,function(e){let i=e.dataTransfer.files,r=new DataTransfer,d=n.multiple;Array.prototype.forEach.call(i,e=>{if((e=>{let t=n.accept;if(!t)return!0;t=t.split(",");let i=e.type,r=i.replace(/\/.*$/,"");for(let n=0;n<t.length;n++){let d=t[n].trim();if("."===d.charAt(0)){if(-1!==e.name.toLowerCase().indexOf(d.toLowerCase(),e.name.length-d.length))return!0}else if(/\/\*$/.test(d)){if(r===d.replace(/\/.*$/,""))return!0}else if(i===d)return!0}return!1})(e)&&(r.items.add(e),!d))return!1});let o=n.settings.beforeFilesAdded,l=r.files;"function"==typeof o&&(l=o.call(t,l)),n.dndzone_input.files=l,n.settings.triggerChange&&n.dndzone_input.dispatchEvent(new Event("change"));let a=n.settings.afterFilesAdded;"function"==typeof a&&a.call(t,l)})}),["click"].forEach(function(e){n.dndzone.addEventListener(e,function(e){n.dndzone_input.click()})})};return r.prototype={initialize:()=>{let t=n.element,i=void 0!==t&&null!=t?t.querySelector(".dndzone-area"):null,r=void 0!==t&&null!=t?t.querySelector(".dndzone-input"):null;if(void 0!==t&&null!=t&&void 0!==i&&null!=i&&void 0!==r&&null!=r){let e=null!=r.getAttribute("accept")?r.getAttribute("accept"):n.settings.accept;r.setAttribute("accept",e),r.style.display="none",n.accept=e,n.multiple=!!r.getAttribute("multiple"),n.dndzone=t,n.dndzone_area=i,n.dndzone_input=r,d()}else console.log("Wrong HTML structure or missing element(s). "+e+" cannot initialize.")}},r});