| 1 |
const selectTab = (tabsWrapper: Element, tab: Element, section: string) => { |
| 2 |
// Swap the active tab class from the previously active tab to the current one. |
| 3 |
tabsWrapper.querySelector('.nav-tab-active')?.classList.remove('nav-tab-active') |
| 4 |
tab.classList.add('nav-tab-active') |
| 5 |
|
| 6 |
// Update the current active tab attribute so that only the active tab is displayed. |
| 7 |
tabsWrapper.closest('.wrap')?.setAttribute('data-active-tab', section) |
| 8 |
} |
| 9 |
|
| 10 |
// Refresh the editor preview if we're viewing the editor section. |
| 11 |
const refreshEditorPreview = (section: string) => { |
| 12 |
if ('editor' === section) { |
| 13 |
window.code_snippets_editor_preview?.codemirror.refresh() |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
// Update the http referer value so that any redirections lead back to this tab. |
| 18 |
const updateHttpReferer = (section: string) => { |
| 19 |
const httpReferer = document.querySelector<HTMLInputElement>('input[name=_wp_http_referer]') |
| 20 |
if (!httpReferer) { |
| 21 |
console.error('could not find http referer') |
| 22 |
return |
| 23 |
} |
| 24 |
|
| 25 |
const newReferer = httpReferer.value.replace(/(?<base>[&?]section=)[^&]+/, `$1${section}`) |
| 26 |
httpReferer.value = newReferer + (newReferer === httpReferer.value ? `§ion=${section}` : '') |
| 27 |
} |
| 28 |
|
| 29 |
export const handleSettingsTabs = () => { |
| 30 |
const tabsWrapper = document.getElementById('settings-sections-tabs') |
| 31 |
if (!tabsWrapper) { |
| 32 |
console.error('Could not find snippets tabs') |
| 33 |
return |
| 34 |
} |
| 35 |
|
| 36 |
const tabs = tabsWrapper.querySelectorAll('.nav-tab') ?? [] |
| 37 |
|
| 38 |
for (const tab of tabs) { |
| 39 |
tab.addEventListener('click', event => { |
| 40 |
event.preventDefault() |
| 41 |
const section = tab.getAttribute('data-section') |
| 42 |
|
| 43 |
if (section) { |
| 44 |
selectTab(tabsWrapper, tab, section) |
| 45 |
refreshEditorPreview(section) |
| 46 |
updateHttpReferer(section) |
| 47 |
} |
| 48 |
}) |
| 49 |
} |
| 50 |
} |
| 51 |
|