Saq Imtiaz
unread,Sep 12, 2006, 2:13:02 PM9/12/06Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to TiddlyWikiDev
Hey guys,
This isnt an actual refactoring request, its more of a "I've started using this, and it might be useful in the core but I'm not sure". :)
One of the somewhat frustrating things when writing TW code can be when you have an array of tiddler titles, and need an array of the corresponding tiddlers. You end up having to create another array... however, in
TW2.1, the newly introduced resolveTiddler function helps out a lot there. You can pass either a tiddler or a tiddler title to a function, and it can make sure it gets the tiddler by using resolveTiddler.
However, sometimes you need the opposite. You have an array of tiddlers, and you need an array of titles..... So I wrote myself the opposite of the resolveTiddler function, a resolveTitle function:
// Resolves a Tiddler reference or tiddler title into a tiddler title string, or null if it doesn't exist
// we might want a 'if t instanceOf Tiddler' check here as well
TiddlyWiki.prototype.resolveTitle = function(t)
{
t = (typeof t == 'string') ? t : t.title;
return store.tiddlerExists(t) ? t : null;
}
A quick look through the core showed that this could possibly prove handy in a few places, though I am not sure that the potential gain is worth the change and overhead. The following core changes could be made:
config.paramifiers.tag = {
onstart: function(v) {
var tagged = store.getTaggedTiddlers(v,"title");
//for(var t=0; t<tagged.length; t++)
story.displayTiddlers("bottom",tagged,null,false,false);
}
};
Story.prototype.displayTiddlers = function(srcElement,titles,template,animate,slowly)
{
for(var t = titles.length-1;t>=0;t--)
this.displayTiddler(srcElement,store.resolveTitle(titles[t]),template,animate,slowly);
}
Story.prototype.search = function(text,useCaseSensitive,useRegExp)
{
this.closeAllTiddlers();
highlightHack = new RegExp(useRegExp ? text : text.escapeRegExp(),useCaseSensitive ? "mg" : "img");
var matches = store.search
(highlightHack,"title","excludeSearch");
//var titles = [];
//for(var t=matches.length-1; t>=0; t--)
//
titles.push(matches[t].title);
this.displayTiddlers(null,matches);
highlightHack = null;
var q = useRegExp ? "/" : "'";
if(matches.length > 0)
displayMessage(config.macros.search.successMsg.format([
matches.length.toString(),q + text + q]));
else
displayMessage(config.macros.search.failureMsg.format([q + text + q]));
}
function onClickTagOpenAll(e)
{
if (!e) var e = window.event;
var tag =
this.getAttribute("tag");
var tagged = store.getTaggedTiddlers(tag);
//var titles = [];
//for(var t=tagged.length-1; t>=0; t--)
// titles.push(tagged[t].title);
displayTiddlers(this,
tagged);
return(false);
}
Cheers,
Saq