74 Zeilen
2,3 KiB
JavaScript
74 Zeilen
2,3 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
var App = {
|
||
|
|
currentUser: null,
|
||
|
|
|
||
|
|
routes: {
|
||
|
|
dashboard: null, // set after all scripts load
|
||
|
|
flashcards: null,
|
||
|
|
tutorials: null,
|
||
|
|
quiz: null,
|
||
|
|
cheatsheets: null,
|
||
|
|
admin: null
|
||
|
|
},
|
||
|
|
|
||
|
|
init: function() {
|
||
|
|
this.routes.dashboard = Dashboard;
|
||
|
|
this.routes.flashcards = Flashcards;
|
||
|
|
this.routes.tutorials = Tutorials;
|
||
|
|
this.routes.quiz = Quiz;
|
||
|
|
this.routes.cheatsheets = CheatSheets;
|
||
|
|
this.routes.admin = Admin;
|
||
|
|
|
||
|
|
document.getElementById('login-form').addEventListener('submit', function(e) { Auth.handleLogin(e); });
|
||
|
|
document.getElementById('logout-btn').addEventListener('click', function() { Auth.handleLogout(); });
|
||
|
|
window.addEventListener('hashchange', function() { App.route(); });
|
||
|
|
this.checkAuth();
|
||
|
|
},
|
||
|
|
|
||
|
|
checkAuth: function() {
|
||
|
|
API.get('/me').then(function(data) {
|
||
|
|
App.setUser(data.user);
|
||
|
|
}).catch(function() {
|
||
|
|
App.showLogin();
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
setUser: function(user) {
|
||
|
|
this.currentUser = user;
|
||
|
|
document.getElementById('nav-username').textContent = user.display_name;
|
||
|
|
document.getElementById('login-screen').style.display = 'none';
|
||
|
|
document.getElementById('app').style.display = 'block';
|
||
|
|
if (user.is_admin) {
|
||
|
|
document.querySelector('.nav-admin').style.display = '';
|
||
|
|
}
|
||
|
|
this.route();
|
||
|
|
},
|
||
|
|
|
||
|
|
showLogin: function() {
|
||
|
|
this.currentUser = null;
|
||
|
|
document.getElementById('login-screen').style.display = 'flex';
|
||
|
|
document.getElementById('app').style.display = 'none';
|
||
|
|
},
|
||
|
|
|
||
|
|
route: function() {
|
||
|
|
var hash = location.hash.slice(2) || 'dashboard';
|
||
|
|
var parts = hash.split('/');
|
||
|
|
var routeName = parts[0];
|
||
|
|
var params = parts.slice(1);
|
||
|
|
|
||
|
|
document.querySelectorAll('.nav-link').forEach(function(link) {
|
||
|
|
link.classList.toggle('active', link.dataset.route === routeName);
|
||
|
|
});
|
||
|
|
|
||
|
|
var handler = this.routes[routeName];
|
||
|
|
if (handler && handler.render) {
|
||
|
|
handler.render(params);
|
||
|
|
} else {
|
||
|
|
document.getElementById('content').textContent = 'Seite nicht gefunden';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
document.addEventListener('DOMContentLoaded', function() { App.init(); });
|