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-overlay, .dlm-no-access-modal-close",function(e){jQuery("#dlm-no-access-modal").remove()}),jQuery("html, body").on("click","a",function(e){const d=jQuery(this).attr("href");let o=!1;var r,s;jQuery(this).hasClass("dlm-no-xhr-download")&&(o=!0),"undefined"!=typeof dlmNonXHRGlobalLinks&&0{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");let r={button:e,href:o,buttonObj:jQuery(e)};-1===r.href.indexOf("blob:http")&&"#"!==r.href&&(d.preventDefault(),dlmXHRinstance.retrieveBlob(r))}retrieveBlob(e){const f=this;let{button:x,href:h,buttonObj:H}=e,w;const g=new XMLHttpRequest,b=dlmXHR.prevent_duplicates,R=H.attr("target");let y=H.attr("class");y=void 0!==y&&""!==y?y.replace("dlm-download-started","").replace("dlm-download-completed",""):"",H.addClass("dlm-download-started"),x.setAttribute("href","#"),x.removeAttribute("download"),x.setAttribute("disabled","disabled"),dlmXHRProgress&&(e='',x.innerHTML+=e),jQuery(document).trigger("dlm_download_triggered",[this,x,H,w,g]);let X=!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-1!==e.indexOf("dlm-")).length)return g.abort(),void(window.location.href=h);if(i&&"true"===i&&m&&0!=m)return dlmXHRinstance.dlmNoAccessModal(f.responseHeaders),x.removeAttribute("download"),x.setAttribute("href",h),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(f.responseHeaders,"failed",!1),x.removeAttribute("download"),x.setAttribute("href",h),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(f.responseHeaders):(H.find(".dlm-xhr-error").remove(),H.append(''+c+"")));if(l&&""!==l&&null!==l)return dlmXHRinstance.dlmLogDownload(f.responseHeaders,"redirected",!1,l,i,R),x.removeAttribute("download"),x.setAttribute("href",h),H.removeClass().addClass(y).find("span.dlm-xhr-progress").remove(),H.find(".dlm-xhr-loading-gif").remove(),void g.abort()}if(404==e&&2==d){let e=document.createElement("p");e.innerHTML=o,x.parentNode.appendChild(e)}if(401==e&&2==d)window.location.href=o;else{if(403==e&&2==d){let e=document.createElement("p");e.innerHTML=o,x.parentNode.appendChild(e)}200==e&&4==d&&(r=g.response,w=URL.createObjectURL(r),x.removeEventListener("click",dlmXHRinstance.handleDownloadClick),x.setAttribute("download",""+s),x.setAttribute("href",w),x.click(),H.removeClass().addClass(y+" dlm-download-complete"),dlmXHRinstance.attachButtonEvent(),jQuery(document).trigger("dlm_download_complete",[this,x,H,w]),dlmXHRinstance.dlmLogDownload(f.responseHeaders,"completed",b),window.URL.revokeObjectURL(w),x.removeAttribute("download"),x.setAttribute("href",h),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!==f.responseHeaders["x-dlm-filesize"]&&(d=f.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(' '+o+"%"),H.removeClass().addClass(y+" "+r),jQuery(document).trigger("dlm_download_progress",[this,x,H,w,e,o])}),g.onerror=function(){x.removeAttribute("download"),x.setAttribute("href",h),H.removeClass().addClass(y+" dlm-no-xhr-download").find("span.dlm-xhr-progress").remove(),H.find(".dlm-xhr-error").remove(),H.append(''+dlmXHRtranslations.error+""),console.log("** An error occurred during the transaction")},g.open("GET",h,!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"){null!==s?window.open(r,n):(s=window.location.href,d={download_id:void 0!==e["x-dlm-download-id"]?e["x-dlm-download-id"]:e["dlm-download-id"],version_id:void 0!==e["x-dlm-version-id"]?e["x-dlm-version-id"]:e["dlm-version-id"],status:d,cookie:o,currentURL:s,action:"log_dlm_xhr_download",responseHeaders:e,nonce:e["x-dlm-nonce"]},jQuery.post(dlmXHR.ajaxUrl,d,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="",n=(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",[n,e]),jQuery.post(dlmXHR.ajaxUrl,n,function(e){jQuery("#dlm-no-access-modal").remove(),jQuery("body").append(e),jQuery(document).trigger(n.action,[e,n]),document.dispatchEvent(new CustomEvent(n.action,{detail:[e,n]}))})}dlmExternalDownload(o,s,n,r,t){const l=new XMLHttpRequest;n.attr("target");let a=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;if(403===e)return dlmXHRinstance.dlmLogDownload(o,"failed",!1),l.abort(),n.find(".dlm-xhr-error").remove(),void n.append('Acces Denied to file.');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(' '+o+"%"),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(''+dlmXHRtranslations.error+""),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()}}