jQuery(function(e){new DLM_XHR_Download});class DLM_XHR_Download{responsHeaders={};constructor(){(dlmXHRinstance=this).init()}init(){dlmXHRinstance.attachButtonEvent()}attachButtonEvent(){jQuery("html,body").on("click",".dlm-no-access-modal-window > div",function(e){e.stopPropagation()}),jQuery("html, body").on("click",".dlm-no-access-modal-overlay, .dlm-no-access-modal-close, .dlm-no-access-modal-window",function(e){e.stopPropagation(),jQuery("#dlm-no-access-modal").remove()}),jQuery(document).on("keydown",function(e){"Escape"===e.key&&jQuery("#dlm-no-access-modal").remove()}),jQuery("html, body").on("click","a",function(e){let d=jQuery(this).attr("href"),o=!1;var r,s;jQuery(this).hasClass("dlm-no-xhr-download")&&(o=!0),"undefined"!=typeof dlmNonXHRGlobalLinks&&0<dlmNonXHRGlobalLinks.length&&void 0!==d&&dlmNonXHRGlobalLinks.forEach(e=>{0<=d.indexOf(e)&&(o=!0)}),o?jQuery("#dlm-no-access-modal").remove():(jQuery(document).trigger("dlm-xhr-download-button-click",[d,this,dlmXHRGlobalLinks]),void 0!==d&&0<=d.indexOf(dlmXHRGlobalLinks)&&(r=jQuery(this).data("redirect"),s=jQuery(this).attr("target"),void 0!==r&&!0===r&&"_blank"===s||dlmXHRinstance.handleDownloadClick(this,e)))})}handleDownloadClick(e,d){d.stopPropagation();var o=e.getAttribute("href"),o={button:e,href:o,buttonObj:jQuery(e)};-1===o.href.indexOf("blob:http")&&"#"!==o.href&&(d.preventDefault(),dlmXHRinstance.retrieveBlob(o))}retrieveBlob(e){let h=this,{button:f,href:x,buttonObj:H}=e,w,g=new XMLHttpRequest,b=dlmXHR.prevent_duplicates,R=H.attr("target"),y=H.attr("class");y=void 0!==y&&""!==y?y.replace("dlm-download-started","").replace("dlm-download-completed",""):"",H.addClass("dlm-download-started"),f.setAttribute("href","#"),f.removeAttribute("download"),f.setAttribute("disabled","disabled"),dlmXHRProgress&&(e='<img src="'+dlmXHRgif+'" class="dlm-xhr-loading-gif" style="display:inline-block; vertical-align: middle; margin-left:15px;">',f.innerHTML+=e),jQuery(document).trigger("dlm_download_triggered",[this,f,H,w,g]);let C=!1;g.responseType="blob",g.onreadystatechange=function(){var{status:e,readyState:d,statusText:o}=g,r=g.getAllResponseHeaders().split("\r\n").reduce((e,d)=>{var[d,o]=d.split(": ");return e[d]=o,e},{});1<Object.keys(r).length&&(C=!0,h.responseHeaders=r);let s="download",n=!1,t=!1,l=!1,a=!1,i=null,m=!1,c=!1,p=!1,v=!1,u=!1;if(C&&void 0!==h.responseHeaders["dlm-file-name"]&&(n=h.responseHeaders["dlm-file-name"]),C&&void 0!==h.responseHeaders["dlm-no-waypoints"]&&(t=!0),C&&void 0!==h.responseHeaders["dlm-redirect"]&&(l=h.responseHeaders["dlm-redirect"]),C&&void 0!==h.responseHeaders["dlm-external-download"]&&(a=!0),C&&void 0!==h.responseHeaders["dlm-no-access"]&&(i=h.responseHeaders["dlm-no-access"]),C&&void 0!==h.responseHeaders["dlm-no-access-modal"]&&(m=h.responseHeaders["dlm-no-access-modal"]),C&&void 0!==h.responseHeaders["dlm-error"]&&(c=h.responseHeaders["dlm-error"]),C&&void 0!==h.responseHeaders["dlm-download-id"]&&(p=h.responseHeaders["dlm-download-id"]),C&&void 0!==h.responseHeaders["dlm-version-id"]&&(v=h.responseHeaders["dlm-version-id"]),C&&void 0!==h.responseHeaders["dlm-no-access-modal-text"]&&(u=h.responseHeaders["dlm-no-access-modal-text"]),C&&void 0!==h.responseHeaders["x-dlm-file-name"]&&(n=h.responseHeaders["x-dlm-file-name"]),C&&void 0!==h.responseHeaders["x-dlm-no-waypoints"]&&(t=!0),C&&void 0!==h.responseHeaders["x-dlm-redirect"]&&(l=h.responseHeaders["x-dlm-redirect"]),C&&void 0!==h.responseHeaders["x-dlm-external-download"]&&(a=!0),C&&void 0!==h.responseHeaders["x-dlm-no-access"]&&(i=h.responseHeaders["x-dlm-no-access"]),C&&void 0!==h.responseHeaders["x-dlm-no-access-modal"]&&(m=h.responseHeaders["x-dlm-no-access-modal"]),C&&void 0!==h.responseHeaders["x-dlm-error"]&&(c=h.responseHeaders["x-dlm-error"]),C&&void 0!==h.responseHeaders["x-dlm-download-id"]&&(p=h.responseHeaders["x-dlm-download-id"]),C&&void 0!==h.responseHeaders["x-dlm-version-id"]&&(v=h.responseHeaders["x-dlm-version-id"]),C&&void 0!==h.responseHeaders["x-dlm-no-access-modal-text"]&&(u=h.responseHeaders["x-dlm-no-access-modal-text"]),n?(s=n.replace(/\"/g,"").replace(";",""),s=decodeURI(s)):C&&void 0!==h.responseHeaders["content-disposition"]&&(s=(s=h.responseHeaders["content-disposition"].split(/(?:filename\*=UTF-8'')|(?:filename=)/)[1]).replace(/\"/g,"").replace(";",""),s=decodeURI(s)),c=c&&(c in dlmXHRtranslations?dlmXHRtranslations[c]:dlmXHRtranslations.error),2===g.readyState){if(C&&void 0!==h.responseHeaders["x-dlm-force-abort"]&&""!==h.responseHeaders["x-dlm-force-abort"])return f.removeAttribute("download"),f.setAttribute("href",x),H.removeClass().addClass(y).find("span.dlm-xhr-progress").remove(),H.find(".dlm-xhr-loading-gif").remove(),g.abort(),void jQuery("#dlm-no-access-modal").remove();if(t)return g.abort(),l?void(window.location.href=l):void(window.location.href=x);if(a)return g.abort(),void dlmXHRinstance.dlmExternalDownload(h.responseHeaders,f,H,s,x);if(0===Object.keys(h.responseHeaders).filter(e=>-1!==e.indexOf("dlm-")).length)return g.abort(),void(window.location.href=x);if(i&&"true"===i&&m&&0!=m)return dlmXHRinstance.dlmNoAccessModal(h.responseHeaders),f.removeAttribute("download"),f.setAttribute("href",x),H.removeClass().addClass(y).find("span.dlm-xhr-progress").remove(),H.find(".dlm-xhr-loading-gif").remove(),void g.abort();if(c&&""!==c&&null!==c)return dlmXHRinstance.dlmLogDownload(h.responseHeaders,"failed",!1),f.removeAttribute("download"),f.setAttribute("href",x),H.removeClass().addClass(y).find("span.dlm-xhr-progress").remove(),H.find(".dlm-xhr-loading-gif").remove(),g.abort(),void(m&&0!=m?dlmXHRinstance.dlmNoAccessModal(h.responseHeaders):(H.find(".dlm-xhr-error").remove(),H.append('<span class="dlm-xhr-error">'+c+"</span>")));if(l&&""!==l&&null!==l)return dlmXHRinstance.dlmLogDownload(h.responseHeaders,"redirected",!1,l,i,R),f.removeAttribute("download"),f.setAttribute("href",x),H.removeClass().addClass(y).find("span.dlm-xhr-progress").remove(),H.find(".dlm-xhr-loading-gif").remove(),void g.abort()}404==e&&2==d&&((r=document.createElement("p")).innerHTML=o,f.parentNode.appendChild(r)),401==e&&2==d?window.location.href=o:(403==e&&2==d&&((r=document.createElement("p")).innerHTML=o,f.parentNode.appendChild(r)),200==e&&4==d&&(o=g.response,w=URL.createObjectURL(o),f.removeEventListener("click",dlmXHRinstance.handleDownloadClick),f.setAttribute("download",""+s),f.setAttribute("href",w),f.click(),H.removeClass().addClass(y+" dlm-download-complete"),dlmXHRinstance.attachButtonEvent(),jQuery(document).trigger("dlm_download_complete",[this,f,H,w]),dlmXHRinstance.dlmLogDownload(h.responseHeaders,"completed",b),window.URL.revokeObjectURL(w),f.removeAttribute("download"),f.setAttribute("href",x),H.find(".dlm-xhr-loading-gif").remove(),setTimeout(function(){H.removeClass().addClass(y).find("span.dlm-xhr-progress").remove()},4e3)))},dlmXHRProgress&&g.addEventListener("progress",function(e){let d=e.total,o=(void 0!==e.total&&"undefined"!==e.total&&0!==e.total||void 0!==h.responseHeaders["x-dlm-filesize"]&&(d=h.responseHeaders["x-dlm-filesize"]),e.loaded/d*100);o=o.toFixed();var r;H.find("span.dlm-xhr-progress").remove(),r="dlm-download-started download-"+10*Math.ceil(o/10),1/0!=o&&H.append('<span class="dlm-xhr-progress">&nbsp;'+o+"%</span>"),H.removeClass().addClass(y+" "+r),jQuery(document).trigger("dlm_download_progress",[this,f,H,w,e,o])}),g.onerror=function(){f.removeAttribute("download"),f.setAttribute("href",x),H.removeClass().addClass(y+" dlm-no-xhr-download").find("span.dlm-xhr-progress").remove(),H.find(".dlm-xhr-error").remove(),H.append('<span class="dlm-xhr-error">'+dlmXHRtranslations.error+"</span>"),console.log("** An error occurred during the transaction")},g.open("GET",x,!0),g.setRequestHeader("Cache-Control","no-store, no-cache, no-transform, max-age=0"),g.setRequestHeader("dlm-xhr-request","dlm_XMLHttpRequest"),g.send()}dlmLogDownload(e,d,o,r=null,s=null,n="_self"){var t,l;null!==s?window.open(r,n):(s=window.location.href,t=void 0!==e["x-dlm-download-id"]?e["x-dlm-download-id"]:e["dlm-download-id"],l=void 0!==e["x-dlm-version-id"]?e["x-dlm-version-id"]:e["dlm-version-id"],jQuery.post(dlmXHR.ajaxUrl,{download_id:t,version_id:l,status:d,cookie:o,currentURL:s,action:"log_dlm_xhr_download",responseHeaders:e,nonce:e["x-dlm-nonce"]},function(e){null!==r&&(null==n&&(n="_self"),window.open(r,n))}))}dlmNoAccessModal(e){let d="empty-download",o="empty-version",r="empty-restriction",s="",t=(void 0!==e["dlm-download-id"]&&(d=e["dlm-download-id"]),void 0!==e["dlm-version-id"]&&(o=e["dlm-version-id"]),void 0!==e["dlm-no-access-modal-text"]&&(s=e["dlm-no-access-modal-text"]),void 0!==e["dlm-no-access-restriction"]&&(r=e["dlm-no-access-restriction"]),void 0!==e["x-dlm-download-id"]&&(d=e["x-dlm-download-id"]),void 0!==e["x-dlm-version-id"]&&(o=e["x-dlm-version-id"]),void 0!==e["x-dlm-no-access-modal-text"]&&(s=e["x-dlm-no-access-modal-text"]),void 0!==e["x-dlm-no-access-restriction"]&&(r=e["x-dlm-no-access-restriction"]),{download_id:d,version_id:o,modal_text:s,restriction:r,action:"no_access_dlm_xhr_download",nonce:e["x-dlm-nonce"]});jQuery(document).trigger("dlm-xhr-modal-data",[t,e]),document.dispatchEvent(new CustomEvent("dlm-xhr-modal-data",{detail:{data:t,headers:e}})),jQuery.post(dlmXHR.ajaxUrl,t,function(e){jQuery("#dlm-no-access-modal").remove();var d=document.createElement("div"),o=(d.innerHTML=e,/<script\b[^>]*>([\s\S]*?)<\/script>/gm);for(d.innerHTML=d.innerHTML.replace(o,"");null!==(s=o.exec(e));){var r=s[0],s=s[1],n=document.createElement("script"),r=r.match(/src=["']([^"']+)["']/),r=(r?n.src=r[1]:n.textContent=s,d.querySelector("#dlm-no-access-modal"));console.error(n),(r||document.body).appendChild(n)}for(;d.firstChild;)document.body.appendChild(d.firstChild);jQuery(document).trigger(t.action,[e,t]),document.dispatchEvent(new CustomEvent(t.action,{detail:[e,t]}))})}dlmExternalDownload(o,s,n,r,t){let l=new XMLHttpRequest,a=(n.attr("target"),n.attr("class")),i,e="";void 0!==o["dlm-external-download"]&&(e=o["dlm-external-download"]),void 0!==o["x-dlm-external-download"]&&(e=o["x-dlm-external-download"]),a=void 0!==a&&""!==a?a.replace("dlm-download-started","").replace("dlm-download-completed",""):"",n.addClass("dlm-download-started"),s.setAttribute("href","#"),s.removeAttribute("download"),s.setAttribute("disabled","disabled"),jQuery(document).trigger("dlm_download_triggered",[this,s,n,i,l]),l.responseType="blob",l.onreadystatechange=function(){var{status:e,readyState:d}=l;403===e?(dlmXHRinstance.dlmLogDownload(o,"failed",!1),l.abort(),n.find(".dlm-xhr-error").remove(),n.append('<span class="dlm-xhr-error">Acces Denied to file.</span>')):200==e&&4==d&&(e=l.response,i=URL.createObjectURL(e),s.removeEventListener("click",dlmXHRinstance.handleDownloadClick),s.setAttribute("download",""+r),s.setAttribute("href",i),s.click(),n.removeClass().addClass(a+" dlm-download-complete"),dlmXHRinstance.attachButtonEvent(),jQuery(document).trigger("dlm_download_complete",[this,s,n,i]),dlmXHRinstance.dlmLogDownload(o,"completed",!1),window.URL.revokeObjectURL(i),s.removeAttribute("download"),s.setAttribute("href",t),n.find(".dlm-xhr-loading-gif").remove(),setTimeout(function(){n.removeClass().addClass(a).find("span.dlm-xhr-progress").remove()},1e3))},dlmXHRProgress&&l.addEventListener("progress",function(e){let d=e.total,o=(void 0!==e.total&&"undefined"!==e.total||(d=l.getResponseHeader("X-DLM-Filesize")),e.loaded/d*100);o=o.toFixed();var r;n.find("span.dlm-xhr-progress").remove(),r="dlm-download-started download-"+10*Math.ceil(o/10),1/0!=o&&n.append('<span class="dlm-xhr-progress">&nbsp;'+o+"%</span>"),n.removeClass().addClass(a+" "+r),jQuery(document).trigger("dlm_download_progress",[this,s,n,i,e,o])}),l.onerror=function(){s.removeAttribute("download"),s.setAttribute("href",t),n.removeClass().addClass(a+" .dlm-no-xhr-download").find("span.dlm-xhr-progress").remove(),n.find(".dlm-xhr-error").remove(),n.append('<span class="dlm-xhr-error">'+dlmXHRtranslations.error+"</span>"),console.log("** An error occurred during the transaction")},l.open("GET",e,!0),l.setRequestHeader("Cache-Control","no-store, no-cache, no-transform, max-age=0"),l.setRequestHeader("dlm-xhr-request","dlm_XMLHttpRequest"),l.send()}};