Skip to content

Instantly share code, notes, and snippets.

@calvintwr
Created February 19, 2015 15:20
Show Gist options
  • Save calvintwr/a063f6ed5e33e23b2996 to your computer and use it in GitHub Desktop.
Save calvintwr/a063f6ed5e33e23b2996 to your computer and use it in GitHub Desktop.
likeText
streamFactory.append.likeText = function(post) {
//console.log('streamFactory.append.likeText');
var theParent = this.parent;
var toPrepend = '';
if(post.totalLikes > 0) {
var likersFollowed = post.likes,
likersFollowedCount = VV.utils.objCount(likersFollowed),
show = 3, //show how many related likers
loopRuns = 0,
likersDispHTML = '',
likersDisp = {};
if(likersFollowedCount > 0) {
if(likersFollowedCount > show) {
likersDisp = VV.utils.getRandom(likersFollowed, show);
loopRuns = show;
} else {
likersDisp = likersFollowed;
loopRuns = likersFollowedCount;
}
for(var k=0;k<loopRuns;k++) {
var name = likersDisp[k].user.userNameDisp;
if(k>0) {
if(k<show-1) {
//put commas after the first case, stop at last case.
likersDispHTML += ', ';
} else {
//for last case put "+"
likersDispHTML += ' + ';
}
}
likersDispHTML += '<a href="' + printHead.p.absPath + '/' + name + '">' + name +'</a>';
}
//if there are only 2 cases, replace the comma with 'and'.
//mary and sally like this.
if(loopRuns === 2) {
likersDispHTML = likersDispHTML.replace(',', ' and');
}
}
var andLikes = '';
if(likersFollowedCount > 0) {
//sally, mary, jane and 99 others like this.
//Number of related likers shown is defined by "show".
//So the offsetted number of likers is minimum of show or likersFollowed.
var offset = Math.min(likersFollowedCount, show)
var offsetCount = post.totalLikes - offset;
if(offsetCount > 0) {
andLikes = ' + ';
andLikes += '<span class="postLikesCount" data-likescount="' + offsetCount + '">';
andLikes += offsetCount;
andLikes += '</span>';
if(offsetCount === 1 && post.totalLikes === 1) {
andLikes += ' like';
} else {
andLikes += ' likes';
}
} else {
if(post.totalLikes > 0 ) {
if(post.totalLikes === 1) {
andLikes += ' like this.';
} else {
andLikes += ' likes this.';
}
}
}
} else if(post.totalLikes > 1) {
// 2-99 people like this.
var offsetCount = post.totalLikes;
andLikes = '<span class="postLikesCount" data-likescount="' + offsetCount + '">';
andLikes += offsetCount;
andLikes += '</span>';
andLikes += ' likes';
} else if(post.totalLikes === 1) {
// 1 person likes this.
var offsetCount = post.totalLikes;
andLikes = '<span class="postLikesCount" data-likescount="' + offsetCount + '">';
andLikes += offsetCount;
andLikes += '</span>';
andLikes += ' like';
}
toPrepend = likersDispHTML + '<span class="postAndLikes">' + andLikes + '</span>';
} else {
toPrepend = '';
}
//console.log(toPrepend);
return toPrepend;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment