Add an HTML view of bag listings

This commit is contained in:
Jeremy Ruston 2024-01-19 19:25:58 +00:00
parent 26ede2839b
commit 54432485e7
4 changed files with 84 additions and 1 deletions

View File

@ -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();
}
};
}());

View File

@ -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

View File

@ -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
*/

View File

@ -0,0 +1,13 @@
title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-bags
! Bag <$text text={{{ [<bag-name>]}}}/>
<ul>
<$list filter="[<bag-titles>jsonget[]sort[]]">
<li>
<a href=`/wiki/${ [<bag-name>encodeuricomponent[]] }$/bags/${ [<bag-name>encodeuricomponent[]] }$/tiddlers/${ [<currentTiddler>encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank">
<$text text=<<currentTiddler>>/>
</a>
</li>
</$list>
</ul>