cleaning up syntax

This commit is contained in:
Sacha Greif 2012-10-01 14:52:32 +09:00
parent b8afef41dd
commit 8774475176
6 changed files with 140 additions and 245 deletions

View file

@ -21,141 +21,12 @@ Meteor.subscribe('settings', function(){
}
});
Session.set('state', 'list');
if (Meteor.is_client) {
SimpleRouter = FilteredRouter.extend({
initialize: function() {
FilteredRouter.prototype.initialize.call(this);
this.filter(this.require_login, {only: ['submit']});
this.filter(this.start_request);
},
start_request: function(page){
// runs at every new page change
Session.set("error", null);
Session.set("openedComments", null);
document.title = getSetting("title");
// Mixpanel
if((mixpanelId=getSetting("mixpanelId")) && window.mixpanel.length==0){
mixpanel.init(mixpanelId);
if(Meteor.user()){
var currentUserEmail=getCurrentUserEmail();
console.log(currentUserEmail);
mixpanel.people.identify(currentUserEmail);
mixpanel.people.set({
'username': Meteor.user().username,
'$last_login': new Date(),
'$created': moment(Meteor.user().createdAt)._d,
'$email': currentUserEmail
});
mixpanel.register({
'username': Meteor.user().username,
'createdAt': moment(Meteor.user().createdAt)._d,
'email': currentUserEmail
});
mixpanel.name_tag(currentUserEmail);
}
}
// GoSquared
if((goSquaredId=getSetting("goSquaredId"))){
GoSquared.acct = goSquaredId;
GoSquaredInit();
}
return page;
},
require_login: function(page) {
if (Meteor.user()) {
return page;
} else {
return 'signin';
}
},
routes: {
'': 'top',
'top':'top',
'new':'new',
'test':'test',
'signin':'signin',
'signup':'signup',
'submit':'submit',
'posts/deleted':'post_deleted',
'posts/:id/edit':'post_edit',
'posts/:id':'post',
'comments/deleted':'comment_deleted',
'comments/:id':'comment',
'comments/:id/edit':'comment_edit',
'settings':'settings',
'users':'users',
'account':'user_edit',
'forgot_password':'forgot_password',
'users/:id': 'user_profile',
'users/:id/edit':'user_edit'
},
top: function() { this.goto('posts_top'); },
new: function() { this.goto('posts_new'); },
signup: function() { this.goto('signup'); },
signin: function() { this.goto('signin'); },
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'); },
post: function(id) {
console.log("post, id="+id);
Session.set('selectedPostId', id);
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) {
console.log("post_edit, id="+id);
Session.set('selectedPostId', id);
this.goto('post_edit');
},
comment: function(id) {
console.log("comment, id="+id);
Session.set('selectedCommentId', id);
this.goto('comment_page');
window.repress_recursion=true;
window.newCommentTimestamp=new Date();
},
comment_edit: function(id) {
console.log("comment_edit, id="+id);
Session.set('selectedCommentId', id);
this.goto('comment_edit');
window.newCommentTimestamp=new Date();
},
user_profile: function(id){
if(typeof id !== undefined){
window.selected_user_id=id;
}
this.goto('user_profile');
},
user_edit: function(id){
if(typeof id !== undefined){
window.selected_user_id=id;
}
this.goto('user_edit');
}
});
var Router = new SimpleRouter();
Meteor.startup(function() {
if(!Backbone.history._hasPushState)
Backbone.history.start({pushState: true});
});
}
$.fn.exists = function () {
return this.length !== 0;
}
getAuthorName = function(item){
// if item is linked to a user, get that user's display name. Else, return the author field.
return item.user_id && (user=Meteor.users.findOne(item.user_id)) ? getDisplayName(user) : this.author;
}

View file

