antora/supplemental-ui/js/tabsBehavior.js (46 lines of code) (raw):
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
;(function () {
'use strict'
var hash = window.location.hash
find('.tabset').forEach(function (tabset) {
var active
var tabs = tabset.querySelector('.tabs')
if (tabs) {
var first
find('li', tabs).forEach(function (tab, idx) {
var id = (tab.querySelector('a[id]') || tab).id
if (!id) return
var pane = getPane(id, tabset)
if (!idx) first = { tab: tab, pane: pane }
if (!active && hash === '#' + id && (active = true)) {
tab.classList.add('is-active')
if (pane) pane.classList.add('is-active')
} else if (!idx) {
tab.classList.remove('is-active')
if (pane) pane.classList.remove('is-active')
}
tab.addEventListener('click', activateTab.bind({ tabset: tabset, tab: tab, pane: pane }))
})
if (!active && first) {
first.tab.classList.add('is-active')
if (first.pane) first.pane.classList.add('is-active')
}
}
tabset.classList.remove('is-loading')
})
function activateTab (e) {
var tab = this.tab
var pane = this.pane
find('.tabs li, .tab-pane', this.tabset).forEach(function (it) {
it === tab || it === pane ? it.classList.add('is-active') : it.classList.remove('is-active')
})
e.preventDefault()
}
function find (selector, from) {
return Array.prototype.slice.call((from || document).querySelectorAll(selector))
}
function getPane (id, tabset) {
return find('.tab-pane', tabset).find(function (it) {
return it.getAttribute('aria-labelledby') === id
})
}
})()