/**
 * CodeDropz Uploader
 * Copyright 2018 Glen Mongaya
 * CodeDrop Drag&Drop Uploader
 * @version 1.3.9.8
 * @author CodeDropz, Glen Don L. Mongaya
 * @license The MIT License (MIT)
 */

// CodeDropz Drag and Drop Plugin
!function(){let e=function(e){let t=document.querySelector("form.wpcf7-form");if(t){let r=new FormData;r.append("action","_wpcf7_check_nonce"),r.append("_ajax_nonce",dnd_cf7_uploader.ajax_nonce),fetch(dnd_cf7_uploader.ajax_url,{method:"POST",body:r}).then(e=>e.json()).then(({data:e,success:t})=>t&&(dnd_cf7_uploader.ajax_nonce=e)).catch(console.error)}let a=function(e=20){let t=new Uint8Array(16);crypto.getRandomValues(t),t[6]=15&t[6]|64,t[8]=63&t[8]|128;let r=Array.from(t,e=>e.toString(16).padStart(2,"0")).join("");return r.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/,"$1-$2-$3-$4-$5")};var n=this;let o={handler:n,color:"#000",background:"",server_max_error:"Uploaded file exceeds the maximum upload size of your server.",max_file:n.dataset.max?parseInt(n.dataset.max):10,max_upload_size:n.dataset.limit?n.dataset.limit:"10485760",supported_type:n.dataset.type?n.dataset.type:"jpg|jpeg|JPG|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv|xls",text:"Drag & Drop Files Here",separator:"or",button_text:"Browse Files",on_success:""},d=Object.assign({},o,e);var s=n.dataset.name+"_count_files";localStorage.setItem(s,1);var l=dnd_upload_cf7_unique_id(),p=l?localStorage.getItem("dnd_cf7_token_"+l):null;l&&p||(l=a(),p=a(),localStorage.setItem("dnd_wpcf7_session_id",JSON.stringify({value:l,savedAt:Date.now()})),localStorage.setItem("dnd_cf7_token_"+l,p));let c=`
            <div class="codedropz-upload-handler">
                <div class="codedropz-upload-container">
                <div class="codedropz-upload-inner">
                    <${dnd_cf7_uploader.drag_n_drop_upload.tag}>${d.text}</${dnd_cf7_uploader.drag_n_drop_upload.tag}>
                    <span>${d.separator}</span>
                    <div class="codedropz-btn-wrap"><a class="cd-upload-btn" href="#">${d.button_text}</a></div>
                </div>
                </div>
                <span class="dnd-upload-counter"><span>0</span> ${dnd_cf7_uploader.dnd_text_counter} ${parseInt(d.max_file)}</span>
            </div>
        `,i=document.createElement("div");i.classList.add("codedropz-upload-wrapper"),d.handler.parentNode.insertBefore(i,d.handler),i.appendChild(d.handler),d.supported_type=d.supported_type.replace(/[^a-zA-Z0-9| ]/g,"");let u=d.handler.closest("form"),f=d.handler.closest(".codedropz-upload-wrapper"),m=u.querySelector('input[type="submit"], button[type="submit"]');d.handler.insertAdjacentHTML("afterend",c),["drag","dragstart","dragend","dragover","dragenter","dragleave","drop"].forEach(function(e){f.querySelector(".codedropz-upload-handler").addEventListener(e,function(e){e.preventDefault(),e.stopPropagation()})}),["dragover","dragenter"].forEach(function(e){f.querySelector(".codedropz-upload-handler").addEventListener(e,function(e){f.querySelector(".codedropz-upload-handler").classList.add("codedropz-dragover")})}),["dragleave","dragend","drop"].forEach(function(e){f.querySelector(".codedropz-upload-handler").addEventListener(e,function(e){f.querySelector(".codedropz-upload-handler").classList.remove("codedropz-dragover")})}),f.querySelector(".cd-upload-btn").addEventListener("click",function(e){e.preventDefault(),d.handler.value=null,d.handler.click()}),f.querySelector(".codedropz-upload-handler").addEventListener("drop",function(e){v(e.dataTransfer.files,"drop")}),d.handler.addEventListener("change",function(e){v(this.files,"click")}),/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)&&n.removeAttribute("accept");var v=function(e,t){if(0==e.length)return;var r=new FormData;r.append("action","dnd_codedropz_upload"),r.append("type",t),r.append("security",dnd_cf7_uploader.ajax_nonce),r.append("form_id",n.dataset.id),r.append("upload_name",n.dataset.name),r.append("upload_folder",l),r.append("token",p);let a=d.handler.querySelector(".has-error"),o=f.querySelector(".codedropz-upload-handler");for(let c of(a&&a.remove(),e)){if(void 0!==r.delete&&r.delete("upload-file"),Number(localStorage.getItem(s))>d.max_file){if(!f.querySelector("span.has-error-msg")){var i=dnd_cf7_uploader.drag_n_drop_upload.max_file_limit,m=document.createElement("span");m.className="has-error-msg",m.textContent=i.replace("%count%",d.max_file),o.parentNode.insertBefore(m,o.nextSibling)}return!1}let v=g.createProgressBar(c);var y=!1;if(c.size>d.max_upload_size){let h=document.getElementById(v),x=document.createElement("span");x.classList.add("has-error"),x.textContent=dnd_cf7_uploader.drag_n_drop_upload.large_file,h.querySelector(".dnd-upload-details").appendChild(x),y=!0}if(regex_type=RegExp("(.*?).("+d.supported_type+")$"),!1!==y||regex_type.test(c.name.toLowerCase())||(document.querySelector("#"+v+" .dnd-upload-details").insertAdjacentHTML("beforeend",'<span class="has-error">'+dnd_cf7_uploader.drag_n_drop_upload.inavalid_type+"</span>"),y=!0),localStorage.setItem(s,Number(localStorage.getItem(s))+1),!1===y){r.append("upload-file",c);var S=new XMLHttpRequest;let b=document.getElementById(v),$=b.querySelector(".dnd-progress-bar"),q=b.querySelector(".dnd-upload-details"),w=u.querySelector('input[type="submit"], button[type="submit"]');S.open(u.getAttribute("method"),d.ajax_url),S.onreadystatechange=function(){if(4===this.readyState){if(200===this.status){var e=JSON.parse(this.responseText);if(e.success)g.setProgressBar(v,100),"function"==typeof d.on_success&&d.on_success.call(this,n,v,e);else{let t=Number(localStorage.getItem(s))-1,r=n.closest(".codedropz-upload-wrapper").querySelector(".dnd-upload-counter span");$.remove(),q.insertAdjacentHTML("beforeend",'<span class="has-error">'+e.data+"</span>"),w&&w.removeAttribute("disabled"),b.classList.remove("in-progress"),r.textContent=t}}else $.remove(),q.insertAdjacentHTML("beforeend",'<span class="has-error">'+d.server_max_error+"</span>"),w&&w.removeAttribute("disabled"),b.classList.remove("in-progress")}},S.upload.addEventListener("progress",function(e){if(e.lengthComputable){var t=parseInt(100*(e.loaded/e.total));g.setProgressBar(v,t-1)}},!1),S.send(r)}}},g={createProgressBar:function(e){var t=f.querySelector(".codedropz-upload-handler"),r="dnd-file-"+Math.random().toString(36).substr(2,9),a=`
                    <div class="dnd-upload-image">
                        <span class="file"></span>
                    </div>
                    <div class="dnd-upload-details">
                        <span class="name"><span>${e.name}</span><em>(${g.bytesToSize(e.size)})</em></span>
                        <a href="#" title="${dnd_cf7_uploader.drag_n_drop_upload.delete.title}" class="remove-file" data-storage="${s}">
                        <span class="dnd-icon-remove"></span>
                        </a>
                        <span class="dnd-progress-bar"><span></span></span>
                    </div>
                `,n=document.createElement("div");return n.id=r,n.className="dnd-upload-status",n.innerHTML=a,t.parentNode.insertBefore(n,t.nextSibling),r},setProgressBar:function(e,t){let r=document.getElementById(e),a=r.querySelector(".dnd-progress-bar");if(a){m&&g.disableBtn(m);let n=t*a.offsetWidth/100;r.classList.add("in-progress"),100==t?(a.querySelector("span").style.width="100%",a.querySelector("span").textContent=`${t}% `):(a.querySelector("span").style.width=n+"px",a.querySelector("span").textContent=`${t}% `),100==t&&(r.classList.add("complete"),r.classList.remove("in-progress"))}return!1},bytesToSize:function(e){return 0===e?"0":fileSize=(kBytes=e/1024)>=1024?(kBytes/1024).toFixed(2)+"MB":kBytes.toFixed(2)+"KB"},disableBtn:function(e){e&&(e.disabled=!0)}}};document.addEventListener("click",function(e){if(e.target.classList.contains("dnd-icon-remove")){e.preventDefault();var t=e.target,r=t.closest(".dnd-upload-status"),a=t.closest(".codedropz-upload-wrapper"),n=t.parentElement.getAttribute("data-storage"),o=Number(localStorage.getItem(n)),d=dnd_upload_cf7_unique_id();if(r.classList.contains("in-progress")||r.querySelector(".has-error"))return r.remove(),localStorage.setItem(n,o-1),a.querySelector(".dnd-upload-counter span").textContent=Number(localStorage.getItem(n))-1,!1;t.classList.add("deleting"),t.textContent=dnd_cf7_uploader.drag_n_drop_upload.delete.text+"...";var s=new XMLHttpRequest;s.open("POST",dnd_cf7_uploader.ajax_url),s.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),s.onload=function(){if(200===this.status){var e=JSON.parse(this.responseText);if(e.success)r.remove(),localStorage.setItem(n,o-1),a.querySelectorAll(".dnd-upload-status").length<=1&&a.querySelector(".has-error-msg")&&a.querySelector(".has-error-msg").remove(),a.querySelector(".dnd-upload-counter span").textContent=Number(localStorage.getItem(n))-1;else{let t=r.querySelector(".dnd-upload-details");if(t){let d=document.createElement("span");d.classList.add("has-error-msg"),d.textContent=e.data,t.appendChild(d)}}}},s.send("path="+r.querySelector('input[type="hidden"]').value+"&action=dnd_codedropz_upload_delete&security="+dnd_cf7_uploader.ajax_nonce+"&upload_folder="+d+"&token="+localStorage.getItem("dnd_cf7_token_"+d)),document.querySelectorAll(".has-error-msg").forEach(function(e){e.remove()})}}),HTMLElement.prototype.CodeDropz_Uploader=e}();var dnd_upload_cf7_event=function(e,t,r){var a=new CustomEvent("dnd_upload_cf7_"+t,{bubbles:!0,detail:r});e.dispatchEvent(a)};function dnd_upload_cf7_unique_id(){let e=localStorage.getItem("dnd_wpcf7_session_id");if(!e)return null;let t=JSON.parse(e);return Date.now()-t.savedAt>864e5?(localStorage.removeItem("dnd_cf7_token_"+t.value),localStorage.removeItem("dnd_wpcf7_session_id"),null):t.value}document.addEventListener("DOMContentLoaded",function(){document.addEventListener("wpcf7mailsent",function(e){let t=e.target;var r=t.querySelectorAll(".wpcf7-drag-n-drop-file"),a=t.querySelectorAll(".dnd-upload-status"),n=t.querySelector(".dnd-upload-counter span"),o=t.querySelectorAll("span.has-error-msg");r.length>0&&r.forEach(function(e){localStorage.setItem(e.getAttribute("data-name")+"_count_files",1)}),a&&a.forEach(function(e){e.remove()}),n&&(n.textContent="0"),o&&o.forEach(function(e){e.remove()})},!1),window.initDragDrop=function(){var e=dnd_cf7_uploader.drag_n_drop_upload;document.querySelectorAll(".wpcf7-drag-n-drop-file").forEach(function(t){t.CodeDropz_Uploader({color:"#fff",ajax_url:dnd_cf7_uploader.ajax_url,text:e.text,separator:e.or_separator,button_text:e.browse,server_max_error:e.server_max_error,on_success:function(e,t,r){var a=document.querySelector(".codedropz-upload-wrapper #"+t),n=e.closest("form"),o=n.querySelector(".wpcf7-acceptance"),d=o?o.querySelector('input[type="checkbox"]'):"";let s=e.closest(".codedropz-upload-wrapper").nextElementSibling;s&&s.classList.contains("wpcf7-not-valid-tip")&&s.remove(),(o&&o.classList.contains("optional")||!o||d.checked||n.classList.contains("wpcf7-acceptance-as-validation"))&&setTimeout(function(){let e=n.querySelector("button[type=submit], input[type=submit]");e&&e.removeAttribute("disabled")},1);var l=a.querySelector(".dnd-upload-details"),p='<span><input type="hidden" name="'+e.dataset.name+'[]" value="'+r.data.path+"/"+r.data.file+'"></span>';l.insertAdjacentHTML("beforeend",p);var c=Number(localStorage.getItem(e.dataset.name+"_count_files"))-1;e.closest(".codedropz-upload-wrapper").querySelector(".dnd-upload-counter span").textContent=c,dnd_upload_cf7_event(a,"success",r)}})})},window.initDragDrop()});