Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit ecc022b

Browse files
committed
feat(nav): add nav toggling
Add new feature nav toggler and navigation container. Nav toggler allows to hide and show the navigation. Navigation container hides the nav automatically based on the window size. --- scss/include/_layout.scss | 2 +- src/app.html | 4 +- src/app.js | 20 +--------- src/legendsrising.js | 3 +- src/resources/side-nav/index.js | 6 +++ src/resources/side-nav/nav-toggler.js | 22 +++++++++++ src/resources/side-nav/navigation.js | 38 +++++++++++++++++++ src/resources/side-nav/ui-state.js | 15 ++++++++ .../view-elements/navigation-top.html | 4 +- src/resources/view-elements/navigation-top.js | 11 +----- src/services/UIState.js | 15 -------- 11 files changed, 91 insertions(+), 49 deletions(-)
1 parent eb04bfa commit ecc022b

File tree

11 files changed

+91
-49
lines changed

11 files changed

+91
-49
lines changed

scss/include/_layout.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ body {
99
display: flex;
1010
}
1111

12-
aside {
12+
navigation {
1313
flex: 1;
1414
min-width: 230px;
1515
max-width: 230px;

src/app.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
<navigation-top router.bind="router"></navigation-top>
1111
</header>
1212

13-
<aside class="sidebar-nav ${uiState.sideNavigationVisibility ? '' : 'sidebar-nav-hidden'}">
13+
<navigation class="sidebar-nav">
1414
<navigation-side></navigation-side>
15-
</aside>
15+
</navigation>
1616

1717
<main>
1818
<div class="container-fluid" id="content-container">

src/app.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
11
import {Endpoint} from 'aurelia-api';
22
import {inject} from 'aurelia-framework';
33
import {Router} from 'aurelia-router';
4-
import {DOM} from 'aurelia-pal';
54

65
import AppRouterConfig from './configuration/router-config';
7-
import {UIState} from './services/UIState';
86
import 'jquery';
97
import 'bootstrap';
108

11-
@inject(Router, Endpoint.of(), AppRouterConfig, UIState)
9+
@inject(Router, Endpoint.of(), AppRouterConfig)
1210
export class App {
13-
showNavigationEvent = () => {
14-
if (window.innerWidth < 1200) {
15-
this.uiState.hideSideNavigation();
16-
} else {
17-
this.uiState.showSideNavigation();
18-
}
19-
};
20-
21-
constructor(router, api, appRouterConfig, uiState) {
11+
constructor(router, api, appRouterConfig) {
2212
this.router = router;
2313
this.api = api;
2414
this.appRouterConfig = appRouterConfig;
25-
this.uiState = uiState;
2615
}
2716

2817
activate() {
2918
this.appRouterConfig.configure();
30-
window.addEventListener('resize', this.showNavigationEvent);
31-
}
32-
33-
deactivate() {
34-
window.removeEventListener('resize', this.showNavigationEvent);
3519
}
3620
}

src/legendsrising.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export function configure(aurelia) {
3434
.plugin('aurelia-notify', settings => {
3535
settings.containerSelector = '#notification-container';
3636
settings.timeout = 10000;
37-
});
37+
})
38+
.feature('resources/side-nav');
3839

3940
aurelia.start()
4041
.then(a => a.setRoot('app', document.body))

src/resources/side-nav/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function configure(config) {
2+
config.globalResources([
3+
'./nav-toggler',
4+
'./navigation'
5+
]);
6+
}

src/resources/side-nav/nav-toggler.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {customElement, inlineView} from 'aurelia-templating';
2+
import {UIState} from './ui-state';
3+
4+
@customElement('nav-toggler')
5+
@inlineView(`
6+
<template>
7+
<button class="navbar-toggler" type="button" click.delegate="toggleNav()">
8+
<i class="fa fa-bars fa-fw"></i>
9+
</button>
10+
</template>
11+
`)
12+
export class NavToggler {
13+
static inject = [UIState];
14+
15+
constructor(uiState) {
16+
this.uiState = uiState;
17+
}
18+
19+
toggleNav() {
20+
this.uiState.toggleNav();
21+
}
22+
}

src/resources/side-nav/navigation.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {BindingEngine, customElement, noView, inject} from 'aurelia-framework';
2+
import {UIState} from './ui-state';
3+
4+
@noView()
5+
@customElement('navigation')
6+
@inject(BindingEngine, UIState, Element)
7+
export class Navigation {
8+
showNavigationEvent = () => {
9+
if (window.innerWidth < 1200) {
10+
this.uiState.hideNav();
11+
} else {
12+
this.uiState.showNav();
13+
}
14+
};
15+
16+
constructor(bindingEngine, uiState, element) {
17+
this.uiState = uiState;
18+
this.element = element;
19+
20+
let subscription = bindingEngine.propertyObserver(this.uiState, 'navVisible').subscribe(this.navStateChanged.bind(this));
21+
}
22+
23+
navStateChanged(newValue, oldValue) {
24+
if (this.uiState.navVisible) {
25+
this.element.classList.remove('sidebar-nav-hidden')
26+
} else {
27+
this.element.classList.add('sidebar-nav-hidden')
28+
}
29+
}
30+
31+
attached() {
32+
window.addEventListener('resize', this.showNavigationEvent);
33+
}
34+
35+
detached() {
36+
window.removeEventListener('resize', this.showNavigationEvent);
37+
}
38+
}

src/resources/side-nav/ui-state.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class UIState {
2+
navVisible = true;
3+
4+
toggleNav() {
5+
this.navVisible = !this.navVisible;
6+
}
7+
8+
showNav() {
9+
this.navVisible = true;
10+
}
11+
12+
hideNav() {
13+
this.navVisible = false;
14+
}
15+
}

src/resources/view-elements/navigation-top.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<template>
22
<nav class="navbar navbar-fixed-top navbar-light bg-faded">
3-
<button class="navbar-toggler" type="button" click.delegate="toggleSideNavigation()">
4-
<i class="fa fa-bars fa-fw"></i>
5-
</button>
3+
<nav-toggler></nav-toggler>
64
<a class="navbar-brand" href="#">${router.title}</a>
75
<ul class="nav navbar-nav pull-right">
86
<li class="nav-item" if.bind="!isAuthenticated">

src/resources/view-elements/navigation-top.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import {bindable, inject} from 'aurelia-framework';
22
import {AuthService} from 'aurelia-authentication';
33

4-
import {UIState} from '../../services/UIState';
5-
6-
@inject(AuthService, UIState)
4+
@inject(AuthService)
75
export class NavigationTop {
86
@bindable router = null;
97

10-
constructor(auth, uiState) {
8+
constructor(auth) {
119
this.auth = auth;
12-
this.uiState = uiState;
13-
}
14-
15-
toggleSideNavigation() {
16-
this.uiState.toggleSideNavigation();
1710
}
1811

1912
get isAuthenticated() {

src/services/UIState.js

-15
This file was deleted.

0 commit comments

Comments
 (0)