/***
|''Name''|RandomTiddlerExpanded|
|''Description''|Generate a random tiddler|
|''Author''|Brent Bartlett (brent dot bartlett1 at gmail dot com)|
|''Version''|2.2|
|''Date''|2011-12-18 (Frimaire 28)|
|''Status''|@@stable@@|
|''Source''|http://starjelly.net/portfolio/javascript.php|
|''License''|[[GNU GPL 3.0|http://www.gnu.org/copyleft/gpl.html]]|
|''CoreVersion''|2.6.2|
|''Browser''|Tested on Firefox 5 |
!Usage
{{{-<term>}}} – Exclude tiddlers with the tag {{{<term>}}}; e.g. {{{-excludeTag}}}.
{{{+<term>}}} – Include tiddlers with the tag {{{<term>}}}. Note that this automatically excludes ALL other tiddlers!
For example, if you wanted only recipes, but only those without meat, assuming the tags ''recipes'' and ''meat'': {{{<<randomTiddler +recipes -meat>>}}}
(Parameters may be added in any order; {{{-meat +recipes}}} would work, too.) With no parameters, it will return //any// random tiddler. (Including system tiddlers. If you want to exclude these, use the parameter {{{-systemConfig}}}.)
You can put the name for your button in the second parameter. For example, to name the button "Random Quote of the Day", the syntax is {{{<<randomTiddler "Random Quote of the Day">>}}}.
Thanks to the original RandomTiddler plug-in for pointing me in the right direction.
***/
//{{{
( function( $ )
{
var macro = config.macros.randomTiddler = {
locale: {
label: "random note",
tooltip: "display a random tiddler"
},
handler: function( place, macroName, params, wikifier, paramString, tiddler )
{
var btnName = '';
if ( params[ 0 ] )
{
btnName = params[ 0 ];
if( ( btnName[ 0 ] == '+' ) || ( btnName[ 0 ] == '-' ) )
btnName = this.locale.label;
}
else
btnName = this.locale.label;
var btn = createTiddlyButton( place, btnName, this.locale.tooltip, function(){} );
btn.onclick = function()
{
story.closeAllTiddlers();
var addAll = true;
var tid = [];
var excludeTags = [];
var tiddlerArray = [];
function displayRandomTiddler()
{
if( !params[ 0 ] ) //if no parameters, do nothing...
{
}
else
for( var i = 0, ii = params.length; i < ii; i++ )
{
var theTag = params[ i ];
if( theTag[ 0 ] == "+" )
{
addAll = false;
tiddlerArray.push( store.getTaggedTiddlers( theTag.slice( 1, theTag.length ) ) );
}
else if( theTag[ 0 ] == "-" )
excludeTags.push( theTag.slice( 1, theTag.length ) );
}
if( addAll )
{
var tiddlers = store.getTiddlers();
var j = Math.floor( Math.random() * tiddlers.length );
tid = tiddlers[ j ];
}
else
{
var k = Math.floor( Math.random() * tiddlerArray.length );
var m = Math.floor( Math.random() * tiddlerArray[ k ].length );
tid = tiddlerArray[ k ][ m ];
}
for( var s = 0, ss = excludeTags.length; s < ss; s++ )
if( tid.tags.contains( excludeTags[ s ] ) )
displayRandomTiddler();
story.displayTiddler( null, tid );
}
displayRandomTiddler();
}
},
};
})( jQuery );
//}}}