[TW 5.1.23] Toggle filter operator how to replace the field value

141 views
Skip to first unread message

Mohammad Rahmani

unread,
Dec 14, 2020, 7:44:42 AM12/14/20
to tiddl...@googlegroups.com
I use the below button to toggle between day/night palette 

<$button>Day/Night
<$action-listops $tiddler="$:/palette" $field="text" $subfilter="+[toggle[$:/palettes/Vanilla],[$:/palettes/SolarFlare]]" />
</$button>  
The problem here is if $:/palette is not empty and has not one of the two values above, then I get the wrong values in the text field (actually the value is appended). How can I replace the field value using toggle operator.



Best wishes
Mohammad

Saq Imtiaz

unread,
Dec 14, 2020, 8:41:13 AM12/14/20
to TiddlyWiki
Hmm. Yes it is a bit tricky since toggle[] is a list ops operator. So you need to handle the situation where this is an existing value that is not a part of the value pair to toggle. I'm short on time at the moment so there might be a better solution, but something like this untested solution should work:

<$action-listops $tiddler="$:/palette" $field="text" $filter="""[enlist{$:/palette}] -$:/palettes/Vanilla -$:/palettes/SolarFlare :and[then[$:/palettes/Vanilla]] :else[enlist{$:/palette}toggle[$:/palettes/Vanilla],[$:/palettes/SolarFlare]]""" />

Saq Imtiaz

unread,
Dec 14, 2020, 9:01:17 AM12/14/20
to TiddlyWiki
OR:

<$action-listops $tiddler="$:/palette" $field="text" $filter="""[{$:/palette}!match[$:/palettes/Vanilla]!match[$:/palettes/SolarFlare]then[$:/palettes/Vanilla]] :else[enlist{$:/palette}toggle[$:/palettes/Vanilla],[$:/palettes/SolarFlare]]""" />

Mohammad Rahmani

unread,
Dec 14, 2020, 9:40:24 AM12/14/20
to tiddl...@googlegroups.com
Hi Saq,
 Many thanks for both solutions. The second is more semantic and I use it.

Best wishes
Mohammad



Best wishes
Mohammad


--
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+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/2a806757-e69c-4838-9b16-04a38f57bbe7n%40googlegroups.com.

Mohammad Rahmani

unread,
Dec 14, 2020, 12:45:16 PM12/14/20
to tiddl...@googlegroups.com
Saq,
 Based on your suggestion, this may be another solution (not a good one but works!!)

<$button>Day/Night II
<$list filter="[{$:/palette}!match[$:/palettes/Vanilla]!match[$:/palettes/SolarFlare]]">
<$action-setfield $tiddler="$:/palette" $field="text" $value=""/>
</$list>

<$action-listops $tiddler="$:/palette" $field="text" $subfilter="+[toggle[$:/palettes/Vanilla],[$:/palettes/SolarFlare]]" />
</$button>

I think using the two $reveal widgets may be simpler :-) :-)

--Mohammad



Best wishes
Mohammad


On Mon, Dec 14, 2020 at 5:31 PM Saq Imtiaz <saq.i...@gmail.com> wrote:
--

Mohammad Rahmani

unread,
Dec 15, 2020, 3:59:11 AM12/15/20
to tiddl...@googlegroups.com
It seems in this case there are other simple solutions too, while in my opinion toggle is the most semantic one but in its current implementation is tricky to use for toggling a field can be changed outside the script.

These are other solutions that do not use toggle but very simple.  Of course simple is the best but not simpler.

!! Simple solution

<$button>Day/Night
<$list filter="[{$:/palette}match[$:/palettes/Vanilla]then[$:/palettes/SolarFlare]else[$:/palettes/Vanilla]]">
<$action-setfield $tiddler="$:/palette" text=<<currentTiddler>>/>
</$list>
</$button>



!! Good practice

<$vars
   lightPalette="$:/palettes/Vanilla"
   darkPalette="$:/palettes/SolarFlare"
