Search filtering
This commit is contained in:
		| @@ -1,9 +1,8 @@ | ||||
| import type { Document, FolderDocument } from '@/repositories/Document'; | ||||
| import type { ISimpleError } from '@/repositories/Client'; | ||||
| import type { Document } from '@/repositories/Document'; | ||||
| import { fetchFile } from '@/repositories/Document' | ||||
| import { formatSize, formatUnixDate } from '@/utils'; | ||||
| import { defineStore } from 'pinia'; | ||||
|  | ||||
| import { localeIncludes } from 'locale-includes' | ||||
|  | ||||
| type FileData = { id: string, mtime: number, size: number, dir: DirectoryData}; | ||||
| type DirectoryData = { | ||||
| @@ -44,6 +43,43 @@ export const useDocumentStore = defineStore({ | ||||
|   }), | ||||
|  | ||||
|   actions: { | ||||
|     updateTable(matched: DirectoryData) { | ||||
|       // Transform data | ||||
|       const dataMapped  = [] | ||||
|       for (const [name, attr] of Object.entries(matched)) { | ||||
|         const {id, size, mtime, dir} = attr | ||||
|         const element: Document = { | ||||
|           name, | ||||
|           key: id, | ||||
|           size, | ||||
|           sizedisp: formatSize(size), | ||||
|           mtime, | ||||
|           modified: formatUnixDate(mtime), | ||||
|           type: dir === undefined ? 'folder-file' : 'folder', | ||||
|         } | ||||
|         dataMapped.push(element) | ||||
|       } | ||||
|       // Pre sort directory entries folders first then files, names in natural ordering | ||||
|       dataMapped.sort((a, b) => a.type === b.type ? a.name.localeCompare(b.name) : a.type === "folder" ? -1 : 1) | ||||
|       this.document = dataMapped | ||||
|       this.loading = false; | ||||
|     }, | ||||
|     setFilter(filter: string){ | ||||
|       function traverseDir(data: FileStructure, path: string){ | ||||
|         if (data.dir === undefined) return | ||||
|         for (const [name, attr] of Object.entries(data.dir)) { | ||||
|           const fullname = `${path}/${name}` | ||||
|           if (localeIncludes(name, filter, {usage: "search", sensitivity: "base"})) { | ||||
|             matched[fullname.slice(1)] = attr  // No initial slash on name | ||||
|           } | ||||
|           traverseDir(attr, fullname) | ||||
|         } | ||||
|       } | ||||
|       this.loading = true | ||||
|       const matched = {} | ||||
|       traverseDir(this.root, "") | ||||
|       this.updateTable(matched) | ||||
|     }, | ||||
|     setActualDocument(location: string){ | ||||
|       location = decodeURIComponent(location) | ||||
|       this.loading = true | ||||
| @@ -65,25 +101,7 @@ export const useDocumentStore = defineStore({ | ||||
|         this.loading = false  // ??? | ||||
|         return | ||||
|       } | ||||
|       // Transform data | ||||
|       const dataMapped  = [] | ||||
|       for (const [name, attr] of Object.entries(data.dir)) { | ||||
|         const {id, size, mtime, dir} = attr | ||||
|         const element: Document = { | ||||
|           name, | ||||
|           key: id, | ||||
|           size, | ||||
|           sizedisp: formatSize(size), | ||||
|           mtime, | ||||
|           modified: formatUnixDate(mtime), | ||||
|           type: dir === undefined ? 'folder-file' : 'folder', | ||||
|         } | ||||
|         dataMapped.push(element) | ||||
|       } | ||||
|       // Pre sort directory entries folders first then files, names in natural ordering | ||||
|       dataMapped.sort((a, b) => a.type === b.type ? a.name.localeCompare(b.name) : a.type === "folder" ? -1 : 1) | ||||
|       this.document = dataMapped | ||||
|       this.loading = false; | ||||
|       this.updateTable(data.dir) | ||||
|     }, | ||||
|     async setActualDocumentFile(path: string){ | ||||
|       this.loading = true; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Leo Vasanko
					Leo Vasanko