Vulcan/client/helpers/router.js

614 lines
15 KiB
JavaScript
Raw Normal View History

2013-10-09 20:59:42 +09:00
/*
2013-10-10 11:41:11 +09:00
//--------------------------------------------------------------------------------------------------//
//---------------------------------------- Table Of Contents ---------------------------------------//
//--------------------------------------------------------------------------------------------------//
---------------------------------------------------------------
# Config #
---------------------------------------------------------------
//
2013-10-09 20:59:42 +09:00
---------------------------------------------------------------
2013-10-10 11:41:11 +09:00
# Filters #
2013-10-09 20:59:42 +09:00
---------------------------------------------------------------
2013-10-10 11:41:11 +09:00
isLoggedIn
isLoggedOut
isAdmin
canView
canPost
canEditPost
canEditComment
hasCompletedProfile
---------------------------------------------------------------
2013-10-25 11:54:19 +09:00
# Controllers #
---------------------------------------------------------------
2013-10-10 11:41:11 +09:00
2013-10-25 11:54:19 +09:00
PostsListController
PostPageController
2013-10-10 11:41:11 +09:00
---------------------------------------------------------------
# Routes #
---------------------------------------------------------------
2013-10-09 20:59:42 +09:00
1) Paginated Lists
----------------------
Top
New
Best
Pending
Categories
2) Digest
--------------------
Digest
3) Posts
--------------------
Post Page
Post Page (scroll to comment)
Post Edit
Post Submit
4) Comments
--------------------
Comment Page
Comment Edit
Comment Submit
5) Users
--------------------
User Profie
User Edit
Forgot Password
Account
All Users
Unsubscribe (from notifications)
Sign Up
Sign In
6) Misc Routes
--------------------
Settings
2013-10-09 22:21:28 +09:00
Categories
2013-10-09 20:59:42 +09:00
Toolbox
2013-10-09 21:34:55 +09:00
2013-10-09 20:59:42 +09:00
*/
2013-10-10 11:41:11 +09:00
//--------------------------------------------------------------------------------------------------//
//--------------------------------------------- Config ---------------------------------------------//
//--------------------------------------------------------------------------------------------------//
2013-10-06 08:37:17 +09:00
Router.configure({
2013-10-12 11:44:29 +09:00
layoutTemplate: 'layout',
2013-10-06 09:33:00 +09:00
loadingTemplate: 'loading',
notFoundTemplate: 'not_found',
2013-10-06 08:37:17 +09:00
});
2013-10-10 11:41:11 +09:00
//--------------------------------------------------------------------------------------------------//
//--------------------------------------------- Filters --------------------------------------------//
//--------------------------------------------------------------------------------------------------//
var filters = {
2013-10-13 10:27:49 +09:00
nProgressHook: function () {
// console.log('// nProgress Hook')
if (this.ready()) {
NProgress.done();
} else {
NProgress.start();
this.stop();
}
},
2013-10-24 20:30:05 +09:00
resetScroll: function () {
var scrollTo = window.currentScroll || 0;
$('body').scrollTop(scrollTo);
$('body').css("min-height", 0);
},
2013-10-13 10:27:49 +09:00
isLoggedIn: function() {
if (!(Meteor.loggingIn() || Meteor.user())) {
throwError('Please Sign In First.')
this.render('signin');
2013-10-13 10:27:49 +09:00
this.stop();
}
},
isLoggedOut: function() {
if(Meteor.user()){
this.render('already_logged_in');
this.stop();
}
},
isAdmin: function() {
2013-10-25 10:04:34 +09:00
if(!Meteor.loggingIn() && Session.get('settingsLoaded') && !isAdmin()){
throwError("Sorry, you have to be an admin to view this page.")
2013-10-13 10:27:49 +09:00
this.render('no_rights');
2013-10-25 10:04:34 +09:00
this.stop();
2013-10-13 10:27:49 +09:00
}
},
canView: function() {
2013-10-24 21:07:55 +09:00
if(Session.get('settingsLoaded') && !canView()){
2013-10-13 10:27:49 +09:00
this.render('no_rights');
this.stop();
}
},
2013-10-12 11:44:29 +09:00
canPost: function () {
if(!canPost()){
throwError("Sorry, you don't have permissions to add new items.")
this.render('no_rights');
this.stop();
}
},
2013-10-12 11:44:29 +09:00
canEditPost: function() {
var post = Posts.findOne(this.params._id);
if(!currentUserCanEdit(post)){
throwError("Sorry, you cannot edit this post.")
2013-10-12 11:44:29 +09:00
this.render('no_rights');
this.stop();
2013-10-10 11:41:11 +09:00
}
},
2013-10-12 11:44:29 +09:00
canEditComment: function() {
var comment = Comments.findOne(this.params._id);
if(!currentUserCanEdit(comment)){
throwError("Sorry, you cannot edit this comment.")
2013-10-12 11:44:29 +09:00
this.render('no_rights');
this.stop();
2013-10-10 11:41:11 +09:00
}
2013-10-13 10:27:49 +09:00
},
hasCompletedProfile: function() {
var user = Meteor.user();
if (user && ! Meteor.loggingIn() && ! userProfileComplete(user)){
// Session.set('selectedUserId', user._id);
this.render('user_email');
this.stop();
}
2013-10-12 11:44:29 +09:00
}
2013-10-10 11:41:11 +09:00
}
2013-10-24 14:36:20 +09:00
// Load Hooks
Router.load( function () {
analyticsRequest(); // log this request with mixpanel, etc
2013-10-24 21:18:55 +09:00
clearSeenErrors(); // set all errors who have already been seen to not show anymore
Session.set('categorySlug', null);
});
2013-10-24 14:36:20 +09:00
// Before Hooks
2013-10-15 12:35:05 +09:00
Router.before(filters.hasCompletedProfile);
Router.before(filters.isLoggedIn, {only: ['comment_reply','post_submit']});
Router.before(filters.canView);
Router.before(filters.isLoggedOut, {only: ['signin', 'signup']});
Router.before(filters.canPost, {only: ['posts_pending', 'comment_reply', 'post_submit']});
Router.before(filters.canEditPost, {only: ['post_edit']});
Router.before(filters.canEditComment, {only: ['comment_edit']});
2013-10-25 09:54:40 +09:00
Router.before(filters.isAdmin, {only: ['posts_pending', 'all-users', 'settings', 'categories', 'toolbox']});
2013-10-24 14:36:20 +09:00
// After Hooks
2013-10-25 09:54:40 +09:00
Router.after(filters.resetScroll, {except:['posts_top', 'posts_new', 'posts_best', 'posts_pending', 'category', 'all-users']});
2013-10-15 12:35:05 +09:00
2013-10-24 14:36:20 +09:00
// Unload Hooks
//
2013-10-15 12:35:05 +09:00
2013-10-25 11:54:19 +09:00
//--------------------------------------------------------------------------------------------------//
//------------------------------------------- Controllers ------------------------------------------//
//--------------------------------------------------------------------------------------------------//
// Common controller for all posts lists
PostsListController = RouteController.extend({
template:'posts_list',
// waitOn: postListSubscription(selectPosts, sortPosts('baseScore'), 13),
waitOn: function () {
var view = this.path == '/' ? 'top' : this.path.split('/')[1];
var limit = this.params.limit || getSetting('postsPerPage', 10);
var parameters = getParameters(view, limit);
return [
Meteor.subscribe('postsList', parameters.find, parameters.options),
Meteor.subscribe('postsListUsers', parameters.find, parameters.options)
]
},
data: function () {
var view = this.path == '/' ? 'top' : this.path.split('/')[1];
var limit = this.params.limit || getSetting('postsPerPage', 10);
var parameters = getParameters(view, limit);
Session.set('postsLimit', limit);
return {
posts: Posts.find(parameters.find, parameters.options)
}
},
before: filters.nProgressHook,
after: function() {
Session.set('view', 'top');
}
});
// Common controller for post pages
PostPageController = RouteController.extend({
template: 'post_page',
waitOn: function () {
return [
Meteor.subscribe('singlePost', this.params._id),
Meteor.subscribe('comments', this.params._id),
Meteor.subscribe('postUsers', this.params._id)
];
},
before: filters.nProgressHook,
data: function () {
return {postId: this.params._id};
},
after: function () {
window.queueComments = false;
window.openedComments = [];
// TODO: scroll to comment position
}
});
//--------------------------------------------------------------------------------------------------//
//--------------------------------------------- Routes ---------------------------------------------//
//--------------------------------------------------------------------------------------------------//
2013-10-09 21:34:55 +09:00
2013-10-25 11:54:19 +09:00
// getParameters gives an object containing the appropriate find and options arguments for the subscriptions's Posts.find()
2013-10-24 20:51:45 +09:00
getParameters = function (view, limit, category) {
2013-10-24 22:22:49 +09:00
var baseParameters = {
find: {
status: 2
2013-10-24 21:02:42 +09:00
},
2013-10-24 22:22:49 +09:00
options: {
limit: 10
2013-10-24 20:30:05 +09:00
}
}
2013-10-24 21:02:42 +09:00
2013-10-24 22:22:49 +09:00
switch (view) {
case 'top':
var parameters = $.extend(true, baseParameters, {options: {sort: {sticky: -1, score: -1}}});
break;
case 'new':
var parameters = $.extend(true, baseParameters, {options: {sort: {sticky: -1, submitted: -1}}});
break;
case 'best':
var parameters = $.extend(true, baseParameters, {options: {sort: {sticky: -1, baseScore: -1}}});
break;
case 'pending':
var parameters = $.extend(true, baseParameters, {find: {status: 1}, options: {sort: {baseScore: -1}}});
break;
case 'category': // same as top for now
var parameters = $.extend(true, baseParameters, {options: {sort: {sticky: -1, score: -1}}});
break;
}
// sort by _id to break ties
$.extend(true, parameters, {options: {sort: {_id: -1}}})
2013-10-24 21:02:42 +09:00
if(typeof limit != 'undefined')
_.extend(parameters.options, {limit: parseInt(limit)});
if(typeof category != 'undefined')
_.extend(parameters.find, {'categories.slug': category});
2013-10-24 22:22:49 +09:00
// console.log(parameters.options.sort)
2013-10-24 21:02:42 +09:00
return parameters;
2013-10-24 20:30:05 +09:00
}
2013-10-06 08:37:17 +09:00
Router.map(function() {
2013-10-06 09:33:00 +09:00
2013-10-09 20:59:42 +09:00
// Top
2013-10-24 20:46:14 +09:00
this.route('posts_top', {
path: '/',
controller: PostsListController
});
this.route('posts_top', {
2013-10-24 20:46:14 +09:00
path: '/top/:limit?',
controller: PostsListController
});
2013-10-09 22:21:28 +09:00
2013-10-09 20:59:42 +09:00
// New
this.route('posts_new', {
2013-10-24 20:46:14 +09:00
path: '/new/:limit?',
controller: PostsListController
});
2013-10-09 22:21:28 +09:00
2013-10-09 20:59:42 +09:00
// Best
this.route('posts_best', {
2013-10-24 20:30:05 +09:00
path: '/best/:limit?',
controller: PostsListController
});
2013-10-09 22:21:28 +09:00
2013-10-09 20:59:42 +09:00
// Pending
this.route('posts_pending', {
2013-10-24 20:51:45 +09:00
path: '/pending/:limit?',
controller: PostsListController
});
2013-10-09 22:21:28 +09:00
2013-10-09 20:59:42 +09:00
// Categories
2013-10-25 11:54:19 +09:00
// category route uses slightly different hooks, so don't inherit from PostsListController for now
// TODO: use PostsListController here as well
this.route('category', {
2013-10-24 21:02:42 +09:00
path: '/category/:slug/:limit?',
// controller: PostsListController,
2013-10-24 21:09:52 +09:00
template:'posts_list',
2013-10-24 20:51:45 +09:00
waitOn: function () {
var limit = this.params.limit || getSetting('postsPerPage', 10);
2013-10-24 21:02:42 +09:00
var parameters = getParameters('category', limit, this.params.slug);
return [
Meteor.subscribe('postsList', parameters.find, parameters.options),
Meteor.subscribe('postsListUsers', parameters.find, parameters.options)
]
2013-10-24 20:51:45 +09:00
},
data: function () {
var limit = this.params.limit || getSetting('postsPerPage', 10);
2013-10-24 21:02:42 +09:00
var parameters = getParameters('category', limit, this.params.slug);
2013-10-24 20:51:45 +09:00
Session.set('postsLimit', limit);
return {
posts: Posts.find(parameters.find, parameters.options)
}
},
before: filters.nProgressHook,
2013-10-15 12:15:32 +09:00
after: function() {
Session.set('view', 'category');
Session.set('categorySlug', this.params.slug);
}
});
2013-10-12 13:01:16 +09:00
2013-10-15 12:35:05 +09:00
// TODO: enable /category/new, /category/best, etc. views
2013-10-09 11:32:36 +09:00
// Digest
2013-10-09 11:21:23 +09:00
this.route('posts_digest', {
2013-10-06 09:33:00 +09:00
path: '/digest/:year/:month/:day',
2013-10-12 12:24:41 +09:00
waitOn: function() {
2013-10-09 11:32:36 +09:00
currentDate = new Date(this.params.year, this.params.month-1, this.params.day);
Session.set('currentDate', currentDate);
return Meteor.subscribe('postDigest', currentDate);
2013-10-06 09:33:00 +09:00
},
before: filters.nProgressHook,
2013-10-06 09:33:00 +09:00
data: function() {
2013-10-09 11:21:23 +09:00
return {
2013-10-09 11:32:36 +09:00
posts: findDigestPosts(moment(currentDate))
2013-10-09 11:21:23 +09:00
}
2013-10-06 09:33:00 +09:00
}
});
2013-10-06 08:37:17 +09:00
this.route('posts_digest_shortcut', {
path: '/digest',
template: 'posts_digest',
waitOn: function() {
2013-10-12 12:24:41 +09:00
// note: this runs twice for some reason? is 'today' changing?
currentDate = Session.get('today');
Session.set('currentDate', currentDate);
return Meteor.subscribe('postDigest', currentDate);
},
before: filters.nProgressHook,
data: function() {
return {
posts: findDigestPosts(moment(currentDate))
}
}
});
2013-10-12 11:44:29 +09:00
// -------------------------------------------- Post -------------------------------------------- //
2013-10-09 11:32:36 +09:00
// Post Page
this.route('post_page', {
path: '/posts/:_id',
2013-10-25 11:54:19 +09:00
controller: PostPageController
2013-10-09 20:11:58 +09:00
});
2013-10-25 11:54:19 +09:00
this.route('post_page_with_comment', {
path: '/posts/:_id/comment/:commentId',
2013-10-25 11:54:19 +09:00
controller: PostPageController,
after: function () {
// TODO: scroll to comment position
}
});
2013-10-09 12:39:05 +09:00
2013-10-09 11:32:36 +09:00
// Post Edit
2013-10-06 09:17:37 +09:00
this.route('post_edit', {
path: '/posts/:_id/edit',
waitOn: function () {
2013-10-06 09:17:37 +09:00
return Meteor.subscribe('singlePost', this.params._id);
},
before: filters.nProgressHook,
2013-10-06 09:17:37 +09:00
data: function() {
return {postId: this.params._id};
}
// after: filters.canEditPost
2013-10-06 09:17:37 +09:00
});
2013-10-09 11:32:36 +09:00
2013-10-09 12:39:05 +09:00
// Post Submit
this.route('post_submit', {path: '/submit'});
2013-10-12 11:44:29 +09:00
// -------------------------------------------- Comment -------------------------------------------- //
2013-10-09 11:32:36 +09:00
// Comment Page
2013-10-09 11:46:44 +09:00
this.route('comment_page', {
2013-10-09 12:39:05 +09:00
path: '/comments/:_id',
waitOn: function() {
return Meteor.subscribe('singleComment', this.params._id);
},
2013-10-09 12:39:05 +09:00
data: function() {
return {
comment: Comments.findOne(this.params._id)
}
}
});
// Comment Reply
this.route('comment_reply', {
path: '/comments/:_id/reply',
2013-10-09 20:11:58 +09:00
waitOn: function() {
return Meteor.subscribe('singleComment', this.params._id);
2013-10-09 20:11:58 +09:00
},
before: filters.nProgressHook,
2013-10-09 11:46:44 +09:00
data: function() {
return {
comment: Comments.findOne(this.params._id)
}
2013-10-23 10:40:29 +08:00
},
after: function() {
window.queueComments = false;
2013-10-09 11:46:44 +09:00
}
});
2013-10-09 11:32:36 +09:00
// Comment Edit
2013-10-09 12:39:05 +09:00
this.route('comment_edit', {
path: '/comments/:_id/edit',
data: function() {
return {
comment: Comments.findOne(this.params._id)
}
2013-10-10 11:41:11 +09:00
},
2013-10-12 11:44:29 +09:00
after: filters.canEditComment
2013-10-09 12:39:05 +09:00
});
2013-10-14 11:37:37 +09:00
// -------------------------------------------- Users -------------------------------------------- //
2013-10-09 20:50:26 +09:00
// User Profile
this.route('user_profile', {
2013-10-24 11:04:27 +09:00
path: '/users/:_idOrSlug',
2013-10-09 22:16:47 +09:00
waitOn: function() {
2013-10-24 11:04:27 +09:00
return Meteor.subscribe('singleUser', this.params._idOrSlug);
2013-10-09 22:16:47 +09:00
},
before: filters.nProgressHook,
2013-10-09 20:50:26 +09:00
data: function() {
2013-10-24 11:04:27 +09:00
var findById = Meteor.users.findOne(this.params._idOrSlug);
var findBySlug = Meteor.users.findOne({slug: this.params._idOrSlug});
2013-10-09 20:50:26 +09:00
return {
2013-10-24 11:04:27 +09:00
user: (typeof findById == "undefined") ? findBySlug : findById
2013-10-09 20:50:26 +09:00
}
}
});
// User Edit
this.route('user_edit', {
path: '/users/:_id/edit',
2013-10-09 22:16:47 +09:00
waitOn: function() {
return Meteor.subscribe('singleUser', this.params._id);
},
before: filters.nProgressHook,
2013-10-09 20:50:26 +09:00
data: function() {
return {
user: Meteor.users.findOne(this.params._id)
}
}
});
// Forgot Password
this.route('forgot_password');
// Account
this.route('account', {
path: '/account',
template: 'user_edit',
data: function() {
return {
user: Meteor.user()
}
}
});
// All Users
2013-10-25 09:54:40 +09:00
this.route('all-users', {
path: '/all-users/:limit?',
template: 'users',
2013-10-09 22:16:47 +09:00
waitOn: function() {
2013-10-25 09:54:40 +09:00
var limit = parseInt(this.params.limit) || 20;
return Meteor.subscribe('allUsers', limit);
2013-10-09 22:16:47 +09:00
},
before: filters.nProgressHook,
2013-10-09 22:16:47 +09:00
data: function() {
2013-10-25 09:54:40 +09:00
var limit = parseInt(this.params.limit) || 20;
Session.set('usersLimit', limit);
2013-10-09 22:16:47 +09:00
return {
2013-10-25 09:54:40 +09:00
users: Meteor.users.find({}, {sort: {createdAt: -1}, limit: limit})
2013-10-09 22:16:47 +09:00
}
}
});
2013-10-09 20:50:26 +09:00
2013-10-09 20:26:58 +09:00
// Unsubscribe (from notifications)
this.route('unsubscribe', {
path: '/unsubscribe/:hash',
data: function() {
return {
hash: this.params.hash
}
}
});
2013-10-09 20:50:26 +09:00
2013-10-09 20:53:55 +09:00
// User Sign-Up
this.route('signup');
2013-10-09 20:59:42 +09:00
2013-10-09 20:53:55 +09:00
// User Sign-In
this.route('signin');
2013-10-14 11:37:37 +09:00
// -------------------------------------------- Other -------------------------------------------- //
2013-10-09 22:21:28 +09:00
// Categories
this.route('categories');
2013-10-09 20:53:55 +09:00
// Settings
this.route('settings');
// Toolbox
this.route('toolbox');
2013-10-06 08:37:17 +09:00
});