2012-06-02 16:33:44 -07:00
|
|
|
NEWSBLUR.Views.StoryComment = Backbone.View.extend({
|
|
|
|
|
|
|
|
className: 'NB-story-comment',
|
|
|
|
|
|
|
|
events: {
|
|
|
|
"click .NB-user-avatar": "open_social_profile_modal",
|
|
|
|
"click .NB-story-comment-username": "open_social_profile_modal",
|
|
|
|
"click .NB-story-comment-reply-button": "open_reply",
|
2012-07-15 17:59:19 -07:00
|
|
|
"click .NB-story-comment-like": "like_comment",
|
2012-06-02 16:33:44 -07:00
|
|
|
"click .NB-story-comment-share-edit-button": "toggle_feed_story_share_dialog",
|
|
|
|
"click .NB-story-comment-reply .NB-modal-submit-button": "save_social_comment_reply"
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function(options) {
|
2012-06-07 09:57:53 -07:00
|
|
|
this.story = options.story;
|
2012-07-02 15:40:03 -07:00
|
|
|
if (!this.options.on_social_page) {
|
|
|
|
this.user = NEWSBLUR.assets.user_profiles.find(this.model.get('user_id'));
|
|
|
|
}
|
2012-07-15 17:59:19 -07:00
|
|
|
this.model.bind('change:liking_users', this.render, this);
|
|
|
|
this.model.bind('change:liking_users', this.call_out_like, this);
|
2012-06-02 16:33:44 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
this.model.set('comments', this.model.get('comments').replace(/\n+/g, '<br><br>'));
|
|
|
|
var reshare_class = this.model.get('source_user_id') ? 'NB-story-comment-reshare' : '';
|
2012-07-15 17:59:19 -07:00
|
|
|
var has_likes = _.any(this.model.get('liking_users'));
|
|
|
|
var liked = _.contains(this.model.get('liking_users'), NEWSBLUR.Globals.user_id);
|
2012-06-05 16:18:30 -07:00
|
|
|
|
2012-06-02 16:33:44 -07:00
|
|
|
var $comment = $.make('div', [
|
2012-07-15 17:59:19 -07:00
|
|
|
$.make('div', { className: 'NB-story-comment-author-avatar NB-user-avatar ' + reshare_class }, [
|
2012-06-05 16:18:30 -07:00
|
|
|
$.make('img', { src: this.user.get('photo_url') })
|
2012-06-02 16:33:44 -07:00
|
|
|
]),
|
|
|
|
$.make('div', { className: 'NB-story-comment-author-container' }, [
|
|
|
|
(this.model.get('source_user_id') && $.make('div', { className: 'NB-story-comment-reshares' }, [
|
2012-06-21 13:32:18 -07:00
|
|
|
NEWSBLUR.Views.ProfileThumb.create(this.model.get('source_user_id')).render().el
|
2012-06-02 16:33:44 -07:00
|
|
|
])),
|
2012-07-15 17:59:19 -07:00
|
|
|
(has_likes && $.make('div', { className: 'NB-story-comment-likes NB-right' }, [
|
|
|
|
$.make('div', { className: 'NB-story-comment-like ' + (liked ? 'NB-active' : '') }),
|
|
|
|
this.render_liking_users()
|
|
|
|
])),
|
2012-06-05 16:18:30 -07:00
|
|
|
$.make('div', { className: 'NB-story-comment-username' }, this.user.get('username')),
|
2012-06-02 16:33:44 -07:00
|
|
|
$.make('div', { className: 'NB-story-comment-date' }, this.model.get('shared_date') + ' ago'),
|
|
|
|
(this.model.get('user_id') == NEWSBLUR.Globals.user_id && $.make('div', { className: 'NB-story-comment-edit-button NB-story-comment-share-edit-button' }, [
|
|
|
|
$.make('div', { className: 'NB-story-comment-edit-button-wrapper' }, 'edit')
|
|
|
|
])),
|
|
|
|
$.make('div', { className: 'NB-story-comment-reply-button' }, [
|
|
|
|
$.make('div', { className: 'NB-story-comment-reply-button-wrapper' }, 'reply')
|
2012-07-15 17:59:19 -07:00
|
|
|
]),
|
|
|
|
(!has_likes && this.model.get('user_id') != NEWSBLUR.Globals.user_id && $.make('div', { className: 'NB-story-comment-likes NB-left' }, [
|
|
|
|
$.make('div', { className: 'NB-story-comment-like' })
|
|
|
|
]))
|
2012-06-02 16:33:44 -07:00
|
|
|
]),
|
2012-06-05 16:18:30 -07:00
|
|
|
$.make('div', { className: 'NB-story-comment-content' }, this.model.get('comments')),
|
|
|
|
this.make_story_share_comment_replies()
|
2012-06-02 16:33:44 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
this.$el.html($comment);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
make_story_share_comment_replies: function() {
|
2012-06-05 16:18:30 -07:00
|
|
|
if (!this.model.replies || !this.model.replies.length) return;
|
|
|
|
|
2012-06-02 16:33:44 -07:00
|
|
|
var user_id = NEWSBLUR.Globals.user_id;
|
2012-06-05 16:18:30 -07:00
|
|
|
var $replies = this.model.replies.map(_.bind(function(reply) {
|
2012-07-28 18:33:07 -07:00
|
|
|
if (!NEWSBLUR.assets.get_user(reply.user_id)) return;
|
2012-06-05 16:18:30 -07:00
|
|
|
return new NEWSBLUR.Views.StoryCommentReply({model: reply, comment: this}).render().el;
|
|
|
|
}, this));
|
2012-06-02 16:33:44 -07:00
|
|
|
$replies = $.make('div', { className: 'NB-story-comment-replies' }, $replies);
|
|
|
|
|
|
|
|
return $replies;
|
|
|
|
},
|
|
|
|
|
2012-07-15 17:59:19 -07:00
|
|
|
render_liking_users: function() {
|
|
|
|
var $users = $.make('div', { className: 'NB-story-comment-likes-users' });
|
|
|
|
|
|
|
|
_.each(this.model.get('liking_users'), function(user_id) {
|
2012-07-28 18:33:07 -07:00
|
|
|
if (!NEWSBLUR.assets.get_user(user_id)) return;
|
2012-07-15 17:59:19 -07:00
|
|
|
var $thumb = NEWSBLUR.Views.ProfileThumb.create(user_id).render().el;
|
|
|
|
$users.append($thumb);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
},
|
|
|
|
|
|
|
|
call_out_like: function() {
|
|
|
|
var $like = this.$('.NB-story-comment-like');
|
|
|
|
var liked = _.contains(this.model.get('liking_users'), NEWSBLUR.Globals.user_id);
|
|
|
|
|
|
|
|
$like.attr({'title': liked ? 'Favorited!' : 'Unfavorited'});
|
|
|
|
$like.tipsy({
|
|
|
|
gravity: 'sw',
|
|
|
|
fade: true,
|
|
|
|
trigger: 'manual',
|
|
|
|
offsetOpposite: -1
|
|
|
|
});
|
|
|
|
var tipsy = $like.data('tipsy');
|
|
|
|
_.defer(function() {
|
|
|
|
tipsy.enable();
|
|
|
|
tipsy.show();
|
|
|
|
});
|
|
|
|
|
|
|
|
$like.animate({
|
|
|
|
'opacity': 1
|
|
|
|
}, {
|
|
|
|
'duration': 850,
|
|
|
|
'queue': false,
|
|
|
|
'complete': function() {
|
|
|
|
if (tipsy.enabled) {
|
|
|
|
tipsy.hide();
|
|
|
|
tipsy.disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2012-06-02 16:33:44 -07:00
|
|
|
|
|
|
|
// ==========
|
|
|
|
// = Events =
|
|
|
|
// ==========
|
|
|
|
|
|
|
|
open_social_profile_modal: function() {
|
|
|
|
NEWSBLUR.reader.open_social_profile_modal(this.model.get("user_id"));
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle_feed_story_share_dialog: function() {
|
2012-06-07 09:57:53 -07:00
|
|
|
this.story.story_share_view.toggle_feed_story_share_dialog();
|
2012-06-02 16:33:44 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
open_reply: function(options) {
|
|
|
|
options = options || {};
|
2012-06-05 16:18:30 -07:00
|
|
|
var current_user = NEWSBLUR.assets.user_profile;
|
2012-06-02 16:33:44 -07:00
|
|
|
|
|
|
|
var $form = $.make('div', { className: 'NB-story-comment-reply NB-story-comment-reply-form' }, [
|
2012-06-05 16:18:30 -07:00
|
|
|
$.make('img', { className: 'NB-story-comment-reply-photo', src: current_user.get('photo_url') }),
|
|
|
|
$.make('div', { className: 'NB-story-comment-username NB-story-comment-reply-username' }, current_user.get('username')),
|
2012-07-27 18:58:35 -07:00
|
|
|
$.make('input', { type: 'text', className: 'NB-input NB-story-comment-reply-comments', value: options.reply && options.reply.get("comments") }),
|
2012-06-02 16:33:44 -07:00
|
|
|
$.make('div', { className: 'NB-modal-submit-button NB-modal-submit-green' }, options.is_editing ? 'Save' : 'Post')
|
|
|
|
]);
|
|
|
|
this.remove_social_comment_reply_form();
|
|
|
|
|
2012-06-05 18:12:34 -07:00
|
|
|
if (options.is_editing && options.$reply) {
|
2012-07-27 18:58:35 -07:00
|
|
|
$form.data('reply_id', options.reply.get("reply_id"));
|
2012-06-02 16:33:44 -07:00
|
|
|
options.$reply.hide().addClass('NB-story-comment-reply-hidden');
|
|
|
|
options.$reply.after($form);
|
|
|
|
} else {
|
|
|
|
this.$el.append($form);
|
|
|
|
}
|
|
|
|
|
|
|
|
$('.NB-story-comment-reply-comments', $form).bind('keydown', 'enter',
|
|
|
|
_.bind(this.save_social_comment_reply, this));
|
|
|
|
$('.NB-story-comment-reply-comments', $form).bind('keydown', 'return',
|
|
|
|
_.bind(this.save_social_comment_reply, this));
|
|
|
|
$('.NB-story-comment-reply-comments', $form).bind('keydown', 'esc', _.bind(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.remove_social_comment_reply_form();
|
|
|
|
}, this));
|
|
|
|
$('input', $form).focus();
|
2012-06-19 14:21:43 -07:00
|
|
|
|
2012-07-02 15:40:03 -07:00
|
|
|
if (NEWSBLUR.app.story_list) {
|
|
|
|
NEWSBLUR.app.story_list.fetch_story_locations_in_feed_view();
|
|
|
|
}
|
2012-06-02 16:33:44 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
remove_social_comment_reply_form: function() {
|
|
|
|
this.$('.NB-story-comment-reply-form').remove();
|
|
|
|
this.$('.NB-story-comment-reply-hidden').show();
|
|
|
|
},
|
|
|
|
|
|
|
|
save_social_comment_reply: function() {
|
|
|
|
var $form = this.$('.NB-story-comment-reply-form');
|
|
|
|
var $submit = $(".NB-modal-submit-button", $form);
|
|
|
|
var comment_user_id = this.model.get('user_id');
|
|
|
|
var comment_reply = $('.NB-story-comment-reply-comments', $form).val();
|
2012-07-27 18:58:35 -07:00
|
|
|
var reply_id = $form.data('reply_id');
|
2012-06-02 16:33:44 -07:00
|
|
|
|
|
|
|
if (!comment_reply || comment_reply.length <= 1) {
|
|
|
|
this.remove_social_comment_reply_form();
|
2012-07-03 17:00:09 -07:00
|
|
|
if (NEWSBLUR.app.story_list) {
|
|
|
|
NEWSBLUR.app.story_list.fetch_story_locations_in_feed_view();
|
|
|
|
}
|
2012-06-02 16:33:44 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($submit.hasClass('NB-disabled')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$submit.addClass('NB-disabled').text('Posting...');
|
|
|
|
NEWSBLUR.assets.save_comment_reply(this.options.story.id, this.options.story.get('story_feed_id'),
|
|
|
|
comment_user_id, comment_reply,
|
2012-07-27 18:58:35 -07:00
|
|
|
reply_id,
|
2012-06-02 16:33:44 -07:00
|
|
|
_.bind(function(data) {
|
2012-07-03 17:00:09 -07:00
|
|
|
if (this.options.on_social_page) {
|
|
|
|
this.options.story_comments_view.replace_comment(this.model.get('user_id'), data);
|
|
|
|
} else {
|
|
|
|
this.model.set(data.comment);
|
|
|
|
this.render();
|
|
|
|
NEWSBLUR.app.story_list.fetch_story_locations_in_feed_view();
|
|
|
|
}
|
2012-06-02 16:33:44 -07:00
|
|
|
}, this), _.bind(function(data) {
|
|
|
|
var message = data && data.message || "Sorry, this reply could not be posted. Probably a bug.";
|
|
|
|
if (!NEWSBLUR.Globals.is_authenticated) {
|
|
|
|
message = "You need to be logged in to reply to a comment.";
|
|
|
|
}
|
|
|
|
var $error = $.make('div', { className: 'NB-error' }, message);
|
|
|
|
$submit.removeClass('NB-disabled').text('Post');
|
|
|
|
$form.find('.NB-error').remove();
|
|
|
|
$form.append($error);
|
2012-07-03 17:00:09 -07:00
|
|
|
if (NEWSBLUR.app.story_list) {
|
|
|
|
NEWSBLUR.app.story_list.fetch_story_locations_in_feed_view();
|
|
|
|
}
|
2012-06-02 16:33:44 -07:00
|
|
|
}, this));
|
2012-07-15 17:59:19 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
like_comment: function() {
|
|
|
|
var liking_user_ids = this.model.get('liking_users') || [];
|
|
|
|
var comment_user_id = this.model.get('user_id');
|
|
|
|
var liked = _.contains(liking_user_ids, NEWSBLUR.Globals.user_id);
|
|
|
|
|
|
|
|
if (!liked) {
|
|
|
|
this.model.set('liking_users', _.union(liking_user_ids, NEWSBLUR.Globals.user_id));
|
|
|
|
NEWSBLUR.assets.like_comment(this.options.story.id,
|
|
|
|
this.options.story.get('story_feed_id'),
|
|
|
|
comment_user_id);
|
|
|
|
} else {
|
|
|
|
this.model.set('liking_users', _.without(liking_user_ids, NEWSBLUR.Globals.user_id));
|
|
|
|
NEWSBLUR.assets.remove_like_comment(this.options.story.id,
|
|
|
|
this.options.story.get('story_feed_id'),
|
|
|
|
comment_user_id);
|
|
|
|
}
|
2012-06-02 16:33:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|