updated router and added nprogress hook

This commit is contained in:
Sacha Greif 2013-10-24 14:34:50 +09:00
parent 7966f0b985
commit c5821d5739
9 changed files with 116 additions and 100 deletions

View file

@ -17,4 +17,5 @@ momentjs
standard-app-packages standard-app-packages
rss rss
iron-router iron-router
mailchimp mailchimp
nprogress

View file

@ -79,45 +79,38 @@ Toolbox
*/ */
//--------------------------------------------------------------------------------------------------// //--------------------------------------------------------------------------------------------------//
//--------------------------------------------- Config ---------------------------------------------// //--------------------------------------------- Config ---------------------------------------------//
//--------------------------------------------------------------------------------------------------// //--------------------------------------------------------------------------------------------------//
Router.configure({ Router.configure({
// autoRender: false,
layoutTemplate: 'layout', layoutTemplate: 'layout',
loadingTemplate: 'loading', loadingTemplate: 'loading',
notFoundTemplate: 'not_found', notFoundTemplate: 'not_found',
after: function() {
console.log('// Routing to '+Router._currentController.template)
// currentScroll stores the position of the user in the page
Session.set('currentScroll', null);
$('body').css('min-height','0');
// set all errors who have already been seen to not show anymore
clearSeenErrors();
// log this request with mixpanel, etc
analyticsRequest();
}
}); });
//--------------------------------------------------------------------------------------------------// //--------------------------------------------------------------------------------------------------//
//--------------------------------------------- Filters --------------------------------------------// //--------------------------------------------- Filters --------------------------------------------//
//--------------------------------------------------------------------------------------------------// //--------------------------------------------------------------------------------------------------//
var filters = { var filters = {
nProgressHook: function () {
// console.log('// nProgress Hook')
if (this.ready()) {
NProgress.done();
} else {
NProgress.start();
this.stop();
}
},
isLoggedIn: function() { isLoggedIn: function() {
if (Meteor.loggingIn()) { if (!(Meteor.loggingIn() || Meteor.user())) {
this.render('loading'); throwError('Please Sign In First.')
this.stop(); this.render('signin');
} else if (!Meteor.user()) {
this.render('user_signin');
this.stop(); this.stop();
} }
}, },
@ -130,22 +123,33 @@ var filters = {
}, },
isAdmin: function() { isAdmin: function() {
if(!isAdmin(Meteor.user())){ if(!isAdmin()){
throwError("Sorry, you have to be an admin to view this page.")
this.render('no_rights'); this.render('no_rights');
this.stop(); this.stop();
} }
}, },
canView: function() { canView: function() {
if(!canView(Meteor.user())){ if(!canView()){
this.render('no_rights'); this.render('no_rights');
this.stop(); this.stop();
} }
}, },
canPost: function () {
console.log('canPost')
if(!canPost()){
throwError("Sorry, you don't have permissions to add new items.")
this.render('no_rights');
this.stop();
}
},
canEditPost: function() { canEditPost: function() {
var post = Posts.findOne(this.params._id); var post = Posts.findOne(this.params._id);
if(!currentUserCanEdit(post)){ if(!currentUserCanEdit(post)){
throwError("Sorry, you cannot edit this post.")
this.render('no_rights'); this.render('no_rights');
this.stop(); this.stop();
} }
@ -154,6 +158,7 @@ var filters = {
canEditComment: function() { canEditComment: function() {
var comment = Comments.findOne(this.params._id); var comment = Comments.findOne(this.params._id);
if(!currentUserCanEdit(comment)){ if(!currentUserCanEdit(comment)){
throwError("Sorry, you cannot edit this comment.")
this.render('no_rights'); this.render('no_rights');
this.stop(); this.stop();
} }
@ -170,18 +175,34 @@ var filters = {
} }
// TODO: enable filters with "only" Router.load( function () {
// console.log('// Load Hook: '+Router._currentController.template)
analyticsRequest(); // log this request with mixpanel, etc
clearSeenErrors(); // set all errors who have already been seen to not show anymore
});
// Meteor.Router.filter('requireProfile'); Router.before( function () {
// Meteor.Router.filter('requireLogin', {only: ['comment_reply','post_submit']}); // console.log('// Before Hook: '+Router._currentController.template)
// Meteor.Router.filter('canView', {only: ['posts_top', 'posts_new', 'posts_digest', 'posts_best']}); });
// Meteor.Router.filter('isLoggedOut', {only: ['user_signin', 'user_signup']});
// Meteor.Router.filter('canPost', {only: ['posts_pending', 'comment_reply', 'post_submit']});
// Meteor.Router.filter('canEdit', {only: ['post_edit', 'comment_edit']});
// Meteor.Router.filter('requirePost', {only: ['post_page', 'post_edit']});
// Meteor.Router.filter('isAdmin', {only: ['posts_pending', 'users', 'settings', 'categories', 'admin']});
// Meteor.Router.filter('setRequestTimestamp', {only: ['post_page']});
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']});
Router.before(filters.isAdmin, {only: ['posts_pending', 'users', 'settings', 'categories', 'toolbox']});
Router.after( function () {
// console.log('// After Hook: '+Router._currentController.template)
var scrollTo = window.currentScroll || 0;
$('body').scrollTop(scrollTo);
});
Router.unload( function () {
// console.log('// Unload Hook: '+Router._currentController.template)
});
//--------------------------------------------------------------------------------------------------// //--------------------------------------------------------------------------------------------------//
//--------------------------------------------- Routes ---------------------------------------------// //--------------------------------------------- Routes ---------------------------------------------//
@ -285,6 +306,7 @@ Router.map(function() {
Session.set('currentDate', currentDate); Session.set('currentDate', currentDate);
return Meteor.subscribe('postDigest', currentDate); return Meteor.subscribe('postDigest', currentDate);
}, },
before: filters.nProgressHook,
data: function() { data: function() {
return { return {
posts: findDigestPosts(moment(currentDate)) posts: findDigestPosts(moment(currentDate))
@ -301,6 +323,7 @@ Router.map(function() {
Session.set('currentDate', currentDate); Session.set('currentDate', currentDate);
return Meteor.subscribe('postDigest', currentDate); return Meteor.subscribe('postDigest', currentDate);
}, },
before: filters.nProgressHook,
data: function() { data: function() {
return { return {
posts: findDigestPosts(moment(currentDate)) posts: findDigestPosts(moment(currentDate))
@ -310,78 +333,59 @@ Router.map(function() {
// -------------------------------------------- Post -------------------------------------------- // // -------------------------------------------- Post -------------------------------------------- //
// Post Page // Post Page
this.route('post_page', { this.route('post_page', {
path: '/posts/:_id', path: '/posts/:_id',
waitOn: function() { waitOn: function () {
return [ // console.log('// Subscription Hook')
Meteor.subscribe('singlePost', this.params._id), this.subscribe('singlePost', this.params._id).wait(),
Meteor.subscribe('comments', this.params._id), this.subscribe('comments', this.params._id).wait(),
Meteor.subscribe('postUsers', this.params._id) this.subscribe('postUsers', this.params._id).wait()
];
}, },
template: 'post_page', before: filters.nProgressHook,
// before: function() { data: function () {
// var postsHandle = Meteor.subscribe('singlePost', this.params._id); return {postId: this.params._id};
// var commentsHandle = Meteor.subscribe('comments', { post : this.params._id })
// if(postsHandle.ready() && commentsHandle.ready()) {
// console.log('ready')
// } else {
// console.log('loading…')
// this.stop();
// }
// },
data: function() {
// note: do not pass actual post object in data property because we don't want the route to be reactive
// and re-run every time a post score changes
return {
postId: this.params._id
}
}, },
after: function() { after: function () {
window.queueComments = false; window.queueComments = false;
window.openedComments = []; window.openedComments = [];
// TODO: scroll to comment position
} }
}); });
// Post Page (scroll to a comment) this.route('post_page', {
// TODO: merge this route with previous one using optional parameters path: '/posts/:_id/comment/:commentId',
this.route('post_page_with_comment', { waitOn: function () {
path: '/posts/:_id/comment/:_commentId', // console.log('// Subscription Hook')
template: 'post_page', this.subscribe('singlePost', this.params._id).wait(),
waitOn: function() { this.subscribe('comments', this.params._id).wait(),
return [ this.subscribe('postUsers', this.params._id).wait()
Meteor.subscribe('singlePost', this.params._id),
Meteor.subscribe('comments', this.params._id ),
Meteor.subscribe('postUsers', this.params._id)
];
}, },
data: function() { before: filters.nProgressHook,
return { data: function () {
postId: this.params._id return {postId: this.params._id};
}
}, },
after: function() { after: function () {
window.queueComments = false; window.queueComments = false;
window.openedComments = []; window.openedComments = [];
// TODO: scroll to comment position
} }
// TODO: scroll window to specific comment
}); });
// Post Edit // Post Edit
this.route('post_edit', { this.route('post_edit', {
path: '/posts/:_id/edit', path: '/posts/:_id/edit',
waitOn: function() { waitOn: function () {
return Meteor.subscribe('singlePost', this.params._id); return Meteor.subscribe('singlePost', this.params._id);
}, },
before: filters.nProgressHook,
data: function() { data: function() {
return { return {postId: this.params._id};
post: Posts.findOne(this.params._id) }
} // after: filters.canEditPost
},
after: filters.canEditPost
}); });
// Post Submit // Post Submit
@ -394,6 +398,9 @@ Router.map(function() {
this.route('comment_page', { this.route('comment_page', {
path: '/comments/:_id', path: '/comments/:_id',
waitOn: function() {
return Meteor.subscribe('singleComment', this.params._id);
},
data: function() { data: function() {
return { return {
comment: Comments.findOne(this.params._id) comment: Comments.findOne(this.params._id)
@ -408,6 +415,7 @@ Router.map(function() {
waitOn: function() { waitOn: function() {
return Meteor.subscribe('singleComment', this.params._id); return Meteor.subscribe('singleComment', this.params._id);
}, },
before: filters.nProgressHook,
data: function() { data: function() {
return { return {
comment: Comments.findOne(this.params._id) comment: Comments.findOne(this.params._id)
@ -439,11 +447,10 @@ Router.map(function() {
waitOn: function() { waitOn: function() {
return Meteor.subscribe('singleUser', this.params._idOrSlug); return Meteor.subscribe('singleUser', this.params._idOrSlug);
}, },
before: filters.nProgressHook,
data: function() { data: function() {
var findById = Meteor.users.findOne(this.params._idOrSlug); var findById = Meteor.users.findOne(this.params._idOrSlug);
var findBySlug = Meteor.users.findOne({'profile.slug': this.params._idOrSlug}); var findBySlug = Meteor.users.findOne({slug: this.params._idOrSlug});
console.log(findById)
console.log(findBySlug)
return { return {
user: (typeof findById == "undefined") ? findBySlug : findById user: (typeof findById == "undefined") ? findBySlug : findById
} }
@ -457,6 +464,7 @@ Router.map(function() {
waitOn: function() { waitOn: function() {
return Meteor.subscribe('singleUser', this.params._id); return Meteor.subscribe('singleUser', this.params._id);
}, },
before: filters.nProgressHook,
data: function() { data: function() {
return { return {
user: Meteor.users.findOne(this.params._id) user: Meteor.users.findOne(this.params._id)
@ -486,6 +494,7 @@ Router.map(function() {
waitOn: function() { waitOn: function() {
return Meteor.subscribe('allUsers'); return Meteor.subscribe('allUsers');
}, },
before: filters.nProgressHook,
data: function() { data: function() {
return { return {
users: Meteor.users.find({}, {sort: {createdAt: -1}}) users: Meteor.users.find({}, {sort: {createdAt: -1}})

View file

@ -1,3 +1,5 @@
<template name="no_rights"> <template name="no_rights">
No rights <div class="grid-small grid-block dialog">
Sorry, you don't have the rights to view this page.
</div>
</template> </template>

View file

@ -6,10 +6,13 @@
// }); // });
Template.post_edit.created = function(){ Template.post_edit.created = function(){
post = this.data; post = Posts.findOne(this.data.postId);
} }
Template.post_edit.helpers({ Template.post_edit.helpers({
post: function () {
return Posts.findOne(this.postId);
},
created: function(){ created: function(){
return moment(this.createdAt).format("MMMM Do, h:mm:ss a"); return moment(this.createdAt).format("MMMM Do, h:mm:ss a");
}, },

View file

@ -3,8 +3,7 @@ isAdminById=function(userId){
return !!(user && isAdmin(user)); return !!(user && isAdmin(user));
} }
isAdmin=function(user){ isAdmin=function(user){
if(!user || typeof user === 'undefined') user = (typeof user === 'undefined') ? Meteor.user() : user;
return false;
return !!user.isAdmin; return !!user.isAdmin;
} }
isInvited=function(user){ isInvited=function(user){

1
packages/.gitignore vendored
View file

@ -7,3 +7,4 @@ page-js-ie-support
HTML5-History-API HTML5-History-API
crypto-base crypto-base
iron-router iron-router
nprogress

View file

@ -1 +1 @@
/Users/sacha/.meteorite/packages/iron-router/EventedMind/iron-router/dddf61e618285ec2045a16f224870b1aeba05cdd /Users/sacha/.meteorite/packages/iron-router/EventedMind/iron-router/c63e92d24c8210e2304ff5fed1b5c463394aeaf6

View file

@ -7,9 +7,7 @@
}, },
"paginated-subscription": {}, "paginated-subscription": {},
"crypto-md5": {}, "crypto-md5": {},
"iron-router": { "iron-router": {},
"git": "https://github.com/EventedMind/iron-router.git", "nprogress": {}
"branch": "dev"
}
} }
} }

View file

@ -9,10 +9,8 @@
}, },
"paginated-subscription": {}, "paginated-subscription": {},
"crypto-md5": {}, "crypto-md5": {},
"iron-router": { "iron-router": {},
"git": "https://github.com/EventedMind/iron-router.git", "nprogress": {}
"branch": "dev"
}
}, },
"packages": { "packages": {
"momentjs": { "momentjs": {
@ -37,8 +35,13 @@
}, },
"iron-router": { "iron-router": {
"git": "https://github.com/EventedMind/iron-router.git", "git": "https://github.com/EventedMind/iron-router.git",
"branch": "dev", "tag": "v0.6.0",
"commit": "dddf61e618285ec2045a16f224870b1aeba05cdd" "commit": "c63e92d24c8210e2304ff5fed1b5c463394aeaf6"
},
"nprogress": {
"git": "https://github.com/zhouzhuojie/meteor-nprogress.git",
"tag": "v0.0.3",
"commit": "ffd1c5dd400af64121197f4e637a215c4b6b1fc7"
}, },
"crypto-base": { "crypto-base": {
"git": "https://github.com/tmeasday/meteor-crypto-base.git", "git": "https://github.com/tmeasday/meteor-crypto-base.git",