@ -6,8 +6,42 @@ SimpleRouter = FilteredRouter.extend({
this.filter(this.require_profile);
},
start_request: function(page){
// runs at every new page change
Session.set("error", null);
Session.set("openedComments", null);
document.title = getSetting("title");
// Mixpanel
if((mixpanelId=getSetting("mixpanelId")) && window.mixpanel.length==0){
mixpanel.init(mixpanelId);
if(Meteor.user()){
var currentUserEmail=getCurrentUserEmail();
console.log(currentUserEmail);
mixpanel.people.identify(currentUserEmail);
mixpanel.people.set({
'username': Meteor.user().username,
'$last_login': new Date(),
'$created': moment(Meteor.user().createdAt)._d,
'$email': currentUserEmail
});
mixpanel.register({
'username': Meteor.user().username,
'createdAt': moment(Meteor.user().createdAt)._d,
'email': currentUserEmail
});
mixpanel.name_tag(currentUserEmail);
}
}
// GoSquared
if((goSquaredId=getSetting("goSquaredId"))){
GoSquared.acct = goSquaredId;
GoSquaredInit();
}
return page;
},
require_login: function(page) {
@ -22,7 +56,7 @@ SimpleRouter = FilteredRouter.extend({
require_profile: function(page) {
var user = Meteor.user();
if (user && !user.loading && !userProfileComplete(user))
return 'user_edit';
return 'user_email';
else
return page;
},
@ -60,7 +94,7 @@ SimpleRouter = FilteredRouter.extend({
forgot_password: function() { this.goto('user_password'); },
post: function(id) {
console.log("post, id="+id);
Session.set('selected_post_id', id);
Session.set('selectedPostId', id);
this.goto('post_page');
// on post page, we show the comment recursion
window.repress_recursion=false;
@ -69,19 +103,19 @@ SimpleRouter = FilteredRouter.extend({
},
post_edit: function(id) {
console.log("post_edit, id="+id);
Session.set('selected_post_id', id);
Session.set('selectedPostId', id);
this.goto('post_edit');
},
comment: function(id) {
console.log("comment, id="+id);
Session.set('selected_comment_id', id);
Session.set('selectedCommentId', id);
this.goto('comment_page');
window.repress_recursion=true;
window.newCommentTimestamp=new Date();
},
comment_edit: function(id) {
console.log("comment_edit, id="+id);
Session.set('selected_comment_id', id);
Session.set('selectedCommentId', id);
this.goto('comment_edit');
window.newCommentTimestamp=new Date();
},

View file

@ -1,28 +1,62 @@
Template.post_item.events = {
var getRank = function(post){
if(window.sortBy=="score"){
var filter = {$or: [
{score: {$gt: post.score}},
{$and: [{score: post.score}, {submitted: {$lt: post.submitted}}]}
]};
}else{
var filter = {$or: [
{submitted: {$gt: post.submitted}},
{$and: [{submitted: post.submitted}, {score: {$lt: post.score}}]}
]};
}
return Posts.find(filter).count()+1;
}
Template.post_item.preserve({
'.post': function (node) {return node.id; }
});
'click .upvote-link': function(e, instance){
e.preventDefault();
if(!Meteor.user()){
throwError("Please log in first");
Template.post_item.helpers({
rank: function() {
return getRank(this);
},
domain: function(){
var a = document.createElement('a');
a.href = this.url;
return a.hostname;
},
current_domain: function(){
return "http://"+document.domain;
},
can_edit: function(){
if(Meteor.user() && (Meteor.user().isAdmin || Meteor.userId() === this.user_id))
return true;
else
return false;
}
Meteor.call('upvotePost', this._id, function(error, result){
trackEvent("post upvoted", {'postId': instance.postId});
});
}
, 'click .share-link': function(e){
var $this = $(e.target);
e.preventDefault();
$(".share-link").not($this).next().addClass("hidden");
$this.next().toggleClass("hidden");
console.log($this);
$this.next().find('.share-replace').sharrre(SharrreOptions);
// $(".overlay").toggleClass("hidden");
}
};
},
authorName: function(){
return getAuthorName(this);
},
short_score: function(){
return Math.floor(this.score*1000)/1000;
},
body_formatted: function(){
var converter = new Markdown.Converter();
var html_body=converter.makeHtml(this.body);
return html_body.autoLink();
},
ago: function(){
return moment(this.submitted).fromNow();
},
voted: function(){
var user = Meteor.user();
if(!user) return false;
return _.include(this.upvoters, user._id);
},
});
Template.post_item.created = function(){
if(this.data){
@ -51,72 +85,25 @@ Template.post_item.rendered = function(){
};
Template.post_item.preserve({
'.post': function (node) {return node.id; }
});
Template.post_item.ago = function(){
return moment(this.submitted).fromNow();
};
Template.post_item.voted = function(){
var user = Meteor.user();
if(!user) return false;
return _.include(this.upvoters, user._id);
};
var getRank = function(post){
if(window.sortBy=="score"){
var filter = {$or: [
{score: {$gt: post.score}},
{$and: [{score: post.score}, {submitted: {$lt: post.submitted}}]}
]};
}else{
var filter = {$or: [
{submitted: {$gt: post.submitted}},
{$and: [{submitted: post.submitted}, {score: {$lt: post.score}}]}
]};
}
return Posts.find(filter).count()+1;
}
Template.post_item.rank = function() {
return getRank(this);
}
Template.post_item.domain = function(){
var a = document.createElement('a');
a.href = this.url;
return a.hostname;
};
Template.post_item.current_domain = function(){
return "http://"+document.domain;
}
Template.post_item.can_edit = function(){
if(Meteor.user() && (Meteor.user().isAdmin || Meteor.userId() === this.user_id))
return true;
else
Template.post_item.events = {
'click .upvote-link': function(e, instance){
e.preventDefault();
if(!Meteor.user()){
throwError("Please log in first");
return false;
};
}
Meteor.call('upvotePost', this._id, function(error, result){
trackEvent("post upvoted", {'postId': instance.postId});
});
}
Template.post_item.authorName = function(){
if(this.user_id && Meteor.users.findOne(this.user_id)){
return Meteor.users.findOne(this.user_id).username;
}else{
return this.author;
, 'click .share-link': function(e){
var $this = $(e.target);
e.preventDefault();
$(".share-link").not($this).next().addClass("hidden");
$this.next().toggleClass("hidden");
console.log($this);
$this.next().find('.share-replace').sharrre(SharrreOptions);
// $(".overlay").toggleClass("hidden");
}
};
Template.post_item.short_score = function(){
return Math.floor(this.score*1000)/1000;
}
Template.post_item.body_formatted = function(){
var converter = new Markdown.Converter();
var html_body=converter.makeHtml(this.body);
return html_body.autoLink();
}

View file

@ -1,11 +1,9 @@
<template name="user_email">
<div class="grid-small grid-block dialog user-edit">
{{#with user}}
{{#if profileIncomplete}}
<div>
Please add your email below before continuing.
Please fill in your email below to finish signing up.
</div>
{{/if}}
<form>
<div class="control-group">
<label>Email</label>
@ -14,7 +12,6 @@
</div>
</div>
<div class="form-actions">
<a href="/forgot_password">Forgot password?</a>
<input type="submit" class="button" value="Submit" />
</div>
</form>

View file

@ -1,4 +1,4 @@
Template.user_edit.events = {
Template.user_email.events = {
'submit form': function(e){
e.preventDefault();
if(!Meteor.user()) throwError('You must be logged in.');
@ -17,18 +17,20 @@ Template.user_edit.events = {
if(error){
throwError(error.reason);
} else {
throwError('Profile updated');
throwError('Thanks for signing up!');
trackEvent("new sign-up", {'userId': user._id, 'auth':'twitter'});
Router.navigate('/', {trigger: true});
}
});
}
};
Template.user_edit.profileIncomplete = function() {
Template.user_email.profileIncomplete = function() {
return Meteor.user() && !this.loading && !userProfileComplete(this);
}
Template.user_edit.user = function(){
Template.user_email.user = function(){
var current_user=Meteor.user();
if(window.selected_user_id && !current_user.loading && current_user.isAdmin){
return Meteor.users.findOne(window.selected_user_id);

View file

@ -1,8 +1,12 @@
// Useful methods on users
getDisplayNameById = function(userId){
getDisplayName(Meteor.users.findOne(userId));
}
getDisplayName = function(user){
return (user.profile && user.profile.name) ? user.profile.name : user.username
}
getCurrentUserEmail = function(){
return Meteor.user() ? Meteor.user().emails[0].address : '';
}
userProfileComplete = function(user) {
return !!user.profile.email;
}