1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-07 22:33:50 +00:00

Refactor the list widget to get rid of some hacks

As promised.
This commit is contained in:
Jeremy Ruston 2013-11-02 10:12:55 +00:00
parent 51394e50e8
commit 0b1f0a5fab
6 changed files with 17 additions and 26 deletions

View File

@ -44,6 +44,7 @@ ListWidget.prototype.execute = function() {
// Get our attributes // Get our attributes
this.template = this.getAttribute("template"); this.template = this.getAttribute("template");
this.editTemplate = this.getAttribute("editTemplate"); this.editTemplate = this.getAttribute("editTemplate");
this.variableName = this.getAttribute("variable","currentTiddler");
// Compose the list elements // Compose the list elements
this.list = this.getTiddlerList(); this.list = this.getTiddlerList();
var members = [], var members = [],
@ -88,27 +89,20 @@ ListWidget.prototype.makeItemTemplate = function(title) {
template = this.editTemplate; template = this.editTemplate;
} }
// Compose the transclusion of the template // Compose the transclusion of the template
if(this.hasAttribute("hackTemplate")) { if(template) {
templateTree = [{type: "transclude", attributes: {tiddler: {type: "string", value: title}}}]; templateTree = [{type: "transclude", attributes: {tiddler: {type: "string", value: template}}}];
} else { } else {
if(template) { if(this.parseTreeNode.children && this.parseTreeNode.children.length > 0) {
templateTree = [{type: "transclude", attributes: {tiddler: {type: "string", value: template}}}]; templateTree = this.parseTreeNode.children;
} else { } else {
if(this.parseTreeNode.children && this.parseTreeNode.children.length > 0) { // Default template is a link to the title
templateTree = this.parseTreeNode.children; templateTree = [{type: "element", tag: this.parseTreeNode.isBlock ? "div" : "span", children: [{type: "link", attributes: {to: {type: "string", value: title}}, children: [
} else { {type: "text", text: title}
// Default template is a link to the title ]}]}];
templateTree = [{type: "element", tag: this.parseTreeNode.isBlock ? "div" : "span", children: [{type: "link", attributes: {to: {type: "string", value: title}}, children: [
{type: "text", text: title}
]}]}];
}
}
if(!this.hasAttribute("hackCurrentTiddler")) {
templateTree = [{type: "tiddler", attributes: {tiddler: {type: "string", value: title}}, children: templateTree}]
} }
} }
// Return the list item // Return the list item
return {type: "listitem", itemTitle: title, children: templateTree}; return {type: "listitem", itemTitle: title, variableName: this.variableName, children: templateTree};
}; };
/* /*
@ -242,7 +236,7 @@ Compute the internal state of the widget
*/ */
ListItemWidget.prototype.execute = function() { ListItemWidget.prototype.execute = function() {
// Set the current list item title // Set the current list item title
this.setVariable("listItem",this.parseTreeNode.itemTitle); this.setVariable(this.parseTreeNode.variableName,this.parseTreeNode.itemTitle);
// Construct the child widgets // Construct the child widgets
this.makeChildWidgets(); this.makeChildWidgets();
}; };

View File

@ -1,5 +1,5 @@
title: $:/core/ui/TiddlerFields title: $:/core/ui/TiddlerFields
<table class="tw-view-field-table"><tbody><$list filter="[is[current]fields[]sort[title]] -text" template="$:/core/ui/TiddlerFieldTemplate" hackCurrentTiddler=true/> <table class="tw-view-field-table"><tbody><$list filter="[is[current]fields[]sort[title]] -text" template="$:/core/ui/TiddlerFieldTemplate" variable="listItem"/>
</tbody> </tbody>
</table> </table>

View File

@ -4,5 +4,5 @@ modifier: JeremyRuston
\define frame-classes() \define frame-classes()
tw-tiddler-frame $(missingTiddlerClass)$ $(shadowTiddlerClass)$ $(systemTiddlerClass)$ tw-tiddler-frame $(missingTiddlerClass)$ $(shadowTiddlerClass)$ $(systemTiddlerClass)$
\end \end
<div class=<<frame-classes>>><$list filter="[is[shadow]!has[draft.of]tag[$:/tags/ViewTemplate]] [!is[shadow]!has[draft.of]tag[$:/tags/ViewTemplate]]" hackTemplate=true/> <div class=<<frame-classes>>><$list filter="[is[shadow]!has[draft.of]tag[$:/tags/ViewTemplate]] [!is[shadow]!has[draft.of]tag[$:/tags/ViewTemplate]]" variable="listItem"><$transclude tiddler=<<listItem>>/></$list>
</div> </div>

View File

@ -3,5 +3,5 @@ title: $:/snippets/allfields
\define renderfield(title) \define renderfield(title)
<tr class="tw-view-field"><td class="tw-view-field-name">''$title$'':</td><td class="tw-view-field-value">//{{$:/docs/fields/$title$}}//</td></tr> <tr class="tw-view-field"><td class="tw-view-field-name">''$title$'':</td><td class="tw-view-field-value">//{{$:/docs/fields/$title$}}//</td></tr>
\end \end
<table class="tw-view-field-table"><tbody><$list filter="[fields[]sort[title]]"><$macrocall $name="renderfield" title=<<listItem>>/></$list> <table class="tw-view-field-table"><tbody><$list filter="[fields[]sort[title]]" variable="listItem"><$macrocall $name="renderfield" title=<<listItem>>/></$list>
</tbody></table> </tbody></table>

View File

@ -449,7 +449,7 @@ describe("Widget module", function() {
var wiki = new $tw.Wiki(); var wiki = new $tw.Wiki();
// Add some tiddlers // Add some tiddlers
wiki.addTiddlers([ wiki.addTiddlers([
{title: "$:/myTemplate", text: "<$tiddler tiddler=<<listItem>>><$view field='title'/></$tiddler>"}, {title: "$:/myTemplate", text: "(<$view field='title'/>)"},
{title: "TiddlerOne", text: "Jolly Old World"}, {title: "TiddlerOne", text: "Jolly Old World"},
{title: "TiddlerTwo", text: "Worldly Old Jelly"}, {title: "TiddlerTwo", text: "Worldly Old Jelly"},
{title: "TiddlerThree", text: "Golly Gosh"}, {title: "TiddlerThree", text: "Golly Gosh"},
@ -462,7 +462,7 @@ describe("Widget module", function() {
var wrapper = renderWidgetNode(widgetNode); var wrapper = renderWidgetNode(widgetNode);
//console.log(require("util").inspect(widgetNode,{depth:8,colors:true})); //console.log(require("util").inspect(widgetNode,{depth:8,colors:true}));
// Test the rendering // Test the rendering
expect(wrapper.innerHTML).toBe("<p>TiddlerFourTiddlerOneTiddlerThreeTiddlerTwo</p>"); expect(wrapper.innerHTML).toBe("<p>(TiddlerFour)(TiddlerOne)(TiddlerThree)(TiddlerTwo)</p>");
}); });
it("should deal with the list widget and empty lists", function() { it("should deal with the list widget and empty lists", function() {

View File

@ -24,16 +24,13 @@ The content of the `<$list>` widget is an optional template to use for rendering
|filter |The TiddlerFilter to display | |filter |The TiddlerFilter to display |
|template |The title of a template tiddler for rendering each tiddler in the list | |template |The title of a template tiddler for rendering each tiddler in the list |
|editTemplate |An alternative template to use for DraftTiddlers in edit mode | |editTemplate |An alternative template to use for DraftTiddlers in edit mode |
|variable |The [[widget variable|WidgetVariables]] name to be assigned the title of each tiddler in the list. Defaults to ''currentTiddler'' |
|emptyMessage |Message to be displayed when the list is empty | |emptyMessage |Message to be displayed when the list is empty |
|listview |Optional name of module responsible for animating/processing the list | |listview |Optional name of module responsible for animating/processing the list |
|history |The title of the tiddler containing the navigation history | |history |The title of the tiddler containing the navigation history |
|hackTemplate |If this attribute is present then the list items are directly transcluded without altering the current tiddler or using a template |
|hackCurrentTiddler |If this attribute is present then the template is directly transcluded without altering the current tiddler |
//`listview` and `history` are temporarily unimplemented // //`listview` and `history` are temporarily unimplemented //
//`hackTemplate` and `hackCurrentTiddler` are temporary hacks that will be removed//
!! Handling edit mode !! Handling edit mode
The `<$list>` widget can optionally render draft tiddlers through a different template tiddler than ordinary tiddlers -- see DraftMechanism for a discussion of how this capability is used. The `<$list>` widget can optionally render draft tiddlers through a different template tiddler than ordinary tiddlers -- see DraftMechanism for a discussion of how this capability is used.