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