Vulcan/client/lib/router.js

211 lines
5.9 KiB
JavaScript
Raw Normal View History

SimpleRouter = FilteredRouter.extend({
2012-10-10 11:25:00 +09:00
initialize: function() {
FilteredRouter.prototype.initialize.call(this);
this.filter(this.require_login, {only: ['submit']});
this.filter(this.start_request);
this.filter(this.require_profile);
2012-11-02 13:34:32 +09:00
this.filter(this.requirePost, {only: ['post_page', 'post_edit']});
2012-10-10 11:25:00 +09:00
},
2012-10-10 11:25:00 +09:00
start_request: function(page){
// runs at every new page change
2012-10-01 14:52:32 +09:00
// openedComments is an Array that tracks which comments
// have been expanded by the user, to make sure they stay expanded
2012-10-10 11:25:00 +09:00
Session.set("openedComments", null);
// currentScroll stores the position of the user in the page
Session.set('currentScroll', null);
2012-10-10 11:25:00 +09:00
document.title = getSetting("title");
2012-10-01 14:52:32 +09:00
// set all errors who have already been seen to not show anymore
2012-10-10 11:25:00 +09:00
clearSeenErrors();
2012-10-18 11:53:59 +11:00
// log this request with mixpanel, etc
instrumentRequest();
2012-10-10 11:25:00 +09:00
return page;
},
2012-10-10 11:25:00 +09:00
require_login: function(page) {
if (Meteor.user()) {
return page;
} else {
return 'signin';
}
},
2012-10-10 11:25:00 +09:00
// if the user is logged in but their profile isn't filled out enough
require_profile: function(page) {
var user = Meteor.user();
2012-10-18 14:53:37 +09:00
if (user && Meteor.userLoaded() && ! userProfileComplete(user)){
2012-10-11 08:23:52 +09:00
Session.set('selectedUserId', user._id);
return 'user_email';
} else {
return page;
}
2012-10-10 11:25:00 +09:00
},
// if we are on a page that requires a post, as set in selectedPostId
requirePost: function(page) {
if (Posts.findOne(Session.get('selectedPostId'))) {
2012-11-02 13:34:32 +09:00
return page;
} else if (! Session.get('postReady')) {
return 'loading';
} else {
return 'not_found';
}
},
2012-11-02 13:34:32 +09:00
requireSettings: function() {
// TO DO
},
// wait for the subscription to be ready, this one is only used manually
awaitSubscription: function(page, subName) {
return Session.get(subName) ? page : 'loading';
},
2012-10-10 11:25:00 +09:00
routes: {
'': 'top',
'top':'top',
2012-10-10 13:54:48 +09:00
'top/':'top',
2012-10-10 11:25:00 +09:00
'top/:page':'top',
'new':'new',
2012-10-10 13:54:48 +09:00
'new/':'new',
2012-10-10 11:25:00 +09:00
'new/:page':'new',
2012-10-24 11:04:42 +09:00
'pending':'pending',
2012-10-10 13:54:48 +09:00
'digest/:year/:month/:day':'digest',
2012-10-10 11:25:00 +09:00
'digest':'digest',
2012-10-10 13:54:48 +09:00
'digest/':'digest',
2012-10-10 11:25:00 +09:00
'test':'test',
'signin':'signin',
'signup':'signup',
'submit':'submit',
'invite':'invite',
'posts/deleted':'post_deleted',
'posts/:id/edit':'post_edit',
'posts/:id/comment/:comment_id':'post',
'posts/:id':'post',
'comments/deleted':'comment_deleted',
'comments/:id':'comment',
'comments/:id/reply':'comment_reply',
'comments/:id/edit':'comment_edit',
'settings':'settings',
'admin':'admin',
'categories':'categories',
'users':'users',
'account':'user_edit',
'forgot_password':'forgot_password',
'users/:id': 'user_profile',
2012-10-22 09:28:42 +09:00
'users/:id/edit':'user_edit',
':year/:month/:day':'digest',
2012-10-10 11:25:00 +09:00
},
top: function() {
var self = this;
// XXX: where do we go if not? Should the if be in the goto (so it's reactive?)
2012-10-10 11:25:00 +09:00
if(canView(Meteor.user(), 'replace')) {
self.goto(function() {
return self.awaitSubscription('posts_top', 'topPostsReady');
});
2012-10-10 11:25:00 +09:00
}
},
new: function(page) {
var self = this;
2012-10-10 11:25:00 +09:00
if(canView(Meteor.user(), 'replace')) {
self.goto(function() {
return self.awaitSubscription('posts_new', 'newPostsReady');
});
2012-10-10 11:25:00 +09:00
}
},
2012-10-24 11:04:42 +09:00
pending: function(page) {
var self = this;
if(canPost(Meteor.user(), 'replace')) {
self.goto(function() {
return self.awaitSubscription('posts_pending', 'pendingPostsReady');
});
}
},
2012-10-10 13:54:48 +09:00
digest: function(year, month, day){
var self = this;
2012-10-10 11:25:00 +09:00
if(canView(Meteor.user(), 'replace')) {
var date = (typeof day === 'undefined') ? new Date() : new Date(year, month-1, day);
2012-10-10 13:54:48 +09:00
sessionSetObject('currentDate', date);
self.goto(function() {
return self.awaitSubscription('posts_digest', 'digestPostsReady');
});
}
2012-10-10 11:25:00 +09:00
},
post: function(id, commentId) {
var self = this;
2012-10-10 11:25:00 +09:00
Session.set('selectedPostId', id);
if(typeof commentId !== 'undefined')
Session.set('scrollToCommentId', commentId);
2012-10-10 11:25:00 +09:00
// on post page, we show the comment recursion
window.repress_recursion=false;
// reset the new comment time at each new request of the post page
window.newCommentTimestamp=new Date();
self.goto(function() {
return self.awaitSubscription('post_page', 'postReady');
});
2012-10-10 11:25:00 +09:00
},
post_edit: function(id) {
var self = this;
2012-10-10 11:25:00 +09:00
Session.set('selectedPostId', id);
self.goto(function() {
return self.awaitSubscription('post_edit', 'postReady');
});
2012-10-10 11:25:00 +09:00
},
comment: function(id) {
Session.set('selectedCommentId', id);
window.repress_recursion=true;
window.newCommentTimestamp=new Date();
2012-10-11 08:23:52 +09:00
this.goto('comment_page');
2012-10-10 11:25:00 +09:00
},
comment_reply: function(id) {
Session.set('selectedCommentId', id);
window.repress_recursion=true;
window.newCommentTimestamp=new Date();
2012-10-11 08:23:52 +09:00
this.goto('comment_reply');
2012-10-10 11:25:00 +09:00
},
comment_edit: function(id) {
Session.set('selectedCommentId', id);
window.newCommentTimestamp=new Date();
2012-10-11 08:23:52 +09:00
this.goto('comment_edit');
2012-10-10 11:25:00 +09:00
},
user_profile: function(id){
if(typeof id !== undefined){
2012-10-11 08:23:52 +09:00
Session.set('selectedUserId', id);
2012-10-10 11:25:00 +09:00
}
this.goto('user_profile');
},
user_edit: function(id){
if(typeof id !== undefined){
2012-10-11 08:23:52 +09:00
Session.set('selectedUserId', id);
2012-10-10 11:25:00 +09:00
}
this.goto('user_edit');
},
signup: function() { this.goto('signup'); },
signin: function() { this.goto('signin'); },
invite: function() { this.goto('no_invite'); },
submit: function() { this.goto('post_submit'); },
settings: function() { this.goto('settings'); },
users: function() { this.goto('users'); },
post_deleted: function() { this.goto('post_deleted'); },
comment_deleted: function() { this.goto('comment_deleted'); },
forgot_password: function() { this.goto('user_password'); },
admin: function() { this.goto('admin'); },
categories: function() { this.goto('categories'); }
});
2012-10-21 12:07:24 +09:00
var Router = new SimpleRouter();