<commit_before>var cdb = require('cartodb.js');
var _ = require('underscore');
var Ps = require('perfect-scrollbar');
var emptyTemplate = require('./custom-list-empty.tpl');
var template = require('./custom-list.tpl');
var ARROW_DOWN_KEY_CODE = 40;
var ARROW_UP_KEY_CODE = 38;
var ENTER_KEY_CODE = 13;

module.exports = cdb.core.View.extend({

  className: 'CDB-Text CDB-Size-medium CustomList-listWrapper',
  tagName: 'div',

  initialize: function () {
    this._initBinds();
  },

  render: function () {
    this.clearSubViews();
    this._removeArrowBinds();
    this._destroyCustomScroll();
    this.$el.empty();

    var query = this.model.get('query');
    var items = this.collection.search(query);

    this.$el.append(template());

    if (items.size() > 0) {
      items.each(this._renderItem, this);
      this._applyCustomScroll();
      this._applyArrowBinds();
    } else {
      this.$el.append(
        emptyTemplate({
          query: query,
          typeLabel: this.options.typeLabel
        })
      );
    }

    return this;
  },

  _renderItem: function (mdl) {
    var ItemViewClass = this.options.ItemView;
    var itemView = new ItemViewClass({
      model: mdl,
      typeLabel: this.options.typeLabel,
      template: this.options.itemTemplate
    });
    this.$('.js-list').append(itemView.render().el);
    this.addView(itemView);
  },

  _initBinds: function () {
    this.model.bind('change:query', this.render, this);
  },

  _applyArrowBinds: function () {
    document.addEventListener('keydown', this._onKeyDown.bind(this));
  },

  _removeArrowBinds: function () {
    document.removeEventListener('keydown', this._onKeyDown);
  },

  _onKeyDown: function (e) {
    var key = e.which;
    var $listItems = this.$('.js-listItem');
    var $highlighted = $listItems.filter('.is-highlighted');
    var $current;

    $listItems.removeClass('is-highlighted');

    if (key === ARROW_DOWN_KEY_CODE) {
      if (!$highlighted.length || $highlighted[0] === $listItems.last()[0]) {
        $current = $listItems.eq(0);
      } else {
        $current = $highlighted.next();
      }
    } else if (key === ARROW_UP_KEY_CODE) {
      if (!$highlighted.length || $highlighted[0] === $listItems.first()[0]) {
        $current = $listItems.last();
      } else {
        $current = $highlighted.prev();
      }
    } else if (key === ENTER_KEY_CODE) {
      e.preventDefault();
      if ($highlighted && $highlighted.length) {
        var mdl = _.first(this.collection.where({ val: $highlighted.data('val') }));
        if (mdl) {
          mdl.set('selected', true);
        }
        return false;
      }
    }

    if ($current) {
      $current.addClass('is-highlighted');
      var itemHeight = $current.outerHeight();
      this.$('.js-list').scrollTop($current.index() * itemHeight);
    }
  },

  _applyCustomScroll: function () {
    Ps.initialize(this.el.querySelector('.js-list'), {
      wheelSpeed: 2,
      wheelPropagation: true,
      minScrollbarLength: 20
    });
  },
<commit_msg>Fixed remove keydown event handler.<commit_after>var cdb = require('cartodb.js');
var _ = require('underscore');
var Ps = require('perfect-scrollbar');
var emptyTemplate = require('./custom-list-empty.tpl');
var template = require('./custom-list.tpl');
var ARROW_DOWN_KEY_CODE = 40;
var ARROW_UP_KEY_CODE = 38;
var ENTER_KEY_CODE = 13;

module.exports = cdb.core.View.extend({

  className: 'CDB-Text CDB-Size-medium CustomList-listWrapper',
  tagName: 'div',

  initialize: function () {
    this._onKeyDownBinded = this._onKeyDown.bind(this);
    this._initBinds();
  },

  render: function () {
    this.clearSubViews();
    this._removeArrowBinds();
    this._destroyCustomScroll();
    this.$el.empty();

    var query = this.model.get('query');
    var items = this.collection.search(query);

    this.$el.append(template());

    if (items.size() > 0) {
      items.each(this._renderItem, this);
      this._applyCustomScroll();
      this._applyArrowBinds();
    } else {
      this.$el.append(
        emptyTemplate({
          query: query,
          typeLabel: this.options.typeLabel
        })
      );
    }

    return this;
  },

  _renderItem: function (mdl) {
    var ItemViewClass = this.options.ItemView;
    var itemView = new ItemViewClass({
      model: mdl,
      typeLabel: this.options.typeLabel,
      template: this.options.itemTemplate
    });
    this.$('.js-list').append(itemView.render().el);
    this.addView(itemView);
  },

  _initBinds: function () {
    this.model.bind('change:query', this.render, this);
  },

  _applyArrowBinds: function () {
    document.addEventListener('keydown', this._onKeyDownBinded);
  },

  _removeArrowBinds: function () {
    document.removeEventListener('keydown', this._onKeyDownBinded);
  },

  _onKeyDown: function (e) {

    if (this.model.get('visible') === false) {
      return;
    }

    var key = e.which;
    var $listItems = this.$('.js-listItem');
    var $highlighted = $listItems.filter('.is-highlighted');
    var $current;
    var mdl;
    var itemHeight;

    $highlighted.removeClass('is-highlighted');

    if (key === ARROW_DOWN_KEY_CODE) {
      if (!$highlighted.length || $highlighted[0] === $listItems.last()[0]) {
        $current = $listItems.eq(0);
      } else {
        $current = $highlighted.next();
      }
    } else if (key === ARROW_UP_KEY_CODE) {
      if (!$highlighted.length || $highlighted[0] === $listItems.first()[0]) {
        $current = $listItems.last();
      } else {
        $current = $highlighted.prev();
      }
    } else if (key === ENTER_KEY_CODE) {
      e.preventDefault();
      if ($highlighted && $highlighted.length) {
        mdl = _.first(this.collection.where({ val: $highlighted.data('val') }));
        if (mdl) {
          mdl.set('selected', true);
        }
        return false;
      }
    }

    if ($current) {
      $current.addClass('is-highlighted');
      itemHeight = $current.outerHeight();
      this.$('.js-list').scrollTop($current.index() * itemHeight);
    }
  },

  _applyCustomScroll: function () {
    Ps.initialize(this.el.querySelector('.js-list'), {
      wheelSpeed: 2,
      wheelPropagation: true,
      minScrollbarLength: 20
    });
  },

{'fraction_non_alphanumeric': 0.16364220330817408, 'fraction_numerical': 0.004817729243616509, 'mean_word_length': 2.839704069050555, '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': '1465858', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1998, 'n_words': 352}