<commit_before>import { showSelector } from "discourse/lib/emoji/emoji-toolbar";
import userSearch from "discourse/lib/user-search";

export default Ember.Component.extend({
  userSearch: userSearch,
  classNames: ['babble-post-composer'],

  _init: function() {
    this.set('placeholder', Discourse.SiteSettings.babble_placeholder || I18n.t('babble.placeholder'))
  }.on('init'),

  _didInsertElement: function() {
    const self = this
    self.$('textarea').autocomplete({
      template: self.container.lookup('template:emoji-selector-autocomplete.raw'),
      key: ":",

      transformComplete(v) {
        if (!v.code) { return }
        return `${v.code}:`
      },

      dataSource(term) {
        return new Ember.RSVP.Promise(resolve => {
          term = term.toLowerCase()
          var options = (term === "" && ['smile', 'smiley', 'wink', 'sunny', 'blush']) ||
                        Discourse.Emoji.translations[`:${term}`] ||
                        Discourse.Emoji.search(term, {maxResults: 5})
          return resolve(options)
        }).then(list => list.map(code => {
          return {code, src: Discourse.Emoji.urlFor(code)};
        }))
      }
    })

    self.$('textarea').autocomplete({
      template: self.container.lookup('template:user-selector-autocomplete.raw'),
      key: '@',
      dataSource(term) {
        return self.userSearch({
          term: term,
          topicId: self.get('topic.id'),
          includeGroups: true,
          exclude: [Discourse.User.current().get('username')]
        })
      },
      transformComplete(v) {
        return v.username || v.name
      }
    })
  }.on('didInsertElement'),

  keyDown: function(event) {
    this.set('showError', false)
    if (event.keyCode == 13 && !(event.ctrlKey || event.altKey || event.shiftKey)) {
      if (!this.get('submitDisabled')) { // ignore if submit is disabled
        this._actions.submit(this) // submit on enter
      }
      return false
    }
  },

  textValidation: function() {
    var validation = { ok: true }
    if (this.get('processing') || Ember.isEmpty(this.get('text'))) {
      var validation = { failed: true }
    }

    return Discourse.InputValidation.create(validation)
  }.property('text', 'processing'),

  submitDisabled: function() {
    if (this.get('textValidation.failed')) return true
  }.property('textValidation'),

  actions: {
    selectEmoji: function() {
      var self = this
      var closeMenuPanelHandler = _.find($._data($('html')[0], 'events')['click'], function(e) {
        return e.namespace == 'close-menu-panel'
      }) // sorry mom.

      $('html').off('click.close-menu-panel')
      $('.emoji-modal-wrapper').on('click', function() {
        $('html').on('click.close-menu-panel', closeMenuPanelHandler.handler)
      })

      showSelector({
        container: this.container,
        onSelect: function(emoji) {
          self.set('text', (self.get('text') || '').trimRight() + ' :' + emoji + ':')

          $('.emoji-modal, .emoji-modal-wrapper').remove()
          $('.babble-post-composer textarea').focus()
          $('html').on('click.close-menu-panel', closeMenuPanelHandler.handler)
          return false
        }
      })
    },

    submit: function(context) {
      const self = context || this;
      const text = self.get('text').trim()
      self.set('text', '')

      if (text === '') {
        self.set('errorMessage', 'babble.error_message')
      } else {
        self.set('processing', true)
        Discourse.Babble.stagePost(text)
        Discourse.ajax("/babble/topics/" + self.get('topic.id') + "/post", {
          type: 'POST',
          data: { raw: text }
        })
        .then(Discourse.Babble.handleNewPost, function() {
          Discourse.Babble.clearStagedPost()
          self.set('errorMessage', 'babble.failed_post')
        })
        .finally(function() { self.set('processing', false) });
      }
    }
  }

});
<commit_msg>Closing emoji box should make chat box closeable<commit_after>import { showSelector } from "discourse/lib/emoji/emoji-toolbar";
import userSearch from "discourse/lib/user-search";

