I wrote a very basic plugin to print text depending on the content of a 
field or a presence of a tag.
I need it to mainly to set the class of rows and cells in a table.

Until now I used a field to do this, simply filling it with the needed 
class value to transclude when the table is generated.

But I'm looking for a way to calculate the class only by changing a tag or 
a value of a field, without the need to manually set also a "class-field" 
in the tiddler.

The plugin works like this:

<$case tiddler="The Tiddler" field="status" 
values="val1=qwerty;val2=xpto;blabla=status-bla" default="none">

If "The Tiddler" has a field "status" with the value "val1", it prints 
"qwerty", if the value is "val2", prints "xpto" and so on... if nothing 
matches, it print the default value "none".

<$case tiddler="The Tiddler" field="tags" values="tag1=qwerty;tag2=xpto" 
default="">

In a similar way, it test the tags if the value "tags" is specified in the 
"field" parameter.
Note that, at this time the widget exits at the first match (ie: if both, 
"tag1" and "tag2" are assigned to "The Tiddler", the response is "qwerty").

The question is: how can I use/rewrite the widget to include it in a table 
written using the inline HTML? Because this does not work:

<tr class=<$case field="tags" values="tag1;tag2;tag3" default="" /> >
  <td>...</td>
</tr>

I forgot to say that when the value at the right of the equal is omitted, 
the widget assumes it is the same as the test value ( "tag1;tag2;tag3" => 
"tag1=tag1;tag2=tag2;tag3=tag3" ).







Here the code:


/*\
title: $:/plugins/test/widgets/case.js
type: application/javascript
module-type: widget

Case widget

\*/
(function(){
    
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

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

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

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

/*
Render this widget into the DOM
*/
CaseWidget.prototype.render = function(parent,nextSibling) {
    this.parentDomNode = parent;
    this.computeAttributes();
    this.execute();
    var textNode = this.document.createTextNode(this.returnValue);
    parent.insertBefore(textNode,nextSibling);
    this.domNodes.push(textNode);
};

/*
Compute the internal state of the widget
*/
CaseWidget.prototype.execute = function() {
    // Get parameters from our attributes
    this.tiddlerTitle = this.getAttribute("tiddler",this.getVariable(
"currentTiddler"));
    this.fieldName = this.getAttribute("field");    // if "tags" tests 
tags, else tests <fieldName> if exists
    this.testValues = this.getAttribute("values");
    this.returnValue = this.getAttribute("default");

    var oTest = {};    
    if (this.testValues) {
        var tagList = this.testValues.split(";");
        for (var i=0; i<tagList.length; i++) {
            var pair = tagList[i].split("=");
            if (pair.length == 1) {
                oTest[pair[0]] = pair[0];
            } else if (pair.length == 2) {
                oTest[pair[0]] = pair[1];
            }
        }
    }
     
    var tiddler = $tw.wiki.getTiddler(this.tiddlerTitle);
    if (tiddler) {
        if (this.fieldName == 'tags') {
            for (var key in oTest) {
                if (tiddler.fields.tags.indexOf(key) >= 0) {
                    this.returnValue = oTest[key];
                    break;
                }
             }
        } else {
            for (var key in oTest) {
                if (tiddler.fields[this.fieldName] == key) {
                    this.returnValue = oTest[key];
                    break;
                }
            }
        }
    }
};

/*
Selectively refreshes the widget if needed. Returns true if the widget or 
any of its children needed re-rendering
*/
CaseWidget.prototype.refresh = function(changedTiddlers) {
    // Re-execute the filter to get the count
    var oldValue = this.returnValue;
    this.execute();
    if(this.returnValue !== oldValue) {
        // Regenerate and rerender the widget and replace the existing DOM 
node
        this.refreshSelf();
        return true;
    } else {
        return false;
    }
};

exports.case = CaseWidget;

})();




-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/tiddlywiki.
For more options, visit https://groups.google.com/d/optout.

Reply via email to