From 54432485e71f84d73d495734fe602f06d1e3d1b1 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Fri, 19 Jan 2024 19:25:58 +0000 Subject: [PATCH] Add an HTML view of bag listings --- .../multiwikiserver/modules/route-get-bag.js | 51 +++++++++++++++++++ .../multiwikiserver/modules/route-get-wiki.js | 2 +- .../modules/sql-tiddler-store.js | 19 +++++++ .../multiwikiserver/templates/get-bags.tid | 13 +++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 plugins/tiddlywiki/multiwikiserver/modules/route-get-bag.js create mode 100644 plugins/tiddlywiki/multiwikiserver/templates/get-bags.tid diff --git a/plugins/tiddlywiki/multiwikiserver/modules/route-get-bag.js b/plugins/tiddlywiki/multiwikiserver/modules/route-get-bag.js new file mode 100644 index 000000000..c1f64c3a9 --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/modules/route-get-bag.js @@ -0,0 +1,51 @@ +/*\ +title: $:/plugins/tiddlywiki/multiwikiserver/route-get-bag.js +type: application/javascript +module-type: route + +GET /wikis/:bag_name/bags/:bag_name + +NOTE: Urls currently include the bag name twice. This is temporary to minimise the changes to the TiddlyWeb plugin + +\*/ +(function() { + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.method = "GET"; + +exports.path = /^\/wiki\/([^\/]+)\/bags\/([^\/]+)$/; + +exports.handler = function(request,response,state) { + // Get the parameters + var bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]), + bag_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]), + titles = bag_name === bag_name_2 && $tw.sqlTiddlerStore.getBagTiddlers(bag_name); + if(bag_name === bag_name_2 && titles) { + // If application/json is requested then this is an API request, and gets the response in JSON + if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) { + state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(titles),"utf8"); + } else { + // This is not a JSON API request, we should return the raw tiddler content + response.writeHead(200, "OK",{ + "Content-Type": "text/html" + }); + // Render the html + var html = $tw.sqlTiddlerStore.adminWiki.renderTiddler("text/html","$:/plugins/tiddlywiki/multiwikiserver/templates/get-bags",{ + variables: { + "bag-name": bag_name, + "bag-titles": JSON.stringify(titles) + } + }); + response.write(html); + response.end();; + } + } else { + response.writeHead(404); + response.end(); + } +}; + +}()); diff --git a/plugins/tiddlywiki/multiwikiserver/modules/route-get-wiki.js b/plugins/tiddlywiki/multiwikiserver/modules/route-get-wiki.js index 14089553c..561c6f961 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/route-get-wiki.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/route-get-wiki.js @@ -28,7 +28,7 @@ exports.handler = function(request,response,state) { // Get the tiddlers in the recipe var titles = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name); // Render the template - var template = $tw.wiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{ + var template = $tw.sqlTiddlerStore.adminWiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{ variables: { saveTiddlerFilter: ` $:/boot/boot.css diff --git a/plugins/tiddlywiki/multiwikiserver/modules/sql-tiddler-store.js b/plugins/tiddlywiki/multiwikiserver/modules/sql-tiddler-store.js index d24a8212c..d794fd543 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/sql-tiddler-store.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/sql-tiddler-store.js @@ -327,6 +327,25 @@ SqlTiddlerStore.prototype.getTiddler = function(title,recipename) { } }; +/* +Get the titles of the tiddlers in a bag. Returns an empty array for bags that do not exist +*/ +SqlTiddlerStore.prototype.getBagTiddlers = function(bagname) { + const rows = this.runStatementGetAll(` + SELECT DISTINCT title + FROM tiddlers + WHERE bag_id IN ( + SELECT bag_id + FROM bags + WHERE bag_name = $bag_name + ) + ORDER BY title ASC + `,{ + bag_name: bagname + }); + return rows.map(value => value.title); +}; + /* Get the titles of the tiddlers in a recipe. Returns an empty array for recipes that do not exist */ diff --git a/plugins/tiddlywiki/multiwikiserver/templates/get-bags.tid b/plugins/tiddlywiki/multiwikiserver/templates/get-bags.tid new file mode 100644 index 000000000..440e54767 --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/templates/get-bags.tid @@ -0,0 +1,13 @@ +title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-bags + +! Bag <$text text={{{ []}}}/> + +