2014-11-17 14:45:23 +09:00
|
|
|
//--------------------------------------------------------------------------------------------------//
|
|
|
|
//--------------------------------------------- Filters --------------------------------------------//
|
|
|
|
//--------------------------------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
Router._filters = {
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
isReady: function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
if (!this.ready()) {
|
|
|
|
// console.log('not ready')
|
|
|
|
this.render(getTemplate('loading'));
|
|
|
|
}else{
|
|
|
|
this.next();
|
|
|
|
// console.log('ready')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-12-06 17:34:08 +09:00
|
|
|
clearSeenMessages: function () {
|
|
|
|
clearSeenMessages();
|
2014-11-17 14:45:23 +09:00
|
|
|
this.next();
|
|
|
|
},
|
|
|
|
|
|
|
|
resetScroll: function () {
|
|
|
|
var scrollTo = window.currentScroll || 0;
|
|
|
|
var $body = $('body');
|
|
|
|
$body.scrollTop(scrollTo);
|
|
|
|
$body.css("min-height", 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2015-02-05 09:32:43 +09:00
|
|
|
isLoggedIn: function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
if (!(Meteor.loggingIn() || Meteor.user())) {
|
2014-11-19 00:00:09 +08:00
|
|
|
throwError(i18n.t('please_sign_in_first'));
|
2014-11-17 14:45:23 +09:00
|
|
|
var current = getCurrentRoute();
|
|
|
|
if (current){
|
|
|
|
Session.set('fromWhere', current);
|
|
|
|
}
|
|
|
|
this.render('entrySignIn');
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
*/
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
isLoggedOut: function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
if(Meteor.user()){
|
|
|
|
this.render('already_logged_in');
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
isAdmin: function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
if(!this.ready()) return;
|
|
|
|
if(!isAdmin()){
|
|
|
|
this.render(getTemplate('no_rights'));
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
canView: function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
if(!this.ready() || Meteor.loggingIn()){
|
|
|
|
this.render(getTemplate('loading'));
|
2015-01-07 08:22:46 +01:00
|
|
|
} else if (!can.view()) {
|
2014-11-17 14:45:23 +09:00
|
|
|
this.render(getTemplate('no_rights'));
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
canViewPendingPosts: function () {
|
|
|
|
var post = this.data();
|
2015-02-11 18:03:08 +09:00
|
|
|
if (!!post && post.status == STATUS_PENDING && !can.viewPendingPosts()) {
|
2015-02-05 09:32:43 +09:00
|
|
|
this.render(getTemplate('no_rights'));
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-22 09:05:47 +01:00
|
|
|
canViewRejectedPosts: function () {
|
|
|
|
var post = this.data();
|
|
|
|
if (!!post && post.status == STATUS_REJECTED && !can.viewRejectedPosts()) {
|
|
|
|
this.render(getTemplate('no_rights'));
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-11-17 14:45:23 +09:00
|
|
|
canPost: function () {
|
|
|
|
if(!this.ready() || Meteor.loggingIn()){
|
|
|
|
this.render(getTemplate('loading'));
|
2015-01-07 08:22:46 +01:00
|
|
|
} else if(!can.post()) {
|
2014-12-06 17:34:08 +09:00
|
|
|
flashMessage(i18n.t("sorry_you_dont_have_permissions_to_add_new_items"), "error");
|
2014-11-17 14:45:23 +09:00
|
|
|
this.render(getTemplate('no_rights'));
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
canEditPost: function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
if(!this.ready()) return;
|
|
|
|
// Already subscribed to this post by route({waitOn: ...})
|
|
|
|
var post = Posts.findOne(this.params._id);
|
2015-01-07 08:22:46 +01:00
|
|
|
if(!can.currentUserEdit(post)){
|
2014-12-06 17:34:08 +09:00
|
|
|
flashMessage(i18n.t("sorry_you_cannot_edit_this_post"), "error");
|
2014-11-17 14:45:23 +09:00
|
|
|
this.render(getTemplate('no_rights'));
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
canEditComment: function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
if(!this.ready()) return;
|
|
|
|
// Already subscribed to this comment by CommentPageController
|
|
|
|
var comment = Comments.findOne(this.params._id);
|
2015-01-07 08:22:46 +01:00
|
|
|
if(!can.currentUserEdit(comment)){
|
2014-12-06 17:34:08 +09:00
|
|
|
flashMessage(i18n.t("sorry_you_cannot_edit_this_comment"), "error");
|
2014-11-17 14:45:23 +09:00
|
|
|
this.render(getTemplate('no_rights'));
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
hasCompletedProfile: function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
if(!this.ready()) return;
|
|
|
|
var user = Meteor.user();
|
|
|
|
if (user && ! userProfileComplete(user)){
|
|
|
|
this.render(getTemplate('user_email'));
|
|
|
|
} else {
|
|
|
|
this.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-05 09:32:43 +09:00
|
|
|
setTitle: function () {
|
2014-12-11 12:38:53 +09:00
|
|
|
// if getTitle is set, use it. Otherwise default to site title.
|
2015-01-05 16:32:26 +09:00
|
|
|
var title = (typeof this.getTitle === 'function') ? this.getTitle() : getSetting("title", "Telescope");
|
2014-12-11 12:38:53 +09:00
|
|
|
document.title = title;
|
2014-11-17 14:45:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
filters = Router._filters;
|
|
|
|
coreSubscriptions = new SubsManager({
|
|
|
|
// cache recent 50 subscriptions
|
|
|
|
cacheLimit: 50,
|
|
|
|
// expire any subscription after 30 minutes
|
|
|
|
expireIn: 30
|
|
|
|
});
|
|
|
|
|
|
|
|
Meteor.startup( function (){
|
|
|
|
|
|
|
|
if(Meteor.isClient){
|
|
|
|
|
|
|
|
// Load Hooks
|
|
|
|
|
2015-01-09 15:23:30 +09:00
|
|
|
Router.onBeforeAction( function () {
|
2014-11-17 14:45:23 +09:00
|
|
|
Session.set('categorySlug', null);
|
|
|
|
|
|
|
|
// if we're not on the search page itself, clear search query and field
|
2014-12-03 00:06:00 -08:00
|
|
|
if(Router.current().route.getName() !== 'search'){
|
2014-11-17 14:45:23 +09:00
|
|
|
Session.set('searchQuery', '');
|
|
|
|
$('.search-field').val('').blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-01-21 11:09:18 +09:00
|
|
|
// onRun Hooks
|
|
|
|
|
2015-02-03 15:43:57 +09:00
|
|
|
// note: this has to run in an onRun hook, because onBeforeAction hooks can get called multiple times
|
|
|
|
// per route, which would erase the message before the user has actually seen it
|
|
|
|
// TODO: find a way to make this work even with HCRs.
|
2015-01-21 11:09:18 +09:00
|
|
|
Router.onRun(filters.clearSeenMessages);
|
|
|
|
|
2014-11-17 14:45:23 +09:00
|
|
|
// Before Hooks
|
|
|
|
|
|
|
|
Router.onBeforeAction(filters.isReady);
|
|
|
|
Router.onBeforeAction(filters.canView, {except: ['atSignIn', 'atSignUp', 'atForgotPwd', 'atResetPwd', 'signOut']});
|
2015-02-05 09:32:43 +09:00
|
|
|
Router.onBeforeAction(filters.canViewPendingPosts, {only: ['post_page']});
|
2015-02-22 09:05:47 +01:00
|
|
|
Router.onBeforeAction(filters.canViewRejectedPosts, {only: ['post_page']});
|
2014-11-17 14:45:23 +09:00
|
|
|
Router.onBeforeAction(filters.hasCompletedProfile);
|
|
|
|
Router.onBeforeAction(filters.isLoggedOut, {only: []});
|
|
|
|
Router.onBeforeAction(filters.canPost, {only: ['posts_pending', 'post_submit']});
|
|
|
|
Router.onBeforeAction(filters.canEditPost, {only: ['post_edit']});
|
|
|
|
Router.onBeforeAction(filters.canEditComment, {only: ['comment_edit']});
|
|
|
|
Router.onBeforeAction(filters.isAdmin, {only: ['posts_pending', 'all-users', 'settings', 'toolbox', 'logs']});
|
|
|
|
|
2015-03-10 12:05:51 +09:00
|
|
|
Router.plugin('ensureSignedIn', {only: ['post_submit', 'post_edit', 'comment_edit']});
|
|
|
|
|
2014-11-17 14:45:23 +09:00
|
|
|
// After Hooks
|
|
|
|
|
|
|
|
// Router.onAfterAction(filters.resetScroll, {except:['posts_top', 'posts_new', 'posts_best', 'posts_pending', 'posts_category', 'all-users']});
|
|
|
|
Router.onAfterAction(analyticsInit); // will only run once thanks to _.once()
|
|
|
|
Router.onAfterAction(analyticsRequest); // log this request with mixpanel, etc
|
|
|
|
Router.onAfterAction(filters.setTitle);
|
|
|
|
|
|
|
|
// Unload Hooks
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|