2020-06-23 04:45:38 -04:00
|
|
|
import _ from 'lodash'
|
2020-05-19 05:02:56 -04:00
|
|
|
import App from '../base'
|
2023-10-13 05:13:49 -04:00
|
|
|
App.directive('bookmarkableTabset', [
|
|
|
|
'$location',
|
2023-10-17 04:36:46 -04:00
|
|
|
function ($location) {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
require: 'tabset',
|
|
|
|
link(scope, el, attrs, tabset) {
|
|
|
|
const _makeActive = function (hash) {
|
|
|
|
if (hash && hash !== '') {
|
|
|
|
const matchingTab = _.find(
|
|
|
|
tabset.tabs,
|
|
|
|
tab => tab.bookmarkableTabId === hash
|
|
|
|
)
|
|
|
|
if (matchingTab) {
|
|
|
|
matchingTab.select()
|
|
|
|
return el.children()[0].scrollIntoView({ behavior: 'smooth' })
|
|
|
|
}
|
2023-10-13 05:13:49 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2023-10-17 04:36:46 -04:00
|
|
|
scope.$applyAsync(function () {
|
|
|
|
// for page load
|
|
|
|
const hash = $location.hash()
|
2023-10-13 05:13:49 -04:00
|
|
|
_makeActive(hash)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2023-10-17 04:36:46 -04:00
|
|
|
// for links within page to a tab
|
|
|
|
// this needs to be within applyAsync because there could be a link
|
|
|
|
// within a tab to another tab
|
|
|
|
const linksToTabs = document.querySelectorAll('.link-to-tab')
|
|
|
|
const _clickLinkToTab = event => {
|
|
|
|
const hash = event.currentTarget
|
|
|
|
.getAttribute('href')
|
|
|
|
.split('#')
|
|
|
|
.pop()
|
|
|
|
_makeActive(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (linksToTabs) {
|
|
|
|
Array.from(linksToTabs).map(link =>
|
|
|
|
link.addEventListener('click', _clickLinkToTab)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2023-10-13 05:13:49 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
App.directive('bookmarkableTab', [
|
|
|
|
'$location',
|
2023-10-17 04:36:46 -04:00
|
|
|
function ($location) {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
require: 'tab',
|
|
|
|
link(scope, el, attrs, tab) {
|
|
|
|
const tabScope = el.isolateScope()
|
|
|
|
const tabId = attrs.bookmarkableTab
|
|
|
|
if (tabScope && tabId && tabId !== '') {
|
|
|
|
tabScope.bookmarkableTabId = tabId
|
|
|
|
tabScope.$watch('active', function (isActive, wasActive) {
|
|
|
|
if (isActive && !wasActive && $location.hash() !== tabId) {
|
|
|
|
return $location.hash(tabId)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2023-10-13 05:13:49 -04:00
|
|
|
])
|