芝麻web文件管理V1.00
编辑当前文件:/home2/sdektunc/.trash/media/com_media/js/edit-images.min.js
if(!Joomla)throw new Error("Joomla API is not properly initialized");Joomla.MediaManager=Joomla.MediaManager||{};class Edit{constructor(){if(this.options=Joomla.getOptions("com_media",{}),!this.options)throw new Error('Initialization error "edit-images.js"');if(this.extension=this.options.uploadPath.split(".").pop(),this.fileType=["jpeg","jpg"].includes(this.extension)?"jpeg":this.extension,this.options.currentUrl=new URL(window.location.href),this.original={filename:this.options.uploadPath.split("/").pop(),extension:this.extension,contents:`data:image/${this.fileType};base64,${this.options.contents}`},this.previousPluginDeactivated=new Promise((e=>e)),this.history={},this.current=this.original,this.plugins={},this.baseContainer=document.getElementById("media-manager-edit-container"),!this.baseContainer)throw new Error("The image preview container is missing");this.createImageContainer(this.original),Joomla.MediaManager.Edit=this,window.dispatchEvent(new CustomEvent("media-manager-edit-init")),customElements.whenDefined("joomla-tab").then((async()=>{const e=document.getElementById("myTab"),t=e.firstElementChild;[].slice.call(t.querySelectorAll("button[aria-controls]")).forEach(((e,t)=>{const i=document.getElementById(e.getAttribute("aria-controls"));0===t&&i.insertAdjacentElement("beforeend",this.baseContainer),e.addEventListener("joomla.tab.hidden",(({target:e})=>{this.previousPluginDeactivated=new Promise(e?(t,i)=>{this.plugins[e.getAttribute("aria-controls").replace("attrib-","")].Deactivate(this.imagePreview).then(t).catch((e=>{console.log(e),i()}))}:e=>e)})),e.addEventListener("joomla.tab.shown",(({target:e})=>{i.insertAdjacentElement("beforeend",this.baseContainer),this.previousPluginDeactivated.then((()=>this.plugins[e.getAttribute("aria-controls").replace("attrib-","")].Activate(this.imagePreview))).catch((e=>{console.log(e)}))}))})),e.activateTab(0,!1)})),this.addHistoryPoint=this.addHistoryPoint.bind(this),this.createImageContainer=this.createImageContainer.bind(this),this.Reset=this.Reset.bind(this),this.Undo=this.Undo.bind(this),this.Redo=this.Redo.bind(this),this.createProgressBar=this.createProgressBar.bind(this),this.updateProgressBar=this.updateProgressBar.bind(this),this.removeProgressBar=this.removeProgressBar.bind(this),this.upload=this.upload.bind(this),window.addEventListener("mediaManager.history.point",this.addHistoryPoint.bind(this))}addHistoryPoint(){if(this.original!==this.current){const e=Object.keys(this.history).length;if(this.history[e]&&this.history[e-1]&&this.history[e]===this.history[e-1])return;this.history[e+1]=this.current}}createImageContainer(e){if(!e.contents)throw new Error('Initialization error "edit-images.js"');this.imagePreview=document.createElement("img"),this.imagePreview.src=e.contents,this.imagePreview.id="image-preview",this.imagePreview.style.height="auto",this.imagePreview.style.maxWidth="100%",this.baseContainer.appendChild(this.imagePreview)}Reset(){this.current.contents=`data:image/${this.fileType};base64,${this.options.contents}`,this.imagePreview.setAttribute("src",this.current.contents),requestAnimationFrame((()=>{requestAnimationFrame((()=>{this.imagePreview.setAttribute("width",this.imagePreview.naturalWidth),this.imagePreview.setAttribute("height",this.imagePreview.naturalHeight)}))}))}Undo(){}Redo(){}createProgressBar(){}updateProgressBar(){}removeProgressBar(){}upload(e,t){let i="jpg"===Joomla.MediaManager.Edit.original.extension?"jpeg":Joomla.MediaManager.Edit.original.extension;if(i||(i=/data:image\/(.+);/gm.exec(Joomla.MediaManager.Edit.original.contents)[1]),!i)throw new Error("Unable to determine image format");this.xhr=new XMLHttpRequest,"function"==typeof t&&(this.xhr.onreadystatechange=t),this.xhr.upload.onprogress=e=>{this.updateProgressBar(e.loaded/e.total*100)},this.xhr.onload=()=>{let e;try{e=JSON.parse(this.xhr.responseText)}catch(t){e=null}e?200===this.xhr.status&&(!0===e.success&&this.removeProgressBar(),"1"===e.status&&(Joomla.renderMessages({success:[e.message]},"true"),this.removeProgressBar())):this.removeProgressBar(),this.xhr=null},this.xhr.onerror=()=>{this.removeProgressBar(),this.xhr=null},this.xhr.open("PUT",e,!0),this.xhr.setRequestHeader("Content-Type","application/json"),this.createProgressBar(),this.xhr.send(JSON.stringify({name:Joomla.MediaManager.Edit.options.uploadPath.split("/").pop(),content:Joomla.MediaManager.Edit.current.contents.replace(`data:image/${i};base64,`,""),[Joomla.MediaManager.Edit.options.csrfToken]:1}))}}new Edit;const getUrl=e=>{const t=Joomla.MediaManager.Edit.options.currentUrl,i=new URLSearchParams(t.search);i.set("view","media"),i.delete("path"),i.delete("mediatypes");const{uploadPath:a}=Joomla.MediaManager.Edit.options;let s=a.split("/");s.pop(),s=s.join("/"),s.endsWith(":")&&(s=`${s}/`),i.set("path",s);const o=document.querySelector('input[name="mediatypes"]');return i.set("mediatypes",o&&o.value?o.value:"0"),e&&i.set("tmpl","component"),t.search=i,t};Joomla.submitbutton=e=>{const t=new URL(`${Joomla.MediaManager.Edit.options.apiBaseUrl}&task=api.files&path=${Joomla.MediaManager.Edit.options.uploadPath}`);switch(e){case"apply":Joomla.MediaManager.Edit.upload(t,null),Joomla.MediaManager.Edit.imagePreview.src=Joomla.MediaManager.Edit.current.contents,Joomla.MediaManager.Edit.original=Joomla.MediaManager.Edit.current,Joomla.MediaManager.Edit.history={},(async()=>{const e=[].slice.call(document.querySelectorAll("joomla-tab-element")).filter((e=>e.hasAttribute("active")));try{await Joomla.MediaManager.Edit.plugins[e[0].id.replace("attrib-","")].Deactivate(Joomla.MediaManager.Edit.imagePreview),await Joomla.MediaManager.Edit.plugins[e[0].id.replace("attrib-","")].Activate(Joomla.MediaManager.Edit.imagePreview)}catch(e){console.log(e)}})();break;case"save":Joomla.MediaManager.Edit.upload(t,(()=>{Joomla.MediaManager.Edit.xhr.readyState===XMLHttpRequest.DONE&&(window.self!==window.top?window.location=getUrl(!0):window.location=getUrl())}));break;case"cancel":window.self!==window.top?window.location=getUrl(!0):window.location=getUrl();break;case"reset":Joomla.MediaManager.Edit.Reset("initial")}};