Skip to content

Commit

Permalink
Merge pull request #12701 from rtibbles/generate_nav_routes_once
Browse files Browse the repository at this point in the history
Move navroute generation into usenav composable.
  • Loading branch information
marcellamaki authored Oct 3, 2024
2 parents 0dfe8a2 + fc1222b commit 7ae3a3d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
26 changes: 20 additions & 6 deletions kolibri/core/assets/src/composables/useNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { KolibriIcons } from 'kolibri-design-system/lib/KIcon/iconDefinitions';
import { get } from '@vueuse/core';
import { UserKinds, NavComponentSections } from 'kolibri.coreVue.vuex.constants';
import logger from 'kolibri.lib.logging';
import { computed } from 'kolibri.lib.vueCompositionApi';
import { computed, getCurrentInstance } from 'kolibri.lib.vueCompositionApi';
import { generateNavRoute } from './generateNavRoutes';

const logging = logger.getLogger(__filename);

Expand Down Expand Up @@ -75,13 +76,26 @@ export const registerNavItem = component => {
}
};

export default function useNav() {
export default function useNav(store) {
store = store || getCurrentInstance().proxy.$store;
const route = computed(() => store.state.route);
const { windowIsSmall } = useKResponsiveWindow();
const topBarHeight = computed(() => (get(windowIsSmall) ? 56 : 64));
const exportedItems = navItems.map(component => ({
...component,
active: window.location.pathname == component.url,
}));
const exportedItems = computed(() =>
navItems.map(item => {
const output = {
...item,
active: window.location.pathname == item.url,
};
if (item.routes) {
output.routes = item.routes.map(routeItem => ({
...routeItem,
href: generateNavRoute(item.url, routeItem.route, get(route).params),
}));
}
return output;
}),
);
return {
navItems: exportedItems,
topBarHeight,
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/assets/src/views/AppBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
const { isLearner, isUserLoggedIn, username, full_name } = useUser();
const { totalPoints, fetchPoints } = useTotalProgress();
const links = computed(() => {
const currentItem = navItems.find(nc => nc.url === window.location.pathname);
const currentItem = get(navItems).find(nc => nc.url === window.location.pathname);
if (!currentItem || !currentItem.routes) {
return [];
}
Expand Down
10 changes: 1 addition & 9 deletions kolibri/core/assets/src/views/BottomNavigationBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:key="key"
>
<a
:href="generateNavRoute(routeDefinition.route)"
:href="routeDefinition.href"
tabindex="-1"
class="nav-menu-item"
:style="{ textDecoration: 'none' }"
Expand Down Expand Up @@ -65,7 +65,6 @@
<script>
import commonCoreStrings from '../mixins/commonCoreStrings';
import { generateNavRoute } from '../utils/generateNavRoutes';
export default {
name: 'BottomNavigationBar',
Expand Down Expand Up @@ -95,15 +94,8 @@
routes() {
return this.bottomMenuItem.routes || [];
},
url() {
return this.bottomMenuItem.url || '';
},
},
methods: {
generateNavRoute(route) {
const params = this.$route.params;
return generateNavRoute(this.url, route, params);
},
isActiveLink(route) {
return this.bottomMenuItem.active && route == this.$router.currentRoute.path;
},
Expand Down
10 changes: 3 additions & 7 deletions kolibri/core/assets/src/views/CoreMenu/CoreMenuOption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
</router-link>
<a
v-else
:href="generateNavRoute(subRoute.route)"
:href="subRoute.href"
class="link"
:class="subRouteInactiveClass"
>
Expand All @@ -102,7 +102,6 @@
<script>
import commonCoreStrings from 'kolibri.coreVue.mixins.commonCoreStrings';
import { generateNavRoute } from '../../utils/generateNavRoutes';
export default {
name: 'CoreMenuOption',
Expand Down Expand Up @@ -136,7 +135,8 @@
required: false,
default: () => [],
// subRoutes should be an array of objects with the name, label, and route properties
validate: subRoutes => subRoutes.every(route => route.name && route.label && route.route),
validate: subRoutes =>
subRoutes.every(route => route.name && route.label && route.route && route.href),
},
disabled: {
type: Boolean,
Expand Down Expand Up @@ -226,10 +226,6 @@
}
return true;
},
generateNavRoute(route) {
const params = this.$route.params;
return generateNavRoute(this.link, route, params);
},
conditionalEmit() {
if (this.disabled || this.link) {
return;
Expand Down

0 comments on commit 7ae3a3d

Please sign in to comment.