parameters in the URL

1 view
Skip to first unread message

BradleyMeck

unread,
Dec 26, 2005, 2:56:47 PM12/26/05
to TiddlyWiki
This has long been asked for but never done, parameters that can be
loaded from the url, search tags and other things are planned in the
upcoming TW's but I figured having something like the wikifier would be
better so people like clint could play with it and make it do
interesting things.

I have made a modified TW with all of this, but unfortunately it must
be hard coded and not a plugin... Anywho, see what you like about it, I
have sent it to jeremy and you might be able to ask for other features
before it becomes part of the next TW (if it does that is).

it uses the location.search part of the url before the hash .

for example, use
empty.html?search=test

to search for test in all of the tiddlers.

the code to do this replaces the main function to the end of the
restart function.

Email me for the file (still trying to think of things to do, and
unfortunately on vacation cannot reach my web hosting...)

anywho, here is the code.

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

var safeMode = false; // Whether to load systemConfig or not
var cancelParams = false;
var TWparams = new Paramifier();

function Paramifier(){
this.params = new Array();
this.actions = new Array();
}

Paramifier.prototype.add = function(ParamMatch,Action){
var addition = {match: ParamMatch, action: Action};
this.actions.push(addition);
}

Paramifier.prototype.paramify = function(){
if(this.params.find("cancelparams")!=null){
cancelParams = true;
return;
}
for(var i = 0; i < this.actions.length; i++){
for(var j = 0; j < this.params.length; j++){
if(this.actions[i].match == this.params[j][0]){
this.actions[i].action(this.params[j]);
}
}
}
}

TWparams.add("lookup",function(str){
if(str.length<3){
displayMessage("cannot use lookup parameter , not enough parts
given");
return;
}
var tiddlers = store.reverseLookup(str[1],str[2],true,"title");
for(var i = 0 ; i < tiddlers.length; i++)
story.displayTiddler(null,tiddlers[i].title)
})

TWparams.add("safemode",function(){
safeMode = true;
})

TWparams.add("tag",function(str){
if(str.length<2){
displayMessage("cannot use tag parameter , not enough parts given");
return;
}
var tiddlers = store.getTaggedTiddlers(str[1]);
if(tiddlers.length==0){
displayMessage("no tiddlers are tagged " + str[1])
}
for(var i = 0 ; i < tiddlers.length; i++)
story.displayTiddler(null,tiddlers[i].title)
})

TWparams.add("search",function(str){
if(str.length<2){
displayMessage("cannot use search parameter , not enough parts
given");
return;
}
var tiddlers = store.search(str[1]);
for(var i = 0 ; i < tiddlers.length; i++)
story.displayTiddler(null,tiddlers[i].title)
})

// Starting up
function main()
{
addEvent(document,"click",Popup.onDocumentClick);
saveTest();
loadOptionsCookie();
for(var s=0; s<config.notifyTiddlers.length; s++)
store.addNotification(config.notifyTiddlers[s].name,config.notifyTiddlers[s].notify);
store.loadFromDiv("storeArea","store");

if(window.location.search){
var params = window.location.search.split("?")
for(var i = 1; i < params.length; i++){
TWparams.params.push(params[i].match(/[^\=]+/g));
}
}

if(TWparams.params.find("safemode")==null)
loadSystemConfig();

formatter = new Formatter(config.formatters);
readOnly = (document.location.toString().substr(0,7) == "http://") ?
config.options.chkHttpReadOnly : false;
store.notifyAll();
TWparams.paramify();
restart();
}

// Restarting
function restart()
{
var start = store.getTiddlerText("DefaultTiddlers");
if(window.location.hash){
var links =
convertUTF8ToUnicode(decodeURI(window.location.hash.substr(1))).split("\#");
for(var i = 0; i < links.length; i++){
if(!store.getTiddler(links[i])){
displayMessage("Tiddler " + links[i] + " does not exist.")
links.splice(i,1);
}
}
if(links.length > 0){
story.displayTiddlers(null,links);
return;
}
}
if(start)
story.displayTiddlers(null,start.readBracketedList());
}

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

Reply all
Reply to author
Forward
0 new messages