<commit_before>import * as vars from '../../config/vars.json';
import * as constants from './constants';
import utils from './utils';
import { Photon } from './providers/photon';
import { OpenStreet } from './providers/osm';
import { MapQuest } from './providers/mapquest';
import { Pelias } from './providers/pelias';
import { Google } from './providers/google';
import { Bing } from './providers/bing';

/**
 * @class Nominatim
 */
export class Nominatim {
  /**
   * @constructor
   * @param {Function} base Base class.
   */
  constructor(base) {
    this.Base = base;

    this.layer_name = utils.randomId('geocoder-layer-');
    this.layer = new ol.layer.Vector({
      name: this.layer_name,
      source: new ol.source.Vector()
    });
    
    this.options = base.options;
    this.options.provider = this.options.provider.toLowerCase();
    
    this.els = this.createControl();
    this.container = this.els.container;
    this.registered_listeners = {
      map_click: false
    };
    this.setListeners();
    
    // providers
    this.Photon = new Photon();
    this.OpenStreet = new OpenStreet();
    this.MapQuest = new MapQuest();
    this.Pelias = new Pelias();
    this.Google = new Google();
    this.Bing = new Bing();
    
    return this;
  }
  
  createControl() {
    const container = utils.createElement([
      'div', { classname: vars.namespace + vars.container_class }
    ], Nominatim.html);
    
    const elements = {
      container: container,
      control: 
        container.querySelector(`.${vars.namespace + vars.control_class}`),
      btn_search: 
        container.querySelector(`.${vars.namespace + vars.btn_search_class}`),
      input_search: 
        container.querySelector(`.${vars.namespace + vars.input_search_class}`),
      result_container: 
        container.querySelector(`.${vars.namespace + vars.result_class}`)
    };
    //set placeholder from options
    elements.input_search.placeholder = this.options.placeholder;
    return elements;
  }
  
  setListeners() {
    let timeout, last_query,
        openSearch = () => {
          if(utils.hasClass(this.els.control, vars.namespace + vars.expanded_class)) {
            this.collapse();
          } else {
            this.expand();
          }
        },
        query = evt => {
          if (evt.keyCode == 13) { // enter key
            evt.preventDefault();
            this.query(evt.target.value);
          }
        },
        autoComplete = evt => {
          const query = evt.target.value;
          
          if (query != last_query) {
            last_query = query;
            
            if (timeout) clearTimeout(timeout);
            
            timeout = setTimeout(() => {
              if(query.length >= this.options.autoCompleteMinLength) {
                this.query(query);
              }
            }, 200);
          }
        };
    this.els.input_search.addEventListener('keydown', query, false);
    this.els.btn_search.addEventListener('click', openSearch, false);
    if (this.options.autoComplete) {
      this.els.input_search.addEventListener('input', autoComplete, false);
    }
  }
  
