Vulcan/client/app.js

200 lines
5.6 KiB
JavaScript
Raw Normal View History

2012-10-18 14:24:36 +09:00
Accounts.ui.config({
passwordSignupFields: 'USERNAME_AND_EMAIL'
});
// ** Client-side helpers **
2012-10-09 12:02:37 +09:00
sessionSetObject=function(name, value){
Session.set(name, JSON.stringify(value));
2012-10-09 12:02:37 +09:00
}
sessionGetObject=function(name){
var data = Session.get(name);
return data && JSON.parse(data);
2012-10-09 12:02:37 +09:00
}
$.fn.exists = function () {
return this.length !== 0;
}
2012-10-18 12:34:13 +09:00
// ** Users **
2012-10-18 14:24:36 +09:00
Meteor.subscribe('currentUser');
Meteor.subscribe('allUsers');
// ** Errors **
// Local (client-only) collection
2012-10-09 12:02:37 +09:00
2012-10-08 10:44:13 +09:00
Errors = new Meteor.Collection(null);
// ** Settings **
Settings = new Meteor.Collection('settings');
Meteor.subscribe('settings', function(){
// runs once on site load
window.settingsLoaded=true;
if((proxinoKey=getSetting('proxinoKey'))){
Proxino.key = proxinoKey;
Proxino.track_errors();
}
// window.Router = new SimpleRouter();
window.Backbone.history.start({pushState: true});
});
// ** Notifications **
// Only load if user is logged in
var Notifications = new Meteor.Collection('notifications');
if(Meteor.user()){
Meteor.subscribe('notifications');
}
// ** Posts **
// We have a few subscriptions here, for the various ways we load posts
//
// The advantage is that
// a) we can change pages a lot quicker
// XXX: and we can animate between them (todo)
// b) we know when an individual page is ready
2012-08-22 21:27:22 -04:00
Posts = new Meteor.Collection('posts');
2012-10-09 12:02:37 +09:00
// TOP page
TOP_PAGE_PER_PAGE = 10;
TOP_PAGE_SORT = {score: -1};
Session.set('topPageLimit', TOP_PAGE_PER_PAGE);
Meteor.autosubscribe(function() {
Session.get('topPostsReady', false);
Meteor.subscribe('posts', {}, {
sort: TOP_PAGE_SORT,
limit: Session.get('topPageLimit')
}, function() {
Session.set('topPostsReady', true);
});
});
var topPosts = function() {
var orderedPosts = Posts.find({}, {sort: TOP_PAGE_SORT});
return limitDocuments(orderedPosts, Session.get('topPageLimit'));
2012-10-09 12:02:37 +09:00
}
// NEW page
NEW_PAGE_PER_PAGE = 10;
NEW_PAGE_SORT = {submitted: -1};
Session.set('newPageLimit', NEW_PAGE_PER_PAGE);
2012-10-08 16:12:47 +02:00
Meteor.autosubscribe(function() {
Session.get('newPostsReady', false);
Meteor.subscribe('posts', {}, {
sort: NEW_PAGE_SORT,
limit: Session.get('newPageLimit')
}, function() {
Session.set('newPostsReady', true);
});
2012-10-08 16:12:47 +02:00
});
var newPosts = function() {
var orderedPosts = Posts.find({}, {sort: NEW_PAGE_SORT});
return limitDocuments(orderedPosts, Session.get('newPageLimit'));
}
// DIGEST page
DIGEST_PAGE_PER_PAGE = 5;
DIGEST_PAGE_SORT = {score: -1};
var digestPageFind = function(mDate) {
return {submitted: {
$gte: mDate.startOf('day').valueOf(),
$lt: mDate.endOf('day').valueOf()
}};
}
Session.set('digestPageLimit', DIGEST_PAGE_PER_PAGE);
Meteor.autosubscribe(function() {
Session.get('digestPostsReady', false);
var mDate = moment(sessionGetObject('currentDate'));
// start yesterday, and subscribe to 3 days
mDate.add('days', -1);
for (var i = 0; i < 3; i++) {
Meteor.subscribe('posts', digestPageFind(mDate), {
sort: DIGEST_PAGE_SORT,
limit: Session.get('digestPageLimit')
}, function() {
Session.set('digestPostsReady', true);
});
mDate.add('days', 1);
}
});
var digestPosts = function() {
var mDate = moment(sessionGetObject('currentDate'))
var orderedPosts = Posts.find(digestPageFind(mDate), {sort: DIGEST_PAGE_SORT});
return limitDocuments(orderedPosts, Session.get('digestPageLimit'));
}
2012-08-22 21:27:22 -04:00
// SINGLE post, e.g. post_page
Meteor.autosubscribe(function() {
Session.set('postReady', false);
Meteor.subscribe('post', Session.get('selectedPostId'), function() {
Session.set('postReady', true);
})
});
2012-08-23 00:15:48 -04:00
// ** Categories **
2012-08-30 21:35:48 -04:00
Categories = new Meteor.Collection('categories');
Meteor.subscribe('categories');
2012-10-01 12:23:35 +09:00
// ** Comments **
// Collection depends on selectedPostId and selectedCommentId session variable
2012-10-08 16:49:01 +09:00
Session.set('selectedPostId', null);
Comments = new Meteor.Collection('comments');
Meteor.autosubscribe(function() {
var query = { $or : [ { post : Session.get('selectedPostId') } , { _id : Session.get('selectedCommentId') } ] };
Meteor.subscribe('comments', query, function() {
//
});
2012-09-24 15:36:37 +09:00
});
2012-09-06 19:42:11 +09:00
2012-10-01 14:52:32 +09:00
// ** Handlebars helpers **
2012-10-05 22:09:13 +09:00
Handlebars.registerHelper('canView', function(action) {
var action=(typeof action !== 'string') ? null : action;
return canView(Meteor.user(), action);
2012-10-05 22:09:13 +09:00
});
Handlebars.registerHelper('canPost', function(action) {
var action=(typeof action !== 'string') ? null : action;
return canPost(Meteor.user(), action);
2012-10-05 22:09:13 +09:00
});
Handlebars.registerHelper('canComment', function(action) {
var action=(typeof action !== 'string') ? null : action;
return canComment(Meteor.user(), action);
2012-10-05 22:09:13 +09:00
});
Handlebars.registerHelper('canUpvote', function(collection, action) {
var action=(typeof action !== 'string') ? null : action;
return canUpvote(Meteor.user()), collection, action;
2012-10-05 22:09:13 +09:00
});
Handlebars.registerHelper('canDownvote', function(collection, action) {
var action=(typeof action !== 'string') ? null : action;
return canDownvote(Meteor.user(), collection, action);
});
Handlebars.registerHelper('isAdmin', function(showError) {
if(isAdmin(Meteor.user())){
return true;
}else{
if((typeof showError === "string") && (showError === "true"))
throwError('Sorry, you do not have access to this page');
return false;
}
});
Handlebars.registerHelper('canEdit', function(collectionName, action) {
var action = (typeof action !== 'string') ? null : action;
var collection = (typeof collectionName !== 'string') ? Posts : eval(collectionName);
var itemId = (collectionName==="Posts") ? Session.get('selectedPostId') : Session.get('selectedCommentId');
var item=collection.findOne(itemId);
return item && canEdit(Meteor.user(), item, action);
2012-10-04 14:54:26 +09:00
});