diff --git a/src/timeline-window.js b/src/timeline-window.js index a8caa1b1939a631cb838a7c40c226638077190e2..2cf9d0523ac24c06b17c20d10f710619b9204629 100644 --- a/src/timeline-window.js +++ b/src/timeline-window.js @@ -95,10 +95,30 @@ TimelineWindow.prototype.load = function(initialEventId, initialWindowSize) { const self = this; initialWindowSize = initialWindowSize || 20; - // given an EventTimeline, and an event index within it, initialise our + // given an EventTimeline, find the event we were looking for, and initialise our // fields so that the event in question is in the middle of the window. - const initFields = function(timeline, eventIndex) { - const endIndex = Math.min(timeline.getEvents().length, + const initFields = function(timeline) { + let eventIndex; + + const events = timeline.getEvents(); + + if (!initialEventId) { + // we were looking for the live timeline: initialise to the end + eventIndex = events.length; + } else { + for (let i = 0; i < events.length; i++) { + if (events[i].getId() == initialEventId) { + eventIndex = i; + break; + } + } + + if (eventIndex === undefined) { + throw new Error("getEventTimeline result didn't include requested event"); + } + } + + const endIndex = Math.min(events.length, eventIndex + Math.ceil(initialWindowSize / 2)); const startIndex = Math.max(0, endIndex - initialWindowSize); self._start = new TimelineIndex(timeline, startIndex - timeline.getBaseIndex()); @@ -133,9 +153,8 @@ TimelineWindow.prototype.load = function(initialEventId, initialWindowSize) { throw new Error("getEventTimeline result didn't include requested event"); }); } else { - // start with the most recent events const tl = this._timelineSet.getLiveTimeline(); - initFields(tl, tl.getEvents().length); + initFields(tl); return q(); } };