Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve activeTab URL modifications #2503

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
<template v-else>
<template v-if="sidebarHasContent">
<ItemMediaSidebar
v-show="showSidebar"
ref="sidebar"
tabindex="0"
:annotation-list="hasAnnotations"
:annotation-search="hasAnnotations && hasSearchService"
:manifest-uri="uri"
:show="showSidebar"
@keydown.escape.native="showSidebar = false"
/>
<ItemMediaSidebarToggle
Expand Down
25 changes: 23 additions & 2 deletions packages/portal/src/components/item/ItemMediaSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
name="fade"
>
<div
v-show="show"
class="media-viewer-sidebar"
data-qa="item media sidebar"
>
Expand Down Expand Up @@ -158,6 +159,10 @@
query: {
type: String,
default: null
},
show: {
type: Boolean,
default: true
}
},

Expand All @@ -173,8 +178,8 @@
tabHashes.push('#links');
}

const { activeTabHash, activeTabHistory, activeTabIndex } = useActiveTab(tabHashes);
return { activeTabHash, activeTabHistory, activeTabIndex };
const { activeTabHash, activeTabHistory, activeTabIndex, watchTabIndex, unwatchTabIndex } = useActiveTab(tabHashes);
return { activeTabHash, activeTabHistory, activeTabIndex, watchTabIndex, unwatchTabIndex };
},

data() {
Expand All @@ -184,6 +189,22 @@
};
},

watch: {
show(val) {
if (val) {
this.watchTabIndex();
} else {
this.unwatchTabIndex();
}
}
},

mounted() {
if (this.show) {
this.watchTabIndex();
}
},

methods: {
handleAnnotationsFetched(annotationsLength) {
this.annotationsCount = annotationsLength;
Expand Down
24 changes: 17 additions & 7 deletions packages/portal/src/composables/activeTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@ export default function useActiveTab(tabHashes) {
const setActiveTabIndexFromRouteHash = () => {
if (tabHashes.includes(route?.hash)) {
activeTabIndex.value = tabHashes.indexOf(route.hash);
activeTabHistory.value.push(activeTabHash.value);
}
};

const activeTabHash = computed(() => {
return tabHashes[activeTabIndex.value];
});

watch(activeTabIndex, () => {
if (activeTabIndex.value !== -1) {
activeTabHistory.value.push(activeTabHash.value);
router.push({ ...route, hash: activeTabHash.value });
}
});
let unwatchTabIndex = () => {};

const watchTabIndex = () => {
// unwatch 1st to prevent duplicate watchers
unwatchTabIndex();

unwatchTabIndex = watch(activeTabIndex, () => {
if (activeTabIndex.value !== -1) {
activeTabHistory.value.push(activeTabHash.value);
router.replace({ ...route, hash: activeTabHash.value });
}
});
};

if (route) {
watch(route, () => {
Expand All @@ -39,6 +47,8 @@ export default function useActiveTab(tabHashes) {
return {
activeTabHash,
activeTabHistory,
activeTabIndex
activeTabIndex,
watchTabIndex,
unwatchTabIndex
};
}
25 changes: 14 additions & 11 deletions packages/portal/tests/unit/composables/activeTab.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { reactive } from 'vue';

import useActiveTab from '@/composables/activeTab.js';

const routerPushSpy = sinon.spy();
const routerReplaceSpy = sinon.spy();

const route = reactive({ hash: '#links' });
sinon.stub(vueRouter, 'useRoute').returns(route);
sinon.stub(vueRouter, 'useRouter').returns({
push: routerPushSpy
replace: routerReplaceSpy
});

const tabHashes = ['#annotations', '#search', '#links'];
Expand All @@ -23,9 +23,9 @@ const component = {
</div>
`,
setup() {
const { activeTabHash, activeTabIndex } = useActiveTab(tabHashes);
const { activeTabHash, activeTabIndex, watchTabIndex } = useActiveTab(tabHashes);

return { activeTabHash, activeTabIndex };
return { activeTabHash, activeTabIndex, watchTabIndex };
}
};

Expand Down Expand Up @@ -65,23 +65,26 @@ describe('useActiveTab', () => {
expect(input.element.value).toBe('2');
});

it('is watched for changes to update route', async() => {
it('is updated on route changes', async() => {
const wrapper = factory();

wrapper.find('#activeTabIndex').setValue(1);
route.hash = '#annotations';
await wrapper.vm.$nextTick();

expect(routerPushSpy.calledWith({ hash: '#search' })).toBe(true);
const input = wrapper.find('#activeTabIndex');
expect(input.element.value).toBe('0');
});
});

it('is updated on route changes', async() => {
describe('watchTabIndex', () => {
it('starts watching for changes to replace hash in route', async() => {
const wrapper = factory();
wrapper.vm.watchTabIndex();

route.hash = '#annotations';
wrapper.find('#activeTabIndex').setValue(1);
await wrapper.vm.$nextTick();

const input = wrapper.find('#activeTabIndex');
expect(input.element.value).toBe('0');
expect(routerReplaceSpy.calledWith({ hash: '#search' })).toBe(true);
});
});
});
Loading