Current File : /home/pacjaorg/public_html/kmm/media/com_media/js/edit-images.min.js |
/**
* @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 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"),i=e.firstElementChild;[].slice.call(i.querySelectorAll("button[aria-controls]")).forEach((t,s)=>{const h=document.getElementById(t.getAttribute("aria-controls"));s===0&&h.insertAdjacentElement("beforeend",this.baseContainer),t.addEventListener("joomla.tab.hidden",({target:n})=>{if(!n){this.previousPluginDeactivated=new Promise(o=>o);return}this.previousPluginDeactivated=new Promise((o,d)=>{this.plugins[n.getAttribute("aria-controls").replace("attrib-","")].Deactivate(this.imagePreview).then(o).catch(l=>{console.log(l),d()})})}),t.addEventListener("joomla.tab.shown",({target:n})=>{h.insertAdjacentElement("beforeend",this.baseContainer),this.previousPluginDeactivated.then(()=>this.plugins[n.getAttribute("aria-controls").replace("attrib-","")].Activate(this.imagePreview)).catch(o=>{console.log(o)})})}),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,i){let a=Joomla.MediaManager.Edit.original.extension.toLowerCase()==="jpg"?"jpeg":Joomla.MediaManager.Edit.original.extension.toLowerCase();if(a||(a=/data:image\/(.+);/gm.exec(Joomla.MediaManager.Edit.original.contents)[1]),!a)throw new Error("Unable to determine image format");this.xhr=new XMLHttpRequest,typeof i=="function"&&(this.xhr.onreadystatechange=i),this.xhr.upload.onprogress=t=>{this.updateProgressBar(t.loaded/t.total*100)},this.xhr.onload=()=>{let t;try{t=JSON.parse(this.xhr.responseText)}catch{t=null}t?this.xhr.status===200&&(t.success===!0&&this.removeProgressBar(),t.status==="1"&&(Joomla.renderMessages({success:[t.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/${a};base64,`,""),[Joomla.MediaManager.Edit.options.csrfToken]:1}))}}new Edit;const getUrl=r=>{const e=Joomla.MediaManager.Edit.options.currentUrl,i=new URLSearchParams(e.search);i.set("view","media"),i.delete("path"),i.delete("mediatypes");const{uploadPath:a}=Joomla.MediaManager.Edit.options;let t=a.split("/");t.pop(),t=t.join("/"),t.endsWith(":")&&(t=`${t}/`),i.set("path",t);const s=document.querySelector('input[name="mediatypes"]');return i.set("mediatypes",s&&s.value?s.value:"0"),r&&i.set("tmpl","component"),e.search=i,e};Joomla.submitbutton=r=>{const e=new URL(`${Joomla.MediaManager.Edit.options.apiBaseUrl}&task=api.files&path=${Joomla.MediaManager.Edit.options.uploadPath}`);switch(r){case"apply":Joomla.MediaManager.Edit.upload(e,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 i=[].slice.call(document.querySelectorAll("joomla-tab-element")).filter(a=>a.hasAttribute("active"));try{await Joomla.MediaManager.Edit.plugins[i[0].id.replace("attrib-","")].Deactivate(Joomla.MediaManager.Edit.imagePreview),await Joomla.MediaManager.Edit.plugins[i[0].id.replace("attrib-","")].Activate(Joomla.MediaManager.Edit.imagePreview)}catch(a){console.log(a)}})();break;case"save":Joomla.MediaManager.Edit.upload(e,()=>{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");break}};