UNPKG

@e280/quay

Version:

File-browser and outliner UI for the web

79 lines 2.58 kB
import { sub } from "@e280/stz"; import { signal } from "@benev/slate"; import { Dropzone } from "./aspects/dropzone.js"; import { TreeTrail } from "./aspects/tree-trail.js"; export class Group { config; trail; dropzone = new Dropzone(this); searchText = signal(""); selectedFilter = signal(""); selectedSort = signal(""); on = { newFolder: sub(), move: sub(), delete: sub(), rename: sub(), upload: sub(), search: sub(), refresh: sub(), }; constructor(config) { this.config = config; this.selectedFilter.value = config.defaultFilter; this.selectedSort.value = config.defaultSort; this.trail = new TreeTrail(config.root); } get permissions() { return this.config.permissions; } getFilterFn(filterKey) { return this.config.filters.get(filterKey) ?? (() => true); } getSearchFn(searchText) { const terms = searchText.trim().toLowerCase().split(/\s+/); return item => this.config.search(terms, item); } getSortFn(sortKey) { return this.config.sorts?.get(sortKey) ?? (() => 0); } sort(items) { const sortFn = this.getSortFn(this.selectedSort.value); return [...items].sort(sortFn); } matches(item) { const filterFn = this.getFilterFn(this.selectedFilter.value); const searchFn = this.getSearchFn(this.searchText.value); return filterFn(item) && searchFn(item); } move(item, target) { if (!this.permissions(item).move) throw new Error("move permission not granted"); item.detach(); target.attach(item); this.on.move.pub({ item, target }); } delete(item) { if (!this.permissions(item).delete) throw new Error("delete permission not granted"); item.destroy(); this.on.delete.pub({ item }); } rename(item, newName) { if (!this.permissions(item).rename) throw new Error("rename permission not granted"); this.on.rename.pub({ item, newName }); } upload(files, folder) { if (!this.permissions(folder).upload) throw new Error("upload permission not granted"); this.on.upload.pub({ files, target: folder }); } addFolder(parent) { if (!this.permissions(parent).newFolder) throw new Error("add folder permission not granted"); this.on.newFolder.pub({ parent }); } components = () => this.components; } //# sourceMappingURL=group.js.map