Skip to content

Commit

Permalink
minor improvements to admin tab js
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Oct 29, 2023
1 parent bdcb5ed commit aae1ee0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
34 changes: 6 additions & 28 deletions assets/src/js/admin/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ const tabs = [].map.call(tabElements, el => {
}
})

if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector
}

function get (id) {
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].id === id) {
Expand Down Expand Up @@ -99,27 +94,11 @@ function title (tab) {

function switchTab (evt) {
const link = evt.target

// get tab ID data attribute
let tabId = link.getAttribute('data-tab')

// alternatively, get tab ID from classname
// TODO: Remove this once MailChimp Top Bar 1.5.6 has been out for at least 3 months
if (!tabId) {
const match = link.className.match(/nav-tab-(\w+)?/)
if (match) {
tabId = match[1]
}
}

const tabId = link.getAttribute('data-tab')
const opened = open(tabId, true)
if (opened) {
evt.preventDefault()
evt.returnValue = false
return false
}

return true
}

function init () {
Expand All @@ -143,8 +122,11 @@ function init () {
title(tab)
}

/**
* @param {MouseEvent} evt
*/
function onDocumentClick (evt) {
if (evt.target.matches('.tab-link')) {
if (evt.target.hasAttribute('data-tab')) {
switchTab(evt)
}
}
Expand All @@ -155,11 +137,7 @@ function onPopState (evt) {
}
}

// add event listeners
for (let i = 0; i < tabNavElements.length; i++) {
tabNavElements[i].addEventListener('click', switchTab)
}
document.body.addEventListener('click', onDocumentClick)
document.addEventListener('click', onDocumentClick)
window.addEventListener('popstate', onPopState)
init()

Expand Down
4 changes: 1 addition & 3 deletions assets/src/js/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ function onSubmit (evt) {
}

const form = forms.getByElement(evt.target)
if (!evt.defaultPrevented) {
forms.trigger('submit', [form, evt])
}
forms.trigger('submit', [form, evt])
}

/**
Expand Down

0 comments on commit aae1ee0

Please sign in to comment.