From 80ee5adb148d7220474096a0e80da05148ca7323 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Fri, 19 Mar 2021 15:37:59 +0000 Subject: [PATCH] Extend transclude widget to optionally set variables Partially fixes #5199 --- core/modules/widgets/transclude.js | 11 +++++- core/modules/widgets/widget.js | 35 +++++++++++++++++-- .../tiddlers/widgets/TranscludeWidget.tid | 3 +- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/core/modules/widgets/transclude.js b/core/modules/widgets/transclude.js index 1af7f9c42..c57579919 100755 --- a/core/modules/widgets/transclude.js +++ b/core/modules/widgets/transclude.js @@ -75,8 +75,17 @@ TranscludeWidget.prototype.execute = function() { ]}]; } } + // Assign any variables set via attributes starting with $ + var variables = Object.create(null); + $tw.utils.each(this.attributes,function(attribute,name) { + if(name.charAt(0) === "$") { + variables[name.substr(1)] = attribute; + } + }); // Construct the child widgets - this.makeChildWidgets(parseTreeNodes); + this.makeChildWidgets(parseTreeNodes,{ + variables: variables + }); }; /* diff --git a/core/modules/widgets/widget.js b/core/modules/widgets/widget.js index 1925bfe00..50405c19c 100755 --- a/core/modules/widgets/widget.js +++ b/core/modules/widgets/widget.js @@ -333,9 +333,22 @@ Widget.prototype.assignAttributes = function(domNode,options) { /* Make child widgets correspondng to specified parseTreeNodes */ -Widget.prototype.makeChildWidgets = function(parseTreeNodes) { +Widget.prototype.makeChildWidgets = function(parseTreeNodes,options) { + options = options || {}; this.children = []; var self = this; + // Create set variable widgets for each variable + $tw.utils.each(options.variables,function(value,name) { + var setVariableWidget = { + type: "set", + attributes: { + name: {type: "string", value: name}, + value: {type: "string", value: value} + }, + children: parseTreeNodes + }; + parseTreeNodes = [setVariableWidget]; + }); $tw.utils.each(parseTreeNodes || (this.parseTreeNode && this.parseTreeNode.children),function(childNode) { self.children.push(self.makeChildWidget(childNode)); }); @@ -343,16 +356,32 @@ Widget.prototype.makeChildWidgets = function(parseTreeNodes) { /* Construct the widget object for a parse tree node +options include: + variables: optional hashmap of variables to wrap around the widget */ -Widget.prototype.makeChildWidget = function(parseTreeNode) { +Widget.prototype.makeChildWidget = function(parseTreeNode,options) { + options = options || {}; var WidgetClass = this.widgetClasses[parseTreeNode.type]; if(!WidgetClass) { WidgetClass = this.widgetClasses.text; parseTreeNode = {type: "text", text: "Undefined widget '" + parseTreeNode.type + "'"}; } + // Create set variable widgets for each variable + $tw.utils.each(options.variables,function(value,name) { + var setVariableWidget = { + type: "set", + attributes: { + name: {type: "string", value: name}, + value: {type: "string", value: value} + }, + children: [ + parseTreeNode + ] + }; + parseTreeNode = setVariableWidget; + }); return new WidgetClass(parseTreeNode,{ wiki: this.wiki, - variables: {}, parentWidget: this, document: this.document }); diff --git a/editions/tw5.com/tiddlers/widgets/TranscludeWidget.tid b/editions/tw5.com/tiddlers/widgets/TranscludeWidget.tid index 0501d7091..c28e44432 100644 --- a/editions/tw5.com/tiddlers/widgets/TranscludeWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/TranscludeWidget.tid @@ -1,5 +1,5 @@ created: 20130824142500000 -modified: 20140717175900970 +modified: 20210319150601867 tags: Widgets title: TranscludeWidget type: text/vnd.tiddlywiki @@ -17,6 +17,7 @@ The TranscludeWidget dynamically imports content from another tiddler. |index |The index of a property in a [[DataTiddler|DataTiddlers]] | |subtiddler |Optional SubTiddler title when the target tiddler is a [[plugin|Plugins]] (see below) | |mode |Override the default parsing mode for the transcluded text to "block" or "inline" | +|//(attributes starting with $)// |<<.from-version "5.1.24">> The $ is removed from each attribute name to specify a variable name that is assigned the specified value for the scope of the transclusion | The TranscludeWidget treats any contained content as a fallback if the target of the transclusion is not defined (ie a missing tiddler or a missing field).