<commit_before>/**
 * Add a comment, reply if the user has permission to do so.
 *
 * @param {Object} event
 * @param {String} type
 * @param {String} selector
 * @param {Function} callback
 */
const addComment = function (event, type, selector, callback) {
  const container = $(event.target).parent();

  if ("submit" === event.type) {
    event.preventDefault();
    executeUserAction(event, type, function addCommentCallback(anonymousData) {
      var textarea = container.find(selector),
        value = textarea.val().trim();

      callback(textarea, value, anonymousData);
    });
  }
};

/**
 * Run user actions, such as adding comments, replying etc.
 *
 * @param {Object} event
 * @param {String} type
 * @param {Function} callback
 */
const executeUserAction = function (event, type, callback) {
  if (userService.isAnonymous()) {
    const container = $(event.target).parent(),
      anonUserData = {
        username: (container.find('.anon-username').val() || ''),
        email: (container.find('.anon-email').val() || '')
      };

    userService.updateAnonymousUser(anonUserData, callback);
  } else {
    Comments.ui.callIfLoggedIn(type, callback);
  }
};

/**
 * Return custom templates with helpers from commentsBox
 *
 * @param {String} templateName
 * @param {Object} templateScope
 *
 * @returns {Object}
 */
const getCustomTemplate = function (templateName, templateScope) {
  if (_.isString(templateScope[templateName])) {
    Template[templateScope[templateName]].inheritsHelpersFrom("commentsBox");
    return Template[templateScope[templateName]];
  }
};

const loadMoreDocuments = (function () {
  const limit = Comments.ui.config().limit;
  let currentOffset = limit,
    handles = [];

  Template.commentsBox.onDestroyed(() => {
    _.forEach(handles, handle => handle.stop());
    currentOffset = limit;
    handles = [];
  });

  /**
   * @param {Object} tplInstance
   */
  return function (tplInstance) {
    const loadMoreCount = Comments.ui.config().loadMoreCount;

    handles.push(
      Meteor.subscribe('comments/reference', tplInstance.data.id, loadMoreCount, currentOffset)
    );

    currentOffset += loadMoreCount;
  }
})();

Comments.session.set('content', {});

Template.commentsBox.onCreated(function () {
  Avatar.setOptions({
    defaultImageUrl: Comments.ui.config().defaultAvatar
  });

  this.subscribe('comments/reference', this.data.id, Comments.ui.config().limit);

  this.autorun(() => {
    Meteor.call('comments/count', this.data.id, (err, count) => {
      Comments.session.set(this.data.id + '_count', count);
    });

    if (userService.isAnonymous()) {
      const userData = userService.getUserData();

      Comments.session.set('loginAction', '');

      userData._id && userData.salt && this.subscribe('comments/anonymous', userData);
    }
  });

  this.autorun(() => {
    if(Meteor.userId()) {
      Comments.session.set('loginAction', '');
    }
  })
});

Template.commentsBox.helpers(_.extend(defaultCommentHelpers, {
  comment() {
    return Comments.get(this.id);
  },
  customBoxTemplate() {
    return getCustomTemplate('boxTemplate', this);
  },
  customLoadingTemplate() {
    return getCustomTemplate('loadingTemplate', this);
  },
  commentsBoxTitle() {
    let title = defaultCommentHelpers.take({
      hash: {
        key: 'title',
        'default': 'Comments'
      }
    });

    const data = Template.instance().data;

    if (data && data.title) {
      title =  `${title} for ${data.title}`;
    }

    return title;
  }
}));

