<commit_before><link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-behaviors/iron-control-state.html">

<script>

  var WebPaperElem = WebPaperElem || {};

  WebPaperElem.InputAutoCompleteBehaviorImpl = {
	  properties: {
      selectedItem: {
        type: Object,
      },
      selectedItemText: {
        type: String,
      },
      /**
       * The candidates from local variable.
       */
      localCandidates:{
        type:Array,
        value:[]
      },
      /**
       * Internal variable holding all matched suggestions.
       */
      _suggestions:{
        type:Array,
        value:[]
      },
      /**
       * Maximum number of suggestions to show up in typeahead.
       */
      maxSuggestions:{
        type:Number,
        value:5
      },
	  },


    /**
     * Returns a reference to the input element.
     */
    get _inputElement() {
      return this.querySelector('paper-input.autocomplete-input, input.autocomplete-input');
    },

    /**
     * Returns a reference to the menu element.
     */
    get _menuElement() {
      return this.querySelector('paper-menu.autocomplete-suggestions');
    },

    // Element Behavior
    /**
     * Callback for keydown event
     *
     * @param {e} event
     */
    _keydown: function(e) {
      if (e.which == 40 || e.which == 38){
         e.preventDefault();
      }
    },
    /**
     * Callback on mouse over event on paper-item
     *
     */
    _onMouseOverSelectable: function(e) {
      var suggestionsMenu = this._menuElement;
      if (suggestionsMenu && typeof(suggestionsMenu) !== 'undefined'){
        var selectedItem = e.currentTarget;
        index = Number(suggestionsMenu.indexOf(selectedItem));
        suggestionsMenu.select(index);
        this._inputElement.focus();
      }
      else{
        // console.log("suggestionsMenu not defined");
      }
    },
    /**
     * Callback on tap event on paper-item
     *
     */
    _onTapSelectable: function(e) {
      var suggestionsMenu = this._menuElement;
      if (suggestionsMenu && typeof(suggestionsMenu) !== 'undefined'){
        this._setSelectedItem(e.currentTarget);
        e.stopPropagation();
      }
      else{
        // console.log("suggestionsMenu not defined");
      }
    },
    /**
     * Callback for keyup event
     *
     * @param {e} event
     */
    _keyup: function(e) {
      if (e.which == 40){
        // Down Key
        // console.log("down");
        var suggestionsMenu = this._menuElement;
        var selectedItem = suggestionsMenu.focusedItem;
        var index = 0;
        if (typeof(selectedItem) !== 'undefined'){
          index = Number(suggestionsMenu.indexOf(selectedItem));
          index = Math.min(index + 1, this._suggestions.length - 1);
        }
        // console.log("index");
        // console.log(index);
        suggestionsMenu.select(index);
        this._inputElement.focus();
      }
      else if (e.which == 38){
        // Up Key
        var suggestionsMenu = this._menuElement;
        var selectedItem = suggestionsMenu.focusedItem;
        if (typeof(selectedItem) !== 'undefined'){
          index = Number(suggestionsMenu.indexOf(selectedItem));
          index = Math.max(index - 1, -1);
          suggestionsMenu.select(index);
        }
        this._inputElement.focus();
      }
      else if (e.which == 13){
        // Return Key
        var suggestionsMenu = this._menuElement;
        if (suggestionsMenu && typeof(suggestionsMenu) !== 'undefined'){
          this._setSelectedItem(suggestionsMenu.focusedItem);
        }
      }
      else{
        var suggestionsMenu = this._menuElement;
        if (suggestionsMenu && typeof(suggestionsMenu) !== 'undefined'){
          suggestionsMenu.select(-1);
        }
        
        this._checkIntegrityOfSelectedItem();
        
        if (this.inputValue) {
          // if (this.inputValue.length >= 3){
          this._search(this.inputValue.trim());
          // this._suggestions = this._search(this.inputValue.trim());
          // }
        }
      }
    },
    _checkIntegrityOfSelectedItem: function () {
      
        if ((this.selectedItem) &&
            ((!this.inputValue) ||
             (this.inputValue.trim() === '')||
             (this.inputValue !== this.selectedItemText))) {
          this._setSelectedItem(null);
        }
    },
    _setSelectedItem: function (selectedItemElement) {
      var suggestionsMenu = this._menuElement;

      if (typeof(selectedItemElement) !== 'undefined') {
        if (this.selectedItem !== selectedItemElement) {
          
          var selectedObject = null;
          var selectedItemText = null;
          
          if (selectedItemElement !== null) {
            index = Number(suggestionsMenu.indexOf(selectedItemElement));
            
            selectedObject = this._suggestions[index];
            
            selectedItemText = selectedObject.text;
            this._suggestions = [];
            this.inputValue = selectedItemText;
          }
          
          this.selectedItemText = selectedItemText;
          this.selectedItem = selectedItemElement;
          this.selectedObject = selectedObject;
          
          // console.log(this.selectedItem);
        }       
      }
    },
    _search:function(term){
      if (term == ""){
        this._suggestions = [];
        return;
      }
      var patt =  new RegExp(term.toLowerCase());
      var matched = [];
      if (typeof(this.localCandidates) !== 'undefined'){
        for (var i = 0; i < this.localCandidates.length; i ++){
          if (matched.length == this.maxSuggestions){
            break;
          }
          if (patt.test(this.localCandidates[i].text.toLowerCase()) == true){
            matched.push(this.localCandidates[i]);
          }
        }
      }
      
      if (typeof(this._prefetchedCandidates) !== 'undefined'){
        for (var i = 0; i < this._prefetchedCandidates.length; i ++){
          if (matched.length == this.maxSuggestions){
            break;
          }<commit_msg>Added a key handler for ESC<commit_after><link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-behaviors/iron-control-state.html">

<script>

  var WebPaperElem = WebPaperElem || {};

  WebPaperElem.InputAutoCompleteBehaviorImpl = {
	  properties: {
      selectedItem: {
        type: Object,
      },
      selectedItemText: {
        type: String,
      },
      /**
       * The candidates from local variable.
       */
      localCandidates:{
        type:Array,
        value:[]
      },
      /**
       * Internal variable holding all matched suggestions.
       */
      _suggestions:{
        type:Array,
        value:[]
      },
      /**
       * Maximum number of suggestions to show up in typeahead.
       */
      maxSuggestions:{
        type:Number,
        value:5
      },
	  },


    /**
     * Returns a reference to the input element.
     */
    get _inputElement() {
      return this.querySelector('paper-input.autocomplete-input, input.autocomplete-input');
    },

    /**
     * Returns a reference to the menu element.
     */
    get _menuElement() {
      return this.querySelector('paper-menu.autocomplete-suggestions');
    },

    // Element Behavior
    /**
     * Callback for keydown event
     *
     * @param {e} event
     */
    _keydown: function(e) {
      if ((e.which === 27) || /* ESC Key */
          (e.which === 40) || 
          (e.which === 38)) {
         e.preventDefault();
      }
    },
    /**
     * Callback on mouse over event on paper-item
     *
     */
    _onMouseOverSelectable: function(e) {
      var suggestionsMenu = this._menuElement;
      if (suggestionsMenu && typeof(suggestionsMenu) !== 'undefined'){
        var selectedItem = e.currentTarget;
        index = Number(suggestionsMenu.indexOf(selectedItem));
        suggestionsMenu.select(index);
        this._inputElement.focus();
      }
      else{
        // console.log("suggestionsMenu not defined");
      }
    },
    /**
     * Callback on tap event on paper-item
     *
     */
    _onTapSelectable: function(e) {
      var suggestionsMenu = this._menuElement;
      if (suggestionsMenu && typeof(suggestionsMenu) !== 'undefined'){
        this._setSelectedItem(e.currentTarget);
        e.stopPropagation();
      }
      else{
        // console.log("suggestionsMenu not defined");
      }
    },
    /**
     * Callback for keyup event
     *
     * @param {e} event
     */
    _keyup: function(e) {
      if (e.which == 27) {
        // ESC Key
        this.inputValue = '';
        
        var searchTerm = this.inputValue || '';

        this._checkIntegrityOfSelectedItem();        
        
        // if (this.inputValue.length >= 3){
        this._search(searchTerm.trim());
      } else if (e.which == 40){
        // Down Key
        // console.log("down");
        var suggestionsMenu = this._menuElement;
        var selectedItem = suggestionsMenu.focusedItem;
        var index = 0;
        if (typeof(selectedItem) !== 'undefined'){
          index = Number(suggestionsMenu.indexOf(selectedItem));
          index = Math.min(index + 1, this._suggestions.length - 1);
        }
        // console.log("index");
        // console.log(index);
        suggestionsMenu.select(index);
        this._inputElement.focus();
      } else if (e.which == 38){
        // Up Key
        var suggestionsMenu = this._menuElement;
        var selectedItem = suggestionsMenu.focusedItem;
        if (typeof(selectedItem) !== 'undefined'){
          index = Number(suggestionsMenu.indexOf(selectedItem));
          index = Math.max(index - 1, -1);
          suggestionsMenu.select(index);
        }
        this._inputElement.focus();
      } else if (e.which == 13){
        // Return Key
        var suggestionsMenu = this._menuElement;
        if (suggestionsMenu && typeof(suggestionsMenu) !== 'undefined'){
          this._setSelectedItem(suggestionsMenu.focusedItem);
        }
      } else{
        var suggestionsMenu = this._menuElement;
        if (suggestionsMenu && typeof(suggestionsMenu) !== 'undefined'){
          suggestionsMenu.select(-1);
        }
        
        this._checkIntegrityOfSelectedItem();
        
        var searchTerm = this.inputValue || '';
        
        // if (this.inputValue.length >= 3){
        this._search(searchTerm.trim());
        // this._suggestions = this._search(this.inputValue.trim());
        // }
      }
    },
    _checkIntegrityOfSelectedItem: function () {
      
        if ((this.selectedItem) &&
            ((!this.inputValue) ||
             (this.inputValue.trim() === '')||
             (this.inputValue !== this.selectedItemText))) {
          this._setSelectedItem(null);
        }
    },
    _setSelectedItem: function (selectedItemElement) {
      var suggestionsMenu = this._menuElement;

      if (typeof(selectedItemElement) !== 'undefined') {
        if (this.selectedItem !== selectedItemElement) {
          
          var selectedObject = null;
          var selectedItemText = null;
          
          if (selectedItemElement !== null) {
            index = Number(suggestionsMenu.indexOf(selectedItemElement));
            
            selectedObject = this._suggestions[index];
            
            selectedItemText = selectedObject.text;
            this._suggestions = [];
            this.inputValue = selectedItemText;
          }
          
          this.selectedItemText = selectedItemText;
          this.selectedItem = selectedItemElement;
          this.selectedObject = selectedObject;
          
          // console.log(this.selectedItem);
        }       
      }
    },
    _search:function(term){
      if (term === ''){
        this._suggestions = [];
        return;
      }
      var patt =  new RegExp(term.toLowerCase());
      var matched = [];
      if (typeof(this.localCandidates) !== 'undefined'){
        for (var i = 0; i < this.localCandidates.length; i ++){
          if (matched.length == this.maxSuggestions){
            break;
          }
          if (patt.test(this.localCandidates[i].text.toLowerCase()) == true){
            matched.push(this.localCandidates[i]);
          }
        }
      }
      
      if (typeof(this._prefetchedCandidates) !== 'undefined'){
        for (var i = 0; i < this._prefetchedCandidates.length; i ++){
          if (matched.length == this.maxSuggestions){
            break;
          }
{'fraction_non_alphanumeric': 0.1284249319019388, 'fraction_numerical': 0.003605191475725044, 'mean_word_length': 1.865702479338843, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, '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': '341795', 'n_tokens_mistral': 3535, 'n_tokens_neox': 3403, 'n_words': 659}