(()=>{(function(){"use strict";var p=function(){var t=this,e=t.$createElement,i=t._self._c||e;return i("div",{staticClass:"janitor-wrapper"},[i("k-button",{class:["janitor",t.button.state],attrs:{id:t.id,icon:t.currentIcon,job:t.job,disabled:!t.isUnsaved&&t.hasChanges},on:{click:t.runJanitor}},[t._v(" "+t._s(t.button.label||t.label)+" ")]),i("a",{directives:[{name:"show",rawName:"v-show",value:t.downloadRequest,expression:"downloadRequest"}],ref:"downloadAnchor",staticClass:"visually-hidden",attrs:{href:t.downloadRequest,download:""}}),i("a",{directives:[{name:"show",rawName:"v-show",value:t.urlRequest,expression:"urlRequest"}],ref:"tabAnchor",staticClass:"visually-hidden",attrs:{href:t.urlRequest,target:"_blank"}})],1)},v=[],q="";function g(t,e,i,c,o,r,l,h){var s=typeof t=="function"?t.options:t;e&&(s.render=e,s.staticRenderFns=i,s._compiled=!0),c&&(s.functional=!0),r&&(s._scopeId="data-v-"+r);var a;if(l?(a=function(n){n=n||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,!n&&typeof __VUE_SSR_CONTEXT__!="undefined"&&(n=__VUE_SSR_CONTEXT__),o&&o.call(this,n),n&&n._registeredComponents&&n._registeredComponents.add(l)},s._ssrRegister=a):o&&(a=h?function(){o.call(this,(s.functional?this.parent:this).$root.$options.shadowRoot)}:o),a)if(s.functional){s._injectStyles=a;var R=s.render;s.render=function(k,b){return a.call(b),R(k,b)}}else{var f=s.beforeCreate;s.beforeCreate=f?[].concat(f,a):[a]}return{exports:t,options:s}}const u="janitor.runAfterAutosave",m={props:{label:String,progress:String,job:String,cooldown:Number,status:String,data:String,pageURI:String,clipboard:Boolean,unsaved:Boolean,autosave:Boolean,intab:Boolean,confirm:String,icon:{type:[Boolean,String],default:!1}},data(){return{button:{label:null,state:null},downloadRequest:null,clipboardRequest:null,urlRequest:null,isUnsaved:!1,icons:{"is-running":"janitorLoader","is-success":"check","has-error":"alert"}}},computed:{id(){var t;return"janitor-"+this.hashCode(this.job+((t=this.button.label)!=null?t:"")+this.pageURI)},hasChanges(){return this.$store.getters["content/hasChanges"]()},currentIcon(){var t;return(t=this.icons[this.status])!=null?t:this.icon}},created(){this.$events.$on("model.update",()=>sessionStorage.getItem(u)&&location.reload()),sessionStorage.getItem(u)===this.id&&(sessionStorage.removeItem(u),this.runJanitor())},methods:{hashCode(t){let e=0;if(t.length===0)return e;for(const i of t)e=(e<<5)-e+t.charCodeAt(i),e=e&e;return e},async runJanitor(){if(this.confirm&&!window.confirm(this.confirm))return;if(this.autosave&&this.hasChanges){const e=document.querySelector(".k-panel .k-form-buttons .k-view").lastChild;if(e){this.isUnsaved=!1,sessionStorage.setItem(u,this.id),this.simulateClick(e);return}}if(this.clipboard){this.clipboardRequest=this.data,this.button.label=this.progress,this.button.state="is-success",setTimeout(this.resetButton,this.cooldown),this.$nextTick(()=>{this.copyToClipboard(this.data)});return}if(this.clipboardRequest){await this.copyToClipboard(this.clipboardRequest),this.resetButton(),this.clipboardRequest=null;return}if(this.status)return;let t=this.job+"/"+encodeURIComponent(this.pageURI);this.data&&(t=t+"/"+encodeURIComponent(this.data)),this.getRequest(t)},async getRequest(t){var h;this.button.label=(h=this.progress)!=null?h:`${this.label} \u2026`,this.button.state="is-running";const{label:e,status:i,reload:c,href:o,download:r,clipboard:l}=await this.$api.get(t);e&&(this.button.label=e),i?this.button.state=i===200?"is-success":"has-error":this.button.state="has-response",c&&location.reload(),o&&(this.intab?(this.urlRequest=o,this.$nextTick(()=>{this.simulateClick(this.$refs.tabAnchor)})):location.href=o),r&&(this.downloadRequest=r,this.$nextTick(()=>{this.simulateClick(this.$refs.downloadAnchor)})),l?this.clipboardRequest=l:setTimeout(this.resetButton,this.cooldown)},resetButton(){this.button.label=null,this.button.state=null},simulateClick(t){const e=new MouseEvent("click",{bubbles:!0,cancelable:!0,view:window});t.dispatchEvent(e)},async copyToClipboard(t){try{await navigator.clipboard.writeText(t)}catch{console.error("navigator.clipboard is not available")}}}},d={};var _=g(m,p,v,!1,w,null,null,null);function w(t){for(let e in d)this[e]=d[e]}var C=function(){return _.exports}();window.panel.plugin("bnomei/janitor",{fields:{janitor:C},icons:{janitorLoader:'<g fill="none" fill-rule="evenodd"><g transform="translate(1 1)" stroke-width="1.75"><circle cx="7" cy="7" r="7.2" stroke="#000" stroke-opacity=".2"/><path d="M14.2,7c0-4-3.2-7.2-7.2-7.2" stroke="#000"><animateTransform attributeName="transform" type="rotate" from="0 7 7" to="360 7 7" dur="1s" repeatCount="indefinite"/></path></g></g>'}})})();})();
