Dynamic Timeline
14 Apr 2017I should start with a disclaimer that this isn’t really the use intended for a timeline. From the TimelineJS website you can see that it is really meant to tell a story, and should probably have only around 20 or so slides. We are using a timeline as essentially another way to browse objects, or to visualise search results. Because this use case isn’t supported out of the box I needed to find a way of dynamically loading objects as a user scrolled back and forth, as clearly loading all the objects at once would not be possible. Ideally something like this could be handled with Ajax requests, retrieving data when needed. But our browse/search already supports paging of results, so a simpler, if slightly hacky way, was to make use of this.
First some variables need to be accessible to the javascript for creating the timeline. This can be done with data attributes.
The attributes provide the total number of pages of results and the current result page that is being viewed. Normally when the end of the timeline is reached the next slide navigation is disabled. What I wanted is to re-enable this but have it retrieve the next page of results rather than the next slide. That is what the javascript below does.
If we are viewing the last slide the next slide navigation is enabled, but the title and description are set to show that it will now load more results. If the navigation is clicked URL query parameters are set (tl_page) to the next page and loading of the new URL is triggered.
Allowing previous pages to be loaded is almost exactly the same, except that we are moving in the opposite direction.
One issue with loading the previous page of results is that the timeline will start from the first slide rather the last, which is not really what you might expect when navigating the timeline. I haven’t found a totally satisfactory solution for that one. The best I have so far is to tell the timeline to start from the end. Now when the previous page loads it automatically scrolls to the last slide.