TiddlyWiki.prototype.resolveTitle

3 views
Skip to first unread message

Saq Imtiaz

unread,
Sep 12, 2006, 2:13:02 PM9/12/06
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

Udo Borkowski

unread,
Sep 13, 2006, 6:01:18 AM9/13/06
to Tiddly...@googlegroups.com
Hi Saq,

I would probably the refactory "on step deeper" and make story.displayTiddler accept both a tiddler object or a tiddler title. And I would do it "inline", and check against the Tiddler class. I.e.:

Story.prototype.displayTiddler = function(srcElement,title,template,animate,slowly)
{
    var place = document.getElementById(this.container);
    if (title instanceof Tiddler) title = title.title;
    var theTiddler = document.getElementById(this.idPrefix + title);
...


With this change also displayTiddlers would accept an array of tiddlers and all your examples could be simplified in the same way.

(BTW: in the search case one needs a matcher.reverse() to ensure the same order as with the copying)

Maybe we should change this in 2.2...


Udo

----------
Udo Borkowski
http://www.abego-software.de

Saq Imtiaz

unread,
Sep 13, 2006, 6:12:41 AM9/13/06
to Tiddly...@googlegroups.com
On 9/13/06, Udo Borkowski <Udo.Bo...@gmx.de> wrote:
Hi Saq,

I would probably the refactory "on step deeper" and make story.displayTiddler accept both a tiddler object or a tiddler title.

Ah, right you are! That is definitely more practical.
 
And I would do it "inline", and check against the Tiddler class. I.e.:

Story.prototype.displayTiddler = function(srcElement,title,template,animate,slowly)
{
    var place = document.getElementById(this.container);
    if (title instanceof Tiddler) title = title.title;
    var theTiddler = document.getElementById(this.idPrefix + title);
...

I like that, it removes the need for the additional resolveTitle prototype, though that could possibly be useful in other places.
I think we should update my changes with your suggestions, and consider this  for the core for 2.2 as you suggested.

Cheers,
Saq

Udo Borkowski

unread,
Sep 13, 2006, 6:17:20 AM9/13/06
to Tiddly...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages