zwave-js-ui
Version:
Z-Wave Control Panel and MQTT Gateway
3 lines (2 loc) • 12.1 kB
JavaScript
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./prismeditor.esm-BTzr7KLE.js","./index-CtTdmJsx.js","./index-2j-kvxFz.css"])))=>i.map(i=>d[i]);
import{L as l,I as c,_ as d,a as f,u as p,C as i,p as n,n as u}from"./index-CtTdmJsx.js";const h=l.get("Store"),v={name:"Store",mixins:[c],components:{PrismEditor:()=>d(()=>import("./prismeditor.esm-BTzr7KLE.js"),__vite__mapDeps([0,1,2]),import.meta.url).then(r=>r.PrismEditor)},watch:{selected(){this.fetchFile()}},computed:{selected(){if(this.active.length)return this.active[0]},storePath(){var r;return(r=this.items[0])==null?void 0:r.path}},data(){return{fab:!1,selectedFiles:[],allowedExt:["json","jsonl","txt","log","js","ts"],active:[],items:[],openFolders:[],fileContent:"",notSupported:!1,loadingStore:!0,loadingFile:!1}},methods:{...f(p,["showSnackbar"]),async deleteFile(r){if(await this.app.confirm("Attention",`Are you sure you want to delete the file <code>${r.name}</code>?`,"alert"))try{const e=await i.deleteFile(r.path);if(e.success)this.showSnackbar("File deleted successfully","success"),await this.refreshTree(!0);else throw Error(e.message)}catch(e){this.showSnackbar(e.message,"error")}},async deleteSelected(){const r=this.selectedFiles.map(e=>e.path);if(await this.app.confirm("Attention",`Are you sure you want to delete ${r.length} files?`,"alert"))try{const e=await i.deleteMultiple(r);if(e.success)this.showSnackbar("Files deleted successfully","success"),await this.refreshTree(!0);else throw Error(e.message)}catch(e){this.showSnackbar(e.message,"error")}},async downloadSelectedZip(){const r=this.selectedFiles.map(e=>e.path);try{const e=await i.downloadZip(r);await this.downloadZip(e,"zwave-js-ui-store.zip")}catch(e){this.showSnackbar(e.message,"error")}},async downloadZip(r,e){const s=/filename="([^"]+){1}"/g.exec(r.headers["content-disposition"])[1]||e;if(window.navigator&&window.navigator.msSaveOrOpenBlob)window.navigator.msSaveOrOpenBlob(new Blob([r.data],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}),s);else{const a=window.URL.createObjectURL(new Blob([r.data],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})),o=document.createElement("a");o.href=a,o.setAttribute("download",s),document.body.appendChild(o),o.click()}},async downloadFile(){if(this.selected){const r=this.selected.name.split(".").slice(0,-1).join(".");this.app.exportConfiguration(this.fileContent,r,this.selected.ext)}},async backupStore(){if(await this.app.confirm("Backup store","Are you sure you want to backup the store? This backup will contain all useful files and settings.","info",{width:500,cancelText:"No",confirmText:"Yes"}))try{const e=await i.backupStore();await this.downloadZip(e,`store-backup_${Date.now()}.zip`),this.refreshTree()}catch(e){this.showSnackbar(e.message,"error")}},async restoreZip(){const r=await this.app.confirm("Restore zip","","info",{confirmText:"Restore",inputs:[{type:"file",label:"Zip file",required:!0,key:"file",accept:"application/zip"}]});if(r.file)try{const e=new FormData;e.append("upload",r.file),e.append("restore","true");const t=await i.storeUpload(e);if(!t.success)throw new Error(t.message||"Restore failed");await this.refreshTree(),this.showSnackbar("Restore successful","success")}catch(e){this.showSnackbar(e.message||e,"error")}},async uploadFile(r){const e=r?r.path.replace(this.storePath,""):"",t=await this.app.confirm("Upload file",`Destination folder: <code>${e||"root"}</code>`,"info",{confirmText:"Upload",width:500,inputs:[{type:"file",label:"File",required:!0,key:"file"}]});if(t.file)try{const s=new FormData;s.append("upload",t.file),e&&s.append("folder",e);const a=await i.storeUpload(s);if(!a.success)throw new Error(a.message||"Upload failed");await this.refreshTree(),this.showSnackbar("Upload successful","success")}catch(s){this.showSnackbar(s.message||s,"error")}},async writeFile(r,e=!1){const t=r&&typeof r=="string",s=this.fileContent||"";if(t){const a=e?"Directory":"File",{name:o}=await this.app.confirm("New "+a,"","info",{confirmText:"Create",inputs:[{type:"text",label:a+" name",required:!0,key:"name",hint:`Insert the ${a} name`}]});if(!o)return;r=r+"/"+o}else if(this.selected)r=this.selected.path;else{this.showSnackbar("No file selected","error");return}if(t||await this.app.confirm("Attention",`Are you sure you want to overwrite the content of the file <code>${this.selected.name}<code>?`,"alert"))try{const a=await i.writeFile(t?"":s,{path:r,isNew:t,isDirectory:e});if(a.success)this.showSnackbar(`${e?"Directory":"File"} ${t?"created":"updated"} successfully`,"success"),await this.refreshTree();else throw Error(a.message)}catch(a){this.showSnackbar(a.message,"error")}},highlighter(r){return n.highlight(r,n.languages.js)},async fetchFile(){if(this.selected&&this.selected.path&&!this.selected.children){this.fileContent="",this.notSupported=!1,this.loadingFile=!0;try{if(!this.allowedExt.includes(this.selected.ext))throw Error(`Preview of .${this.selected.ext} files is not supported`);const r=await i.getFile(this.selected.path);if(r.success)this.fileContent=r.data;else throw Error(r.message)}catch(r){this.notSupported=!0,this.showSnackbar(r.message,"error")}this.loadingFile=!1}},async refreshTree(r){try{const e=await i.getStore();if(e.success)this.items=e.data;else throw Error(e.message)}catch(e){this.showSnackbar("Error while fetching store files: "+e.message,"error"),h.error(e)}this.loadingStore=!1,this.loadingFile=!1,r&&(this.active=[]),this.openFolders.length===0&&this.openFolders.push(this.items[0])}},async mounted(){await this.refreshTree()},beforeDestroy(){}};var m=function(){var e=this,t=e._self._c;return t("v-container",{staticClass:"full-height pa-0",attrs:{fluid:""}},[t("v-row",{attrs:{"no-gutters":""}},[t("v-col",{staticClass:"sticky-top fill-remaining-space overflow-y-auto",attrs:{cols:"6",md:"5",lg:"4",xl:"3"}},[e.loadingStore?t("div",[t("v-progress-circular",{staticStyle:{"align-self":"center"},attrs:{indeterminate:"",color:"primary"}})],1):t("v-treeview",{staticStyle:{"max-height":"calc(100vh - 64px)","overflow-y":"auto"},attrs:{active:e.active,items:e.items,activatable:"",selectable:"","item-key":"path",open:e.openFolders,"return-object":""},on:{"update:active":function(s){e.active=s},"update:open":function(s){e.openFolders=s}},scopedSlots:e._u([{key:"prepend",fn:function({item:s,open:a}){return[s.children?t("v-icon",{attrs:{color:"#FFC107"}},[e._v(" "+e._s(a?"folder_open":"folder")+" ")]):t("v-icon",{attrs:{color:"blue"}},[e._v(" text_snippet ")])]}},{key:"label",fn:function({item:s}){return[t("span",{staticClass:"subtitle-2"},[e._v(e._s(s.name))]),t("div",{staticClass:"caption grey--text"},[e._v(" "+e._s(s.size!=="n/a"?s.size:"")+" ")])]}},{key:"append",fn:function({item:s}){return[t("v-row",{staticClass:"ma-1",attrs:{"justify-end":""}},[s.children?t("v-menu",{attrs:{"offset-y":""},scopedSlots:e._u([{key:"activator",fn:function({on:a}){return[t("v-icon",e._g({},a),[e._v("more_vert")])]}}],null,!0)},[t("v-list",{staticClass:"py-0",attrs:{dense:""}},[t("v-list-item",{attrs:{dense:""},on:{click:function(a){return a.stopPropagation(),e.writeFile(s.path,!0)}}},[t("v-list-item-icon",[t("v-icon",{attrs:{color:"yellow"}},[e._v("create_new_folder")])],1),t("v-list-item-title",[e._v("Create New Folder")])],1),t("v-list-item",{attrs:{dense:""},on:{click:function(a){return a.stopPropagation(),e.writeFile(s.path,!1)}}},[t("v-list-item-icon",[t("v-icon",{attrs:{color:"primary"}},[e._v("post_add")])],1),t("v-list-item-title",[e._v("Add File")])],1),s.isRoot?e._e():t("v-list-item",{attrs:{dense:""},on:{click:function(a){return a.stopPropagation(),e.deleteFile(s)}}},[t("v-list-item-icon",[t("v-icon",{attrs:{color:"red"}},[e._v("delete")])],1),t("v-list-item-title",[e._v("Delete")])],1),t("v-list-item",{attrs:{dense:""},on:{click:function(a){return a.stopPropagation(),e.uploadFile(s)}}},[t("v-list-item-icon",[t("v-icon",{attrs:{color:"orange"}},[e._v("upload")])],1),t("v-list-item-title",[e._v("Upload File")])],1)],1)],1):t("v-icon",{attrs:{color:"red"},on:{click:function(a){return a.stopPropagation(),e.deleteFile(s)}}},[e._v("delete")])],1)]}}],null,!1,3047385306),model:{value:e.selectedFiles,callback:function(s){e.selectedFiles=s},expression:"selectedFiles"}}),t("v-speed-dial",{attrs:{bottom:"",fab:"",right:"",absolute:""},scopedSlots:e._u([{key:"activator",fn:function(){return[t("v-btn",{attrs:{color:"blue darken-2",dark:"",fab:"",hover:""},model:{value:e.fab,callback:function(s){e.fab=s},expression:"fab"}},[e.fab?t("v-icon",[e._v("close")]):t("v-icon",[e._v("settings")])],1)]},proxy:!0}]),model:{value:e.fab,callback:function(s){e.fab=s},expression:"fab"}},[t("v-tooltip",{attrs:{left:""},scopedSlots:e._u([{key:"activator",fn:function({on:s,attrs:a}){return[t("v-btn",e._g(e._b({attrs:{fab:"",dark:"",small:"",color:"green"},on:{click:function(o){return e.restoreZip()}}},"v-btn",a,!1),s),[t("v-icon",[e._v("restore")])],1)]}}])},[t("span",[e._v("Restore")])]),t("v-tooltip",{attrs:{left:""},scopedSlots:e._u([{key:"activator",fn:function({on:s,attrs:a}){return[t("v-btn",e._g(e._b({attrs:{fab:"",dark:"",small:"",color:"orange"},on:{click:function(o){return e.uploadFile()}}},"v-btn",a,!1),s),[t("v-icon",[e._v("upload")])],1)]}}])},[t("span",[e._v("Upload File")])]),t("v-tooltip",{attrs:{left:""},scopedSlots:e._u([{key:"activator",fn:function({on:s,attrs:a}){return[t("v-btn",e._g(e._b({attrs:{fab:"",dark:"",small:"",color:"purple"},on:{click:e.backupStore}},"v-btn",a,!1),s),[t("v-icon",[e._v("backup")])],1)]}}])},[t("span",[e._v("Backup")])]),t("v-tooltip",{attrs:{left:""},scopedSlots:e._u([{key:"activator",fn:function({on:s,attrs:a}){return[t("v-btn",e._g(e._b({attrs:{fab:"",dark:"",small:"",color:"yellow"},on:{click:e.refreshTree}},"v-btn",a,!1),s),[t("v-icon",[e._v("refresh")])],1)]}}])},[t("span",[e._v("Refresh")])]),t("v-tooltip",{attrs:{left:""},scopedSlots:e._u([{key:"activator",fn:function({on:s,attrs:a}){return[e.selectedFiles.length>0?t("v-btn",e._g(e._b({attrs:{fab:"",dark:"",small:"",color:"primary"},on:{click:e.downloadSelectedZip}},"v-btn",a,!1),s),[t("v-icon",[e._v("file_download")])],1):e._e()]}}])},[t("span",[e._v("Download selected")])]),t("v-tooltip",{attrs:{left:""},scopedSlots:e._u([{key:"activator",fn:function({on:s,attrs:a}){return[e.selectedFiles.length>0?t("v-btn",e._g(e._b({attrs:{fab:"",dark:"",small:"",color:"red"},on:{click:e.deleteSelected}},"v-btn",a,!1),s),[t("v-icon",[e._v("delete")])],1):e._e()]}}])},[t("span",[e._v("Delete selected")])])],1)],1),t("v-divider",{staticClass:"mx-0",attrs:{vertical:""}}),t("v-col",{staticClass:"text-center overflow-y-auto d-flex justify-center"},[!e.selected||!e.selected.ext?t("div",{staticClass:"title grey--text text--lighten-1 font-weight-light align-self-center"},[t("v-icon",{attrs:{color:"grey lighten-4","x-large":""}},[e._v(" text_snippet ")]),t("br"),e._v(" Please select a file ")],1):e.loadingFile?t("div",{staticClass:"title grey--text text--lighten-1 font-weight-light align-self-center"},[t("v-progress-circular",{attrs:{indeterminate:"",color:"primary"}})],1):t("div",{key:e.selected.path,staticClass:"fill-remaining-space flex-grow-1"},[t("div",{staticClass:"file-content pa-4 pb-8"},[e.notSupported?e._e():t("prism-editor",{staticClass:"custom-font",attrs:{lineNumbers:"",highlight:e.highlighter},on:{keydown:function(s){return!s.type.indexOf("key")&&e._k(s.keyCode,"s",void 0,s.key,void 0)||!s.ctrlKey?null:(s.preventDefault(),e.writeFile.apply(null,arguments))}},model:{value:e.fileContent,callback:function(s){e.fileContent=s},expression:"fileContent"}})],1),e.notSupported?e._e():t("div",{staticClass:"sticky-bottom pa-0"},[t("v-toolbar",[t("v-spacer"),t("v-btn",{attrs:{color:"purple darken-1",text:""},on:{click:e.writeFile}},[e._v(" SAVE "),t("v-icon",{attrs:{right:"",dark:""}},[e._v("save")])],1),t("v-btn",{attrs:{color:"green darken-1",text:""},on:{click:e.downloadFile}},[e._v(" DOWNLOAD "),t("v-icon",{attrs:{right:"",dark:""}},[e._v("file_download")])],1)],1)],1)])])],1)],1)},w=[],g=u(v,m,w,!1,null,"020e4236");const b=g.exports;export{b as default};