Vulcan/client/views/posts/post_item.js

174 lines
5.3 KiB
JavaScript
Raw Normal View History

2013-10-28 13:35:20 +09:00
Template.post_item.created = function () {
instance = this;
};
2012-10-01 14:52:32 +09:00
Template.post_item.helpers({
2013-10-09 12:39:05 +09:00
post: function(){
// note: when the data context is set by the router, it will be "this.post". When set by a parent template it'll be "this"
return this.post || this;
},
2013-02-18 12:13:27 +09:00
postLink: function(){
return !!this.url ? this.url : "/posts/"+this._id;
2012-11-21 14:31:58 +09:00
},
2013-10-14 11:43:55 +09:00
postTarget: function() {
return !!this.url ? '_blank' : '';
},
2013-10-28 13:35:20 +09:00
oneBasedRank: function(){
if(typeof this.rank != 'undefined')
return this.rank + 1;
2012-10-18 14:51:15 +09:00
},
2012-10-01 14:52:32 +09:00
domain: function(){
var a = document.createElement('a');
a.href = this.url;
return a.hostname;
},
current_domain: function(){
return "http://"+document.domain;
},
can_edit: function(){
2012-11-21 14:31:58 +09:00
return canEdit(Meteor.user(), this);
2012-10-01 14:52:32 +09:00
},
authorName: function(){
return getAuthorName(this);
},
profileUrl: function(){
// note: we don't want the post to be re-rendered every time user properties change
var user = Meteor.users.findOne(this.userId, {reactive: false});
2013-10-25 22:01:46 +09:00
if(user)
return getProfileUrl(user);
},
2012-10-01 14:52:32 +09:00
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(){
2013-01-13 08:52:35 +09:00
// if post is approved show submission time, else show creation time.
time = this.status == STATUS_APPROVED ? this.submitted : this.createdAt;
return moment(time).fromNow();
2012-10-01 14:52:32 +09:00
},
2012-10-09 12:02:37 +09:00
timestamp: function(){
2013-01-13 08:52:35 +09:00
time = this.status == STATUS_APPROVED ? this.submitted : this.createdAt;
return moment(time).format("MMMM Do, h:mm:ss a");
2012-10-09 12:02:37 +09:00
},
2012-10-01 14:52:32 +09:00
voted: function(){
var user = Meteor.user();
if(!user) return false;
return _.include(this.upvoters, user._id);
},
2012-10-22 09:28:42 +09:00
userAvatar: function(){
if(author=Meteor.users.findOne(this.userId), {reactive: false})
2012-10-22 09:28:42 +09:00
return getAvatarUrl(author);
},
inactiveClass: function(){
2012-12-24 16:21:56 +01:00
return (isAdmin(Meteor.user()) && this.inactive) ? "inactive" : "";
2013-02-18 12:13:27 +09:00
},
categoryLink: function(){
return getCategoryUrl(this.slug);
},
commentsDisplayText: function(){
return this.comments == 1 ? 'comment' : 'comments';
},
pointsUnitDisplayText: function(){
return this.votes == 1 ? 'point' : 'points';
},
lastRender: function(){
return Session.get('postItemLastRender');
}
2012-10-01 14:52:32 +09:00
});
var recalculatePosition = function ($object, positionsArray) {
var positionsLength = positionsArray.length,
previousPosition = positionsArray[positionsLength-2]
newPosition = positionsArray[positionsLength-1]
// calculate difference between old position and new position and send element here
var delta = previousPosition - newPosition;
console.log('previousPosition: '+previousPosition)
console.log('newPosition: '+newPosition)
console.log('delta: '+delta)
// send object back to previous position, then animate it to new one
$object.css("top", delta + "px").addClass('animate').delay(1).queue(function(next){
$(this).css('top','0px');
next();
});
}
2012-09-06 10:18:26 +09:00
Template.post_item.rendered = function(){
2013-03-19 10:54:01 +09:00
// animate post from previous position to new position
var instance = this,
$instance = $(instance.firstNode.nextSibling),
top = $instance.position().top;
console.log('-----------------------')
console.log('headline: '+this.data.headline)
if(typeof instance.positionsHistory == "undefined"){
instance.positionsHistory = [top];
}else{
instance.positionsHistory.push(top);
recalculatePosition($instance, instance.positionsHistory)
}
console.log('render positions: '+instance.positionsHistory)
// var rank = instance.data.rank;
// var rank = $instance.index(); // use jQuery instead of decorating the data object
// var previousPosition = 0;
// var newPosition = $instance.position().top;
2013-10-28 13:35:20 +09:00
2013-10-28 16:04:55 +09:00
// console.log('newPosition: '+newPosition)
// console.log(instance)
2012-10-18 16:11:08 +09:00
// if it's not the first ever render, element should have a currentPosition
// if(instance.renderCount > 1){
2013-04-07 17:41:15 +09:00
// calculate difference between old position and new position and send element here
2013-03-19 10:54:01 +09:00
// var delta = previousPosition - newPosition;
// console.log('delta: '+delta)
// $instance.css("top", delta + "px");
// }
// Meteor.defer(function() {
// instance.currentPosition = newPosition;
// // bring element back to its new original position
// $instance.addClass('animate').css("top", "0px");
// });
};
2012-10-01 14:52:32 +09:00
Template.post_item.events = {
'click .upvote-link': function(e, instance){
2013-01-15 08:46:03 +09:00
var post = this;
2012-10-01 14:52:32 +09:00
e.preventDefault();
if(!Meteor.user()){
Router.go('/signin');
throwError("Please log in first");
}
Meteor.call('upvotePost', post, function(error, result){
trackEvent("post upvoted", {'_id': post._id});
});
2013-07-04 12:51:26 +09:00
},
'click .share-link': function(e){
var $this = $(e.target).parents('.post-share').find('.share-link');
var $share = $this.parents('.post-share').find('.share-options');
e.preventDefault();
$('.share-link').not($this).removeClass("active");
$(".share-options").not($share).addClass("hidden");
$this.toggleClass("active");
$share.toggleClass("hidden");
$share.find('.share-replace').sharrre(SharrreOptions);
2013-07-04 12:51:26 +09:00
},
'click .post-title': function(e){
Meteor.call('clickedPost', this, function(error, result){
if(error)
console.log(error);
});
}
2012-10-01 14:52:32 +09:00
};