mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-10-02 08:50:46 +00:00
471f28e52b
When using the widget with only short form SetText attributes (field="value) -- the default field (text) of the default tiddler is set to the default value ("") -- thus clearing the text field of the current tiddler. I have inserted a conditional to test for the presence of the `field` attribute. Whilst I was about this, I have inserted code to enable the setting of any number of TextReferences -- this fixes the issue of only being able to set one index per widget and also allows different tiddlers to be targeted by the same widget.
80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
/*\
|
|
title: $:/core/modules/widgets/action-setfield.js
|
|
type: application/javascript
|
|
module-type: widget
|
|
|
|
Action widget to set a single field or index on a tiddler.
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
var SetFieldWidget = function(parseTreeNode,options) {
|
|
this.initialise(parseTreeNode,options);
|
|
};
|
|
|
|
/*
|
|
Inherit from the base widget class
|
|
*/
|
|
SetFieldWidget.prototype = new Widget();
|
|
|
|
/*
|
|
Render this widget into the DOM
|
|
*/
|
|
SetFieldWidget.prototype.render = function(parent,nextSibling) {
|
|
this.computeAttributes();
|
|
this.execute();
|
|
};
|
|
|
|
/*
|
|
Compute the internal state of the widget
|
|
*/
|
|
SetFieldWidget.prototype.execute = function() {
|
|
this.actionTiddler = this.getAttribute("$tiddler",this.getVariable("currentTiddler"));
|
|
this.actionField = this.getAttribute("$field");
|
|
this.actionIndex = this.getAttribute("$index");
|
|
this.actionValue = this.getAttribute("$value");
|
|
this.actionTimestamp = this.getAttribute("$timestamp","yes") === "yes";
|
|
};
|
|
|
|
/*
|
|
Refresh the widget by ensuring our attributes are up to date
|
|
*/
|
|
SetFieldWidget.prototype.refresh = function(changedTiddlers) {
|
|
var changedAttributes = this.computeAttributes();
|
|
if(changedAttributes["$tiddler"] || changedAttributes["$field"] || changedAttributes["$index"] || changedAttributes["$value"]) {
|
|
this.refreshSelf();
|
|
return true;
|
|
}
|
|
return this.refreshChildren(changedTiddlers);
|
|
};
|
|
|
|
/*
|
|
Invoke the action associated with this widget
|
|
*/
|
|
SetFieldWidget.prototype.invokeAction = function(triggeringWidget,event) {
|
|
var self = this,
|
|
options = {};
|
|
options.suppressTimestamp = !this.actionTimestamp;
|
|
if (this.actionField) {
|
|
this.wiki.setText(this.actionTiddler, this.actionField, this.actionIndex, this.actionValue, options);
|
|
}
|
|
$tw.utils.each(this.attributes, function (attribute, name) {
|
|
if (name.charAt(0) !== "$") {
|
|
(name.search("!!") !== -1 || name.search("##") !== -1) ?
|
|
self.wiki.setTextReference(name, attribute, self.actionTiddler):
|
|
self.wiki.setText(self.actionTiddler, name, undefined, attribute, options);
|
|
}
|
|
});
|
|
return true; // Action was invoked
|
|
};
|
|
|
|
exports["action-setfield"] = SetFieldWidget;
|
|
|
|
})();
|