front/general: replace manual DI with require(...)
This commit is contained in:
parent
7e26fc87ce
commit
36ffa5b4e7
13 changed files with 112 additions and 153 deletions
|
@ -92,4 +92,4 @@ class Api {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = Api;
|
||||
module.exports = new Api();
|
||||
|
|
|
@ -2,16 +2,17 @@
|
|||
|
||||
const cookies = require('js-cookie');
|
||||
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 {
|
||||
constructor(api, topNavigationController, loginView) {
|
||||
this.api = api;
|
||||
this.topNavigationController = topNavigationController;
|
||||
this.loginView = loginView;
|
||||
constructor() {
|
||||
this.loginView = new LoginView();
|
||||
|
||||
const auth = cookies.getJSON('auth');
|
||||
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');
|
||||
page('/');
|
||||
this.loginView.notifyError(
|
||||
|
@ -22,11 +23,11 @@ class AuthController {
|
|||
}
|
||||
|
||||
loginRoute() {
|
||||
this.topNavigationController.activate('login');
|
||||
topNavController.activate('login');
|
||||
this.loginView.render({
|
||||
login: (name, password, doRemember) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.api.login(name, password)
|
||||
api.login(name, password)
|
||||
.then(() => {
|
||||
const options = {};
|
||||
if (doRemember) {
|
||||
|
@ -45,11 +46,11 @@ class AuthController {
|
|||
}
|
||||
|
||||
logoutRoute() {
|
||||
this.api.logout();
|
||||
api.logout();
|
||||
cookies.remove('auth');
|
||||
page('/');
|
||||
this.loginView.notifySuccess('Logged out');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AuthController;
|
||||
module.exports = new AuthController();
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
const topNavController = require('../controllers/top_nav_controller.js');
|
||||
|
||||
class CommentsController {
|
||||
constructor(topNavigationController) {
|
||||
this.topNavigationController = topNavigationController;
|
||||
}
|
||||
|
||||
listCommentsRoute() {
|
||||
this.topNavigationController.activate('comments');
|
||||
topNavController.activate('comments');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CommentsController;
|
||||
module.exports = new CommentsController();
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
'use strict';
|
||||
|
||||
const topNavController = require('../controllers/top_nav_controller.js');
|
||||
const HelpView = require('../views/help_view.js');
|
||||
|
||||
class HelpController {
|
||||
constructor(topNavigationController, helpView) {
|
||||
this.topNavigationController = topNavigationController;
|
||||
this.helpView = helpView;
|
||||
constructor() {
|
||||
this.helpView = new HelpView();
|
||||
}
|
||||
|
||||
showHelpRoute(section) {
|
||||
this.topNavigationController.activate('help');
|
||||
topNavController.activate('help');
|
||||
this.helpView.render(section);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HelpController;
|
||||
module.exports = new HelpController();
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
const topNavController = require('../controllers/top_nav_controller.js');
|
||||
|
||||
class HistoryController {
|
||||
constructor(topNavigationController) {
|
||||
this.topNavigationController = topNavigationController;
|
||||
}
|
||||
|
||||
listHistoryRoute() {
|
||||
this.topNavigationController.activate('');
|
||||
topNavController.activate('');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HistoryController;
|
||||
module.exports = new HistoryController();
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
'use strict';
|
||||
|
||||
const topNavController = require('../controllers/top_nav_controller.js');
|
||||
const HomeView = require('../views/home_view.js');
|
||||
|
||||
class HomeController {
|
||||
constructor(topNavigationController, homeView) {
|
||||
this.topNavigationController = topNavigationController;
|
||||
this.homeView = homeView;
|
||||
constructor() {
|
||||
this.homeView = new HomeView();
|
||||
}
|
||||
|
||||
indexRoute() {
|
||||
this.topNavigationController.activate('home');
|
||||
topNavController.activate('home');
|
||||
this.homeView.render();
|
||||
}
|
||||
|
||||
notFoundRoute() {
|
||||
this.topNavigationController.activate('');
|
||||
topNavController.activate('');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HomeController;
|
||||
module.exports = new HomeController();
|
||||
|
|
|
@ -1,25 +1,23 @@
|
|||
'use strict';
|
||||
|
||||
class PostsController {
|
||||
constructor(topNavigationController) {
|
||||
this.topNavigationController = topNavigationController;
|
||||
}
|
||||
const topNavController = require('../controllers/top_nav_controller.js');
|
||||
|
||||
class PostsController {
|
||||
uploadPostsRoute() {
|
||||
this.topNavigationController.activate('upload');
|
||||
topNavController.activate('upload');
|
||||
}
|
||||
|
||||
listPostsRoute() {
|
||||
this.topNavigationController.activate('posts');
|
||||
topNavController.activate('posts');
|
||||
}
|
||||
|
||||
showPostRoute(id) {
|
||||
this.topNavigationController.activate('posts');
|
||||
topNavController.activate('posts');
|
||||
}
|
||||
|
||||
editPostRoute(id) {
|
||||
this.topNavigationController.activate('posts');
|
||||
topNavController.activate('posts');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PostsController;
|
||||
module.exports = new PostsController();
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
const topNavController = require('../controllers/top_nav_controller.js');
|
||||
|
||||
class TagsController {
|
||||
constructor(topNavigationController) {
|
||||
this.topNavigationController = topNavigationController;
|
||||
}
|
||||
|
||||
listTagsRoute() {
|
||||
this.topNavigationController.activate('tags');
|
||||
topNavController.activate('tags');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TagsController;
|
||||
module.exports = new TagsController();
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
'use strict';
|
||||
|
||||
const api = require('../api.js');
|
||||
const TopNavView = require('../views/top_nav_view.js');
|
||||
|
||||
class NavigationItem {
|
||||
constructor(accessKey, name, url) {
|
||||
this.accessKey = accessKey;
|
||||
|
@ -9,10 +12,9 @@ class NavigationItem {
|
|||
}
|
||||
}
|
||||
|
||||
class TopNavigationController {
|
||||
constructor(topNavigationView, api) {
|
||||
this.api = api;
|
||||
this.topNavigationView = topNavigationView;
|
||||
class TopNavController {
|
||||
constructor() {
|
||||
this.topNavView = new TopNavView();
|
||||
this.activeItem = null;
|
||||
|
||||
this.items = {
|
||||
|
@ -29,15 +31,15 @@ class TopNavigationController {
|
|||
'help': new NavigationItem('E', 'Help', '/help'),
|
||||
};
|
||||
|
||||
this.api.authenticated.listen(() => {
|
||||
api.authenticated.listen(() => {
|
||||
this.updateVisibility();
|
||||
this.topNavigationView.render(this.items, this.activeItem);
|
||||
this.topNavigationView.activate(this.activeItem);
|
||||
this.topNavView.render(this.items, this.activeItem);
|
||||
this.topNavView.activate(this.activeItem);
|
||||
});
|
||||
|
||||
this.updateVisibility();
|
||||
this.topNavigationView.render(this.items, this.activeItem);
|
||||
this.topNavigationView.activate(this.activeItem);
|
||||
this.topNavView.render(this.items, this.activeItem);
|
||||
this.topNavView.activate(this.activeItem);
|
||||
}
|
||||
|
||||
updateVisibility() {
|
||||
|
@ -45,22 +47,22 @@ class TopNavigationController {
|
|||
for (let key of b) {
|
||||
this.items[key].available = true;
|
||||
}
|
||||
if (!this.api.hasPrivilege('posts:list')) {
|
||||
if (!api.hasPrivilege('posts:list')) {
|
||||
this.items.posts.available = false;
|
||||
}
|
||||
if (!this.api.hasPrivilege('posts:create')) {
|
||||
if (!api.hasPrivilege('posts:create')) {
|
||||
this.items.upload.available = false;
|
||||
}
|
||||
if (!this.api.hasPrivilege('comments:list')) {
|
||||
if (!api.hasPrivilege('comments:list')) {
|
||||
this.items.comments.available = false;
|
||||
}
|
||||
if (!this.api.hasPrivilege('tags:list')) {
|
||||
if (!api.hasPrivilege('tags:list')) {
|
||||
this.items.tags.available = false;
|
||||
}
|
||||
if (!this.api.hasPrivilege('users:list')) {
|
||||
if (!api.hasPrivilege('users:list')) {
|
||||
this.items.users.available = false;
|
||||
}
|
||||
if (this.api.isLoggedIn()) {
|
||||
if (api.isLoggedIn()) {
|
||||
this.items.register.available = false;
|
||||
this.items.login.available = false;
|
||||
} else {
|
||||
|
@ -71,8 +73,8 @@ class TopNavigationController {
|
|||
|
||||
activate(itemName) {
|
||||
this.activeItem = itemName;
|
||||
this.topNavigationView.activate(this.activeItem);
|
||||
this.topNavView.activate(this.activeItem);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TopNavigationController;
|
||||
module.exports = new TopNavController();
|
|
@ -1,61 +1,57 @@
|
|||
'use strict';
|
||||
|
||||
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 {
|
||||
constructor(
|
||||
api, topNavigationController, authController, registrationView) {
|
||||
this.api = api;
|
||||
this.topNavigationController = topNavigationController;
|
||||
this.authController = authController;
|
||||
this.registrationView = registrationView;
|
||||
constructor() {
|
||||
this.registrationView = new RegistrationView();
|
||||
}
|
||||
|
||||
listUsersRoute() {
|
||||
this.topNavigationController.activate('users');
|
||||
topNavController.activate('users');
|
||||
}
|
||||
|
||||
createUserRoute() {
|
||||
this.topNavigationController.activate('register');
|
||||
this.registrationView.render({
|
||||
register: (userName, userPassword, userEmail) => {
|
||||
topNavController.activate('register');
|
||||
this.registrationView.render({register: this._register});
|
||||
}
|
||||
|
||||
_register(name, password, email) {
|
||||
const data = {
|
||||
'name': userName,
|
||||
'password': userPassword,
|
||||
'email': userEmail
|
||||
'name': name,
|
||||
'password': password,
|
||||
'email': email
|
||||
};
|
||||
// TODO: reduce callback hell
|
||||
return new Promise((resolve, reject) => {
|
||||
this.api.post('/users/', data)
|
||||
.then(() => {
|
||||
this.authController.login(userName, userPassword)
|
||||
.then(() => {
|
||||
api.post('/users/', data).then(() => {
|
||||
api.login(name, password).then(() => {
|
||||
resolve();
|
||||
page('/');
|
||||
})
|
||||
.catch(response => {
|
||||
}).catch(response => {
|
||||
reject(response.description);
|
||||
});
|
||||
})
|
||||
.catch(response => {
|
||||
}).catch(response => {
|
||||
reject(response.description);
|
||||
});
|
||||
});
|
||||
}});
|
||||
}
|
||||
|
||||
showUserRoute(user) {
|
||||
if (this.authController.isLoggedIn() &&
|
||||
user == this.authController.getCurrentUser().name) {
|
||||
this.topNavigationController.activate('account');
|
||||
if (api.isLoggedIn() &&
|
||||
user == api.getCurrentUser().name) {
|
||||
topNavController.activate('account');
|
||||
} else {
|
||||
this.topNavigationController.activate('users');
|
||||
topNavController.activate('users');
|
||||
}
|
||||
}
|
||||
|
||||
editUserRoute(user) {
|
||||
this.topNavigationController.activate('users');
|
||||
topNavController.activate('users');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UsersController;
|
||||
module.exports = new UsersController();
|
||||
|
|
|
@ -1,52 +1,16 @@
|
|||
'use strict';
|
||||
|
||||
// ------------------
|
||||
// - import objects -
|
||||
// ------------------
|
||||
const Api = require('./api.js');
|
||||
const HomeView = require('./views/home_view.js');
|
||||
const HelpView = require('./views/help_view.js');
|
||||
const LoginView = require('./views/login_view.js');
|
||||
const RegistrationView = require('./views/registration_view.js');
|
||||
const TopNavigationView = require('./views/top_navigation_view.js');
|
||||
const TopNavigationController
|
||||
= require('./controllers/top_navigation_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);
|
||||
// ----------------------
|
||||
// - import controllers -
|
||||
// ----------------------
|
||||
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');
|
||||
|
||||
// -----------------
|
||||
// - setup routing -
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
const BaseView = require('./base_view.js');
|
||||
|
||||
class TopNavigationView extends BaseView {
|
||||
class TopNavView extends BaseView {
|
||||
constructor() {
|
||||
super();
|
||||
this.template = this.getTemplate('top-navigation-template');
|
||||
this.template = this.getTemplate('top-nav-template');
|
||||
this.navHolder = document.getElementById('top-nav-holder');
|
||||
}
|
||||
|
||||
|
@ -34,4 +34,4 @@ class TopNavigationView extends BaseView {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = TopNavigationView;
|
||||
module.exports = TopNavView;
|
Loading…
Reference in a new issue