From f77f88ec64edbe41cf95faec95a8f2a123235401 Mon Sep 17 00:00:00 2001 From: Adam Zapletal Date: Fri, 20 Dec 2024 20:30:08 -0600 Subject: [PATCH] Refactored `version-switcher.js` - Simplified code - Stopped using jQuery - Moved refactored code to `djangoproject.js` This patch should bring no user-facing changes. Refs #1827 --- djangoproject/static/js/djangoproject.js | 7 ++++++ djangoproject/static/js/main.js | 4 --- .../static/js/mod/version-switcher.js | 25 ------------------- djangoproject/templates/base.html | 1 - 4 files changed, 7 insertions(+), 30 deletions(-) delete mode 100644 djangoproject/static/js/mod/version-switcher.js diff --git a/djangoproject/static/js/djangoproject.js b/djangoproject/static/js/djangoproject.js index 474231d6d..f6e9229eb 100644 --- a/djangoproject/static/js/djangoproject.js +++ b/djangoproject/static/js/djangoproject.js @@ -4,3 +4,10 @@ document.querySelectorAll('.doc-switcher li.current').forEach(function (el) { this.parentElement.classList.toggle('open'); }); }); + +// Propagate the current fragment identifier when switching docs versions +document.querySelectorAll('#doc-versions a').forEach(function (el) { + el.addEventListener('click', function () { + this.href = this.href.split('#')[0] + window.location.hash; + }); +}); diff --git a/djangoproject/static/js/main.js b/djangoproject/static/js/main.js index 8d9dd2797..a51dcd812 100644 --- a/djangoproject/static/js/main.js +++ b/djangoproject/static/js/main.js @@ -19,10 +19,6 @@ define(function () { mods.push('mod/list-collapsing'); } - if (hasClass('version-switcher')) { - mods.push('mod/version-switcher'); - } - if (hasClass('doc-floating-warning')) { mods.push('mod/floating-warning'); } diff --git a/djangoproject/static/js/mod/version-switcher.js b/djangoproject/static/js/mod/version-switcher.js deleted file mode 100644 index b494c9597..000000000 --- a/djangoproject/static/js/mod/version-switcher.js +++ /dev/null @@ -1,25 +0,0 @@ -define([ - 'jquery', //requires jquery -], function ($) { - var VersionSwitcher = function (switcher) { - this.switcher = $(switcher); // the version switcher - this.init(); - }; - - VersionSwitcher.prototype = { - init: function () { - var self = this; - $(document).ready(function () { - // Propagate the fragment identifier to the links in the version switcher - self.switcher.find('a').on('click', function () { - var hrefWithoutFragment = this.href.split('#')[0]; - this.href = hrefWithoutFragment + window.location.hash; - // do nothing and let the event bubble up - }); - }); - }, - }; - - // Export a single instance of our module: - return new VersionSwitcher('#doc-versions'); -}); diff --git a/djangoproject/templates/base.html b/djangoproject/templates/base.html index 896483b61..57e76365f 100644 --- a/djangoproject/templates/base.html +++ b/djangoproject/templates/base.html @@ -154,7 +154,6 @@ "mod/list-collapsing": extless("{% static 'js/mod/list-collapsing.js' %}"), "mod/list-feature": extless("{% static 'js/mod/list-feature.js' %}"), "mod/mobile-menu": extless("{% static 'js/mod/mobile-menu.js' %}"), - "mod/version-switcher": extless("{% static 'js/mod/version-switcher.js' %}"), "mod/search-key": extless("{% static 'js/mod/search-key.js' %}"), "mod/stripe-change-card": extless("{% static 'js/mod/stripe-change-card.js' %}"), "mod/switch-dark-mode": extless("{% static 'js/mod/switch-dark-mode.js' %}"),