Jan
23
2008

Gravl: Autocompleting and Funky Timelines

If you haven’t had a chance to play with the RichUI plugin, you’ve got to take it for a spin – it’s just fantastic. I’ve been meaning to implement autocomplete for Gravl tags on new blog entries, and with the RichUI plugin (which uses Yahoo Autocomplete under the hood) it’s a total snack.

First you declare the RichUI taglibs in your gsp file (note: I’m using the undocumented delimChar setting so that I separate multiple tags with a space):

<resource:autoComplete skin="default" />
<richui:autoComplete name="tagList" delimChar=" " style="width: 100%"
    action="${createLinkTo(dir: params.blog+ '/blog/tagcomplete')}"/>

Then you add yourself a backend action that returns some xml representing the items for the list. I filter the tags by hand because I want to keep everything case insensitive, but your might be able to do the lot in a dynamic finder.

def tagcomplete = {
    Blog blogObj = Blog.findByBlogid(params.blog)
    def queryRegex = "(?i)${params.query}" // case insensitive...
    def tags = blogObj.tags.findAll { tag -> tag.name =~ queryRegex }
    render(contentType: "text/xml") {
        results() {
            tags.each {t ->
                result() {
                    name(t.name)
                }
            }
        }
    }
}

And you’ve got yourself an autocomplete for tags…

Gravl autocomplete in action

Not that we’ve got a nifty tag autocomplete, it’s time to take the timeline virtualisation tag for a spin… One the way into the timeline populate the start date for your entries…

def timeline = {
        Blog blogObj = Blog.findByBlogid(params.blog)
        def entries = BlogEntry.findAllByBlogAndStatus(blogObj, "published", [ sort: 'created', order: 'asc'])

        return [ blogObj: blogObj, startDate: entries[0]?.created  ]
    }

Then create the tags in your page to kick things off..

<resource:timeline />

<richui:timeline style="height: 500px; border: 1px solid #aaa" startDate="${startDate}" datasource="${createLinkTo(dir: params.blog+ '/timelineData')}" />
        <g:javascript>
            initTimeline();
        </g:javascript>

Which will callback to render the data on a timeline…


def timelineData = {

        def baseUri = request.scheme + "://" + request.serverName +
                 (request.serverPort != 80 ? ":" +  request.serverPort : "") +
                 grailsAttributes.getApplicationUri(request)

        Blog blogObj = Blog.findByBlogid(params.blog)
        def entries = BlogEntry.findAllByBlogAndStatus(blogObj, "published", [ sort: 'created', order: 'asc'])

        println "Timeline rendering for ${entries.size()} entries"

        render(contentType: "text/xml") {
            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm:ss", Locale.US)

            data() {
                entries.each { entry ->
                    event(start: sdf.format(entry.created),
                            title: entry.title,
                            link: baseUri + entry.toPermalink(),
                            "" )
                    }
            }
        }
    }

And you have one funky looking timeline… (click on the Timeline link on the right gutter if you want to play with the live one)

Gravl timeline in action

Well thats enough UI work for one day… Huge props to Andreas for an outstanding plugin!

About the Author: Glen Smith

7 Comments + Add Comment

  • Hi Glen!

    I like the timeline a lot. How about displaying a thumbnail of the blog post in the popup?

  • wonder why the delimChar is not documented, seems like something that could be useful for us developers :)

  • Man, your posts are always interesting … is it the aussie climate?

    Keep up. All of the Grails community owe you for evangelizing al of the great features there are in Grails.

    - a faithful reader.

  • Nice job Glen!

    But you have too many entries on Gravl recently to fit on your timeline!

  • Well, it seems I forgot to document the delimChar attribute. I updated the wiki, so it’s now there. Thanks for your praise and keep up your excellent work.

  • Hello Glen!

    Lots of funky looking widgets you show us! Thanks!

    But would you have some architecting tips on how to have a library of ajax widgets used by an application..?

    My concern is to decouple (if possible) my viewing code from these ajax add ons.

    Sepcially as the ajax species are sprouting up from every where.

    I would like a way to make sure my application design can take in newer ajax stuff.

    Cheers

  • I’m late to this party but just had my first crack at using the timeline plugin (RichUI). I love it, wasn’t hard to implement. Now if I could get it to display the dates in the popup in PDT rather than GMT….

Leave a comment

Glen Smith

About Glen

Co-author Grails in Action