You can create customized versions of the various javascript functions
and use those instead of the default ones.
For example, I might want a timeline showing movies playing in local
theaters. I have a database of movies on my server and I can return a
list of movies for a given date range as JSON. The data might look
like this:
{"events" : [
{"start":"2009 Aug 06 15:30",
"end" : "2009 Aug 06 17:35",
"title" : "Shark Attack!",
"genre" : "Action/Adventure",
"rating" : "PG13",
"description" : "Angry sharks with lasers cause mayhem."},
{....etc}
When the user clicks on a movie, I want to show them a nice little
summary of the movie in the bubble. The default fillInfoBubble method
will automatically show the title and description in the bubble, but I
want it to show the start and end times, the rating and the genre for
the movie. I'll also highlight any movie that has an "R" rating with a
note.
To do this, just make your own version of this function: (I used
innerHTML here because it's quick and dirty. There are many different
ways of filling in the content of the bubble).
Timeline.DefaultEventSource.Event.prototype.fillInfoBubble =
function(elmt, theme, labeller) {
var rating = this.getProperty("rating");
var bubbleText = "<div class='title'>" + this.getText() + "</div>" +
"<table class='movie'><tr><th>Description:</th>" +
"<td style='width:65%'>" + this.getDescription() + "</td></tr>"+
"<tr><th>Genre:</th><td>" +
this.getProperty("genre") + "</td></tr>" +
"<tr><th>Rating" + "</th><td>" +
rating +
(rating == "R" ? "<br>(Note: No one under 13 allowed)" : "") +
"</td></tr>" +
"<tr><th>Start:</th>" +
"<td>" + this.getStart() + "</td></tr>" +
"<tr><th>End:</th>" +
"<td>" + this.getEnd() + "</td></tr></table>";
elmt.innerHTML= bubbleText;
}
I include this custom fillInfoBubble function in my javascript and
make sure it loads after I load Timeline (so it overrides the
default). Now when the user clicks on a movie, they see this nice
little table of movie information.
I was able to include whatever attributes I want in the JSON that is
sent back from my server (genre, rating, etc) and customize the
infoBubble to show those custom attributes using the .getProperty()
method on my event.
Suppose I wanted to show "rating" on the timeline itself, as part of
the label. This is done by the painter. Suppose we're using the
original painter. The labels for the events are made in
Timeline.OriginalEventPainter.prototype._paintEventLabel.
We can copy and paste the entire function into our own file and then
change it:
labelDiv.innerHTML = text + " (" + evt.getProperty("rating") + ")";
Now, the genre for every movie on our timeline is appended to the label:
Shark Attack! (PG13)
So there are two examples of customizing the contents of the bubble
and the labels on the timeline using custom attributes for an event
that are passed inside the JSON.
Feel free to ask if you have any more questions.
--Mike Nosal
Timeline.OriginalEventPainter.prototype._paintEventLabel = function(evt, text, left, top, width,
height, theme, labelDivClassName, highlightIndex) {
var doc = this._timeline.getDocument();
var labelDiv = doc.createElement("div");
labelDiv.className = labelDivClassName;
labelDiv.id = this._encodeEventElID('label', evt);
labelDiv.style.left = left + "px";
labelDiv.style.width = width + "px";
labelDiv.style.top = top + "px";
labelDiv.innerHTML = text;
if(evt._title != null)
labelDiv.title = evt._title;
var color = evt.getTextColor();
if (color == null) {
color = evt.getColor();
}
if (color != null) {
labelDiv.style.color = color;
}
if (theme.event.highlightLabelBackground && highlightIndex >= 0) {
labelDiv.style.background = this._getHighlightColor(highlightIndex, theme);
}
this._eventLayer.appendChild(labelDiv);
return {
left: left,
top: top,
width: width,
height: height,
elmt: labelDiv
};
};
Line 500:labelDiv.innerHTML = text;is how the text gets on the timeline. Inside this function, evt is the variable that holds the actual Event object that is responsible for the item being shown on the timeline. "this" would refer to the instance of the OriginalEventPainter that is doing the drawing, not the Event object. So you want to call evt.getProperty("propertyName") to add additional text to the label. There are several different approaches one can take to customize how Timeline works. In the case of relatively small modifications like this, it is easiest to simply copy the entire ._paintEventLabel function code into a new file, and make your changes to that copy. As long as your copy of the function is loaded after Timeline has been loaded, then it will replace (override) the default Timeline behavior. Good luck.--Mike NosalOn Aug 7, 2009, at 1:10 PM, mabra wrote:...