|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _is_file_uri = (uri) => uri.startsWith('file://'); |
| 4 | + |
| 5 | +const _IS_LOCAL = _is_file_uri(window.location.href); |
| 6 | +const _CONTENT_ROOT = document.documentElement.dataset.content_root; |
| 7 | +const _CURRENT_PREFIX = _IS_LOCAL |
| 8 | + ? null |
| 9 | + : new URL(_CONTENT_ROOT, window.location).pathname; |
| 10 | +const _CURRENT_RELEASE = DOCUMENTATION_OPTIONS.VERSION || ''; |
| 11 | +const _CURRENT_VERSION = _CURRENT_RELEASE.split('.').slice(0, 2).join('.'); |
| 12 | +const _CURRENT_LANGUAGE = DOCUMENTATION_OPTIONS.LANGUAGE?.toLowerCase() || 'en'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Change the current page to the first existing URL in the list. |
| 16 | + * @param {Array<string>} urls |
| 17 | + * @private |
| 18 | + */ |
| 19 | +const _navigate_to_first_existing = async (urls) => { |
| 20 | + // Navigate to the first existing URL of urls. |
| 21 | + for (const url of urls) { |
| 22 | + try { |
| 23 | + const response = await fetch(url, { method: 'GET' }) |
| 24 | + if (response.ok) { |
| 25 | + window.location.href = url; |
| 26 | + return url; // Avoid race conditions with multiple redirects |
| 27 | + } |
| 28 | + } catch(err) { |
| 29 | + console.error(`Error in: ${url}`); |
| 30 | + console.error(err) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // if all else fails, redirect to the d.p.o root |
| 35 | + window.location.href = '/'; |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * Navigate to the selected version. |
| 40 | + * @param {Event} event |
| 41 | + * @returns {Promise<void>} |
| 42 | + */ |
| 43 | +const on_version_switch = async (event) => { |
| 44 | + if (_IS_LOCAL) return; |
| 45 | + |
| 46 | + const selected_version = event.target.value; |
| 47 | + // Special 'default' case for English. |
| 48 | + const new_prefix = |
| 49 | + _CURRENT_LANGUAGE === 'en' |
| 50 | + ? `/${selected_version}/` |
| 51 | + : `/${_CURRENT_LANGUAGE}/${selected_version}/`; |
| 52 | + const new_prefix_en = `/${selected_version}/`; |
| 53 | + if (_CURRENT_PREFIX !== new_prefix) { |
| 54 | + // Try the following pages in order: |
| 55 | + // 1. The current page in the current language with the new version |
| 56 | + // 2. The current page in English with the new version |
| 57 | + // 3. The documentation home in the current language with the new version |
| 58 | + // 4. The documentation home in English with the new version |
| 59 | + await _navigate_to_first_existing([ |
| 60 | + window.location.href.replace(_CURRENT_PREFIX, new_prefix), |
| 61 | + window.location.href.replace(_CURRENT_PREFIX, new_prefix_en), |
| 62 | + new_prefix, |
| 63 | + new_prefix_en, |
| 64 | + ]); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +/** |
| 69 | + * Navigate to the selected language. |
| 70 | + * @param {Event} event |
| 71 | + * @returns {Promise<void>} |
| 72 | + */ |
| 73 | +const on_language_switch = async (event) => { |
| 74 | + if (_IS_LOCAL) return; |
| 75 | + |
| 76 | + const selected_language = event.target.value; |
| 77 | + // Special 'default' case for English. |
| 78 | + const new_prefix = |
| 79 | + selected_language === 'en' |
| 80 | + ? `/${_CURRENT_VERSION}/` |
| 81 | + : `/${selected_language}/${_CURRENT_VERSION}/`; |
| 82 | + if (_CURRENT_PREFIX !== new_prefix) { |
| 83 | + // Try the following pages in order: |
| 84 | + // 1. The current page in the new language with the current version |
| 85 | + // 2. The documentation home in the new language with the current version |
| 86 | + await _navigate_to_first_existing([ |
| 87 | + window.location.href.replace(_CURRENT_PREFIX, new_prefix), |
| 88 | + new_prefix, |
| 89 | + ]); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * Set up the version and language switchers. |
| 95 | + * @returns {Promise<void>} |
| 96 | + */ |
| 97 | +const initialise_switchers = async () => { |
| 98 | + try { |
| 99 | + // Update the version select elements |
| 100 | + document |
| 101 | + .querySelectorAll('.version_switcher_placeholder select') |
| 102 | + .forEach((select) => { |
| 103 | + if (_IS_LOCAL) { |
| 104 | + select.disabled = true; |
| 105 | + select.title = 'Version switching is disabled in local builds'; |
| 106 | + } |
| 107 | + select.addEventListener('change', on_version_switch); |
| 108 | + select.parentElement.classList.remove('version_switcher_placeholder'); |
| 109 | + }); |
| 110 | + |
| 111 | + // Update the language select elements |
| 112 | + document |
| 113 | + .querySelectorAll('.language_switcher_placeholder select') |
| 114 | + .forEach((select) => { |
| 115 | + if (_IS_LOCAL) { |
| 116 | + select.disabled = true; |
| 117 | + select.title = 'Language switching is disabled in local builds'; |
| 118 | + } |
| 119 | + select.addEventListener('change', on_language_switch); |
| 120 | + select.parentElement.classList.remove('language_switcher_placeholder'); |
| 121 | + }); |
| 122 | + } catch (error) { |
| 123 | + console.error(error); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +if (document.readyState !== 'loading') { |
| 128 | + initialise_switchers(); |
| 129 | +} else { |
| 130 | + document.addEventListener('DOMContentLoaded', initialise_switchers); |
| 131 | +} |
0 commit comments