From 6788a8fbe6af9418374b633e92d6307056701e70 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Sun, 24 Dec 2023 23:50:50 +0100 Subject: Tag tab --- public/gallery.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) (limited to 'public/gallery.js') diff --git a/public/gallery.js b/public/gallery.js index f97a162..4933064 100644 --- a/public/gallery.js +++ b/public/gallery.js @@ -185,6 +185,74 @@ let Browse = { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +let TagsModel = { + ns: null, + namespaces: {}, + + async reload(ns) { + if (this.ns !== ns) { + this.ns = ns + this.namespaces = {} + } + + this.namespaces = await call('tags', {namespace: ns}) + }, +} + +let TagsList = { + view(vnode) { + // TODO: Make it possible to sort by count. + const tags = Object.entries(vnode.attrs.tags) + .sort(([a, b]) => a[0].localeCompare(b[0])) + + return (tags.length == 0) + ? "No tags" + : m("ul", tags.map(([name, count]) => + m("li", `${name} ×${count}`))) + }, +} + +let TagsView = { + // See BrowseView. + oncreate(vnode) { vnode.dom.focus() }, + + view(vnode) { + // XXX: The empty-named tag namespace gets a bit shafted, + // in particular in the router, as well as with its header. + // Maybe we could refer to it by its numeric ID in routing. + const names = Object.keys(TagsModel.namespaces) + .sort((a, b) => a.localeCompare(b)) + + let children = (names.length == 0) + ? "No namespaces" + : names.map(name => { + const ns = TagsModel.namespaces[name] + return [ + m("h2", name), + ns.description ? m("p", ns.description) : [], + m(TagsList, {tags: ns.tags}), + ] + }) + return m('.tags[tabindex=0]', {}, children) + }, +} + +let Tags = { + oninit(vnode) { + let ns = vnode.attrs.key + TagsModel.reload(ns) + }, + + view(vnode) { + return m('.container', {}, [ + m(Header), + m('.body', {}, m(TagsView)), + ]) + }, +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + let ViewModel = { sha1: undefined, width: 0, @@ -242,7 +310,7 @@ let ViewBar = { m('li', m(ViewBarPath, {path})))), m('h2', "Tags"), Object.entries(ViewModel.tags).map(([group, tags]) => [ - m("h3", group), + m("h3", m(m.route.Link, {href: `/tags/${group}`}, group)), m("ul.tags", Object.entries(tags) .sort(([t1, w1], [t2, w2]) => (w2 - w1)) .map(([tag, weight]) => m("li", [ @@ -478,14 +546,14 @@ window.addEventListener('load', () => { // The path doesn't need to be escaped, perhaps change that (":key..."). "/browse/": Browse, "/browse/:key": Browse, + "/tags": Tags, + "/tags/:key": Tags, "/duplicates": Duplicates, "/orphans": Orphans, "/view/:key": View, "/similar/:key": Similar, - "/tags": undefined, - "/tags/:space": undefined, - "/tags/:space/:tag": undefined, + "/search/:space/:tag": undefined, }) }) -- cgit v1.2.3-70-g09d2