  query(q) {
    let this_ = this,
        ajax = {},
        options = this.options,
        input = this.els.input_search,
        provider = this.getProvider({
          query: q,
          provider: options.provider,
          key: options.key,
          lang: options.lang,
          countrycodes: options.countrycodes,
          limit: options.limit
        });
    if(this.prev_params == provider.params){
      return false;
    }
    this.prev_params = provider.params;
    this.clearResults();
    utils.addClass(input, vars.namespace + vars.loading_class);
    
    ajax.url = provider.url;
    ajax.data = provider.params;
    
    if (options.provider === constants.providers.BING) {
      ajax.data_type = 'jsonp';
      ajax.callbackName = provider.callbackName;
    }
    
    utils.json(ajax).when({
      ready: response => {
        if (options.debug) {
          console.info(response);
        }
        
        utils.removeClass(input, vars.namespace + vars.loading_class);
        
        //will be fullfiled according to provider
        let response__;
        
        switch (options.provider) {<commit_msg>Made the query function only check the last query<commit_after>import * as vars from '../../config/vars.json';
import * as constants from './constants';
import utils from './utils';
import { Photon } from './providers/photon';
import { OpenStreet } from './providers/osm';
import { MapQuest } from './providers/mapquest';
import { Pelias } from './providers/pelias';
import { Google } from './providers/google';
import { Bing } from './providers/bing';

/**
 * @class Nominatim
 */
export class Nominatim {
  /**
   * @constructor
   * @param {Function} base Base class.
   */
  constructor(base) {
    this.Base = base;

    this.layer_name = utils.randomId('geocoder-layer-');
    this.layer = new ol.layer.Vector({
      name: this.layer_name,
      source: new ol.source.Vector()
    });
    
    this.options = base.options;
    this.options.provider = this.options.provider.toLowerCase();
    
    this.els = this.createControl();
    this.container = this.els.container;
    this.registered_listeners = {
      map_click: false
    };
    this.setListeners();
    
    // providers
    this.Photon = new Photon();
    this.OpenStreet = new OpenStreet();
    this.MapQuest = new MapQuest();
    this.Pelias = new Pelias();
    this.Google = new Google();
    this.Bing = new Bing();
    
    return this;
  }
  
  createControl() {
    const container = utils.createElement([
      'div', { classname: vars.namespace + vars.container_class }
    ], Nominatim.html);
    
    const elements = {
      container: container,
      control: 
        container.querySelector(`.${vars.namespace + vars.control_class}`),
      btn_search: 
        container.querySelector(`.${vars.namespace + vars.btn_search_class}`),
      input_search: 
        container.querySelector(`.${vars.namespace + vars.input_search_class}`),
      result_container: 
        container.querySelector(`.${vars.namespace + vars.result_class}`)
    };
    //set placeholder from options
    elements.input_search.placeholder = this.options.placeholder;
    return elements;
  }
  
  setListeners() {
    let timeout, last_query,
        openSearch = () => {
          if(utils.hasClass(this.els.control, vars.namespace + vars.expanded_class)) {
            this.collapse();
          } else {
            this.expand();
          }
        },
        query = evt => {
          if (evt.keyCode == 13) { // enter key
            evt.preventDefault();
            this.query(evt.target.value);
          }
        },
        autoComplete = evt => {
          const query = evt.target.value;
          
          if (query != last_query) {
            last_query = query;
            
            if (timeout) clearTimeout(timeout);
            
            timeout = setTimeout(() => {
              if(query.length >= this.options.autoCompleteMinLength) {
                this.query(query);
              }
            }, 200);
          }
        };
    this.els.input_search.addEventListener('keydown', query, false);
    this.els.btn_search.addEventListener('click', openSearch, false);
    if (this.options.autoComplete) {
      this.els.input_search.addEventListener('input', autoComplete, false);
    }
  }
  
  query(q) {
    let this_ = this,
        ajax = {},
        options = this.options,
        input = this.els.input_search,
        provider = this.getProvider({
          query: q,
          provider: options.provider,
          key: options.key,
          lang: options.lang,
          countrycodes: options.countrycodes,
          limit: options.limit
        });
    if (this.last_query === q) return;
    this.last_query = q;
    this.clearResults();
    utils.addClass(input, vars.namespace + vars.loading_class);
    
    ajax.url = provider.url;
    ajax.data = provider.params;
    
    if (options.provider === constants.providers.BING) {
      ajax.data_type = 'jsonp';
      ajax.callbackName = provider.callbackName;
    }
    
    utils.json(ajax).when({
      ready: response => {
        if (options.debug) {
          console.info(response);
        }
        
        utils.removeClass(input, vars.namespace + vars.loading_class);
        
        //will be fullfiled according to provider
        let response__;
        
        switch (options.provider) {
{'fraction_non_alphanumeric': 0.13004799250848648, 'fraction_numerical': 0.0011705489874751257, 'mean_word_length': 2.104651162790698, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '2315441', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2452, 'n_words': 490}