client/routing: move routing to controllers

This commit is contained in:
rr- 2016-04-06 21:49:26 +02:00
parent 55cc7b59e4
commit 5f29fa12c2
13 changed files with 92 additions and 62 deletions

View file

@ -119,6 +119,7 @@ nav.text-nav ul li.active a {
} }
.messages { .messages {
margin: 0 auto;
width: 30em; width: 30em;
max-width: 100%; max-width: 100%;
} }

View file

@ -23,6 +23,16 @@ class AuthController {
} }
} }
registerRoutes() {
page(/\/password-reset\/([^:]+):([^:]+)$/,
(ctx, next) => {
this.passwordResetFinishRoute(ctx.params[0], ctx.params[1]);
});
page('/password-reset', (ctx, next) => { this.passwordResetRoute(); });
page('/login', (ctx, next) => { this.loginRoute(); });
page('/logout', (ctx, next) => { this.logoutRoute(); });
}
loginRoute() { loginRoute() {
topNavController.activate('login'); topNavController.activate('login');
this.loginView.render({ this.loginView.render({

View file

@ -1,8 +1,13 @@
'use strict'; 'use strict';
const page = require('page');
const topNavController = require('../controllers/top_nav_controller.js'); const topNavController = require('../controllers/top_nav_controller.js');
class CommentsController { class CommentsController {
registerRoutes() {
page('/comments', (ctx, next) => { this.listCommentsRoute(); });
}
listCommentsRoute() { listCommentsRoute() {
topNavController.activate('comments'); topNavController.activate('comments');
} }

View file

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const page = require('page');
const topNavController = require('../controllers/top_nav_controller.js'); const topNavController = require('../controllers/top_nav_controller.js');
const HelpView = require('../views/help_view.js'); const HelpView = require('../views/help_view.js');
@ -8,6 +9,13 @@ class HelpController {
this.helpView = new HelpView(); this.helpView = new HelpView();
} }
registerRoutes() {
page('/help', () => { this.showHelpRoute(); });
page(
'/help/:section',
(ctx, next) => { this.showHelpRoute(ctx.params.section); });
}
showHelpRoute(section) { showHelpRoute(section) {
topNavController.activate('help'); topNavController.activate('help');
this.helpView.render(section); this.helpView.render(section);

View file

@ -1,8 +1,13 @@
'use strict'; 'use strict';
const page = require('page');
const topNavController = require('../controllers/top_nav_controller.js'); const topNavController = require('../controllers/top_nav_controller.js');
class HistoryController { class HistoryController {
registerRoutes() {
page('/history', (ctx, next) => { this.showHistoryRoute(); });
}
listHistoryRoute() { listHistoryRoute() {
topNavController.activate(''); topNavController.activate('');
} }

View file

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const page = require('page');
const topNavController = require('../controllers/top_nav_controller.js'); const topNavController = require('../controllers/top_nav_controller.js');
const HomeView = require('../views/home_view.js'); const HomeView = require('../views/home_view.js');
@ -8,6 +9,11 @@ class HomeController {
this.homeView = new HomeView(); this.homeView = new HomeView();
} }
registerRoutes() {
page('/', (ctx, next) => { this.indexRoute(); });
page('*', (ctx, next) => { this.notFoundRoute(); });
}
indexRoute() { indexRoute() {
topNavController.activate('home'); topNavController.activate('home');
this.homeView.render(); this.homeView.render();

View file

@ -1,8 +1,20 @@
'use strict'; 'use strict';
const page = require('page');
const topNavController = require('../controllers/top_nav_controller.js'); const topNavController = require('../controllers/top_nav_controller.js');
class PostsController { class PostsController {
registerRoutes() {
page('/upload', (ctx, next) => { this.uploadPostsRoute(); });
page('/posts', (ctx, next) => { this.listPostsRoute(); });
page(
'/post/:id',
(ctx, next) => { this.showPostRoute(ctx.params.id); });
page(
'/post/:id/edit',
(ctx, next) => { this.editPostRoute(ctx.params.id); });
}
uploadPostsRoute() { uploadPostsRoute() {
topNavController.activate('upload'); topNavController.activate('upload');
} }

View file

@ -1,8 +1,13 @@
'use strict'; 'use strict';
const page = require('page');
const topNavController = require('../controllers/top_nav_controller.js'); const topNavController = require('../controllers/top_nav_controller.js');
class TagsController { class TagsController {
registerRoutes() {
page('/tags', (ctx, next) => { this.listTagsRoute(); });
}
listTagsRoute() { listTagsRoute() {
topNavController.activate('tags'); topNavController.activate('tags');
} }

View file

@ -43,7 +43,7 @@ class TopNavController {
} }
updateVisibility() { updateVisibility() {
this.items['account'].url = '/user/' + api.userName; this.items.account.url = '/user/' + api.userName;
const b = Object.keys(this.items); const b = Object.keys(this.items);
for (let key of b) { for (let key of b) {

View file

@ -5,10 +5,23 @@ const page = require('page');
const api = require('../api.js'); const api = require('../api.js');
const topNavController = require('../controllers/top_nav_controller.js'); const topNavController = require('../controllers/top_nav_controller.js');
const RegistrationView = require('../views/registration_view.js'); const RegistrationView = require('../views/registration_view.js');
const UserView = require('../views/user_view.js');
class UsersController { class UsersController {
constructor() { constructor() {
this.registrationView = new RegistrationView(); this.registrationView = new RegistrationView();
this.userView = new UserView();
}
registerRoutes() {
page('/register', () => { this.createUserRoute(); });
page('/users', () => { this.listUsersRoute(); });
page(
'/user/:name',
(ctx, next) => { this.showUserRoute(ctx.params.name); });
page(
'/user/:name/edit',
(ctx, next) => { this.editUserRoute(ctx.params.name); });
} }
listUsersRoute() { listUsersRoute() {
@ -28,7 +41,6 @@ class UsersController {
'password': password, 'password': password,
'email': email 'email': email
}; };
// TODO: reduce callback hell
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
api.post('/users/', data).then(() => { api.post('/users/', data).then(() => {
api.login(name, password).then(() => { api.login(name, password).then(() => {
@ -45,12 +57,18 @@ class UsersController {
}); });
} }
showUserRoute(user) { showUserRoute(name) {
if (api.isLoggedIn() && user == api.userName) { if (api.isLoggedIn() && name == api.userName) {
topNavController.activate('account'); topNavController.activate('account');
} else { } else {
topNavController.activate('users'); topNavController.activate('users');
} }
this.userView.empty();
api.get('/user/' + name).then(response => {
this.userView.render({user: response.user});
}).catch(response => {
this.userView.notifyError(response.description);
});
} }
editUserRoute(user) { editUserRoute(user) {

View file

@ -2,63 +2,19 @@
require('./util/handlebars-helpers.js'); require('./util/handlebars-helpers.js');
// ---------------------- let controllers = [];
// - import controllers - controllers.push(require('./controllers/posts_controller.js'));
// ---------------------- controllers.push(require('./controllers/users_controller.js'));
const homeController = require('./controllers/home_controller.js'); controllers.push(require('./controllers/help_controller.js'));
const postsController = require('./controllers/posts_controller.js'); controllers.push(require('./controllers/auth_controller.js'));
const usersController = require('./controllers/users_controller.js'); controllers.push(require('./controllers/comments_controller.js'));
const helpController = require('./controllers/help_controller.js'); controllers.push(require('./controllers/history_controller.js'));
const authController = require('./controllers/auth_controller.js'); controllers.push(require('./controllers/tags_controller.js'));
const commentsController = require('./controllers/comments_controller.js');
const historyController = require('./controllers/history_controller.js'); controllers.push(require('./controllers/home_controller.js'));
const tagsController = require('./controllers/tags_controller.js');
// -----------------
// - setup routing -
// -----------------
const page = require('page'); const page = require('page');
for (let controller of controllers) {
page('/', () => { homeController.indexRoute(); }); controller.registerRoutes();
}
page('/upload', () => { postsController.uploadPostsRoute(); });
page('/posts', () => { postsController.listPostsRoute(); });
page('/post/:id', id => { postsController.showPostRoute(id); });
page('/post/:id/edit', id => { postsController.editPostRoute(id); });
page('/register', () => { usersController.createUserRoute(); });
page('/users', () => { usersController.listUsersRoute(); });
page(
'/user/:name',
(ctx, next) => {
usersController.showUserRoute(ctx.params.name);
});
page(
'/user/:name/edit',
(ctx, next) => {
usersController.editUserRoute(ctx.params.name);
});
page('/history', () => { historyController.showHistoryRoute(); });
page('/tags', () => { tagsController.listTagsRoute(); });
page('/comments', () => { commentsController.listCommentsRoute(); });
page(/\/password-reset\/([^:]+):([^:]+)$/,
(ctx, next) => {
authController.passwordResetFinishRoute(ctx.params[0], ctx.params[1]);
});
page('/password-reset', () => { authController.passwordResetRoute(); });
page('/login', () => { authController.loginRoute(); });
page('/logout', () => { authController.logoutRoute(); });
page(
'/help/:section',
(ctx, next) => {
helpController.showHelpRoute(ctx.params.section);
});
page('/help', () => { helpController.showHelpRoute(); });
page('*', () => { homeController.notFoundRoute(); });
page(); page();

View file

@ -70,6 +70,10 @@ class BaseView {
} }
} }
empty() {
this.showView('<div class="messages"></div>');
}
showView(html) { showView(html) {
this.contentHolder.innerHTML = html; this.contentHolder.innerHTML = html;
} }

View file

@ -25,7 +25,7 @@ class HelpView extends BaseView {
} }
const content = this.sectionTemplates[section]({ const content = this.sectionTemplates[section]({
'name': config.name, name: config.name,
}); });
this.showView(this.template({'content': content})); this.showView(this.template({'content': content}));