<commit_before>import Ember from 'ember';
import SelectPicker from './select-picker';
import KeyboardShortcutsMixin from 'ember-keyboard-shortcuts/mixins/component';

function makeKeyboardAction(fn) {
  return function() {
    if (!this.get('showDropdown')) {
      // ignore keyboard input on components that are not *in focus*
      return true;
    }
    fn.apply(this, arguments);
    return false;
  };
}

var KeyboardSelectPickerComponent = SelectPicker.extend(
  KeyboardShortcutsMixin, {

  layoutName: 'components/select-picker',

  nativeMobile: true,

  activeCursor: null,

  classNames: ['select-picker', 'keyboard-select-picker'],

  groupedContentList: Ember.computed(
    'groupedContentListWithoutActive', 'activeIndex',
    function() {
      var activeIndex = this.get('activeIndex');
      var result = Ember.A(this.get('groupedContentListWithoutActive'));
      result.forEach(function(item, index) {
        item.set('active', index === activeIndex);
      });
      return result;
    }
  ),

  activeIndex: Ember.computed(
    'activeCursor', 'contentList.length',
    function() {
      var cursor = this.get('activeCursor');
      if (Ember.isNone(cursor)) {
        return null;
      }
      var len = this.get('contentList.length');
      return (cursor % len + len) % len;
    }
  ),

  activeItem: Ember.computed(
    'activeIndex', 'contentList.[]',
    function() {
      return this.get('contentList').objectAt(this.get('activeIndex'));
    }
  ),

  keyboardShortcuts: {
    'enter': 'selectActiveItem',
    'up': 'activePrev',
    'down': 'activeNext',
    'shift+tab': 'activePrev',
    'tab': 'activeNext',
    'esc': 'closeDropdown'
  },

  actions: {
    activeNext: makeKeyboardAction(function() {
      if (Ember.isNone(this.get('activeCursor'))) {
        this.set('activeCursor', 0);
      } else {
        this.incrementProperty('activeCursor');
      }
    }),

    activePrev: makeKeyboardAction(function() {
      if (Ember.isNone(this.get('activeCursor'))) {
        this.set('activeCursor', -1);
      } else {
        this.decrementProperty('activeCursor');
      }
    }),

    selectActiveItem: makeKeyboardAction(function() {
      this.send('selectItem', this.get('activeItem'));
    }),
  }
});

export default KeyboardSelectPickerComponent;<commit_msg>Prevent an undefined selection<commit_after>import Ember from 'ember';
import SelectPicker from './select-picker';
import KeyboardShortcutsMixin from 'ember-keyboard-shortcuts/mixins/component';

function makeKeyboardAction(fn) {
  return function() {
    if (!this.get('showDropdown')) {
      // ignore keyboard input on components that are not *in focus*
      return true;
    }
    fn.apply(this, arguments);
    return false;
  };
}

var KeyboardSelectPickerComponent = SelectPicker.extend(
  KeyboardShortcutsMixin, {

  layoutName: 'components/select-picker',

  nativeMobile: true,

  activeCursor: null,

  classNames: ['select-picker', 'keyboard-select-picker'],

  groupedContentList: Ember.computed(
    'groupedContentListWithoutActive', 'activeIndex',
    function() {
      var activeIndex = this.get('activeIndex');
      var result = Ember.A(this.get('groupedContentListWithoutActive'));
      result.forEach(function(item, index) {
        item.set('active', index === activeIndex);
      });
      return result;
    }
  ),

  activeIndex: Ember.computed(
    'activeCursor', 'contentList.length',
    function() {
      var cursor = this.get('activeCursor');
      if (Ember.isNone(cursor)) {
        return null;
      }
      var len = this.get('contentList.length');
      return (cursor % len + len) % len;
    }
  ),

  activeItem: Ember.computed(
    'activeIndex', 'contentList.[]',
    function() {
      return this.get('contentList').objectAt(this.get('activeIndex'));
    }
  ),

  keyboardShortcuts: {
    'enter': 'selectActiveItem',
    'up': 'activePrev',
    'down': 'activeNext',
    'shift+tab': 'activePrev',
    'tab': 'activeNext',
    'esc': 'closeDropdown'
  },

  actions: {
    activeNext: makeKeyboardAction(function() {
      if (Ember.isNone(this.get('activeCursor'))) {
        this.set('activeCursor', 0);
      } else {
        this.incrementProperty('activeCursor');
      }
    }),

    activePrev: makeKeyboardAction(function() {
      if (Ember.isNone(this.get('activeCursor'))) {
        this.set('activeCursor', -1);
      } else {
        this.decrementProperty('activeCursor');
      }
    }),

    selectActiveItem: makeKeyboardAction(function() {
      var item = this.get('activeItem');
      if (Ember.isPresent(item)) {
        this.send('selectItem', item);
      }
    }),
  }
});

export default KeyboardSelectPickerComponent;
{'fraction_non_alphanumeric': 0.14777801403359558, 'fraction_numerical': 0.0008505209440782479, 'mean_word_length': 3.24165915238954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3506322', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1418, 'n_words': 265}