NewsBlur-viq/media/js/newsblur/views/story_comment_view.js

293 lines
13 KiB
JavaScript
Raw Normal View History

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",
"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-green": "save_social_comment_reply",
"click .NB-story-comment-reply .NB-modal-submit-delete": "delete_social_comment_reply"
2012-06-02 16:33:44 -07:00
},
initialize: function(options) {
this.story = options.story;
if (!this.options.on_social_page) {
this.user = NEWSBLUR.assets.user_profiles.find(this.model.get('user_id'));
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() {
2012-08-09 23:50:49 -07:00
var comments = this.model.get('comments').replace(/\n+/g, '<br><br>');
2012-06-02 16:33:44 -07:00
var reshare_class = this.model.get('source_user_id') ? 'NB-story-comment-reshare' : '';
var has_likes = _.any(this.model.get('liking_users'));
var liked = _.contains(this.model.get('liking_users'), NEWSBLUR.Globals.user_id);
2016-12-16 13:31:03 -08:00
var profile_thumb = NEWSBLUR.Views.ProfileThumb.create(this.model.get('source_user_id'));
var $comment = $.make('div', { className: (this.options.friend_share ? "NB-story-comment-friend-share" : "") }, [
$.make('div', { className: 'NB-story-comment-author-avatar NB-user-avatar ' + reshare_class }, [
$.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' }, [
2016-12-16 13:31:03 -08:00
profile_thumb && profile_thumb.render().el
2012-06-02 16:33:44 -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'),
$.make('div', { className: 'NB-story-comment-reply-button' }, [
$.make('div', { className: 'NB-story-comment-reply-button-wrapper' }, [
2013-03-06 16:49:23 -08:00
(this.user.get('protected') && $.make('img', { src: NEWSBLUR.Globals.MEDIA_URL + 'img/icons/circular/g_icn_lock.png' })),
'reply'
])
]),
2013-02-20 12:03:06 -08:00
(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')
])),
(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()
])),
(!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' })
])),
$.make('div', { className: 'NB-story-comment-error' })
2012-06-02 16:33:44 -07:00
]),
2012-08-09 23:50:49 -07:00
$.make('div', { className: 'NB-story-comment-content' }, 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() {
if (!this.model.replies || !this.model.replies.length) return;
2012-06-02 16:33:44 -07:00
var user_id = NEWSBLUR.Globals.user_id;
var $replies = this.model.replies.map(_.bind(function(reply) {
if (!NEWSBLUR.assets.get_user(reply.get('user_id'))) return;
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;
},
render_liking_users: function() {
var $users = $.make('div', { className: 'NB-story-comment-likes-users' });
_.each(this.model.get('liking_users'), function(user_id) {
if (!NEWSBLUR.assets.get_user(user_id)) return;
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() {
2013-02-04 16:16:03 -08:00
if (!tipsy) return;
tipsy.enable();
tipsy.show();
});
$like.animate({
'opacity': 1
}, {
'duration': 850,
'queue': false,
'complete': function() {
2013-02-04 16:16:03 -08:00
if (tipsy && 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_save_dialog: function() {
this.story.story_save_view.toggle_feed_story_save_dialog();
},
2012-06-02 16:33:44 -07:00
toggle_feed_story_share_dialog: function() {
this.story.story_share_view.toggle_feed_story_share_dialog();
2012-06-02 16:33:44 -07:00
},
open_reply: function(options) {
options = options || {};
var current_user = NEWSBLUR.assets.user_profile;
2012-06-02 16:33:44 -07:00
if (this.user.get('protected') && this.options.public_comment) {
var $error = this.$('.NB-story-comment-error');
$error.text("You must be following " + this.user.get('username') + " to reply");
return;
}
var reply_comments = options.reply && options.reply.stripped_comments();
2012-06-02 16:33:44 -07:00
var $form = $.make('div', { className: 'NB-story-comment-reply NB-story-comment-reply-form' }, [
$.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')),
$.make('input', { type: 'text', className: 'NB-input NB-story-comment-reply-comments', value: reply_comments }),
$.make('div', { className: 'NB-modal-submit-button NB-modal-submit-green' }, options.is_editing ? 'Save' : 'Post'),
2013-02-26 12:37:13 -08:00
(options.is_editing && $.make('div', { className: 'NB-modal-submit-button NB-modal-submit-grey NB-modal-submit-delete' }, 'Delete'))
2012-06-02 16:33:44 -07:00
]);
this.remove_social_comment_reply_form();
2012-06-05 18:12:34 -07:00
if (options.is_editing && options.$reply) {
$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', 'return',
_.bind(this.save_social_comment_reply, this));
$('.NB-story-comment-reply-comments', $form).bind('keydown', 'ctrl+return',
_.bind(this.save_social_comment_reply, this));
$('.NB-story-comment-reply-comments', $form).bind('keydown', 'meta+return',
_.bind(this.save_social_comment_reply, this));
2012-06-02 16:33:44 -07:00
$('.NB-story-comment-reply-comments', $form).bind('keydown', 'esc', _.bind(function(e) {
e.preventDefault();
this.remove_social_comment_reply_form();
}, this));
$('input', $form).focus();
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-green", $form);
var $delete_button = $(".NB-modal-submit-delete", $form);
2012-06-02 16:33:44 -07:00
var comment_user_id = this.model.get('user_id');
var comment_reply = $('.NB-story-comment-reply-comments', $form).val();
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();
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;
}
$delete_button.hide();
2012-06-02 16:33:44 -07:00
$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,
reply_id,
2012-06-02 16:33:44 -07:00
_.bind(function(data) {
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) {
2014-07-17 10:11:06 -07:00
var message = data && data.message || "Sorry, this reply could not be posted. Probably Adblock.";
2012-06-02 16:33:44 -07:00
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);
if (NEWSBLUR.app.story_list) {
NEWSBLUR.app.story_list.fetch_story_locations_in_feed_view();
}
}, this));
},
delete_social_comment_reply: function() {
var $form = this.$('.NB-story-comment-reply-form');
var $submit = $(".NB-modal-submit-green", $form);
var $delete_button = $(".NB-modal-submit-delete", $form);
var comment_user_id = this.model.get('user_id');
var reply_id = $form.data('reply_id');
if ($submit.hasClass('NB-disabled') || $delete_button.hasClass('NB-disabled')) {
return;
}
$submit.addClass('NB-disabled');
$delete_button.addClass('NB-disabled').text('Deleting...');
NEWSBLUR.assets.delete_comment_reply(this.options.story.id,
this.options.story.get('story_feed_id'),
comment_user_id, reply_id,
_.bind(function(data) {
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();
}
}, this), _.bind(function(data) {
var message = data && data.message || "Sorry, this reply could not be deleted.";
var $error = $.make('div', { className: 'NB-error' }, message);
$submit.removeClass('NB-disabled').text('Post');
$delete_button.removeClass('NB-disabled').text('Delete');
$form.find('.NB-error').remove();
$form.append($error);
if (NEWSBLUR.app.story_list) {
NEWSBLUR.app.story_list.fetch_story_locations_in_feed_view();
}
2012-06-02 16:33:44 -07:00
}, this));
},
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
}
});