custom-admin.js
112 lines
| 1 | /* Email Encoder Admin */ |
| 2 | (function () { |
| 3 | |
| 4 | 'use strict'; |
| 5 | |
| 6 | jQuery(function ($) { |
| 7 | |
| 8 | // (Encoder form table styling now lives in style-admin.css; the |
| 9 | // previous form-table class was inheriting WP-admin spacing that |
| 10 | // fought our redesigned look.) |
| 11 | |
| 12 | // Make the whole bool/option row clickable (not just the toggle + text label). |
| 13 | // The inner <label for=X> still handles its own clicks; we only fire on |
| 14 | // "dead zone" clicks (gaps + the row container itself), and we skip the |
| 15 | // (i) tooltip so that doesn't accidentally toggle the setting. |
| 16 | $(document).on('click', '.eeb-option-row, .eeb-setting--bool', function (e) { |
| 17 | if ($(e.target).closest('label, input, .eeb-tooltip').length) { |
| 18 | return; |
| 19 | } |
| 20 | var input = this.querySelector('input[type="checkbox"], input[type="radio"]'); |
| 21 | if (input) { |
| 22 | input.click(); |
| 23 | } |
| 24 | }); |
| 25 | |
| 26 | // Copy support info to clipboard. On success, swap the clipboard |
| 27 | // dashicon for a check (no text fallback) and tint the button so the |
| 28 | // click registers visually; revert after 2s. |
| 29 | var $btn = $('#eeb-copy-support-info'); |
| 30 | if ($btn.length) { |
| 31 | var copying = false; |
| 32 | |
| 33 | $btn.on('click', function (e) { |
| 34 | e.preventDefault(); |
| 35 | if (copying) return; |
| 36 | copying = true; |
| 37 | |
| 38 | var text = $btn.data('support-text'); |
| 39 | |
| 40 | var onCopied = function () { |
| 41 | var $icon = $btn.find('.dashicons'); |
| 42 | $icon.removeClass('dashicons-clipboard').addClass('dashicons-yes-alt'); |
| 43 | $btn.addClass('eeb-action-btn--copied'); |
| 44 | setTimeout(function () { |
| 45 | $icon.removeClass('dashicons-yes-alt').addClass('dashicons-clipboard'); |
| 46 | $btn.removeClass('eeb-action-btn--copied'); |
| 47 | copying = false; |
| 48 | }, 2000); |
| 49 | }; |
| 50 | |
| 51 | if (navigator.clipboard && navigator.clipboard.writeText) { |
| 52 | navigator.clipboard.writeText(text).then(onCopied).catch(function () { |
| 53 | prompt((window.eebAdmin && window.eebAdmin.copyFallbackPrompt) || 'Copy this text:', text); |
| 54 | copying = false; |
| 55 | }); |
| 56 | } else { |
| 57 | prompt((window.eebAdmin && window.eebAdmin.copyFallbackPrompt) || 'Copy this text:', text); |
| 58 | copying = false; |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | }); |
| 64 | |
| 65 | document.addEventListener('DOMContentLoaded', function () { |
| 66 | |
| 67 | var tabs = document.querySelectorAll('.eeb-tab'); |
| 68 | var panels = document.querySelectorAll('.eeb-panel'); |
| 69 | |
| 70 | if (!tabs.length) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | function switchTab(tabKey, updateUrl) { |
| 75 | tabs.forEach(function (t) { |
| 76 | t.classList.remove('eeb-tab-active'); |
| 77 | }); |
| 78 | panels.forEach(function (p) { |
| 79 | p.style.display = 'none'; |
| 80 | }); |
| 81 | |
| 82 | var activeTab = document.querySelector('.eeb-tab[data-tab="' + tabKey + '"]'); |
| 83 | var activePanel = document.getElementById('eeb-tab-' + tabKey); |
| 84 | |
| 85 | if (!activeTab || !activePanel) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | activeTab.classList.add('eeb-tab-active'); |
| 90 | activePanel.style.display = 'block'; |
| 91 | |
| 92 | // Sync the URL so reloads, back/forward, and bookmarking land |
| 93 | // on the same tab. replaceState (not pushState) avoids piling up |
| 94 | // history entries for every tab click. |
| 95 | if (updateUrl !== false) { |
| 96 | var url = new URL(window.location.href); |
| 97 | url.searchParams.set('tab', tabKey); |
| 98 | window.history.replaceState(null, '', url.toString()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | tabs.forEach(function (tab) { |
| 103 | tab.addEventListener('click', function (e) { |
| 104 | e.preventDefault(); |
| 105 | switchTab(this.getAttribute('data-tab'), true); |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | }); |
| 110 | |
| 111 | })(); |
| 112 |