valid TW5 rule for checkbox?

162 views
Skip to first unread message

senechaux

unread,
Apr 23, 2015, 7:54:51 PM4/23/15
to tiddly...@googlegroups.com
Hi Guys,

Not sure if this is possible but found it creating the checkbox with some gotchas.
/*\
title: $:/core/modules/parsers/wikiparser/rules/checkboxrule.js
type: application/javascript
module-type: wikirule
\*/

(function(){

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

  exports
.name = "checkboxrule";
  exports
.types = {inline: true};

  exports
.init = function(parser) {
   
this.parser = parser;
   
// Regexp to match
   
this.matchRegExp = /\[([xX_ ])\] ?(.*)/mg;
 
};

  exports
.parse = function() {
   
this.parser.pos = this.matchRegExp.lastIndex;
   
var checked = ($tw.utils.trim(this.match[1]).toUpperCase()=="X");
   
var lbl = $tw.utils.trim(this.match[2]);
   
return [{type: "checkbox",
        tag
: "$checkbox",
        attributes
: {
            id            
: {type: "string", value: "AM_"+lbl},
            field        
: {type: "string", value: lbl},
           
checked        : {type: "string", value: true},
           
unchecked    : {type: "string", value: false},
           
default        : {type: "string", value: checked}
       
},
        children
: [{
            type
: "text",
            text
: " "+lbl
       
}]
   
},
   
{type: "element", tag: "br"}
   
];
};
})();
The use of a default appears to cause problems (a 3rd state appears where 'checked' but field shows false) and the label doesn't get wikified.  Am I going about this the wrong way or is this a valid rule?

Thanks for your thoughts.

Jeremy Ruston

unread,
Apr 24, 2015, 11:21:19 AM4/24/15
to TiddlyWikiDev
Hi Senechaux

That's a good start, but not quite the correct way to have the label parsed. I'd suggest looking at the rule for headings:


Best wishes

Jeremy.


--
You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywikide...@googlegroups.com.
To post to this group, send email to tiddly...@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywikidev.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywikidev/2b6be1b7-fdc1-46cc-8d7a-b06e6619057e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jeremy Ruston
mailto:jeremy...@gmail.com

senechaux

unread,
Apr 24, 2015, 5:57:36 PM4/24/15
to tiddly...@googlegroups.com
Thanks Jeremy.. that helps a lot for the wikification.  Still struggling with the checkbox "default" to work properly.  Is there some way to get current tiddler name and tiddler field values from within the parser? (I suspect I have to check for field existence to get the checkbox "default" to work correctly.)  FYI, lots of time with TWC but still feeling my way around TW5

Jeremy Ruston

unread,
May 4, 2015, 4:19:59 PM5/4/15
to TiddlyWikiDev, senechaux
Hi Senechaux

> Is there some way to get current tiddler name and tiddler field values from within the parser?

No. Tiddlers are parsed independently of the current tiddler; it's only at the rendering part of the process that the current tiddler concept exists.

Unfortunately, if you're trying to have the checkbox dynamically update the underlying wikitext to change the state, then I'm afraid you've chosen a very difficult first project! How are you getting on now?

Best wishes

Jeremy.






--
You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywikide...@googlegroups.com.
To post to this group, send email to tiddly...@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywikidev.

For more options, visit https://groups.google.com/d/optout.

senechaux

unread,
Feb 5, 2016, 11:17:50 AM2/5/16
to TiddlyWikiDev
Hi Jeremy,

It's been some time but I've finally found time to continue this effort with TW5.1.11.  (Love the tiddler folding)  I've removed the previous "default" idea from the checkbox (and an equivalent radio) rule and moving forward.  I'm assuming one can "initialize" the tiddler defaults by setting tiddler field values "manually" when creating the tiddlers containing multiple checkboxes only or radios only.

This turned into a new problem for both the checkbox rule (similar to that shown previously) and radio rule.

The checkbox rule needs to wikifyPlain() (TWC term) and remove/change invalid chars for the corresponding tiddler field -- value set to true/false.  In addition, I would like the radio rule to wikifyPlain() the radio label to provide the value of a static field name.

