2014-08-31 23:22:56 +02:00
|
|
|
var App = App || {};
|
|
|
|
|
2014-09-11 13:59:12 +02:00
|
|
|
App.Router = function(pathJs, _, jQuery, util, appState, presenterManager) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
var root = '#/';
|
|
|
|
|
|
|
|
injectRoutes();
|
|
|
|
|
|
|
|
function navigateToMainPage() {
|
|
|
|
window.location.href = root;
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function navigate(url) {
|
|
|
|
window.location.href = url;
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-12 22:58:07 +02:00
|
|
|
function navigateInplace(url) {
|
|
|
|
if ('replaceState' in history) {
|
|
|
|
history.replaceState('', '', url);
|
|
|
|
pathJs.dispatch(document.location.hash);
|
|
|
|
} else {
|
|
|
|
navigate(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
function start() {
|
2014-09-08 22:02:28 +02:00
|
|
|
pathJs.listen();
|
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function injectRoutes() {
|
2014-09-07 19:49:11 +02:00
|
|
|
inject('#/home', 'homePresenter');
|
2014-09-07 09:30:48 +02:00
|
|
|
inject('#/login', 'loginPresenter');
|
|
|
|
inject('#/logout', 'logoutPresenter');
|
|
|
|
inject('#/register', 'registrationPresenter');
|
2014-09-07 19:49:11 +02:00
|
|
|
inject('#/upload', 'postUploadPresenter');
|
2014-09-08 13:06:32 +02:00
|
|
|
inject('#/password-reset(/:token)', 'userActivationPresenter', {operation: 'passwordReset'});
|
|
|
|
inject('#/activate(/:token)', 'userActivationPresenter', {operation: 'activation'});
|
2014-09-07 19:49:11 +02:00
|
|
|
inject('#/users(/:searchArgs)', 'userListPresenter');
|
|
|
|
inject('#/user/:userName(/:tab)', 'userPresenter');
|
|
|
|
inject('#/posts(/:searchArgs)', 'postListPresenter');
|
|
|
|
inject('#/comments(/:searchArgs)', 'commentListPresenter');
|
|
|
|
inject('#/tags(/:searchArgs)', 'tagListPresenter');
|
|
|
|
inject('#/help', 'helpPresenter');
|
|
|
|
setRoot('#/home');
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function setRoot(newRoot) {
|
|
|
|
root = newRoot;
|
2014-09-08 22:02:28 +02:00
|
|
|
pathJs.root(newRoot);
|
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
function inject(path, presenterName, additionalParams) {
|
2014-09-08 22:02:28 +02:00
|
|
|
pathJs.map(path).to(function() {
|
2014-09-10 13:55:35 +02:00
|
|
|
var finalParams = _.extend(
|
|
|
|
this.params,
|
|
|
|
additionalParams,
|
|
|
|
{previousRoute: pathJs.routes.previous});
|
|
|
|
|
2014-09-11 13:59:12 +02:00
|
|
|
presenterManager.switchContentPresenter( presenterName, finalParams);
|
2014-08-31 23:22:56 +02:00
|
|
|
});
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
start: start,
|
|
|
|
navigate: navigate,
|
2014-09-12 22:58:07 +02:00
|
|
|
navigateInplace: navigateInplace,
|
2014-08-31 23:22:56 +02:00
|
|
|
navigateToMainPage: navigateToMainPage,
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-09-11 13:59:12 +02:00
|
|
|
App.DI.registerSingleton('router', ['pathJs', '_', 'jQuery', 'util', 'appState', 'presenterManager'], App.Router);
|