diff --git a/core/modules/widgets/action-deletefield.js b/core/modules/widgets/action-deletefield.js new file mode 100644 index 000000000..0669e97d4 --- /dev/null +++ b/core/modules/widgets/action-deletefield.js @@ -0,0 +1,73 @@ +/*\ +title: $:/core/modules/widgets/action-deletefield.js +type: application/javascript +module-type: widget + +Action widget to delete fields of a tiddler. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var Widget = require("$:/core/modules/widgets/widget.js").widget; + +var DeleteFieldWidget = function(parseTreeNode,options) { + this.initialise(parseTreeNode,options); +}; + +/* +Inherit from the base widget class +*/ +DeleteFieldWidget.prototype = new Widget(); + +/* +Render this widget into the DOM +*/ +DeleteFieldWidget.prototype.render = function(parent,nextSibling) { + this.computeAttributes(); + this.execute(); +}; + +/* +Compute the internal state of the widget +*/ +DeleteFieldWidget.prototype.execute = function() { + this.actionTiddler = this.getAttribute("$tiddler",this.getVariable("currentTiddler")); +}; + +/* +Refresh the widget by ensuring our attributes are up to date +*/ +DeleteFieldWidget.prototype.refresh = function(changedTiddlers) { + var changedAttributes = this.computeAttributes(); + if(changedAttributes["$tiddler"]) { + this.refreshSelf(); + return true; + } + return this.refreshChildren(changedTiddlers); +}; + +/* +Invoke the action associated with this widget +*/ +DeleteFieldWidget.prototype.invokeAction = function(triggeringWidget,event) { + var self = this, + tiddler = this.wiki.getTiddler(self.actionTiddler), + removeFields = {}; + if(tiddler) { + $tw.utils.each(this.attributes,function(attribute,name) { + if(name.charAt(0) !== "$" && name !== "title") { + removeFields[name] = undefined; + } + }); + this.wiki.addTiddler(new $tw.Tiddler(tiddler,removeFields)); + } + return true; // Action was invoked +}; + +exports["action-deletefield"] = DeleteFieldWidget; + +})(); diff --git a/editions/prerelease/tiddlers/ActionDeleteFieldWidget.tid b/editions/prerelease/tiddlers/ActionDeleteFieldWidget.tid new file mode 100644 index 000000000..d01703131 --- /dev/null +++ b/editions/prerelease/tiddlers/ActionDeleteFieldWidget.tid @@ -0,0 +1,36 @@ +caption: action-deletefield +created: 20141025120850184 +modified: 20141025120850184 +tags: Widgets ActionWidgets +title: ActionDeleteFieldWidget +type: text/vnd.tiddlywiki + +! Introduction + +The ''action-deletefield'' widget is an [[action widget|ActionWidgets]] that deletes specified fields of a tiddler. ActionWidgets are used within triggering widgets such as the ButtonWidget. + +! Content and Attributes + +The ''action-deletefield'' widget is invisible. Any content within it is ignored. + +|!Attribute |!Description | +|$tiddler |The title of the tiddler whose fields are to be modified (if not provided defaults to the [[WidgetVariable: currentTiddler]] | +|//{any attributes not starting with $}// |Each attribute name specifies a field to be deleted. The attribute value is ignored and need not be specified | + +! Examples + +Here is an example of a button that deletes the caption and tags fields of the current tiddler: + +<$macrocall $name='wikitext-example-without-html' +src='<$button> +<$action-deletefield caption tags/> +Delete "caption" and "tags" +'/> + +Here is an example of a button that deletes the modified date and tags fields of the tiddler HelloThere: + +<$macrocall $name='wikitext-example-without-html' +src='<$button> +<$action-deletefield $tiddler="HelloThere" modified tags/> +Delete "modified" and "tags" from ~HelloThere +'/>