Initialize VUE project with WS connection, main pages, and various small features
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
import { ref, computed } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
export const useCounterStore = defineStore('counter', () => {
|
||||
const count = ref(0)
|
||||
const doubleCount = computed(() => count.value * 2)
|
||||
function increment() {
|
||||
count.value++
|
||||
}
|
||||
|
||||
return { count, doubleCount, increment }
|
||||
})
|
||||
144
cista-front/src/stores/documents.ts
Normal file
144
cista-front/src/stores/documents.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import type { Document } from '@/repositories/Document';
|
||||
import type { ISimpleError } from '@/repositories/Client';
|
||||
import { fetchFile } from '@/repositories/Document'
|
||||
import { formatUnixDate } from '@/utils';
|
||||
import { defineStore } from 'pinia';
|
||||
|
||||
|
||||
type FileData = { mtime: number, size: number, dir: DirectoryData};
|
||||
type DirectoryData = {
|
||||
[filename: string]: FileData;
|
||||
};
|
||||
export type FileStructure = {mtime: number, size: number, dir: DirectoryData};
|
||||
|
||||
export type DocumentStore = {
|
||||
root: FileStructure,
|
||||
document: Document[],
|
||||
loading: boolean,
|
||||
uploadingDocuments: Array<{key: number, name: string, progress: number}>,
|
||||
uploadCount: number,
|
||||
wsWatch: WebSocket | undefined,
|
||||
wsUpload: WebSocket | undefined,
|
||||
selectedDocuments: Document[],
|
||||
error: string,
|
||||
}
|
||||
|
||||
export const useDocumentStore = defineStore({
|
||||
id: 'documents',
|
||||
state: (): DocumentStore => ({
|
||||
root: {} as FileStructure,
|
||||
document: [] as Document[],
|
||||
loading: true as boolean,
|
||||
uploadingDocuments: [],
|
||||
uploadCount: 0 as number,
|
||||
wsWatch: undefined,
|
||||
wsUpload: undefined,
|
||||
selectedDocuments: [] as Document[],
|
||||
error: '' as string,
|
||||
}),
|
||||
|
||||
actions: {
|
||||
setActualDocument(location: string){
|
||||
this.loading = true;
|
||||
let data = this.root
|
||||
const dataMapped = [];
|
||||
const locations = location.split('/').slice(1)
|
||||
// Get data target location
|
||||
locations.forEach(location => {
|
||||
location = decodeURIComponent(location)
|
||||
if(data && data.dir){
|
||||
for (const key in data.dir) {
|
||||
if(key === location) data = data.dir[key]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Transform data
|
||||
let count = 0
|
||||
for (const key in data.dir) {
|
||||
const element: Document = {
|
||||
name: key,
|
||||
key: count,
|
||||
size: data.dir[key].size,
|
||||
modified: formatUnixDate(data.dir[key].mtime),
|
||||
type: 'folder',
|
||||
}
|
||||
count++
|
||||
dataMapped.push(element)
|
||||
}
|
||||
this.document = dataMapped
|
||||
this.loading = false;
|
||||
},
|
||||
async setActualDocumentFile(path: string){
|
||||
this.loading = true;
|
||||
const file = await fetchFile(path)
|
||||
this.document = [file];
|
||||
this.loading = false;
|
||||
},
|
||||
setSelectedDocuments(document: Document[]){
|
||||
this.selectedDocuments = document
|
||||
},
|
||||
deleteDocument(document: Document){
|
||||
this.document = this.document.filter(e => document.key !== e.key)
|
||||
this.selectedDocuments = this.selectedDocuments.filter(e => document.key !== e.key)
|
||||
},
|
||||
pushUploadingDocuments(name: string){
|
||||
this.uploadCount++;
|
||||
const document = {
|
||||
key: this.uploadCount,
|
||||
name: name,
|
||||
progress: 0
|
||||
}
|
||||
this.uploadingDocuments.push(document)
|
||||
return document
|
||||
},
|
||||
deleteUploadingDocument(key: number){
|
||||
this.uploadingDocuments = this.uploadingDocuments.filter((e)=> e.key !== key)
|
||||
},
|
||||
getNextDocumentInRoute(direction: number, path: string){
|
||||
const locations = path.split('/').slice(1)
|
||||
locations.pop()
|
||||
let data = this.root
|
||||
const actualDirArr = []
|
||||
// Get data target location
|
||||
locations.forEach(location => {
|
||||
// location = decodeURIComponent(location)
|
||||
if(data && data.dir){
|
||||
for (const key in data.dir) {
|
||||
if(key === location) data = data.dir[key]
|
||||
}
|
||||
}
|
||||
})
|
||||
//Store in a temporary array
|
||||
for (const key in data.dir) {
|
||||
actualDirArr.push({
|
||||
name: key,
|
||||
content: data.dir[key]
|
||||
})
|
||||
}
|
||||
const actualFileName = decodeURIComponent(this.mainDocument[0].name).split('/').pop()
|
||||
let index = actualDirArr.findIndex(e => e.name === actualFileName)
|
||||
|
||||
if(index < 1 && direction === -1 ){
|
||||
index = actualDirArr.length -1
|
||||
}else if(index >= actualDirArr.length - 1 && direction === 1){
|
||||
index = 0
|
||||
}else {
|
||||
index = index + direction
|
||||
}
|
||||
return actualDirArr[index].name
|
||||
}
|
||||
},
|
||||
|
||||
getters: {
|
||||
mainDocument(): Document[] {
|
||||
return this.document;
|
||||
},
|
||||
rootSize(): number | undefined {
|
||||
if(this.root) return this.root.size
|
||||
},
|
||||
rootMain(): DirectoryData | undefined {
|
||||
if(this.root) return this.root.dir
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user