jQuery(function(e){new DLM_XHR_Download});class DLM_XHR_Download{xhrNonce=!1;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;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():void 0!==d&&0<=d.indexOf(dlmXHRGlobalLinks)&&dlmXHRinstance.handleDownloadClick(this,e)})}handleDownloadClick(e,d){d.stopPropagation();var o=e.getAttribute("href");let t={button:e,href:o,buttonObj:jQuery(e)};-1===t.href.indexOf("blob:http")&&"#"!==t.href&&(d.preventDefault(),dlmXHRinstance.retrieveBlob(t))}retrieveBlob(e){const h=this;let{button:x,href:f,buttonObj:w}=e,g;dlmXHRinstance.request=new XMLHttpRequest;const b=dlmXHR.prevent_duplicates,R=w.attr("target");let H=w.attr("class");H=void 0!==H&&""!==H?H.replace("dlm-download-started","").replace("dlm-download-completed",""):"",w.addClass("dlm-download-started"),x.setAttribute("href","#"),x.removeAttribute("download"),x.setAttribute("disabled","disabled");e='';x.innerHTML+=e,jQuery(document).trigger("dlm_download_triggered",[this,x,w,g,dlmXHRinstance.request]),dlmXHRinstance.request.responseType="blob",dlmXHRinstance.request.onreadystatechange=function(){var{status:e,readyState:d,statusText:o}=dlmXHRinstance.request;let t=dlmXHRinstance.request.getAllResponseHeaders().split("\r\n").reduce((e,d)=>{var[d,o]=d.split(": ");return e[d]=o,e},{}),n=(h.xhrNonce=t["x-dlm-nonce"],"download"),l=!1,r=!1,a=!1,s=!1,i=null,m=!1,c=!1,u=!1,v=!1,p=!1;if(void 0!==t["dlm-file-name"]&&(l=t["dlm-file-name"]),void 0!==t["dlm-no-waypoints"]&&(r=!0),void 0!==t["dlm-redirect"]&&(a=t["dlm-redirect"]),void 0!==t["dlm-external-download"]&&(s=!0),void 0!==t["dlm-no-access"]&&(i=t["dlm-no-access"]),void 0!==t["dlm-no-access-modal"]&&(m=t["dlm-no-access-modal"]),void 0!==t["dlm-error"]&&(c=t["dlm-error"]),void 0!==t["dlm-download-id"]&&(u=t["dlm-download-id"]),void 0!==t["dlm-version-id"]&&(v=t["dlm-version-id"]),void 0!==t["dlm-no-access-modal-text"]&&(p=t["dlm-no-access-modal-text"]),void 0!==t["x-dlm-file-name"]&&(l=t["x-dlm-file-name"]),void 0!==t["x-dlm-no-waypoints"]&&(r=!0),void 0!==t["x-dlm-redirect"]&&(a=t["x-dlm-redirect"]),void 0!==t["x-dlm-external-download"]&&(s=!0),void 0!==t["x-dlm-no-access"]&&(i=t["x-dlm-no-access"]),void 0!==t["x-dlm-no-access-modal"]&&(m=t["x-dlm-no-access-modal"]),void 0!==t["x-dlm-error"]&&(c=t["x-dlm-error"]),void 0!==t["x-dlm-download-id"]&&(u=t["x-dlm-download-id"]),void 0!==t["x-dlm-version-id"]&&(v=t["x-dlm-version-id"]),void 0!==t["x-dlm-no-access-modal-text"]&&(p=t["x-dlm-no-access-modal-text"]),l?(n=l.replace(/\"/g,"").replace(";",""),n=decodeURI(n)):void 0!==t["content-disposition"]&&(n=(n=t["content-disposition"].split(/(?:filename\*=UTF-8'')|(?:filename=)/)[1]).replace(/\"/g,"").replace(";",""),n=decodeURI(n)),2===dlmXHRinstance.request.readyState){if(void 0!==t["x-dlm-force-abort"]&&""!==t["x-dlm-force-abort"])return x.removeAttribute("download"),x.setAttribute("href",f),w.removeClass().addClass(H).find("span.dlm-xhr-progress").remove(),w.find(".dlm-xhr-loading-gif").remove(),dlmXHRinstance.request.abort(),void jQuery("#dlm-no-access-modal").remove();if(r)return dlmXHRinstance.request.abort(),a?void(window.location.href=a):void(window.location.href=f);if(s)return dlmXHRinstance.request.abort(),void dlmXHRinstance.dlmExternalDownload(t,x,w,n,f);if(0===Object.keys(t).filter(e=>-1!==e.indexOf("dlm-")).length)return dlmXHRinstance.request.abort(),void(window.location.href=f);if(i&&"true"===i&&m&&0!=m)return dlmXHRinstance.dlmNoAccessModal(t),x.removeAttribute("download"),x.setAttribute("href",f),w.removeClass().addClass(H).find("span.dlm-xhr-progress").remove(),w.find(".dlm-xhr-loading-gif").remove(),void dlmXHRinstance.request.abort();if(c&&""!==c&&null!==c)return dlmXHRinstance.dlmLogDownload(t,"failed",!1),x.removeAttribute("download"),x.setAttribute("href",f),w.removeClass().addClass(H).find("span.dlm-xhr-progress").remove(),w.find(".dlm-xhr-loading-gif").remove(),dlmXHRinstance.request.abort(),void(m&&0!=m?dlmXHRinstance.dlmNoAccessModal(u,v,p):(w.find(".dlm-xhr-error").remove(),w.append(''+c+"")));if(a&&""!==a&&null!==a)return dlmXHRinstance.dlmLogDownload(t,"redirected",!1,a,i,R),x.removeAttribute("download"),x.setAttribute("href",f),w.removeClass().addClass(H).find("span.dlm-xhr-progress").remove(),w.find(".dlm-xhr-loading-gif").remove(),void dlmXHRinstance.request.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&&(o=dlmXHRinstance.request.response,g=URL.createObjectURL(o),x.removeEventListener("click",dlmXHRinstance.handleDownloadClick),x.setAttribute("download",""+n),x.setAttribute("href",g),x.click(),w.removeClass().addClass(H+" dlm-download-complete"),dlmXHRinstance.attachButtonEvent(),jQuery(document).trigger("dlm_download_complete",[this,x,w,g]),dlmXHRinstance.dlmLogDownload(t,"completed",b),window.URL.revokeObjectURL(g),x.removeAttribute("download"),x.setAttribute("href",f),w.find(".dlm-xhr-loading-gif").remove(),setTimeout(function(){w.removeClass().addClass(H).find("span.dlm-xhr-progress").remove()},4e3))}},dlmXHRinstance.request.addEventListener("progress",function(e){let d=e.loaded/e.total*100;d=d.toFixed();var o;w.find("span.dlm-xhr-progress").remove(),o="dlm-download-started download-"+10*Math.ceil(d/10),1/0!=d&&w.append(' '+d+"%"),w.removeClass().addClass(H+" "+o),jQuery(document).trigger("dlm_download_progress",[this,x,w,g,e,d])}),dlmXHRinstance.request.onerror=function(){x.removeAttribute("download"),x.setAttribute("href",f),w.removeClass().addClass(H+" dlm-no-xhr-download").find("span.dlm-xhr-progress").remove(),w.find(".dlm-xhr-error").remove(),w.append(''+dlmXHRtranslations.error+""),console.log("** An error occurred during the transaction")},dlmXHRinstance.request.open("GET",f,!0),dlmXHRinstance.request.setRequestHeader("Cache-Control","no-store, no-cache, no-transform, max-age=0"),dlmXHRinstance.request.setRequestHeader("dlm-xhr-request","dlm_XMLHttpRequest"),dlmXHRinstance.request.send()}dlmLogDownload(e,d,o,t=null,n=null,l="_self"){null!==n?window.location.href=t:(n=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:n,action:"log_dlm_xhr_download",responseHeaders:e,nonce:this.xhrNonce},jQuery.post(dlmXHR.ajaxUrl,d,function(e){null!==t&&(null==l&&(l="_self"),window.open(t,l))}))}dlmNoAccessModal(e){let d="empty-download",o="empty-version",t="empty-restriction",n="",l=(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"]&&(n=e["dlm-no-access-modal-text"]),void 0!==e["dlm-no-access-restriction"]&&(t=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"]&&(n=e["x-dlm-no-access-modal-text"]),void 0!==e["x-dlm-no-access-restriction"]&&(t=e["x-dlm-no-access-restriction"]),{download_id:d,version_id:o,modal_text:n,restriction:t,action:"no_access_dlm_xhr_download",nonce:this.xhrNonce});jQuery(document).trigger("dlm-xhr-modal-data",[l,e]),jQuery.post(dlmXHR.ajaxUrl,l,function(e){jQuery("#dlm-no-access-modal").remove(),jQuery("body").append(e),jQuery(document).trigger(l.action,[e,l])})}dlmExternalDownload(e,t,n,l,r){const a=new XMLHttpRequest;n.attr("target");let s=n.attr("class"),i,d="";void 0!==e["dlm-external-download"]&&(d=e["dlm-external-download"]),void 0!==e["x-dlm-external-download"]&&(d=e["x-dlm-external-download"]),s=void 0!==s&&""!==s?s.replace("dlm-download-started","").replace("dlm-download-completed",""):"",n.addClass("dlm-download-started"),t.setAttribute("href","#"),t.removeAttribute("download"),t.setAttribute("disabled","disabled"),jQuery(document).trigger("dlm_download_triggered",[this,t,n,i,a]),a.responseType="blob",a.onreadystatechange=function(){var{status:e,readyState:d}=a,o=a.getAllResponseHeaders().split("\r\n").reduce((e,d)=>{var[d,o]=d.split(": ");return e[d]=o,e},{});if(403===e)return dlmXHRinstance.dlmLogDownload(o,"failed",!1),a.abort(),n.find(".dlm-xhr-error").remove(),void n.append('Acces Denied to file.');200==e&&4==d&&(e=a.response,i=URL.createObjectURL(e),t.removeEventListener("click",dlmXHRinstance.handleDownloadClick),t.setAttribute("download",""+l),t.setAttribute("href",i),t.click(),n.removeClass().addClass(s+" dlm-download-complete"),dlmXHRinstance.attachButtonEvent(),jQuery(document).trigger("dlm_download_complete",[this,t,n,i]),dlmXHRinstance.dlmLogDownload(o,"completed",!1),window.URL.revokeObjectURL(i),t.removeAttribute("download"),t.setAttribute("href",r),n.find(".dlm-xhr-loading-gif").remove(),setTimeout(function(){n.removeClass().addClass(s).find("span.dlm-xhr-progress").remove()},1e3))},a.addEventListener("progress",function(e){let d=e.loaded/e.total*100;d=d.toFixed();var o;n.find("span.dlm-xhr-progress").remove(),o="dlm-download-started download-"+10*Math.ceil(d/10),1/0!=d&&n.append(' '+d+"%"),n.removeClass().addClass(s+" "+o),jQuery(document).trigger("dlm_download_progress",[this,t,n,i,e,d])}),a.onerror=function(){t.removeAttribute("download"),t.setAttribute("href",r),n.removeClass().addClass(s+" .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",d,!0),a.setRequestHeader("Cache-Control","no-store, no-cache, no-transform, max-age=0"),a.setRequestHeader("dlm-xhr-request","dlm_XMLHttpRequest"),a.send()}}