Vulcan/packages/telescope-comments/lib/client/templates/comment_submit.js

84 lines
2.7 KiB
JavaScript
Raw Normal View History

Template.comment_submit.helpers({
commentFields: function () {
return Comments.simpleSchema().getEditableFields(Meteor.user());
},
isLoggedIn: function () {
return !!Meteor.user();
}
});
2015-04-28 09:44:43 +09:00
AutoForm.hooks({
submitCommentForm: {
2014-06-27 19:42:33 +09:00
2015-04-28 09:44:43 +09:00
before: {
method: function(doc) {
2014-12-24 10:13:48 +09:00
var comment = doc;
2015-04-28 09:44:43 +09:00
this.template.$('button[type=submit]').addClass('loading');
this.template.$('input, textarea').not(":disabled").addClass("disabled").prop("disabled", true);
var parent = this.formAttributes.parentContext;
2014-12-24 10:13:48 +09:00
2015-04-28 09:44:43 +09:00
if (!!parent.comment) { // child comment
var parentComment = parent.comment;
comment.parentCommentId = parentComment._id;
comment.postId = parentComment.postId;
if(!parentComment.topLevelCommentId) { // root comment
comment.topLevelCommentId = parentComment._id;
} else { // nested comment
comment.topLevelCommentId = parentComment.topLevelCommentId;
}
2015-04-28 09:44:43 +09:00
} else { // root comment
var post = parent;
comment.postId = post._id;
}
2015-04-28 09:44:43 +09:00
// ------------------------------ Checks ------------------------------ //
2014-12-27 18:34:01 +09:00
2015-04-28 09:44:43 +09:00
if (!Meteor.user()) {
Messages.flash(i18n.t('you_must_be_logged_in'), 'error');
return false;
}
// ------------------------------ Callbacks ------------------------------ //
// run all comment submit client callbacks on properties object successively
comment = Telescope.callbacks.run("commentSubmitClient", comment);
return comment;
}
},
2015-04-28 09:44:43 +09:00
onSuccess: function(operation, comment) {
this.template.$('button[type=submit]').removeClass('loading');
this.template.$('.disabled').removeClass("disabled").prop("disabled", false);
var post = Posts.findOne(comment.postId);
2015-04-28 09:44:43 +09:00
Events.track("new comment", {'commentId': comment._id});
FlowRouter.go('postPage', {_id: comment.postId, slug: post.slug});
2015-04-28 09:44:43 +09:00
if (comment.status === Posts.config.STATUS_PENDING) {
Messages.flash(i18n.t('thanks_your_post_is_awaiting_approval'), 'success');
}
},
onError: function(operation, error) {
this.template.$('button[type=submit]').removeClass('loading');
this.template.$('.disabled').removeClass("disabled").prop("disabled", false);
2015-04-28 09:44:43 +09:00
Messages.flash(error.message.split('|')[0], 'error'); // workaround because error.details returns undefined
Messages.clearSeen();
}
2015-04-28 09:44:43 +09:00
}
});
Template.comment_submit.onRendered(function() {
var self = this;
this.$("textarea").keydown(function (e) {
2015-04-12 21:19:30 +09:00
if(((e.metaKey || e.ctrlKey) && e.keyCode == 13) || (e.ctrlKey && e.keyCode == 13)){
self.$('#submitCommentForm').submit();
2015-04-12 21:19:30 +09:00
}
});
});