<commit_before>
declare var _;

class SelectOption extends Gnd.Model
{}

/**
  This class is mainly used to break a circular dependency that affects the
  retain/release mechanism to work properly.
*/

class DropDownProxy extends Gnd.Base
{
  private dropdown: DropDown;
  
  constructor(dropdown: DropDown){
    super();
    
    this.dropdown = dropdown;
    
    this.bind('selectedId', dropdown);
    this.bind('selectedName', dropdown);
  }
  
  selectHandler(el)
  {
    var item = el['gnd-obj'];
    if(!item.disabled) {
      this.dropdown.selectItem(item);
      this.dropdown.deactivate();
    }
  }
}

// SelectOption.bucket()
// SelectOption.schema()
// SelectOption.decorate('selectOptions', schema)

class DropDown extends Gnd.View {
  private selectingId: string;
  private selectedId: string;
  private selectedItem: Gnd.Model;
  private viewModel: Gnd.ViewModel;
  private selectOptions: Gnd.Collection;
  private opts;
  private dropdownProxy: DropDownProxy;

  constructor(collection: Gnd.Collection, opts)
  {
    super({
      templateUrl: 'gnd/dropdown/dropdown.html.tmpl',
      templateEngine: _.template
    });
      
    this.opts = opts = _.defaults(opts || {}, {
      standard: true,
      nameKeyPath:'name'
    });
    
    this.dropdownProxy = new DropDownProxy(this);
      
    if(opts.selection){
      opts.selection.model.on(opts.selection.key, (id) => {
        this.selectById(id);
      });
      this.on('selectedId', function(id){
        opts.selection.model.set(opts.selection.key, id);
      });
    }

    var selectOptions = this.selectOptions = 
      new Gnd.Collection(SelectOption, {nosync: true});
        
    selectOptions.link(collection, function(evt, item: Gnd.Model, fields?){
      switch(evt){
        case 'added:':
          selectOptions.add(new SelectOption(
            {item: item, 
             name: item.get(opts.nameKeyPath),
             itemId: item.id()}));
          break;
        case 'removed:':
          var opt = selectOptions['find']({item: item});
          opt && selectOptions.remove(opt.id());
          break;
        case 'updated:':
          selectOptions['find'](function(oldItem){
            if(oldItem.item == item){
              oldItem.set('itemId', item.id());
              oldItem.set('name', item.get(opts.nameKeyPath));
            }
          });
          break;
        }
    });      
    
    // Select added items when there is no selection
    selectOptions.on('added:', (item: SelectOption) => {
      if(this.selectingId){
        if(item.get('item').checkId(this.selectingId)){
          this.selectItem(item);
        }
      }else if(!this.get('selectedItem')){
        this.selectItem(item);
      }
    });

    // Select first item when the current selection is removed
    selectOptions.on('removed:', (item: Gnd.Model) => {
      if(item === this.selectedItem){
        this.selectItem(selectOptions['first']());
      }
    });
      
    if(opts.selectedId) {
      // Wait a tick to activate any selection event that might be attached
      Gnd.Util.nextTick(() => {
        this.selectById(opts.selectedId);  
      })
    }

    opts.parent && this.parent(opts.parent);
  }
    
  destroy()
  {
    Gnd.Base.release(this.viewModel, this.selectOptions, this.dropdownProxy);
    super.destroy();
  }
    
  render(context?: {}): Gnd.Promise<HTMLElement>
  {
    context = _.extend({
      standard: this.opts.standard,
      classname: false
    }, context);
      
    return super.render(context).then<HTMLElement>((el) => {
      this.viewModel = new Gnd.ViewModel(el, {
        collection: this.selectOptions,<commit_msg>Reverted tick on constructor selection<commit_after>
declare var _;

class SelectOption extends Gnd.Model
{}

/**
  This class is mainly used to break a circular dependency that affects the
  retain/release mechanism to work properly.
*/

class DropDownProxy extends Gnd.Base
{
  private dropdown: DropDown;
  
  constructor(dropdown: DropDown){
    super();
    
    this.dropdown = dropdown;
    
    this.bind('selectedId', dropdown);
    this.bind('selectedName', dropdown);
  }
  
  selectHandler(el)
  {
    var item = el['gnd-obj'];
    if(!item.disabled) {
      this.dropdown.selectItem(item);
      this.dropdown.deactivate();
    }
  }
}

// SelectOption.bucket()
// SelectOption.schema()
// SelectOption.decorate('selectOptions', schema)

class DropDown extends Gnd.View {
  private selectingId: string;
  private selectedId: string;
  private selectedItem: Gnd.Model;
  private viewModel: Gnd.ViewModel;
  private selectOptions: Gnd.Collection;
  private opts;
  private dropdownProxy: DropDownProxy;

  constructor(collection: Gnd.Collection, opts)
  {
    super({
      templateUrl: 'gnd/dropdown/dropdown.html.tmpl',
      templateEngine: _.template
    });
      
    this.opts = opts = _.defaults(opts || {}, {
      standard: true,
      nameKeyPath:'name'
    });
    
    this.dropdownProxy = new DropDownProxy(this);
      
    if(opts.selection){
      opts.selection.model.on(opts.selection.key, (id) => {
        this.selectById(id);
      });
      this.on('selectedId', function(id){
        opts.selection.model.set(opts.selection.key, id);
      });
    }

    var selectOptions = this.selectOptions = 
      new Gnd.Collection(SelectOption, {nosync: true});
        
    selectOptions.link(collection, function(evt, item: Gnd.Model, fields?){
      switch(evt){
        case 'added:':
          selectOptions.add(new SelectOption(
            {item: item, 
             name: item.get(opts.nameKeyPath),
             itemId: item.id()}));
          break;
        case 'removed:':
          var opt = selectOptions['find']({item: item});
          opt && selectOptions.remove(opt.id());
          break;
        case 'updated:':
          selectOptions['find'](function(oldItem){
            if(oldItem.item == item){
              oldItem.set('itemId', item.id());
              oldItem.set('name', item.get(opts.nameKeyPath));
            }
          });
          break;
        }
    });      
    
    // Select added items when there is no selection
    selectOptions.on('added:', (item: SelectOption) => {
      if(this.selectingId){
        if(item.get('item').checkId(this.selectingId)){
          this.selectItem(item);
        }
      }else if(!this.get('selectedItem')){
        this.selectItem(item);
      }
    });

    // Select first item when the current selection is removed
    selectOptions.on('removed:', (item: Gnd.Model) => {
      if(item === this.selectedItem){
        this.selectItem(selectOptions['first']());
      }
    });
      
    if(opts.selectedId) {
      this.selectById(opts.selectedId);  
    }

    opts.parent && this.parent(opts.parent);
  }
    
  destroy()
  {
    Gnd.Base.release(this.viewModel, this.selectOptions, this.dropdownProxy);
    super.destroy();
  }
    
  render(context?: {}): Gnd.Promise<HTMLElement>
  {
    context = _.extend({
      standard: this.opts.standard,
      classname: false
    }, context);
      
    return super.render(context).then<HTMLElement>((el) => {
      this.viewModel = new Gnd.ViewModel(el, {
        collection: this.selectOptions,
{'fraction_non_alphanumeric': 0.13777777777777778, 'fraction_numerical': 0.0, 'mean_word_length': 2.4355916030534353, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '6388192', 'n_tokens_mistral': 2163, 'n_tokens_neox': 2154, 'n_words': 418}