Here's a variation on action-setfield. You call it almost the same: 
action-setfield-inc. The difference is that if there is a +value field, it 
will use that instead of $value and will ADD it to the existing specified 
field of the specified tiddler. I've only briefly tested it. Consider it 
"alpha" and be sure to make a back-up of your files before playing with it.

Example:

<$button>
<$action-setfield-inc $tiddler="ActionSetValueTarget" $field="increment" 
+value="2" />

<$action-navigate $to="$:/ControlPanel"/>
Go to Control Panel "Appearance" tab
</$button>

This will let you save and count by two every time you click the button 
into field "increment" (previously created) of tiddler ActionSetValueTarget 
(previously created).
 
Mark

/*\
title: $:/mars/widgets/action-setfield-inc.js
type: application/javascript
module-type: widget

Action widget to set a single field or index on a tiddler. 

PLUS ANY OTHERS SPECIFIED  (per the actual code)

If inc value specified then that value will be used to increment value 
found in field of tiddler.
and value will be ignore.

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

var Widget = require("$:/core/modules/widgets/widget.js").widget;

var ActionSetFieldPlusWidget = function(parseTreeNode,options) {
    this.initialise(parseTreeNode,options);
};

/*
Inherit from the base widget class
*/
ActionSetFieldPlusWidget.prototype = new Widget();

/*
Render this widget into the DOM
*/
ActionSetFieldPlusWidget.prototype.render = function(parent,nextSibling) {
    this.computeAttributes();
    this.execute();
};

/*
Compute the internal state of the widget
*/
ActionSetFieldPlusWidget.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.actionValuePlus = this.getAttribute("+value") ;
};

/*
Refresh the widget by ensuring our attributes are up to date
*/
ActionSetFieldPlusWidget.prototype.refresh = function(changedTiddlers) {
    var changedAttributes = this.computeAttributes();
    if(changedAttributes["$tiddler"] || changedAttributes["$field"] || 
         changedAttributes["$index"]   || changedAttributes["$value"] ||
         changedAttributes["+value"]         ) 
        {
        this.refreshSelf();
        return true;
    }
    return this.refreshChildren(changedTiddlers);
};

/*
Invoke the action associated with this widget 
*/
ActionSetFieldPlusWidget.prototype.invokeAction = 
function(triggeringWidget,event) {
    var self = this;
    var actionval = this.actionValue ;
    if(typeof this.actionValuePlus === "string") {
            actionval = Number(this.actionValuePlus) ;
            var fv = this.getFieldVal() ;
//alert("I just fetched value from tiddler/field 
"+this.actionTiddler+"/"+this.actionField+": " + fv) ;
            fv = (typeof fv === "string") ? Number(fv) : 0 ;
//alert("I think actionval before adding is: "+actionval) ;
            actionval = actionval + fv ;
            actionval = actionval.toString() ;
//alert("I think actionval after adding and toString is: " + actionval) ;

    }
    if(typeof actionval === "string") {
        
this.wiki.setText(this.actionTiddler,this.actionField,this.actionIndex,actionval);
    
    
    }
    $tw.utils.each(this.attributes,function(attribute,name) {
        if(name.charAt(0) !== "$" && name.charAt(0) !== "+" ) {
            self.wiki.setText(self.actionTiddler,name,undefined,attribute);
        }
    });
    return true; // Action was invoked
};


ActionSetFieldPlusWidget.prototype.getFieldVal = function() {
    // Get the edit value
        var tiddler = this.wiki.getTiddler(this.actionTiddler);
        if(tiddler) {
//alert("I found a tiddler") ;

            // If we've got a tiddler, the value to display is the field 
string value
            return tiddler.getFieldString(this.actionField);
        } else {
                return "" ;            
        }

};

//(setq tab-width 2) ; 

exports["action-setfield-inc"] = ActionSetFieldPlusWidget;


})();



-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tiddlywiki+unsubscr...@googlegroups.com.
To post to this group, send email to tiddlywiki@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywiki.
For more options, visit https://groups.google.com/d/optout.

Reply via email to