Is there a TW5 method I can use to provide the equivalent of wikifyPlain() in a TW5 rule?

The overall implementation effort is attempting to allow an end-user (new TW5 user) to create the "questions" (using checkbox/radio rules rather than the more cryptic widget syntax).  User will then process the wiki's "wikifyPlain()" fields/values as needed.

Hope this makes sense.  Thanks for yours or anyone's thoughts.
PS. Is there an easy way to enable folding on a tiddler being edited?


On Thursday, April 23, 2015 at 6:54:51 PM UTC-5, senechaux wrote:

Jeremy Ruston

unread,
Feb 5, 2016, 3:25:16 PM2/5/16
to tiddly...@googlegroups.com
Hi Senechaux

Good stuff. The task you’ve set yourself is quite a tricky one, with some subtleties that have put me off addressing it myself, I’ll be interested to see how you get on, and happy to help.

It's been some time but I've finally found time to continue this effort with TW5.1.11.  (Love the tiddler folding)  I've removed the previous "default" idea from the checkbox (and an equivalent radio) rule and moving forward.  I'm assuming one can "initialize" the tiddler defaults by setting tiddler field values "manually" when creating the tiddlers containing multiple checkboxes only or radios only.

I’m not quite sure what you’re asking here. What do you mean by “creating the tiddlers containing multiple checkboxes only or radios only”

The checkbox rule needs to wikifyPlain() (TWC term) and remove/change invalid chars for the corresponding tiddler field -- value set to true/false.  In addition, I would like the radio rule to wikifyPlain() the radio label to provide the value of a static field name.

Parsers have no access to the results of wikification; parsers are in fact the first stage of the processing pipeline that implements the tw5 equivalent of wikifyPlain.

The usual way to approach things like this is to have both a parser and widget: the parser would emit the widget, and the widget would handle the runtime logic. Look at how the transclusion rules relate to the transclude widget.

The tricky part, though, is making sure that you’ve got the right reference to change the [X] to a [ ]. Widgets don’t have access to the wikitext that generated them. Roughly, it's because that wikitext might have been dynamically generated, eg by a macro. So your parser will have to pass along enough information to the widget to allow it to locate the right part of the right tiddler.

Best wishes

Jeremy.

--
You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywikide...@googlegroups.com.
To post to this group, send email to tiddly...@googlegroups.com.

senechaux

unread,
Feb 5, 2016, 4:22:06 PM2/5/16
to TiddlyWikiDev
Hi Jeremy,

Sorry about any confusion.  

The meaning of phrase "creating the tiddlers containing multiple checkboxes only or radios only” is this:
There are two types of tiddlers/questions (think quiz with tiddler per question) relevant to my issue:
Each tiddler containing multiple radio widgets, for example, is simple a multiple choice "question".  Using the radio widget field parameter, the answer is stored in this tiddler's dedicated field (named 'answer' for example).  The value is the "plain" radio's label (the span next to the button; not sure what to call it).  Similar with tiddler containing checkbox widgets but "plain", in this case, is a tiddler field "name".

I see I'll need a "wrapper widget" for the checkbox/radio widget so data is more easily handled deeper in the pipeline.  This makes sense but I fear I still need the wikifyPlain() ability to handle the radio field (from $radio field parameter) value and converted (TW5 field name requirement) checkbox (from $checkbox field parameter) field names.  I've abandoned the idea of using the "default" syntax mentioned earlier (see solution in previous post) so tiddler will only appear as, for example:

Choose the best answer(s):
[ ] //multichoice 1//
[ ] //multichoice 2//
[ ] //multichoice 3//

Here, the tiddler would have tiddler field names of "multichoice 1", "multichoice 2" and "multichoice 3" (note use if wikifyPlain()) and values of true/checked or false/unchecked.

The single-answer questions use the radio widget and another custom rule for questions using $radio.  That is:
( ) //choice 1//
( ) //choice 2//
...

Hope that helps you understand my use case better.  I'll get busy creating a widget that essentially "wraps" the radio or checkbox widget.  Will post again with progress and any unsolved issue with my need for wikiPlain().

Thank you Jeremy.
Reply all
Reply to author
Forward
0 new messages