// The top menu's dark mode toggle: switches between light and dark right away and saves the choice (with the // user's preferences when logged in, else in this browser). "Automatic" is chosen in My account. (function () { var MOON = ''; var SUN = ''; function isDark() { return document.documentElement.classList.contains('dark-mode'); } function save(mode) { var dm = window.RedmineDarkMode; if (!dm.loggedIn) { try { localStorage.setItem('redmine_dark_mode', mode); } catch (e) {} return; } var token = document.querySelector('meta[name="csrf-token"]'); var body = new URLSearchParams({ mode: mode }); fetch((window.relativeUrlRoot || '') + '/dark_mode', { method: 'POST', credentials: 'same-origin', headers: { 'X-CSRF-Token': token ? token.content : '', 'X-Requested-With': 'XMLHttpRequest' }, body: body }); } // The top bar's icon button, and on phones a line in the slide-out menu (the top bar is hidden there) function render(button, link) { var dm = window.RedmineDarkMode; var label = isDark() ? dm.labels.light : dm.labels.dark; var icon = isDark() ? SUN : MOON; button.innerHTML = icon; button.title = label; button.setAttribute('aria-label', label); if (link) link.innerHTML = icon + '' + label + ''; } document.addEventListener('DOMContentLoaded', function () { var menu = document.querySelector('#top-menu .profile-menu'); if (!menu || !window.RedmineDarkMode) return; var button = document.createElement('button'); button.type = 'button'; button.className = 'dark-mode-toggle'; var link = null; var flyout = document.querySelector('.js-flyout-menu'); if (flyout) { var list = document.createElement('ul'); list.className = 'dark-mode-flyout'; var item = document.createElement('li'); link = document.createElement('a'); link.href = '#'; link.className = 'dark-mode-flyout-toggle'; item.appendChild(link); list.appendChild(item); flyout.appendChild(list); } function toggle(e) { e.preventDefault(); var mode = isDark() ? 'light' : 'dark'; window.RedmineDarkMode.set(mode); render(button, link); save(mode); } render(button, link); button.addEventListener('click', toggle); if (link) link.addEventListener('click', toggle); menu.insertBefore(button, menu.firstChild); // The device switching light/dark (Automatic) changes the icon too var media = window.matchMedia('(prefers-color-scheme: dark)'); if (media.addEventListener) media.addEventListener('change', function () { render(button, link); }); }); })(); // "Are you sure?" confirmations (data-confirm, through rails-ujs). The browser draws its own box in the device's // colours, so on a dark page it can flash up bright; while the page is dark, a dark dialog asks instead, and OK // carries on with the link, button or form. Light pages keep the browser's box. (function () { if (!window.RedmineDarkMode || !window.HTMLDialogElement) return; var nativeConfirm = window.Rails && Rails.confirm; var approved = null; function ask(message, onOk) { var labels = window.RedmineDarkMode.labels; var dialog = document.createElement('dialog'); dialog.className = 'dark-mode-confirm'; var text = document.createElement('p'); text.textContent = message; var buttons = document.createElement('div'); buttons.className = 'dark-mode-confirm-buttons'; var cancel = document.createElement('button'); cancel.type = 'button'; cancel.textContent = labels.cancel; var ok = document.createElement('button'); ok.type = 'button'; ok.className = 'dark-mode-confirm-ok'; ok.textContent = labels.ok; buttons.append(cancel, ok); dialog.append(text, buttons); document.body.appendChild(dialog); var confirmed = false; cancel.addEventListener('click', function () { dialog.close(); }); ok.addEventListener('click', function () { confirmed = true; dialog.close(); }); // Clicking the dimmed page outside the dialog cancels, like Escape dialog.addEventListener('click', function (e) { if (e.target === dialog) dialog.close(); }); dialog.addEventListener('close', function () { dialog.remove(); if (confirmed) onOk(); }); dialog.showModal(); ok.focus(); } // For other scripts: asks in the dark dialog on dark pages, or the browser's box on light ones, then runs onOk window.RedmineDarkMode.confirm = function (message, onOk) { if (document.documentElement.classList.contains('dark-mode')) { ask(message, onOk); } else if (window.confirm(message)) { onOk(); } }; if (!window.Rails) return; Rails.confirm = function (message, element) { if (!document.documentElement.classList.contains('dark-mode')) { return nativeConfirm ? nativeConfirm(message, element) : window.confirm(message); } // The second pass, after OK: let it through if (approved === element) { approved = null; return true; } ask(message, function () { approved = element; if (element.tagName === 'FORM') { element.requestSubmit ? element.requestSubmit() : element.submit(); } else { element.click(); } }); return false; }; })();