Commit 1b3a5d9b authored by scmoritz's avatar scmoritz Committed by scmoritz
Browse files

navbar refactored into model (navigation) and view (navbar)

parent 25300917
import m from 'mithril';
import { checkLogin, isLoggedIn } from '../models/auth';
const defaultTabs = ['AMIV', 'Events', 'Studienunterlagen', 'Jobs'];
const tabsLoggedOut = ['Login'];
const tabsLoggedIn = ['Profile', 'Logout'];
const tabToUrl = {
AMIV: { href: '/', onupdate: m.route.link, index: 0 },
Events: { href: '/events', onupdate: m.route.link, index: 1 },
Studienunterlagen: { href: '/studydocuments', onupdate: m.route.link, index: 2 },
Jobs: { href: '/jobs', onupdate: m.route.link, index: 3 },
Login: { href: '/login', onupdate: m.route.link, index: 4 },
Profile: { href: '/profile', onupdate: m.route.link, index: 4 },
Logout: { href: '/logout', onupdate: m.route.link, index: 5 },
};
export default class Navigation {
constructor() {
checkLogin();
this._wasLoggedIn = isLoggedIn();
this._selectedTabIndex = 0;
Object.values(tabToUrl)
.filter(tab => m.route.get().includes(tab.href))
.forEach(tab => {
this._selectedTabIndex = tab.index;
});
this._tabOptions = {
className: 'themed-tabs',
activeSelected: true,
element: 'tab',
selectedTab: this._selectedTabIndex,
};
this.setTabs();
}
setTabs() {
if (isLoggedIn()) {
this._tabs = [...defaultTabs, ...tabsLoggedIn];
} else {
this._tabs = [...defaultTabs, ...tabsLoggedOut];
}
this._tabOptions.tabs = [];
this._tabs.forEach(tab => {
this._tabOptions.tabs.push({
label: tab,
url: tabToUrl[tab],
});
});
}
get tabs() {
return this._tabOptions;
}
onupdate() {
Object.values(tabToUrl)
.filter(tab => m.route.get().includes(tab.href))
.forEach(tab => {
this._selectedTabIndex = tab.index;
});
this._tabOptions.selectedTab = this._selectedTabIndex;
if (this._wasLoggedIn !== isLoggedIn()) {
this._wasLoggedIn = isLoggedIn();
this.setTabs();
}
}
}
import m from 'mithril';
import { Tabs } from '../components';
import { checkLogin, isLoggedIn } from '../models/auth';
const defaultTabs = ['AMIV', 'Events', 'Studienunterlagen', 'Jobs'];
const tabsLoggedOut = ['Login'];
const tabsLoggedIn = ['Profile', 'Logout'];
const tabToUrl = {
AMIV: { href: '/', onupdate: m.route.link, index: 0 },
Events: { href: '/events', onupdate: m.route.link, index: 1 },
Studienunterlagen: { href: '/studydocuments', onupdate: m.route.link, index: 2 },
Jobs: { href: '/jobs', onupdate: m.route.link, index: 3 },
Login: { href: '/login', onupdate: m.route.link, index: 4 },
Profile: { href: '/profile', onupdate: m.route.link, index: 4 },
Logout: { href: '/logout', onupdate: m.route.link, index: 5 },
};
import Navigation from '../models/navigation';
export default class Navbar {
constructor() {
checkLogin();
this.wasLoggedIn = isLoggedIn();
this.selectedTabIndex = 0;
Object.values(tabToUrl)
.filter(tab => m.route.get().includes(tab.href))
.forEach(tab => {
this.selectedTabIndex = tab.index;
});
this.tabOptions = {
className: 'themed-tabs',
activeSelected: true,
element: 'tab',
selectedTab: this.selectedTabIndex,
};
this.setTabs();
}
setTabs() {
if (isLoggedIn()) {
this.tabs = [...defaultTabs, ...tabsLoggedIn];
} else {
this.tabs = [...defaultTabs, ...tabsLoggedOut];
}
this.tabOptions.tabs = [];
this.tabs.forEach(tab => {
this.tabOptions.tabs.push({
label: tab,
url: tabToUrl[tab],
});
});
this.nav = new Navigation();
}
onupdate() {
Object.values(tabToUrl)
.filter(tab => m.route.get().includes(tab.href))
.forEach(tab => {
this.selectedTabIndex = tab.index;
});
this.tabOptions.selectedTab = this.selectedTabIndex;
if (this.wasLoggedIn !== isLoggedIn()) {
this.wasLoggedIn = isLoggedIn();
this.setTabs();
}
this.nav.onupdate();
}
view() {
return m(Tabs, this.tabOptions);
return m(Tabs, this.nav.tabs);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment