primevue-mirror/apps/showcase/components/doc/DocSectionNav.vue

127 lines
4.1 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
<ul ref="nav" class="doc-section-nav">
<li v-for="doc of docs" :key="doc.label" :class="['navbar-item', { 'active-navbar-item': activeId === doc.id }]">
<div class="navbar-item-content">
2024-03-28 14:34:15 +00:00
<button @click="onButtonClick(doc)">{{ doc.label }}</button>
2023-02-28 08:29:30 +00:00
</div>
<template v-if="doc.children">
<ul>
<li v-for="child of doc.children" :key="child.label" :class="['navbar-item', { 'active-navbar-item': activeId === child.id }]">
<div class="navbar-item-content">
2024-03-28 14:34:15 +00:00
<button @click="onButtonClick(child)">
{{ child.label }}
</button>
2023-02-28 08:29:30 +00:00
</div>
</li>
</ul>
</template>
</li>
</ul>
</template>
<script>
2024-06-11 12:21:12 +00:00
import { DomHandler, ObjectUtils } from '@primevue/core/utils';
2023-02-28 08:29:30 +00:00
export default {
props: ['docs'],
data() {
return {
activeId: null,
isScrollBlocked: false,
scrollEndTimer: null,
topbarHeight: 0
};
},
2023-03-06 10:59:56 +00:00
watch: {
'$route.hash'() {
this.scrollCurrentUrl();
}
},
2023-02-28 08:29:30 +00:00
mounted() {
2023-03-06 10:59:56 +00:00
this.scrollCurrentUrl();
2023-02-28 08:29:30 +00:00
window.addEventListener('scroll', this.onScroll, { passive: true });
},
beforeUnmount() {
window.removeEventListener('scroll', this.onScroll, { passive: true });
},
methods: {
onScroll() {
if (!this.isScrollBlocked) {
2023-04-01 11:13:05 +00:00
const labels = [...document.querySelectorAll(':is(h1,h2,h3).doc-section-label')].filter((el) => DomHandler.isVisible(el));
2023-03-06 10:59:56 +00:00
2023-02-28 08:29:30 +00:00
const windowScrollTop = DomHandler.getWindowScrollTop();
labels.forEach((label) => {
const { top } = DomHandler.getOffset(label);
const threshold = this.getThreshold(label);
if (top - threshold <= windowScrollTop) {
const link = DomHandler.findSingle(label, 'a');
this.activeId = link.id;
}
});
}
clearTimeout(this.scrollEndTimer);
this.scrollEndTimer = setTimeout(() => {
this.isScrollBlocked = false;
const activeItem = DomHandler.findSingle(this.$refs.nav, '.active-navbar-item');
activeItem && activeItem.scrollIntoView({ block: 'nearest', inline: 'start' });
}, 50);
},
scrollToLabelById(id, behavior = 'smooth') {
const label = document.getElementById(id);
label && label.parentElement.scrollIntoView({ block: 'start', behavior });
},
onButtonClick(doc) {
this.$router.push(`${this.checkRouteName}/#${doc.id}`);
2023-02-28 08:29:30 +00:00
setTimeout(() => {
this.activeId = doc.id;
2023-02-28 08:29:30 +00:00
this.scrollToLabelById(doc.id, 'smooth');
this.isScrollBlocked = true;
}, 1);
},
getThreshold(label) {
if (!this.topbarHeight) {
const topbar = DomHandler.findSingle(document.body, '.layout-topbar');
this.topbarHeight = topbar ? DomHandler.getHeight(topbar) : 0;
}
return this.topbarHeight + DomHandler.getHeight(label) * 1.5;
},
getIdOfTheSection(section) {
return section.querySelector('a').getAttribute('id');
2023-03-06 10:59:56 +00:00
},
scrollCurrentUrl() {
const hash = window.location.hash.substring(1);
const hasHash = ObjectUtils.isNotEmpty(hash);
const id = hasHash ? hash : (this.docs[0] || {}).id;
this.activeId = id;
hasHash &&
setTimeout(() => {
this.scrollToLabelById(id);
}, 1);
2023-02-28 08:29:30 +00:00
}
2023-03-02 14:47:18 +00:00
},
computed: {
checkRouteName() {
const path = this.$router.currentRoute.value.path;
if (path.lastIndexOf('/') === path.length - 1) {
return path.slice(0, -1);
}
return path;
}
2023-02-28 08:29:30 +00:00
}
};
</script>