<commit_before>import Ember from 'ember';
import layout from './template';
import Registry from '../../mixins/registry';
import ControlState from '../../mixins/control-state';
import KeyBindings from '../../mixins/key-bindings';

export default Ember.Component.extend(Registry, ControlState, KeyBindings, {
  layout: layout,

  tagName: 'deluge-dropdown-menu',

  classNames: ['deluge-dropdown-menu'],

  attributeBindings: [
    'autocomplete:aria-autocomplete',
  ],

  autocomplete: 'none',

  /**
   * Indicates that we will handle the default action of a child element
   *
   * @type {Boolean}
   */
  acceptsChildActions: true,

  /**
   * Name of the action which handles the item activate action
   *
   * @type {String}
   */
  childActionName: 'itemSelected',

  keyBindings: {
    'esc': 'close',
  },

  /**
   * Indicates the currently selected items, by value.
   *
   * @readOnly
   * @type {Array}
   */
  selection: Ember.A(),

  open() {
    const menuButton = this.get('button');
    if (!menuButton) { return; }
    menuButton.send('open');
  },

  close() {
    const menuButton = this.get('button');
    if (!menuButton) { return; }
    menuButton.send('close');
  },

  focusOut() {
    const button = this.get('button');
    if (!button) { return; }
    Ember.run.next(this, function(){
      if (button.$().has(document.activeElement).length === 0) {
        button.send("close");
      }
    });
  },

  actions: {
    itemSelected(values) {
      console.log('[dropdown-menu] item selected', values);

      this.sendAction('selection-changed', values);

      Ember.run.later(this, this.close, 125);
    },

    registerButton(buttonComponent) {
      this.set('button', buttonComponent);

      console.log('>',this.get('selection'));<commit_msg>Use document click to handle closing outside<commit_after>import Ember from 'ember';
import layout from './template';
import Registry from '../../mixins/registry';
import ControlState from '../../mixins/control-state';
import KeyBindings from '../../mixins/key-bindings';

export default Ember.Component.extend(Registry, ControlState, KeyBindings, {
  layout: layout,

  tagName: 'deluge-dropdown-menu',

  classNames: ['deluge-dropdown-menu'],

  attributeBindings: [
    'autocomplete:aria-autocomplete',
  ],

  autocomplete: 'none',

  /**
   * Indicates that we will handle the default action of a child element
   *
   * @type {Boolean}
   */
  acceptsChildActions: true,

  /**
   * Name of the action which handles the item activate action
   *
   * @type {String}
   */
  childActionName: 'itemSelected',

  keyBindings: {
    'esc': 'close',
  },

  didInsertElement() {
    this.addGlobalEventHandlers();
  },

  willDestroyElement() {
    this.removeGlobalEventHandlers();
  },

  addGlobalEventHandlers() {
    document.addEventListener('click', this.handleRootClick.bind(this));
  },

  removeGlobalEventHandlers() {
    document.removeEventListener('click', this.handleRootClick.bind(this));
  },

  handleRootClick(event) {
    if (!this.element.contains(event.target)) {
      this.close();
    }
  },

  /**
   * Indicates the currently selected items, by value.
   *
   * @readOnly
   * @type {Array}
   */
  selection: Ember.A(),

  open() {
    const menuButton = this.get('button');
    if (!menuButton) { return; }
    menuButton.send('open');
  },

  close() {
    const menuButton = this.get('button');
    if (!menuButton) { return; }
    menuButton.send('close');
  },

  // focusOut() {
  //   const button = this.get('button');
  //   if (!button) { return; }
  //   Ember.run.next(this, function(){
  //     if (button.$().has(document.activeElement).length === 0) {
  //       button.send("close");
  //     }
  //   });
  // },

  actions: {
    itemSelected(values) {
      console.log('[dropdown-menu] item selected', values);

      this.sendAction('selection-changed', values);

      Ember.run.later(this, this.close, 125);
    },

    registerButton(buttonComponent) {
      this.set('button', buttonComponent);

      console.log('>',this.get('selection'));
{'fraction_non_alphanumeric': 0.1642927308447937, 'fraction_numerical': 0.0019646365422396855, 'mean_word_length': 3.068931068931069, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2006881', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1243, 'n_words': 267}