Submit comment when press Crtl/Cmd+Enter

This commit is contained in:
Delgermurun 2015-04-11 14:35:51 +09:00
parent a756aeb161
commit 5748f50d66
2 changed files with 86 additions and 54 deletions

View file

@ -1,21 +1,37 @@
var editComment = function(instance) {
var comment = instance.data.comment;
var content = instance.$('#body').val();
if(!Meteor.user())
throw i18n.t('you_must_be_logged_in');
Comments.update(comment._id, {
$set: {
body: content
}
});
trackEvent("edit comment", {'postId': comment.postId, 'commentId': comment._id});
Router.go('post_page_comment', {_id: comment.postId, commentId: comment._id});
};
Template[getTemplate('comment_edit')].onRendered(function() {
var self = this;
this.$('#body').bind('keypress', 'ctrl+return', function() {
editComment(self);
});
this.$('#body').bind('keypress', 'meta+return', function() {
editComment(self);
});
});
Template[getTemplate('comment_edit')].events({
'click input[type=submit]': function(e, instance){
var comment = this;
var content = instance.$('#body').val();
e.preventDefault();
if(!Meteor.user())
throw i18n.t('you_must_be_logged_in');
Comments.update(comment._id, {
$set: {
body: content
}
});
trackEvent("edit comment", {'postId': comment.postId, 'commentId': comment._id});
Router.go('post_page_comment', {_id: comment.postId, commentId: comment._id});
editComment(instance);
},
'click .delete-link': function(e){
var comment = this;

View file

@ -1,3 +1,57 @@
var submitComment = function(instance) {
var data = instance.data;
instance.$('form').addClass('disabled');
Messages.clearSeen();
var comment = {};
var $commentForm = instance.$('#comment');
var $submitButton = instance.$('.btn-submit');
var body = $commentForm.val();
// now that the form is latency compensated, we don't actually need to show this
// $commentForm.prop('disabled', true);
// $submitButton.addClass('loading');
// context can be either post, or comment property
var postId = !!data._id ? data._id: data.comment.postId;
var post = Posts.findOne(postId);
comment = {
postId: post._id,
body: body
};
// child comment
if (getCurrentTemplate() === 'comment_reply') {
comment.parentCommentId = data.comment._id;
}
Meteor.call('submitComment', comment, function(error, newComment){
// $commentForm.prop('disabled', false);
// $submitButton.removeClass('loading');
if(error){
console.log(error);
Messages.flash(error.reason, "error");
}else{
trackEvent("newComment", newComment);
$commentForm.val('');
}
});
};
Template[getTemplate('comment_form')].onRendered(function() {
var self = this;
this.$('#comment').bind('keypress', 'ctrl+return', function() {
submitComment(self);
});
this.$('#comment').bind('keypress', 'meta+return', function() {
submitComment(self);
});
});
Template[getTemplate('comment_form')].helpers({
reason: function () {
return !!Meteor.user() ? i18n.t('sorry_you_do_not_have_the_rights_to_comments'): i18n.t('please_log_in_to_comment');
@ -6,45 +60,7 @@ Template[getTemplate('comment_form')].helpers({
Template[getTemplate('comment_form')].events({
'submit form': function(e, instance){
e.preventDefault();
$(e.target).addClass('disabled');
Messages.clearSeen();
var comment = {};
var $commentForm = instance.$('#comment');
var $submitButton = instance.$('.btn-submit');
var body = $commentForm.val();
// now that the form is latency compensated, we don't actually need to show this
// $commentForm.prop('disabled', true);
// $submitButton.addClass('loading');
// context can be either post, or comment property
var postId = !!this._id ? this._id: this.comment.postId;
var post = Posts.findOne(postId);
comment = {
postId: post._id,
body: body
}
// child comment
if (getCurrentTemplate() == 'comment_reply') {
comment.parentCommentId = this.comment._id;
}
Meteor.call('submitComment', comment, function(error, newComment){
// $commentForm.prop('disabled', false);
// $submitButton.removeClass('loading');
if(error){
console.log(error);
Messages.flash(error.reason, "error");
}else{
trackEvent("newComment", newComment);
$commentForm.val('');
}
});
submitComment(instance);
}
});