diff --git a/static/html/top_navigation.tpl b/static/html/top_nav.tpl similarity index 100% rename from static/html/top_navigation.tpl rename to static/html/top_nav.tpl diff --git a/static/js/api.js b/static/js/api.js index a71d0b1f..0c86177c 100644 --- a/static/js/api.js +++ b/static/js/api.js @@ -92,4 +92,4 @@ class Api { } } -module.exports = Api; +module.exports = new Api(); diff --git a/static/js/controllers/auth_controller.js b/static/js/controllers/auth_controller.js index 908e7f40..39a0c432 100644 --- a/static/js/controllers/auth_controller.js +++ b/static/js/controllers/auth_controller.js @@ -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(); diff --git a/static/js/controllers/comments_controller.js b/static/js/controllers/comments_controller.js index b6ff02cd..ff934df4 100644 --- a/static/js/controllers/comments_controller.js +++ b/static/js/controllers/comments_controller.js @@ -1,13 +1,11 @@ 'use strict'; -class CommentsController { - constructor(topNavigationController) { - this.topNavigationController = topNavigationController; - } +const topNavController = require('../controllers/top_nav_controller.js'); +class CommentsController { listCommentsRoute() { - this.topNavigationController.activate('comments'); + topNavController.activate('comments'); } } -module.exports = CommentsController; +module.exports = new CommentsController(); diff --git a/static/js/controllers/help_controller.js b/static/js/controllers/help_controller.js index 5b68eb51..86788f62 100644 --- a/static/js/controllers/help_controller.js +++ b/static/js/controllers/help_controller.js @@ -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(); diff --git a/static/js/controllers/history_controller.js b/static/js/controllers/history_controller.js index ff0b2644..4ecad224 100644 --- a/static/js/controllers/history_controller.js +++ b/static/js/controllers/history_controller.js @@ -1,13 +1,11 @@ 'use strict'; -class HistoryController { - constructor(topNavigationController) { - this.topNavigationController = topNavigationController; - } +const topNavController = require('../controllers/top_nav_controller.js'); +class HistoryController { listHistoryRoute() { - this.topNavigationController.activate(''); + topNavController.activate(''); } } -module.exports = HistoryController; +module.exports = new HistoryController(); diff --git a/static/js/controllers/home_controller.js b/static/js/controllers/home_controller.js index 85a6ad60..1eede35d 100644 --- a/static/js/controllers/home_controller.js +++ b/static/js/controllers/home_controller.js @@ -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(); diff --git a/static/js/controllers/posts_controller.js b/static/js/controllers/posts_controller.js index bbd540ce..08cd2d2b 100644 --- a/static/js/controllers/posts_controller.js +++ b/static/js/controllers/posts_controller.js @@ -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(); diff --git a/static/js/controllers/tags_controller.js b/static/js/controllers/tags_controller.js index 3124d40c..f16123b5 100644 --- a/static/js/controllers/tags_controller.js +++ b/static/js/controllers/tags_controller.js @@ -1,13 +1,11 @@ 'use strict'; -class TagsController { - constructor(topNavigationController) { - this.topNavigationController = topNavigationController; - } +const topNavController = require('../controllers/top_nav_controller.js'); +class TagsController { listTagsRoute() { - this.topNavigationController.activate('tags'); + topNavController.activate('tags'); } } -module.exports = TagsController; +module.exports = new TagsController(); diff --git a/static/js/controllers/top_navigation_controller.js b/static/js/controllers/top_nav_controller.js similarity index 68% rename from static/js/controllers/top_navigation_controller.js rename to static/js/controllers/top_nav_controller.js index 4082b093..8fcafcfb 100644 --- a/static/js/controllers/top_navigation_controller.js +++ b/static/js/controllers/top_nav_controller.js @@ -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(); diff --git a/static/js/controllers/users_controller.js b/static/js/controllers/users_controller.js index b0f81238..3872d3f0 100644 --- a/static/js/controllers/users_controller.js +++ b/static/js/controllers/users_controller.js @@ -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) => { - const data = { - 'name': userName, - 'password': userPassword, - 'email': userEmail - }; - // TODO: reduce callback hell - return new Promise((resolve, reject) => { - this.api.post('/users/', data) - .then(() => { - this.authController.login(userName, userPassword) - .then(() => { - resolve(); - page('/'); - }) - .catch(response => { - reject(response.description); - }); - }) - .catch(response => { - reject(response.description); - }); + topNavController.activate('register'); + this.registrationView.render({register: this._register}); + } + + _register(name, password, email) { + const data = { + 'name': name, + 'password': password, + 'email': email + }; + // TODO: reduce callback hell + return new Promise((resolve, reject) => { + api.post('/users/', data).then(() => { + api.login(name, password).then(() => { + resolve(); + page('/'); + }).catch(response => { + reject(response.description); }); - }}); + }).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(); diff --git a/static/js/main.js b/static/js/main.js index 50fc2956..c031edf8 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -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 - diff --git a/static/js/views/top_navigation_view.js b/static/js/views/top_nav_view.js similarity index 87% rename from static/js/views/top_navigation_view.js rename to static/js/views/top_nav_view.js index 23c76bf3..6864c633 100644 --- a/static/js/views/top_navigation_view.js +++ b/static/js/views/top_nav_view.js @@ -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;