| 1 |
import { updateQueryParams } from '../../utils/urls' |
| 2 |
|
| 3 |
const selectTab = (tabsWrapper: Element, tab: Element, section: string) => { |
| 4 |
// Swap the active tab class from the previously active tab to the current one. |
| 5 |
tabsWrapper.querySelector('.active-type')?.classList.remove('active-type') |
| 6 |
tab.classList.add('active-type') |
| 7 |
updateQueryParams({ section }) |
| 8 |
|
| 9 |
// Update the current active tab attribute so that only the active tab is displayed. |
| 10 |
tabsWrapper.closest('.wrap')?.setAttribute('data-active-tab', section) |
| 11 |
|
| 12 |
//Hide all cloud messages - this is a bit of a hack, but it works make better **TODO** |
| 13 |
document.querySelectorAll('.cloud-message').forEach(element => { |
| 14 |
element.classList.add('hidden') |
| 15 |
}) |
| 16 |
|
| 17 |
if ('version-switch' !== section) { |
| 18 |
document.getElementById('version-switch-warning')?.classList.add('hidden') |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
// Refresh the editor preview if we're viewing the editor section. |
| 23 |
const refreshEditorPreview = (section: string) => { |
| 24 |
if ('editor' === section) { |
| 25 |
window.code_snippets_editor_preview?.codemirror.refresh() |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// Update the http referer value so that any redirections lead back to this tab. |
| 30 |
const updateHttpReferer = (section: string) => { |
| 31 |
const httpReferer: HTMLInputElement | null = document.querySelector('input[name=_wp_http_referer]') |
| 32 |
if (!httpReferer) { |
| 33 |
console.error('could not find http referer') |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
const newReferer = httpReferer.value.replace(/(?<base>[&?]section=)[^&]+/, `$1${section}`) |
| 38 |
httpReferer.value = newReferer + (newReferer === httpReferer.value ? `§ion=${section}` : '') |
| 39 |
} |
| 40 |
|
| 41 |
const setupHorizontalOverflow = (tabs: HTMLElement) => { |
| 42 |
const wrapper = tabs.closest<HTMLElement>('.snippet-type-nav-wrapper') |
| 43 |
if (!wrapper) { |
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
const updateFades = () => { |
| 48 |
const remainingScroll = tabs.scrollWidth - tabs.clientWidth - tabs.scrollLeft |
| 49 |
wrapper.classList.toggle('has-scroll-start', 1 < tabs.scrollLeft) |
| 50 |
wrapper.classList.toggle('has-scroll-end', 1 < remainingScroll) |
| 51 |
} |
| 52 |
|
| 53 |
tabs.addEventListener('scroll', updateFades, { passive: true }) |
| 54 |
|
| 55 |
const observer = new ResizeObserver(updateFades) |
| 56 |
observer.observe(tabs) |
| 57 |
observer.observe(tabs.firstElementChild ?? tabs) |
| 58 |
updateFades() |
| 59 |
} |
| 60 |
|
| 61 |
export const handleSettingsTabs = () => { |
| 62 |
const tabsWrapper = document.getElementById('settings-sections-tabs') |
| 63 |
if (!tabsWrapper) { |
| 64 |
console.error('Could not find snippets tabs') |
| 65 |
return |
| 66 |
} |
| 67 |
|
| 68 |
setupHorizontalOverflow(tabsWrapper) |
| 69 |
|
| 70 |
const tabs = tabsWrapper.querySelectorAll('.snippet-type-link') |
| 71 |
|
| 72 |
for (const tab of tabs) { |
| 73 |
tab.addEventListener('click', event => { |
| 74 |
event.preventDefault() |
| 75 |
const section = tab.getAttribute('data-section') |
| 76 |
|
| 77 |
if (section) { |
| 78 |
selectTab(tabsWrapper, tab, section) |
| 79 |
refreshEditorPreview(section) |
| 80 |
updateHttpReferer(section) |
| 81 |
} |
| 82 |
}) |
| 83 |
} |
| 84 |
} |
| 85 |
|