From 24538fe6cdfa452fa2d719254762cb3d9f920e11 Mon Sep 17 00:00:00 2001 From: Paul Hammant Date: Sat, 13 Aug 2022 10:08:49 +0100 Subject: [PATCH] suggestion for issue 676 --- src/js/timeline/Timeline.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/js/timeline/Timeline.js b/src/js/timeline/Timeline.js index 9e654682..e0f0884c 100644 --- a/src/js/timeline/Timeline.js +++ b/src/js/timeline/Timeline.js @@ -45,6 +45,22 @@ function make_keydown_handler(timeline) { } } +if (typeof makeHash !== 'function') { + function makeHash(id) { + var hash = "#" + "event-" + id.toString(); + return hash; + } +} + +if (typeof idFromHash !== 'function') { + function idFromHash(hash) { + if (!hash.startsWith("#event")) { + return null; + } + return hash.replace("#event-", ""); + } +} + /** * Primary entry point for using TimelineJS. * @constructor @@ -791,15 +807,17 @@ class Timeline { this.goTo(this.options.start_at_slide); } if (this.options.hash_bookmark) { - if (window.location.hash != "") { - this.goToId(window.location.hash.replace("#event-", "")); + let hash = window.location.hash; + if (hash != "") { + this.goToId(idFromHash(hash)); } else { this._updateHashBookmark(this.current_id); } let the_timeline = this; window.addEventListener('hashchange', function() { - if (window.location.hash.indexOf('#event-') == 0) { - the_timeline.goToId(window.location.hash.replace("#event-", "")); + let id = idFromHash(hash); + if (id !== null) { + the_timeline.goToId(id); } }, false); } @@ -810,9 +828,9 @@ class Timeline { // Update hashbookmark in the url bar _updateHashBookmark(id) { if (id) { // TODO: validate the id... - var hash = "#" + "event-" + id.toString(); + var hash = makeHash(id); window.history.replaceState(null, "Browsing TimelineJS", hash); - this.fire("hash_updated", { unique_id: this.current_id, hashbookmark: "#" + "event-" + id.toString() }, this); + this.fire("hash_updated", { unique_id: this.current_id, hashbookmark: hash }, this); } }