1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-26 01:50:28 +00:00
TiddlyWiki5/core/modules/widgets/jsontiddler.js
jeremy@jermolene.com d5d73e02e9 Fix bug with innerwiki template
The problem was that the innerwiki template included the tiddler $:/plugins/tiddlywiki/railroad, which was omitted from the wiki. Unexpectedly, missing tiddlers were rendered by the jsontiddler widget as an empty object {}. The fix is to always include the title when the tiddler is missing.

Also cleaned up the template to remove unneeded tiddlers
2021-10-02 16:17:07 +01:00

91 lines
2.3 KiB
JavaScript

/*\
title: $:/core/modules/widgets/jsontiddler.js
type: application/javascript
module-type: widget
Render a tiddler as JSON text
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var Widget = require("$:/core/modules/widgets/widget.js").widget;
var JSONTiddlerWidget = function(parseTreeNode,options) {
this.initialise(parseTreeNode,options);
};
/*
Inherit from the base widget class
*/
JSONTiddlerWidget.prototype = new Widget();
/*
Render this widget into the DOM
*/
JSONTiddlerWidget.prototype.render = function(parent,nextSibling) {
var self = this;
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
// Collect the fields from the optional base tiddler
var fields = this.getTiddlerFields();
// Add custom fields specified in attributes starting with $
$tw.utils.each(this.attributes,function(attribute,name) {
if(name.charAt(0) === "$") {
fields[name.slice(1)] = attribute;
}
});
// JSONify
var json = JSON.stringify(fields);
// Escape unsafe script characters
if(this.attEscapeUnsafeScriptChars) {
json = json.replace(/</g,"\\u003C");
}
// Update the DOM
var textNode = this.document.createTextNode(json);
parent.insertBefore(textNode,nextSibling);
this.domNodes.push(textNode);
};
/*
Compute the internal state of the widget
*/
JSONTiddlerWidget.prototype.execute = function() {
this.attTiddler = this.getAttribute("tiddler");
this.attExclude = this.getAttribute("exclude","");
this.attEscapeUnsafeScriptChars = this.getAttribute("escapeUnsafeScriptChars","no") === "yes";
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
JSONTiddlerWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if($tw.utils.count(changedAttributes) > 0 || (this.attTiddler && changedTiddlers[this.attTiddler])) {
this.refreshSelf();
return true;
} else {
return false;
}
};
JSONTiddlerWidget.prototype.getTiddlerFields = function() {
var fields = {};
if(this.attTiddler) {
var tiddler = this.wiki.getTiddler(this.attTiddler);
if(tiddler) {
fields = tiddler.getFieldStrings({exclude: this.attExclude.split(" ")});
} else {
fields = {title: this.attTiddler};
}
}
return fields;
};
exports.jsontiddler = JSONTiddlerWidget;
})();