<commit_before>import Ember from 'ember';

export default Ember.Component.extend({
  tagName: 'select',
  attributeBindings: ['prompt:data-placeholder', 'multiple'],
  classNameBindings: ['isRtl:chosen-rtl'],
  classNames: ['chosen-select'],
  prompt: null,
  isRtl: false,
  multiple: false,
  disableSearchThreshold: null,
  noResultsText: null,
  maxSelectedOptions: null,
  value: null,
  width: '100%',
  selectionDidChange: null,
  updateChosen: false,


  _options: function() {
    var options = {};

    if(!Ember.isNone(this.get('disableSearchThreshold'))) { options['disable_search_threshold'] = this.get('disableSearchThreshold'); }
    if(!Ember.isNone(this.get('noResultsText'))) { options['no_results_text'] = this.get('noResultsText'); }
    if(!Ember.isNone(this.get('maxSelectedOptions'))) { options['max_selected_options'] = this.get('maxSelectedOptions'); }
    if(!Ember.isNone(this.get('width'))) { options['width'] = this.get('width'); }

    return options;
  }.property('prompt', 'isRtl', 'multiple', 'disableSearchThreshold',
             'noResultsText', 'maxSelectedOptions', 'width'),
  _setupChosen: function() {
    var _this = this,
      options = _this.get('_options'),
      isMultiple = _this.get('multiple'),
      currentValue = _this.get('value'),
      selectedValue;

    if(isMultiple) {
      currentValue = Ember.makeArray(currentValue);
      _this.set('value', currentValue);
    } else {
      // If we're going from multiple -> single select, make the selected
      // value the first item in the selected value array, if an item exists
      if(Ember.isArray(currentValue) && currentValue.length > 0) {
        currentValue = currentValue[0];
        _this.set('value', currentValue);
      }
    }

    _this.$().chosen(options)
    .on('change', function (e, params) {
      var index;

      if(isMultiple) {
        currentValue = _this.get('value');

        if(params.selected) {
          currentValue.pushObject(params.selected);
        } else {
          index = currentValue.indexOf(params.deselected);

          if(index !== -1) {
            currentValue.removeAt(index);
          }
        }

        selectedValue = currentValue;
      } else {
        selectedValue = params.selected;
      }

      _this.set('value', selectedValue);
      _this.sendAction('selectionDidChange', selectedValue);
    }).on('chosen:maxselected', function(e, chosen) {
      _this.sendAction('chosenMaxSelected', e, chosen);
    });
      _this.$().val(currentValue).trigger('chosen:updated')
  }.observes('_options'),
  didRender: function() {
    this._super();
    this.cleanUp();
    this._setupChosen();
  },

  cleanUp: function(){
    this.$().chosen('destroy')
  }<commit_msg>make the clean up of chosen events more throughout<commit_after>import Ember from 'ember';

export default Ember.Component.extend({
  tagName: 'select',
  attributeBindings: ['prompt:data-placeholder', 'multiple'],
  classNameBindings: ['isRtl:chosen-rtl'],
  classNames: ['chosen-select'],
  prompt: null,
  isRtl: false,
  multiple: false,
  disableSearchThreshold: null,
  noResultsText: null,
  maxSelectedOptions: null,
  value: null,
  width: '100%',
  selectionDidChange: null,
  updateChosen: false,


  _options: function() {
    var options = {};

    if(!Ember.isNone(this.get('disableSearchThreshold'))) { options['disable_search_threshold'] = this.get('disableSearchThreshold'); }
    if(!Ember.isNone(this.get('noResultsText'))) { options['no_results_text'] = this.get('noResultsText'); }
    if(!Ember.isNone(this.get('maxSelectedOptions'))) { options['max_selected_options'] = this.get('maxSelectedOptions'); }
    if(!Ember.isNone(this.get('width'))) { options['width'] = this.get('width'); }

    return options;
  }.property('prompt', 'isRtl', 'multiple', 'disableSearchThreshold',
             'noResultsText', 'maxSelectedOptions', 'width'),
  _setupChosen: function() {
    var _this = this,
      options = _this.get('_options'),
      isMultiple = _this.get('multiple'),
      currentValue = _this.get('value'),
      selectedValue;

    if(isMultiple) {
      currentValue = Ember.makeArray(currentValue);
      _this.set('value', currentValue);
    } else {
      // If we're going from multiple -> single select, make the selected
      // value the first item in the selected value array, if an item exists
      if(Ember.isArray(currentValue) && currentValue.length > 0) {
        currentValue = currentValue[0];
        _this.set('value', currentValue);
      }
    }

    _this.$().chosen(options)
    .on('change', function (e, params) {
      var index;

      if(isMultiple) {
        currentValue = _this.get('value');

        if(params.selected) {
          currentValue.pushObject(params.selected);
        } else {
          index = currentValue.indexOf(params.deselected);

          if(index !== -1) {
            currentValue.removeAt(index);
          }
        }

        selectedValue = currentValue;
      } else {
        selectedValue = params.selected;
      }

      _this.set('value', selectedValue);
      _this.sendAction('selectionDidChange', selectedValue);
    }).on('chosen:maxselected', function(e, chosen) {
      _this.sendAction('chosenMaxSelected', e, chosen);
    });
      _this.$().val(currentValue).trigger('chosen:updated')
  }.observes('_options'),
  didRender: function() {
    this._super();
    this.cleanUp();
    this._setupChosen();
  },

  cleanUp: function(){
    this.$().off('change');
    this.$().off('chosen:maxselected');
    this.$().chosen('destroy')
  }
{'fraction_non_alphanumeric': 0.1559863579249686, 'fraction_numerical': 0.002154011847065159, 'mean_word_length': 3.3633516053249806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3606003', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1659, 'n_words': 314}