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 o=jQuery(this).attr("href"),d=!1;if(jQuery(this).hasClass("dlm-no-xhr-download")&&(d=!0),"undefined"!=typeof dlmNonXHRGlobalLinks&&0{0<=o.indexOf(e)&&(d=!0)}),d)jQuery("#dlm-no-access-modal").remove();else{if("string"==typeof o)try{o=new URL(o,window.location.origin).href}catch(e){}var r,s;jQuery(document).trigger("dlm-xhr-download-button-click",[o,this,dlmXHRGlobalLinks]),void 0!==o&&0<=o.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,o){o.stopPropagation();var d=e.getAttribute("href"),d={button:e,href:d,buttonObj:jQuery(e)};-1===d.href.indexOf("blob:http")&&"#"!==d.href&&(o.preventDefault(),dlmXHRinstance.retrieveBlob(d))}retrieveBlob(e){let f=this,{button:h,href:w,buttonObj:x}=e,H,g=new XMLHttpRequest,b=dlmXHR.prevent_duplicates,R=x.attr("target"),y=x.attr("class");y=void 0!==y&&""!==y?y.replace("dlm-download-started","").replace("dlm-download-completed",""):"",x.addClass("dlm-download-started"),h.setAttribute("href","#"),h.removeAttribute("download"),h.setAttribute("disabled","disabled"),dlmXHRProgress&&(e='',h.innerHTML+=e),jQuery(document).trigger("dlm_download_triggered",[this,h,x,H,g]);let C=!1;g.responseType="blob",g.onreadystatechange=function(){var{status:e,readyState:o,statusText:d}=g,r=g.getAllResponseHeaders().split("\r\n").reduce((e,o)=>{var[o,d]=o.split(": ");return e[o]=d,e},{});1-1!==e.indexOf("dlm-")).length)return g.abort(),void(window.location.href=w);if(i&&"true"===i&&m&&0!=m)return dlmXHRinstance.dlmNoAccessModal(f.responseHeaders),h.removeAttribute("download"),h.setAttribute("href",w),x.removeClass().addClass(y).find("span.dlm-xhr-progress").remove(),x.find(".dlm-xhr-loading-gif").remove(),void g.abort();if(c&&""!==c&&null!==c)return dlmXHRinstance.dlmLogDownload(f.responseHeaders,"failed",!1),h.removeAttribute("download"),h.setAttribute("href",w),x.removeClass().addClass(y).find("span.dlm-xhr-progress").remove(),x.find(".dlm-xhr-loading-gif").remove(),g.abort(),void(m&&0!=m?dlmXHRinstance.dlmNoAccessModal(f.responseHeaders):(x.find(".dlm-xhr-error").remove(),x.append(''+c+"")));if(a&&""!==a&&null!==a)return dlmXHRinstance.dlmLogDownload(f.responseHeaders,"redirected",!1,a,i,R),h.removeAttribute("download"),h.setAttribute("href",w),x.removeClass().addClass(y).find("span.dlm-xhr-progress").remove(),x.find(".dlm-xhr-loading-gif").remove(),void g.abort()}404==e&&2==o&&((r=document.createElement("p")).innerHTML=d,h.parentNode.appendChild(r)),401==e&&2==o?window.location.href=d:(403==e&&2==o&&((r=document.createElement("p")).innerHTML=d,h.parentNode.appendChild(r)),200==e&&4==o&&(d=g.response,H=URL.createObjectURL(d),h.removeEventListener("click",dlmXHRinstance.handleDownloadClick),h.setAttribute("download",""+s),h.setAttribute("href",H),h.click(),x.removeClass().addClass(y+" dlm-download-complete"),dlmXHRinstance.attachButtonEvent(),jQuery(document).trigger("dlm_download_complete",[this,h,x,H]),dlmXHRinstance.dlmLogDownload(f.responseHeaders,"completed",b),window.URL.revokeObjectURL(H),h.removeAttribute("download"),h.setAttribute("href",w),x.find(".dlm-xhr-loading-gif").remove(),setTimeout(function(){x.removeClass().addClass(y).find("span.dlm-xhr-progress").remove()},4e3)))},dlmXHRProgress&&g.addEventListener("progress",function(e){let o=e.total,d=(void 0!==e.total&&"undefined"!==e.total&&0!==e.total||void 0!==f.responseHeaders["x-dlm-filesize"]&&(o=f.responseHeaders["x-dlm-filesize"]),e.loaded/o*100);d=d.toFixed();var r;x.find("span.dlm-xhr-progress").remove(),r="dlm-download-started download-"+10*Math.ceil(d/10),1/0!=d&&x.append(' '+d+"%"),x.removeClass().addClass(y+" "+r),jQuery(document).trigger("dlm_download_progress",[this,h,x,H,e,d])}),g.onerror=function(){h.removeAttribute("download"),h.setAttribute("href",w),x.removeClass().addClass(y+" dlm-no-xhr-download").find("span.dlm-xhr-progress").remove(),x.find(".dlm-xhr-error").remove(),x.append(''+dlmXHRtranslations.error+""),console.log("** An error occurred during the transaction")},g.open("GET",w,!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,o,d,r=null,s=null,n="_self"){var t=window.location.href,a=e["x-dlm-download-id"]??e["dlm-download-id"],l=e["x-dlm-version-id"]??e["dlm-version-id"],i=navigator.userAgent||"",i=/^((?!chrome|android|crios|fxios|edgios).)*safari/i.test(i)&&!/CriOS|FxiOS|EdgiOS/i.test(i);if(s&&r)if(i)window.location.href=r;else{let e=n||"_self",o=window.open(r,e,"noopener");o&&(o.location.href=r)}else{var m,c,p=new URLSearchParams({action:"log_dlm_xhr_download",download_id:a,version_id:l,status:o,cookie:d,currentURL:t,nonce:e["x-dlm-nonce"]});for([m,c]of Object.entries(e))p.append(`responseHeaders[${m}]`,c);try{navigator.sendBeacon(dlmXHR.ajaxUrl,new Blob([p.toString()],{type:"application/x-www-form-urlencoded;charset=UTF-8"}))}catch(e){try{fetch(dlmXHR.ajaxUrl,{method:"POST",body:p,keepalive:!0})}catch(e){}}if(r)if(i)window.location.href(r);else{let e=n||"_self",o=window.open(r,e,"noopener");s&&o&&(o.location.href=r)}}}dlmNoAccessModal(e){let o="empty-download",d="empty-version",r="empty-restriction",s="",t=(void 0!==e["dlm-download-id"]&&(o=e["dlm-download-id"]),void 0!==e["dlm-version-id"]&&(d=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"]&&(o=e["x-dlm-download-id"]),void 0!==e["x-dlm-version-id"]&&(d=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:o,version_id:d,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 o=document.createElement("div"),d=(o.innerHTML=e,/]*>([\s\S]*?)<\/script>/gm);for(o.innerHTML=o.innerHTML.replace(d,"");null!==(s=d.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,o.querySelector("#dlm-no-access-modal"));(r||document.body).appendChild(n)}for(;o.firstChild;)document.body.appendChild(o.firstChild);jQuery(document).trigger(t.action,[e,t]),document.dispatchEvent(new CustomEvent(t.action,{detail:[e,t]}))})}dlmExternalDownload(d,s,n,r,t){let a=new XMLHttpRequest,l=(n.attr("target"),n.attr("class")),i,e="";void 0!==d["dlm-external-download"]&&(e=d["dlm-external-download"]),void 0!==d["x-dlm-external-download"]&&(e=d["x-dlm-external-download"]),l=void 0!==l&&""!==l?l.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,a]),a.responseType="blob",a.onreadystatechange=function(){var{status:e,readyState:o}=a;403===e?(dlmXHRinstance.dlmLogDownload(d,"failed",!1),a.abort(),n.find(".dlm-xhr-error").remove(),n.append('Acces Denied to file.')):200==e&&4==o&&(e=a.response,i=URL.createObjectURL(e),s.removeEventListener("click",dlmXHRinstance.handleDownloadClick),s.setAttribute("download",""+r),s.setAttribute("href",i),s.click(),n.removeClass().addClass(l+" dlm-download-complete"),dlmXHRinstance.attachButtonEvent(),jQuery(document).trigger("dlm_download_complete",[this,s,n,i]),dlmXHRinstance.dlmLogDownload(d,"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(l).find("span.dlm-xhr-progress").remove()},1e3))},dlmXHRProgress&&a.addEventListener("progress",function(e){let o=e.total,d=(void 0!==e.total&&"undefined"!==e.total||(o=a.getResponseHeader("X-DLM-Filesize")),e.loaded/o*100);d=d.toFixed();var r;n.find("span.dlm-xhr-progress").remove(),r="dlm-download-started download-"+10*Math.ceil(d/10),1/0!=d&&n.append(' '+d+"%"),n.removeClass().addClass(l+" "+r),jQuery(document).trigger("dlm_download_progress",[this,s,n,i,e,d])}),a.onerror=function(){s.removeAttribute("download"),s.setAttribute("href",t),n.removeClass().addClass(l+" .dlm-no-xhr-download").find("span.dlm-xhr-progress").remove(),n.find(".dlm-xhr-error").remove(),n.append(''+dlmXHRtranslations.error+""),console.log("** An error occurred during the transaction")},a.open("GET",e,!0),a.setRequestHeader("Cache-Control","no-store, no-cache, no-transform, max-age=0"),a.setRequestHeader("dlm-xhr-request","dlm_XMLHttpRequest"),a.send()}}