export default Ember.Component.extend({
  userSearch: userSearch,
  classNames: ['babble-post-composer'],

  _init: function() {
    this.set('placeholder', Discourse.SiteSettings.babble_placeholder || I18n.t('babble.placeholder'))
  }.on('init'),

  _didInsertElement: function() {
    const self = this
    self.$('textarea').autocomplete({
      template: self.container.lookup('template:emoji-selector-autocomplete.raw'),
      key: ":",

      transformComplete(v) {
        if (!v.code) { return }
        return `${v.code}:`
      },

      dataSource(term) {
        return new Ember.RSVP.Promise(resolve => {
          term = term.toLowerCase()
          var options = (term === "" && ['smile', 'smiley', 'wink', 'sunny', 'blush']) ||
                        Discourse.Emoji.translations[`:${term}`] ||
                        Discourse.Emoji.search(term, {maxResults: 5})
          return resolve(options)
        }).then(list => list.map(code => {
          return {code, src: Discourse.Emoji.urlFor(code)};
        }))
      }
    })

    self.$('textarea').autocomplete({
      template: self.container.lookup('template:user-selector-autocomplete.raw'),
      key: '@',
      dataSource(term) {
        return self.userSearch({
          term: term,
          topicId: self.get('topic.id'),
          includeGroups: true,
          exclude: [Discourse.User.current().get('username')]
        })
      },
      transformComplete(v) {
        return v.username || v.name
      }
    })
  }.on('didInsertElement'),

  keyDown: function(event) {
    this.set('showError', false)
    if (event.keyCode == 13 && !(event.ctrlKey || event.altKey || event.shiftKey)) {
      if (!this.get('submitDisabled')) { // ignore if submit is disabled
        this._actions.submit(this) // submit on enter
      }
      return false
    }
  },

  textValidation: function() {
    var validation = { ok: true }
    if (this.get('processing') || Ember.isEmpty(this.get('text'))) {
      var validation = { failed: true }
    }

    return Discourse.InputValidation.create(validation)
  }.property('text', 'processing'),

  submitDisabled: function() {
    if (this.get('textValidation.failed')) return true
  }.property('textValidation'),

  actions: {
    selectEmoji: function() {
      var self = this
      var closeMenuPanelHandler = _.find($._data($('html')[0], 'events')['click'], function(e) {
        return e.namespace == 'close-menu-panel'
      }) // sorry mom.

      $('html').off('click.close-menu-panel')

      showSelector({
        container: this.container,
        onSelect: function(emoji) {
          self.set('text', (self.get('text') || '').trimRight() + ' :' + emoji + ':')

          $('.emoji-modal, .emoji-modal-wrapper').remove()
          $('.babble-post-composer textarea').focus()
          $('html').on('click.close-menu-panel', closeMenuPanelHandler.handler)
          return false
        }
      })

      $('.emoji-modal-wrapper').on('click', function(event) {
        $('html').on('click.close-menu-panel', closeMenuPanelHandler.handler)
        // The click responsible for closing emoji box doesn't count
        // as a click to close chat box.
        event.stopPropagation()
      })
    },

    submit: function(context) {
      const self = context || this;
      const text = self.get('text').trim()
      self.set('text', '')

      if (text === '') {
        self.set('errorMessage', 'babble.error_message')
      } else {
        self.set('processing', true)
        Discourse.Babble.stagePost(text)
        Discourse.ajax("/babble/topics/" + self.get('topic.id') + "/post", {
          type: 'POST',
          data: { raw: text }
        })
        .then(Discourse.Babble.handleNewPost, function() {
          Discourse.Babble.clearStagedPost()
          self.set('errorMessage', 'babble.failed_post')
        })
        .finally(function() { self.set('processing', false) });
      }
    }
  }

});

{'fraction_non_alphanumeric': 0.17148171109733415, 'fraction_numerical': 0.0014879107253564787, 'mean_word_length': 2.4707401032702236, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 9, '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': '4440254', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2410, 'n_words': 426}