Template.commentsBox.events({
  'keydown .create-comment, keydown .create-reply': _.debounce((e) => {
    if (e.originalEvent instanceof KeyboardEvent && e.keyCode === 13 && e.ctrlKey) {
      e.preventDefault();
      $(e.target).closest('form').find('.submit.button').click();
    }
  }, 50),
  'submit .comment-form' : function (e) {
    const eventScope = this;

    addComment(e, 'add comments', '.create-comment', function (textarea, trimmedValue, anonData) {
      function addComment() {
        Comments.add(eventScope.id, trimmedValue);
        textarea.val('');
      }

      if (trimmedValue) {
        if (anonData) {
          userService.updateAnonymousUser(anonData, () => addComment());
        } else {<commit_msg>Do not submit form if cursor is in anon input fields<commit_after>/**
 * Add a comment, reply if the user has permission to do so.
 *
 * @param {Object} event
 * @param {String} type
 * @param {String} selector
 * @param {Function} callback
 */
const addComment = function (event, type, selector, callback) {
  const container = $(event.target).parent();

  if ("submit" === event.type) {
    event.preventDefault();
    executeUserAction(event, type, function addCommentCallback(anonymousData) {
      var textarea = container.find(selector),
        value = textarea.val().trim();

      callback(textarea, value, anonymousData);
    });
  }
};

/**
 * Run user actions, such as adding comments, replying etc.
 *
 * @param {Object} event
 * @param {String} type
 * @param {Function} callback
 */
const executeUserAction = function (event, type, callback) {
  if (userService.isAnonymous()) {
    const container = $(event.target).parent(),
      anonUserData = {
        username: (container.find('.anon-username').val() || ''),
        email: (container.find('.anon-email').val() || '')
      };

    userService.updateAnonymousUser(anonUserData, callback);
  } else {
    Comments.ui.callIfLoggedIn(type, callback);
  }
};

/**
 * Return custom templates with helpers from commentsBox
 *
 * @param {String} templateName
 * @param {Object} templateScope
 *
 * @returns {Object}
 */
const getCustomTemplate = function (templateName, templateScope) {
  if (_.isString(templateScope[templateName])) {
    Template[templateScope[templateName]].inheritsHelpersFrom("commentsBox");
    return Template[templateScope[templateName]];
  }
};

const loadMoreDocuments = (function () {
  const limit = Comments.ui.config().limit;
  let currentOffset = limit,
    handles = [];

  Template.commentsBox.onDestroyed(() => {
    _.forEach(handles, handle => handle.stop());
    currentOffset = limit;
    handles = [];
  });

  /**
   * @param {Object} tplInstance
   */
  return function (tplInstance) {
    const loadMoreCount = Comments.ui.config().loadMoreCount;

    handles.push(
      Meteor.subscribe('comments/reference', tplInstance.data.id, loadMoreCount, currentOffset)
    );

    currentOffset += loadMoreCount;
  }
})();

Comments.session.set('content', {});

Template.commentsBox.onCreated(function () {
  Avatar.setOptions({
    defaultImageUrl: Comments.ui.config().defaultAvatar
  });

  this.subscribe('comments/reference', this.data.id, Comments.ui.config().limit);

  this.autorun(() => {
    Meteor.call('comments/count', this.data.id, (err, count) => {
      Comments.session.set(this.data.id + '_count', count);
    });

    if (userService.isAnonymous()) {
      const userData = userService.getUserData();

      Comments.session.set('loginAction', '');

      userData._id && userData.salt && this.subscribe('comments/anonymous', userData);
    }
  });

  this.autorun(() => {
    if(Meteor.userId()) {
      Comments.session.set('loginAction', '');
    }
  })
});

Template.commentsBox.helpers(_.extend(defaultCommentHelpers, {
  comment() {
    return Comments.get(this.id);
  },
  customBoxTemplate() {
    return getCustomTemplate('boxTemplate', this);
  },
  customLoadingTemplate() {
    return getCustomTemplate('loadingTemplate', this);
  },
  commentsBoxTitle() {
    let title = defaultCommentHelpers.take({
      hash: {
        key: 'title',
        'default': 'Comments'
      }
    });

    const data = Template.instance().data;

    if (data && data.title) {
      title =  `${title} for ${data.title}`;
    }

    return title;
  }
}));

Template.commentsBox.events({
  'keydown .create-comment, keydown .create-reply': _.debounce((e) => {
    if (e.originalEvent instanceof KeyboardEvent && e.keyCode === 13 && e.ctrlKey) {
      e.preventDefault();
      $(e.target).closest('form').find('.submit.button').click();
    }
  }, 50),
  'keydown .anon-email, keydown .anon-username': (e) => {
    if (e.keyCode === 13) {
      e.preventDefault();
    }
  },
  'submit .comment-form' : function (e) {
    const eventScope = this;

    addComment(e, 'add comments', '.create-comment', function (textarea, trimmedValue, anonData) {
      function addComment() {
        Comments.add(eventScope.id, trimmedValue);
        textarea.val('');
      }

      if (trimmedValue) {
        if (anonData) {
          userService.updateAnonymousUser(anonData, () => addComment());
        } else {
{'fraction_non_alphanumeric': 0.16093677895708353, 'fraction_numerical': 0.0011536686663590216, 'mean_word_length': 3.391590678824721, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '119970', 'n_tokens_mistral': 2638, 'n_tokens_neox': 2588, 'n_words': 525}