>
<$button>Day/Night II
<$list filter="[{$:/palette}match<lightPalette>then<darkPalette>else<lightPalette>]" variable=selectedPalette>
<$action-setfield $tiddler="$:/palette" text=<<selectedPalette>>/>
</$list>
</$button>
</$vars>



You may read darkPalette and lightPalete from a config tiddler fields.

Best wishes
Mohammad

Saq Imtiaz

unread,
Dec 15, 2020, 4:09:55 AM12/15/20
to TiddlyWiki
@Mohammad yes the toggle operator is best used with list fields.

You can further simplify and skip the list widget:

<$vars
   lightPalette="$:/palettes/Vanilla"
   darkPalette="$:/palettes/SolarFlare"
>
<$button>Day/Night II
<$action-setfield $tiddler="$:/palette" text={{{[{$:/palette}match<lightPalette>then<darkPalette>else<lightPalette>]}}}/>
</$button>
</$vars>

Mohammad Rahmani

unread,
Dec 15, 2020, 4:26:53 AM12/15/20
to tiddl...@googlegroups.com
Hi Saq,
 Yes, dropping the list makes sense!
Your last solution seems the simplest by now ;-)

I will document this in TW-Script when I find free time!

Cheers
Mohammad



--
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+...@googlegroups.com.

TW Tones

unread,
Dec 15, 2020, 6:28:37 PM12/15/20
to TiddlyWiki
Saq et al,

I love these semi-competitive focused coding task threads. I like to generalise and simplify too, 

As a result I seem to have identified that the new toggle operator and parameters is not accepting this form (as in Saq's example)
text={{{[{$:/palette}toggle<lightPalette>,< darkPalette>] }}}/>

Is this a bug, limitation or a restriction?

Although; this generalised toggle does work
\define toggle-text(tiddler value1 value2)
<$button>Day/Night IV
<$action-setfield $tiddler="$tiddler$" text={{{ [[$tiddler$]get[text]] +[toggle[$value1$],[$value2$]] }}}/>
</$button>
\end
<<toggle-text "$:/palette" "$:/palettes/Vanilla" "$:/palettes/SolarFlare" >>

Since the above is worthy of a reusable global macro placing the macro definition in a tiddler tagged $:/tags/Macro
Then create a tiddler day-night containing;
<<toggle-text "$:/palette" "$:/palettes/Vanilla" "$:/palettes/SolarFlare" >>  

Then wherever you want the day night toggle just use
{{night-day}}

Your comments please!
This approach of mine is designed to capture functionality in a way it is very easy to use. 
  • I would invite my coding companions to consider the development of some de facto standards
  • The find a way to easily package it to other to acquire it (I have some already)
  • For a complete solution I would look at displaying the button as a icon that changes color eg red/green
Regards
Tones

TW Tones

unread,
Dec 15, 2020, 6:36:15 PM12/15/20
to TiddlyWiki
Post script,

In another project I am using unicode and I would call the night-day tiddler  night-dayⒷ
then if you search night or day this is listed and indicates its a button.  
{{night-dayⒷ}}  

Regards
tones

Mohammad Rahmani

unread,
Dec 15, 2020, 11:43:27 PM12/15/20
to tiddl...@googlegroups.com
Saq et al,

I love these semi-competitive focused coding task threads. I like to generalise and simplify too, 

I love these kind of scripting, as I learn new things ;-)



As a result I seem to have identified that the new toggle operator and parameters is not accepting this form (as in Saq's example)
text={{{[{$:/palette}toggle<lightPalette>,< darkPalette>] }}}/>

Is this a bug, limitation or a restriction?

Although; this generalised toggle does work
\define toggle-text(tiddler value1 value2)
<$button>Day/Night IV
<$action-setfield $tiddler="$tiddler$" text={{{ [[$tiddler$]get[text]] +[toggle[$value1$],[$value2$]] }}}/>
</$button>
\end
<<toggle-text "$:/palette" "$:/palettes/Vanilla" "$:/palettes/SolarFlare" >>

This is a very nice and semantic code! Note that the only caveat is users may have changed the palette from $:/ControlPanel and then your code will not work!
What we discussed above is how to prevent this!
--
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+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages