Vulcan/client/lib/router.js

236 lines
6.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);
},
start_request: function(page){
// runs at every new page change
2012-10-01 14:52:32 +09:00
2012-10-10 11:25:00 +09:00
Session.set("openedComments", null);
document.title = getSetting("title");
2012-10-01 14:52:32 +09:00
2012-10-10 11:25:00 +09:00
// set all errors who have been seen to not show anymore
clearSeenErrors();
2012-10-08 10:44:13 +09:00
2012-10-10 11:25:00 +09:00
// Mixpanel
2012-10-01 14:52:32 +09:00
2012-10-10 11:25:00 +09:00
if((mixpanelId=getSetting("mixpanelId")) && window.mixpanel.length==0){
mixpanel.init(mixpanelId);
if(Meteor.user()){
var currentUserEmail=getCurrentUserEmail();
mixpanel.people.identify(currentUserEmail);
mixpanel.people.set({
'username': getDisplayName(Meteor.user()),
'$last_login': new Date(),
'$created': moment(Meteor.user().createdAt)._d,
'$email': currentUserEmail
});
mixpanel.register({
'username': getDisplayName(Meteor.user()),
'createdAt': moment(Meteor.user().createdAt)._d,
'email': currentUserEmail
});
mixpanel.name_tag(currentUserEmail);
}
}
2012-10-01 14:52:32 +09:00
2012-10-10 11:25:00 +09:00
// GoSquared
2012-10-01 14:52:32 +09:00
2012-10-10 11:25:00 +09:00
if((goSquaredId=getSetting("goSquaredId"))){
GoSquared.acct = goSquaredId;
GoSquaredInit();
}
2012-10-01 14:52:32 +09:00
2012-10-10 11:25:00 +09:00
// Intercom
if((intercomId=getSetting("intercomId")) && Meteor.user()){
window.intercomSettings = {
app_id: intercomId,
email: currentUserEmail,
created_at: moment(Meteor.user().createdAt).unix(),
custom_data: {
'profile link': 'http://'+document.domain+'/users/'+Meteor.user()._id
},
widget: {
activator: '#Intercom',
use_counter: true,
activator_html: function ( obj ) {
return obj.activator_html_functions.brackets();
}
}
};
IntercomInit();
}
2012-10-05 10:34:34 +09:00
2012-10-10 11:25:00 +09:00
return page;
},
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-11 08:23:52 +09:00
if (user && !user.loading && !userProfileComplete(user)){
Session.set('selectedUserId', user._id);
return 'user_email';
} else {
return page;
}
2012-10-10 11:25:00 +09:00
},
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-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',
'users/:id/edit':'user_edit'
},
top: function(page) {
if(canView(Meteor.user(), 'replace')) {
2012-10-11 08:23:52 +09:00
var pageNumber = (typeof page === 'undefined') ? 1 : page;
var postsPerPage=10;
var postsView={
find: {},
sort: {score: -1},
skip: (pageNumber-1)*postsPerPage,
limit: postsPerPage,
postsPerPage: postsPerPage,
page: pageNumber
}
sessionSetObject('postsView', postsView);
2012-10-10 11:25:00 +09:00
this.goto('posts_top');
}
},
new: function(page) {
if(canView(Meteor.user(), 'replace')) {
2012-10-11 08:23:52 +09:00
var pageNumber = (typeof page === 'undefined') ? 1 : page;
var postsPerPage=10;
var postsView={
find: {},
sort: {submitted: -1},
postsPerPage: postsPerPage,
skip:(pageNumber-1)*postsPerPage,
limit: postsPerPage,
page: pageNumber
}
sessionSetObject('postsView', postsView);
2012-10-10 11:25:00 +09:00
this.goto('posts_new');
}
},
2012-10-10 13:54:48 +09:00
digest: function(year, month, day){
2012-10-10 11:25:00 +09:00
if(canView(Meteor.user(), 'replace')) {
2012-10-10 13:54:48 +09:00
if(typeof day === 'undefined'){
2012-10-11 08:23:52 +09:00
// if day is not defined, just use today
// and change the URL to today's date
2012-10-10 13:54:48 +09:00
var date = new Date();
var mDate = moment(date);
this.navigate(getDigestURL(mDate));
}else{
var date=new Date(year, month-1, day);
var mDate = moment(date);
}
sessionSetObject('currentDate', date);
var postsPerPage=5;
var postsView={
find: {submitted: {$gte: mDate.startOf('day').valueOf(), $lt: mDate.endOf('day').valueOf()}},
sort: {score: -1},
skip:0,
postsPerPage: postsPerPage,
limit: postsPerPage
}
sessionSetObject('postsView', postsView);
2012-10-10 11:25:00 +09:00
this.goto('posts_digest');
}
},
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'); },
post: function(id, commentId) {
window.template='post_page';
Session.set('selectedPostId', id);
if(typeof commentId !== 'undefined')
Session.set('scrollToCommentId', commentId);
this.goto('post_page');
// 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();
},
post_edit: function(id) {
Session.set('selectedPostId', id);
this.goto('post_edit');
},
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');
}
});
var Router = new SimpleRouter();
Meteor.startup(function() {
});