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");if(jQuery(this).hasClass("dlm-no-xhr-download"))return!0;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:w,href:x,buttonObj:f}=e,h;const g=new XMLHttpRequest,b=dlmXHR.prevent_duplicates,R=f.attr("target");let H=f.attr("class");H=void 0!==H&&""!==H?H.replace("dlm-download-started","").replace("dlm-download-completed",""):"",f.addClass("dlm-download-started"),w.setAttribute("href","#"),w.removeAttribute("download"),w.setAttribute("disabled","disabled");e='',w.innerHTML+=e,e=0{var[d,o]=d.split(": ");return e[d]=o,e},{}),t="download",n=!1,r=!1,a=!1,s=!1,i=null,m=!1,c=!1,v=!1,u=!1,p=!1;if(void 0!==l["dlm-file-name"]&&(n=l["dlm-file-name"]),void 0!==l["dlm-no-waypoints"]&&(r=!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"]&&(n=l["x-dlm-file-name"]),void 0!==l["x-dlm-no-waypoints"]&&(r=!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"]),n?(t=n.replace(/\"/g,"").replace(";",""),t=decodeURI(t)):void 0!==l["content-disposition"]&&(t=(t=l["content-disposition"].split("filename=")[1]).replace(/\"/g,"").replace(";",""),t=decodeURI(t)),2===g.readyState){if(r)return g.abort(),a?void(window.location.href=a):void(window.location.href=x);if(s)return g.abort(),void dlmXHRinstance.dlmExternalDownload(l,w,f,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),w.removeAttribute("download"),w.setAttribute("href",x),f.removeClass().addClass(H).find("span.dlm-xhr-progress").remove(),f.find(".dlm-xhr-loading-gif").remove(),void g.abort();if(c&&""!==c&&null!==c)return dlmXHRinstance.dlmLogDownload(l,"failed",!1),w.removeAttribute("download"),w.setAttribute("href",x),f.removeClass().addClass(H).find("span.dlm-xhr-progress").remove(),f.find(".dlm-xhr-loading-gif").remove(),g.abort(),void(m&&0!=m?dlmXHRinstance.dlmNoAccessModal(v,u,p):(f.find(".dlm-xhr-error").remove(),f.append(''+c+"")));if(a&&""!==a&&null!==a)return dlmXHRinstance.dlmLogDownload(l,"redirected",!1,a,i,R),w.removeAttribute("download"),w.setAttribute("href",x),f.removeClass().addClass(H).find("span.dlm-xhr-progress").remove(),f.find(".dlm-xhr-loading-gif").remove(),void g.abort()}if(404==e&&2==d){let e=document.createElement("p");e.innerHTML=o,w.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,w.parentNode.appendChild(e)}200==e&&4==d&&(o=g.response,h=URL.createObjectURL(o),w.removeEventListener("click",dlmXHRinstance.handleDownloadClick),w.setAttribute("download",""+t),w.setAttribute("href",h),w.click(),f.removeClass().addClass(H+" dlm-download-complete"),dlmXHRinstance.attachButtonEvent(),jQuery(document).trigger("dlm_download_complete",[this,w,f,h]),dlmXHRinstance.dlmLogDownload(l,"completed",b),window.URL.revokeObjectURL(h),w.removeAttribute("download"),w.setAttribute("href",x),f.find(".dlm-xhr-loading-gif").remove(),setTimeout(function(){f.removeClass().addClass(H).find("span.dlm-xhr-progress").remove()},4e3))}},g.addEventListener("progress",function(e){let d=e.loaded/e.total*100;d=d.toFixed();var o;f.find("span.dlm-xhr-progress").remove(),o="dlm-download-started download-"+10*Math.ceil(d/10),1/0!=d&&f.append(' '+d+"%"),f.removeClass().addClass(H+" "+o),jQuery(document).trigger("dlm_download_progress",[this,w,f,h,e,d])}),g.onerror=function(){w.removeAttribute("download"),w.setAttribute("href",x),f.removeClass().addClass(H+" dlm-no-xhr-download").find("span.dlm-xhr-progress").remove(),f.find(".dlm-xhr-error").remove(),f.append(''+dlmXHRtranslations.error+""),console.log("** An error occurred during the transaction")},g.open("GET",e,!0),g.setRequestHeader("dlm-xhr-request","dlm_XMLHttpRequest"),g.send()}dlmLogDownload(e,d,o,l=null,t=null,n="_self"){null!==t?window.location.href=l:(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:dlmXHR.nonce},jQuery.post(dlmXHR.ajaxUrl,d,function(e){null!==l&&(null==n&&(n="_self"),window.open(l,n))}))}dlmNoAccessModal(e){let d="empty-download",o="empty-version",l="empty-restriction",t="",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"]&&(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:dlmXHR.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])})}dlmExternalDownload(e,l,t,n,r){const a=new XMLHttpRequest;t.attr("target");let s=t.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",""):"",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.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(),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",""+n),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",r),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",r),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",d,!0),a.setRequestHeader("dlm-xhr-request","dlm_XMLHttpRequest"),a.send()}}