/** * @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */if(!Joomla)throw new Error("Joomla API was not initialised properly");Joomla.Update=window.Joomla.Update||{stat_total:0,stat_files:0,stat_inbytes:0,stat_outbytes:0,password:null,totalsize:0,ajax_url:null,return_url:null,cached_instance:null,genericErrorMessage:e=>{const t=document.getElementById("errorDialogLabel"),o=document.getElementById("errorDialogMessage"),a=document.getElementById("joomlaupdate-progress"),s=document.getElementById("joomlaupdate-error");t.innerHTML=Joomla.Text._("COM_JOOMLAUPDATE_ERRORMODAL_HEAD_GENERIC"),o.innerHTML=e,e.toLowerCase()==="invalid login"&&(o.innerHTML=Joomla.Text._("COM_JOOMLAUPDATE_ERRORMODAL_BODY_INVALIDLOGIN")),a.classList.add("d-none"),s.classList.remove("d-none")},handleErrorResponse:e=>{const t=e.status===403,o=document.getElementById("errorDialogLabel"),a=document.getElementById("errorDialogMessage"),s=document.getElementById("joomlaupdate-progress"),r=document.getElementById("joomlaupdate-error");t?(o.innerHTML=Joomla.Text._("COM_JOOMLAUPDATE_ERRORMODAL_HEAD_FORBIDDEN"),a.innerHTML=Joomla.Text._("COM_JOOMLAUPDATE_ERRORMODAL_BODY_FORBIDDEN")):(o.innerHTML=Joomla.Text._("COM_JOOMLAUPDATE_ERRORMODAL_HEAD_SERVERERROR"),a.innerHTML=Joomla.Text._("COM_JOOMLAUPDATE_ERRORMODAL_BODY_SERVERERROR")),s.classList.add("d-none"),r.classList.remove("d-none")},startExtract:()=>{Joomla.Update.stat_files=0,Joomla.Update.stat_inbytes=0,Joomla.Update.stat_outbytes=0,Joomla.Update.cached_instance=null,document.getElementById("extbytesin").innerText=Joomla.Update.formatBytes(Joomla.Update.stat_inbytes),document.getElementById("extbytesout").innerText=Joomla.Update.formatBytes(Joomla.Update.stat_outbytes),document.getElementById("extfiles").innerText=Joomla.Update.stat_files;const e=new FormData;e.append("task","startExtract"),e.append("password",Joomla.Update.password),Joomla.request({url:Joomla.Update.ajax_url,data:e,method:"POST",perform:!0,onSuccess:t=>{try{const o=JSON.parse(t);Joomla.Update.stepExtract(o)}catch(o){Joomla.Update.genericErrorMessage(o.message)}},onError:Joomla.Update.handleErrorResponse})},stepExtract:e=>{if(e.status===!1){Joomla.Update.genericErrorMessage(e.message);return}const t=document.getElementById("progress-bar"),o=document.getElementById("update-title");if(Joomla.Update.stat_inbytes=e.bytesIn,Joomla.Update.stat_percent=e.percent,Joomla.Update.stat_percent=Joomla.Update.stat_percent||100*(Joomla.Update.stat_inbytes/Joomla.Update.totalsize),Joomla.Update.stat_outbytes=e.bytesOut,Joomla.Update.stat_files=e.files,Joomla.Update.stat_percent<100?(t.classList.remove("bg-success"),t.style.width=`${Joomla.Update.stat_percent}%`,t.setAttribute("aria-valuenow",Joomla.Update.stat_percent)):Joomla.Update.stat_percent>=100&&(t.classList.add("bg-success"),t.style.width="100%",t.setAttribute("aria-valuenow",100)),t.innerText=`${Joomla.Update.stat_percent.toFixed(1)}%`,document.getElementById("extbytesin").innerText=Joomla.Update.formatBytes(Joomla.Update.stat_inbytes),document.getElementById("extbytesout").innerText=Joomla.Update.formatBytes(Joomla.Update.stat_outbytes),document.getElementById("extfiles").innerText=Joomla.Update.stat_files,e.done){t.classList.add("bg-success"),t.style.width="100%",t.setAttribute("aria-valuenow",100),o.innerHTML=Joomla.Text._("COM_JOOMLAUPDATE_UPDATING_COMPLETE"),Joomla.Update.finalizeUpdate();return}window.setTimeout(()=>{Joomla.Update.delayedStepExtract(e.instance)},50)},delayedStepExtract:e=>{Joomla.Update.cached_instance=e;const t=new FormData;t.append("task","stepExtract"),t.append("password",Joomla.Update.password),e&&t.append("instance",e),Joomla.request({url:Joomla.Update.ajax_url,data:t,method:"POST",perform:!0,onSuccess:o=>{try{const a=JSON.parse(o);Joomla.Update.stepExtract(a)}catch(a){Joomla.Update.genericErrorMessage(a.message)}},onError:Joomla.Update.handleErrorResponse})},finalizeUpdate:()=>{const e=new FormData;e.append("task","finalizeUpdate"),e.append("password",Joomla.Update.password),Joomla.request({url:Joomla.Update.ajax_url,data:e,method:"POST",perform:!0,onSuccess:()=>{window.location=Joomla.Update.return_url},onError:Joomla.Update.handleErrorResponse})},formatBytes:(e,t=2)=>{if(e===0)return`0 ${Joomla.Text._("JLIB_SIZE_BYTES")}`;const o=1024,a=t<0?0:t,s=[Joomla.Text._("JLIB_SIZE_BYTES"),Joomla.Text._("JLIB_SIZE_KB"),Joomla.Text._("JLIB_SIZE_MB"),Joomla.Text._("JLIB_SIZE_GB"),Joomla.Text._("JLIB_SIZE_TB"),Joomla.Text._("JLIB_SIZE_PB"),Joomla.Text._("JLIB_SIZE_EB"),Joomla.Text._("JLIB_SIZE_ZB"),Joomla.Text._("JLIB_SIZE_YB")],r=Math.floor(Math.log(e)/Math.log(o));return`${parseFloat((e/o**r).toFixed(a))} ${s[r]}`},resumeButtonHandler:e=>{e.preventDefault(),document.getElementById("joomlaupdate-progress").classList.remove("d-none"),document.getElementById("joomlaupdate-error").classList.add("d-none"),Joomla.Update.cached_instance===!1?Joomla.Update.startExtract():Joomla.Update.delayedStepExtract(Joomla.Update.cached_instance)},restartButtonHandler:e=>{e.preventDefault(),document.getElementById("joomlaupdate-progress").classList.remove("d-none"),document.getElementById("joomlaupdate-error").classList.add("d-none"),Joomla.Update.startExtract()}};const elResume=document.getElementById("joomlaupdate-resume"),elRestart=document.getElementById("joomlaupdate-restart");elResume&&elResume.addEventListener("click",Joomla.Update.resumeButtonHandler),elRestart&&elRestart.addEventListener("click",Joomla.Update.restartButtonHandler);const JoomlaUpdateOptions=Joomla.getOptions("joomlaupdate");JoomlaUpdateOptions&&Object.keys(JoomlaUpdateOptions).length&&(Joomla.Update.password=JoomlaUpdateOptions.password,Joomla.Update.totalsize=JoomlaUpdateOptions.totalsize,Joomla.Update.ajax_url=JoomlaUpdateOptions.ajax_url,Joomla.Update.return_url=JoomlaUpdateOptions.return_url,Joomla.Update.startExtract());