From af65b68ac3eec07bc81d8cdfb046cc2bc10998c5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 1 Jun 2023 05:56:02 +0530 Subject: [PATCH] Make timestamps.js re-useable --- docs/_static/custom.js | 13 ++++++++++--- docs/_static/timestamps.css | 6 +++--- docs/_static/timestamps.js | 22 ++++++++++++++-------- docs/intro_vid.rst | 7 ++----- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/docs/_static/custom.js b/docs/_static/custom.js index ae833145d..87021c61c 100644 --- a/docs/_static/custom.js +++ b/docs/_static/custom.js @@ -4,6 +4,7 @@ * * Distributed under terms of the GPLv3 license */ +/*jshint esversion: 6 */ (function() { "use strict"; @@ -39,7 +40,7 @@ function mark_current_link(sidebar_tree, a, onload) { } function show_hash_in_sidebar(onload) { - var sidebar_tree = document.querySelector('.sidebar-tree'); + const sidebar_tree = get_sidebar_tree(); if (document.location.hash.length > 1) { var a = sidebar_tree.querySelector('a[href="' + document.location.hash + '"]'); if (a) mark_current_link(sidebar_tree, a, onload); @@ -48,10 +49,16 @@ function show_hash_in_sidebar(onload) { } } -document.addEventListener("DOMContentLoaded", function() { +function init_sidebar() { + const sidebar_tree = document.querySelector('.sidebar-tree'); + if (!sidebar_tree || sidebar_tree.dataset.inited === 'true') return; + sidebar_tree.dataset.inited = 'true'; show_hash_in_sidebar(true); window.addEventListener('hashchange', show_hash_in_sidebar.bind(null, false)); -}); +} + +document.addEventListener("DOMContentLoaded", init_sidebar); +init_sidebar(); }()); diff --git a/docs/_static/timestamps.css b/docs/_static/timestamps.css index 813d61ca6..cffc04a44 100644 --- a/docs/_static/timestamps.css +++ b/docs/_static/timestamps.css @@ -1,13 +1,13 @@ -.timestamp-list dl { +.video-with-timestamps dl { display: grid; grid-template-columns: max-content auto; } -.timestamp-list dt { +.video-with-timestamps dt { grid-column-start: 1; } -.timestamp-list dd { +.video-with-timestamps dd { grid-column-start: 2; margin-left: 1em; } diff --git a/docs/_static/timestamps.js b/docs/_static/timestamps.js index 3590f72f6..3ba00e6d7 100644 --- a/docs/_static/timestamps.js +++ b/docs/_static/timestamps.js @@ -1,29 +1,35 @@ /*jshint esversion: 6 */ (function() { + 'use strict'; + function init_timestamps() { - document.querySelectorAll('.timestamp-list').forEach(loc => { - var dl = loc.querySelector('dl'); + document.querySelectorAll('.video-with-timestamps').forEach(loc => { + if (loc.dataset.inited === 'true') return; + loc.dataset.inited = 'true'; + const container_id = loc.id; + const dl = loc.querySelector('dl'); dl.querySelectorAll('dt').forEach(dt => { dt.innerHTML = ''; dt.style.display = 'inline'; }); - dl.addEventListener('click', handle_timestamp_click); + dl.addEventListener('click', handle_timestamp_click.bind(null, container_id)); }); } - function handle_timestamp_click(e) { + function handle_timestamp_click(container_id, e) { if (e.target.tagName.toUpperCase() === 'TIME') { const timestamp = e.target.textContent; if (timestamp) { const [minutes, seconds] = timestamp.split(':'); - const totalSeconds = parseInt(minutes) * 60 + parseInt(seconds); - const video = document.querySelector('video'); - video.currentTime = totalSeconds; + const total_seconds = parseInt(minutes) * 60 + parseInt(seconds); + const video = document.querySelector('#' + container_id + ' video'); + video.currentTime = total_seconds; video.play(); } } } - window.addEventListener('load', init_timestamps); + init_timestamps(); + document.addEventListener('DOMContentloaded', init_timestamps); })(); diff --git a/docs/intro_vid.rst b/docs/intro_vid.rst index 4d40c54ce..82c6771e4 100644 --- a/docs/intro_vid.rst +++ b/docs/intro_vid.rst @@ -1,5 +1,7 @@ .. raw:: html +
+