front/general: replace manual DI with require(...)

This commit is contained in:
rr- 2016-04-01 00:20:34 +02:00
parent 7e26fc87ce
commit 36ffa5b4e7
13 changed files with 112 additions and 153 deletions

View file

@ -92,4 +92,4 @@ class Api {
} }
} }
module.exports = Api; module.exports = new Api();

View file

@ -2,16 +2,17 @@
const cookies = require('js-cookie'); const cookies = require('js-cookie');
const page = require('page'); const page = require('page');
const api = require('../api.js');
const topNavController = require('../controllers/top_nav_controller.js');
const LoginView = require('../views/login_view.js');
class AuthController { class AuthController {
constructor(api, topNavigationController, loginView) { constructor() {
this.api = api; this.loginView = new LoginView();
this.topNavigationController = topNavigationController;
this.loginView = loginView;
const auth = cookies.getJSON('auth'); const auth = cookies.getJSON('auth');
if (auth && auth.user && auth.password) { if (auth && auth.user && auth.password) {
this.api.login(auth.user, auth.password).catch(errorMessage => { api.login(auth.user, auth.password).catch(errorMessage => {
cookies.remove('auth'); cookies.remove('auth');
page('/'); page('/');
this.loginView.notifyError( this.loginView.notifyError(
@ -22,11 +23,11 @@ class AuthController {
} }
loginRoute() { loginRoute() {
this.topNavigationController.activate('login'); topNavController.activate('login');
this.loginView.render({ this.loginView.render({
login: (name, password, doRemember) => { login: (name, password, doRemember) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.api.login(name, password) api.login(name, password)
.then(() => { .then(() => {
const options = {}; const options = {};
if (doRemember) { if (doRemember) {
@ -45,11 +46,11 @@ class AuthController {
} }
logoutRoute() { logoutRoute() {
this.api.logout(); api.logout();
cookies.remove('auth'); cookies.remove('auth');
page('/'); page('/');
this.loginView.notifySuccess('Logged out'); this.loginView.notifySuccess('Logged out');
} }
} }
module.exports = AuthController; module.exports = new AuthController();

View file

@ -1,13 +1,11 @@
'use strict'; 'use strict';
class CommentsController { const topNavController = require('../controllers/top_nav_controller.js');
constructor(topNavigationController) {
this.topNavigationController = topNavigationController;
}
class CommentsController {
listCommentsRoute() { listCommentsRoute() {
this.topNavigationController.activate('comments'); topNavController.activate('comments');
} }
} }
module.exports = CommentsController; module.exports = new CommentsController();

View file

@ -1,15 +1,17 @@
'use strict'; 'use strict';
const topNavController = require('../controllers/top_nav_controller.js');
const HelpView = require('../views/help_view.js');
class HelpController { class HelpController {
constructor(topNavigationController, helpView) { constructor() {
this.topNavigationController = topNavigationController; this.helpView = new HelpView();
this.helpView = helpView;
} }
showHelpRoute(section) { showHelpRoute(section) {
this.topNavigationController.activate('help'); topNavController.activate('help');
this.helpView.render(section); this.helpView.render(section);
} }
} }
module.exports = HelpController; module.exports = new HelpController();

View file

@ -1,13 +1,11 @@
'use strict'; 'use strict';
class HistoryController { const topNavController = require('../controllers/top_nav_controller.js');
constructor(topNavigationController) {
this.topNavigationController = topNavigationController;
}
class HistoryController {
listHistoryRoute() { listHistoryRoute() {
this.topNavigationController.activate(''); topNavController.activate('');
} }
} }
module.exports = HistoryController; module.exports = new HistoryController();

View file

@ -1,19 +1,21 @@
'use strict'; 'use strict';
const topNavController = require('../controllers/top_nav_controller.js');
const HomeView = require('../views/home_view.js');
class HomeController { class HomeController {
constructor(topNavigationController, homeView) { constructor() {
this.topNavigationController = topNavigationController; this.homeView = new HomeView();
this.homeView = homeView;
} }
indexRoute() { indexRoute() {
this.topNavigationController.activate('home'); topNavController.activate('home');
this.homeView.render(); this.homeView.render();
} }
notFoundRoute() { notFoundRoute() {
this.topNavigationController.activate(''); topNavController.activate('');
} }
} }
module.exports = HomeController; module.exports = new HomeController();

View file

@ -1,25 +1,23 @@
'use strict'; 'use strict';
class PostsController { const topNavController = require('../controllers/top_nav_controller.js');
constructor(topNavigationController) {
this.topNavigationController = topNavigationController;
}
class PostsController {
uploadPostsRoute() { uploadPostsRoute() {
this.topNavigationController.activate('upload'); topNavController.activate('upload');
} }
listPostsRoute() { listPostsRoute() {
this.topNavigationController.activate('posts'); topNavController.activate('posts');
} }
showPostRoute(id) { showPostRoute(id) {
this.topNavigationController.activate('posts'); topNavController.activate('posts');
} }
editPostRoute(id) { editPostRoute(id) {
this.topNavigationController.activate('posts'); topNavController.activate('posts');
} }
} }
module.exports = PostsController; module.exports = new PostsController();

View file

@ -1,13 +1,11 @@
'use strict'; 'use strict';
class TagsController { const topNavController = require('../controllers/top_nav_controller.js');
constructor(topNavigationController) {
this.topNavigationController = topNavigationController;
}
class TagsController {
listTagsRoute() { listTagsRoute() {
this.topNavigationController.activate('tags'); topNavController.activate('tags');
} }
} }
module.exports = TagsController; module.exports = new TagsController();

View file

@ -1,5 +1,8 @@
'use strict'; 'use strict';
const api = require('../api.js');
const TopNavView = require('../views/top_nav_view.js');
class NavigationItem { class NavigationItem {
constructor(accessKey, name, url) { constructor(accessKey, name, url) {
this.accessKey = accessKey; this.accessKey = accessKey;
@ -9,10 +12,9 @@ class NavigationItem {
} }
} }
class TopNavigationController { class TopNavController {
constructor(topNavigationView, api) { constructor() {
this.api = api; this.topNavView = new TopNavView();
this.topNavigationView = topNavigationView;
this.activeItem = null; this.activeItem = null;
this.items = { this.items = {
@ -29,15 +31,15 @@ class TopNavigationController {
'help': new NavigationItem('E', 'Help', '/help'), 'help': new NavigationItem('E', 'Help', '/help'),
}; };
this.api.authenticated.listen(() => { api.authenticated.listen(() => {
this.updateVisibility(); this.updateVisibility();
this.topNavigationView.render(this.items, this.activeItem); this.topNavView.render(this.items, this.activeItem);
this.topNavigationView.activate(this.activeItem); this.topNavView.activate(this.activeItem);
}); });
this.updateVisibility(); this.updateVisibility();
this.topNavigationView.render(this.items, this.activeItem); this.topNavView.render(this.items, this.activeItem);
this.topNavigationView.activate(this.activeItem); this.topNavView.activate(this.activeItem);
} }
updateVisibility() { updateVisibility() {
@ -45,22 +47,22 @@ class TopNavigationController {
for (let key of b) { for (let key of b) {
this.items[key].available = true; this.items[key].available = true;
} }
if (!this.api.hasPrivilege('posts:list')) { if (!api.hasPrivilege('posts:list')) {
this.items.posts.available = false; this.items.posts.available = false;
} }
if (!this.api.hasPrivilege('posts:create')) { if (!api.hasPrivilege('posts:create')) {
this.items.upload.available = false; this.items.upload.available = false;
} }
if (!this.api.hasPrivilege('comments:list')) { if (!api.hasPrivilege('comments:list')) {
this.items.comments.available = false; this.items.comments.available = false;
} }
if (!this.api.hasPrivilege('tags:list')) { if (!api.hasPrivilege('tags:list')) {
this.items.tags.available = false; this.items.tags.available = false;
} }
if (!this.api.hasPrivilege('users:list')) { if (!api.hasPrivilege('users:list')) {
this.items.users.available = false; this.items.users.available = false;
} }
if (this.api.isLoggedIn()) { if (api.isLoggedIn()) {
this.items.register.available = false; this.items.register.available = false;
this.items.login.available = false; this.items.login.available = false;
} else { } else {
@ -71,8 +73,8 @@ class TopNavigationController {
activate(itemName) { activate(itemName) {
this.activeItem = itemName; this.activeItem = itemName;
this.topNavigationView.activate(this.activeItem); this.topNavView.activate(this.activeItem);
} }
} }
module.exports = TopNavigationController; module.exports = new TopNavController();

View file

@ -1,61 +1,57 @@
'use strict'; 'use strict';
const page = require('page'); const page = require('page');
const api = require('../api.js');
const topNavController = require('../controllers/top_nav_controller.js');
const RegistrationView = require('../views/registration_view.js');
class UsersController { class UsersController {
constructor( constructor() {
api, topNavigationController, authController, registrationView) { this.registrationView = new RegistrationView();
this.api = api;
this.topNavigationController = topNavigationController;
this.authController = authController;
this.registrationView = registrationView;
} }
listUsersRoute() { listUsersRoute() {
this.topNavigationController.activate('users'); topNavController.activate('users');
} }
createUserRoute() { createUserRoute() {
this.topNavigationController.activate('register'); topNavController.activate('register');
this.registrationView.render({ this.registrationView.render({register: this._register});
register: (userName, userPassword, userEmail) => { }
const data = {
'name': userName, _register(name, password, email) {
'password': userPassword, const data = {
'email': userEmail 'name': name,
}; 'password': password,
// TODO: reduce callback hell 'email': email
return new Promise((resolve, reject) => { };
this.api.post('/users/', data) // TODO: reduce callback hell
.then(() => { return new Promise((resolve, reject) => {
this.authController.login(userName, userPassword) api.post('/users/', data).then(() => {
.then(() => { api.login(name, password).then(() => {
resolve(); resolve();
page('/'); page('/');
}) }).catch(response => {
.catch(response => { reject(response.description);
reject(response.description);
});
})
.catch(response => {
reject(response.description);
});
}); });
}}); }).catch(response => {
reject(response.description);
});
});
} }
showUserRoute(user) { showUserRoute(user) {
if (this.authController.isLoggedIn() && if (api.isLoggedIn() &&
user == this.authController.getCurrentUser().name) { user == api.getCurrentUser().name) {
this.topNavigationController.activate('account'); topNavController.activate('account');
} else { } else {
this.topNavigationController.activate('users'); topNavController.activate('users');
} }
} }
editUserRoute(user) { editUserRoute(user) {
this.topNavigationController.activate('users'); topNavController.activate('users');
} }
} }
module.exports = UsersController; module.exports = new UsersController();

View file

@ -1,52 +1,16 @@
'use strict'; 'use strict';
// ------------------ // ----------------------
// - import objects - // - import controllers -
// ------------------ // ----------------------
const Api = require('./api.js'); const homeController = require('./controllers/home_controller.js');
const HomeView = require('./views/home_view.js'); const postsController = require('./controllers/posts_controller.js');
const HelpView = require('./views/help_view.js'); const usersController = require('./controllers/users_controller.js');
const LoginView = require('./views/login_view.js'); const helpController = require('./controllers/help_controller.js');
const RegistrationView = require('./views/registration_view.js'); const authController = require('./controllers/auth_controller.js');
const TopNavigationView = require('./views/top_navigation_view.js'); const commentsController = require('./controllers/comments_controller.js');
const TopNavigationController const historyController = require('./controllers/history_controller.js');
= require('./controllers/top_navigation_controller.js'); const tagsController = require('./controllers/tags_controller.js');
const HomeController = require('./controllers/home_controller.js');
const PostsController = require('./controllers/posts_controller.js');
const UsersController = require('./controllers/users_controller.js');
const HelpController = require('./controllers/help_controller.js');
const AuthController = require('./controllers/auth_controller.js');
const CommentsController = require('./controllers/comments_controller.js');
const HistoryController = require('./controllers/history_controller.js');
const TagsController = require('./controllers/tags_controller.js');
// -------------------
// - resolve objects -
// -------------------
const api = new Api();
const topNavigationView = new TopNavigationView();
const homeView = new HomeView();
const helpView = new HelpView();
const loginView = new LoginView();
const registrationView = new RegistrationView();
const topNavigationController
= new TopNavigationController(topNavigationView, api);
const authController = new AuthController(
api, topNavigationController, loginView);
const homeController = new HomeController(topNavigationController, homeView);
const postsController = new PostsController(topNavigationController);
const usersController = new UsersController(
api,
topNavigationController,
authController,
registrationView);
const helpController = new HelpController(topNavigationController, helpView);
const commentsController = new CommentsController(topNavigationController);
const historyController = new HistoryController(topNavigationController);
const tagsController = new TagsController(topNavigationController);
// ----------------- // -----------------
// - setup routing - // - setup routing -

View file

@ -2,10 +2,10 @@
const BaseView = require('./base_view.js'); const BaseView = require('./base_view.js');
class TopNavigationView extends BaseView { class TopNavView extends BaseView {
constructor() { constructor() {
super(); super();
this.template = this.getTemplate('top-navigation-template'); this.template = this.getTemplate('top-nav-template');
this.navHolder = document.getElementById('top-nav-holder'); this.navHolder = document.getElementById('top-nav-holder');
} }
@ -34,4 +34,4 @@ class TopNavigationView extends BaseView {
} }
} }
module.exports = TopNavigationView; module.exports = TopNavView;