From c72a0b7a679fd8a019921571560f60b557ccb3d4 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Fri, 21 Oct 2016 11:26:26 +0100 Subject: [PATCH] Add action-createtiddler widget Basically the same as sending a tm-new-tiddler message except that the newly created tiddler is not added to the story. --- core/modules/widgets/action-createtiddler.js | 81 +++++++++++++++++++ .../widgets/ActionCreateTiddlerWidget.tid | 30 +++++++ .../ActionCreateTiddlerWidget_Example.tid | 12 +++ 3 files changed, 123 insertions(+) create mode 100644 core/modules/widgets/action-createtiddler.js create mode 100644 editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid create mode 100644 editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid diff --git a/core/modules/widgets/action-createtiddler.js b/core/modules/widgets/action-createtiddler.js new file mode 100644 index 000000000..01010b940 --- /dev/null +++ b/core/modules/widgets/action-createtiddler.js @@ -0,0 +1,81 @@ +/*\ +title: $:/core/modules/widgets/action-createtiddler.js +type: application/javascript +module-type: widget + +Action widget to create a new tiddler with a unique name and specified fields. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var Widget = require("$:/core/modules/widgets/widget.js").widget; + +var CreateTiddlerWidget = function(parseTreeNode,options) { + this.initialise(parseTreeNode,options); +}; + +/* +Inherit from the base widget class +*/ +CreateTiddlerWidget.prototype = new Widget(); + +/* +Render this widget into the DOM +*/ +CreateTiddlerWidget.prototype.render = function(parent,nextSibling) { + this.computeAttributes(); + this.execute(); +}; + +/* +Compute the internal state of the widget +*/ +CreateTiddlerWidget.prototype.execute = function() { + this.actionBaseTitle = this.getAttribute("$basetitle"); + this.actionSaveTitle = this.getAttribute("$savetitle"); + this.actionTimestamp = this.getAttribute("$timestamp","yes") === "yes"; +}; + +/* +Refresh the widget by ensuring our attributes are up to date +*/ +CreateTiddlerWidget.prototype.refresh = function(changedTiddlers) { + var changedAttributes = this.computeAttributes(); + if($tw.utils.count(changedAttributes) > 0) { + this.refreshSelf(); + return true; + } + return this.refreshChildren(changedTiddlers); +}; + +/* +Invoke the action associated with this widget +*/ +CreateTiddlerWidget.prototype.invokeAction = function(triggeringWidget,event) { + var title = this.wiki.generateNewTitle(this.actionBaseTitle), + fields = {}, + creationFields, + modificationFields; + $tw.utils.each(this.attributes,function(attribute,name) { + if(name.charAt(0) !== "$") { + fields[name] = attribute; + } + }); + if(this.actionTimestamp) { + creationFields = this.wiki.getCreationFields(); + modificationFields = this.wiki.getModificationFields(); + } + var tiddler = this.wiki.addTiddler(new $tw.Tiddler(creationFields,fields,modificationFields,{title: title})); + if(this.actionSaveTitle) { + this.wiki.setTextReference(this.actionSaveTitle,title,this.getVariable("currentTiddler")); + } + return true; // Action was invoked +}; + +exports["action-createtiddler"] = CreateTiddlerWidget; + +})(); diff --git a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid new file mode 100644 index 000000000..a7d223e4e --- /dev/null +++ b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid @@ -0,0 +1,30 @@ +caption: action-createtiddler +created: 20161020152745942 +modified: 20161020155119177 +tags: Widgets ActionWidgets +title: ActionCreateTiddlerWidget +type: text/vnd.tiddlywiki + +! Introduction + +The ''action-createtiddler'' widget is an [[action widget|ActionWidgets]] that creates new tiddlers. ActionWidgets are used within triggering widgets such as the ButtonWidget. + +There are several differences from the [[tm-new-tiddler message|WidgetMessage: tm-new-tiddler]]: + +* The new tiddler is not automatically displayed in the [[story river|StoryRiver]] +* The title of the new tiddler is made available for subsequent operations + +! Content and Attributes + +The ''action-createtiddler'' widget is invisible. Any content within it is ignored. + +|!Attribute |!Description | +|$basetitle |The initial title that will be attempted. If a tiddler with that title already exists, then a numerical counter is added to the title and incremented until it is unique| +|$savetitle |A text reference identifying a field or index into which the title of the newly created tiddler will be stored after it is created | +|$timestamp |Specifies whether the timestamp(s) of the target tiddler will be updated (''modified'' and ''modifier'', plus ''created'' and ''creator'' for newly created tiddlers). Can be "yes" (the default) or "no" | +|//{any attributes not starting with $}// |Each attribute name specifies a field to be created in the new tiddler | + +! Examples + +<$macrocall $name='wikitext-example-without-html' +src={{ActionCreateTiddlerWidget Example}}/> diff --git a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid new file mode 100644 index 000000000..2be233566 --- /dev/null +++ b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid @@ -0,0 +1,12 @@ +created: 20161020153426686 +modified: 20161020155142990 +tags: ActionCreateTiddlerWidget +title: ActionCreateTiddlerWidget Example +type: text/vnd.tiddlywiki + +New button caption: <$edit-text tiddler="$:/state/new-button-caption" tag="input" default=""/> + +<$button> +<$action-createtiddler $basetitle="Homemade Button" tags="$:/tags/PageControls" text={{$:/state/new-button-caption}}/> +Create non-functional page control button + \ No newline at end of file