<commit_before>var View = require('./view.js');
var api = require('../api.js');
var $ = require('jquery');

module.exports = View.extend({

  $id          : 'address-view',

  template     : require('./templates/address-lookup.hbs'),

  multipleElections : require('./templates/partials/multiple-elections.hbs'),

  events : {
    '#plus-icon click' : 'openAboutModal',
    '#close-button click' : 'closeAboutModal',
  },

  hasSubmitted: false,

  address : '',

  onAfterRender : function(apiCallback) {
    var $address = this.find('#address-input');
    var $aboutModal = this.find('#about');
    var $notFoundModal = this.find('#address-not-found');
    var $currentLocationModal = this.find('#current-location');
    // var electionChoiceTemplate = require('./templates/elections.hbs');

    this.$container.css('max-width', 800);

    if (this.$container.width() > 600) {
      $('#user-image').css('max-width', '450px');
    }

    $('body').on('click', function(e) {
      if (e.target !== $aboutModal) $aboutModal.hide();
      if (e.target !== $notFoundModal) $notFoundModal.hide();
      if (e.target !== $currentLocationModal) $currentLocationModal.hide();
      if (e.target !== this.find('#fade')) this.find('#fade').hide();
    }.bind(this));
    this.autocomplete = new google.maps.places.Autocomplete($address[0]);
    console.log('in on after render', this.autocompleteListener)
    this.hasSubmitted = false;
    this.autocompleteListener = function () {
      if (this.hasSubmitted) return;
      enteredAddress = this.autocomplete.getPlace().formatted_address;
      if (typeof enteredAddress === 'undefined') return;
      this.address = enteredAddress;
      console.log('autocomplete triggered for address: ' + this.address);
      this.hasSubmitted = true;

      api({
        address: enteredAddress, 
        success: this.handleElectionData.bind(this), 
        error: this.handleAddressNotFound.bind(this)
      });
    }.bind(this);
    console.log(this.keypressListener)
    this.keypressListener = function(e) {
      console.log(this.find('#address-input').val(), this.hasSubmitted)
      if (this.hasSubmitted) return;
      var key = e.which || e.keyCode;
      if (key === 13) {
        var address = this.find('#address-input').val();
        this.address = address;
        console.log('enter key pressed for address: ' + this.address);
        this.hasSubmitted = true;

        api({
          address: address, 
          success: this.handleElectionData.bind(this), 
          error: this.handleAddressNotFound.bind(this)
        });
      }
    }

    $(window).on('keypress.keypressListener', this.keypressListener.bind(this));

    google.maps.event.addListener(this.autocomplete, 'place_changed', this.autocompleteListener);
  },

  onRemove: function() {
    google.maps.event.clearInstanceListeners(this.autocomplete);
    $(window).unbind('keypress.keypressListener')
  },

  handleElectionData: function(response) {
    var that = this;
    // if response has multiple elections, select which election
    // console.log('handling electiondata')
    // if (!response.otherElections) {
    //   response.otherElections = [{
    //     name: "<NAME>",
    //     date: "01/25/1900",
    //     id: "2000"
    //   }];
    // }
    var stateName = response.state[0].name;
    console.log(stateName)
    if (stateName === 'New York' || stateName === 'Oregon') {
      $('#current-location, #fade')
        .show();

      $('#use-current-location').one('click', function() {
        if ('geolocation' in navigator) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var lat = position.coords.latitude
              , lng = position.coords.longitude;
            that._reverseGeocode(<commit_msg>use correct states for current location<commit_after>var View = require('./view.js');
var api = require('../api.js');
var $ = require('jquery');

module.exports = View.extend({

  $id          : 'address-view',

  template     : require('./templates/address-lookup.hbs'),

  multipleElections : require('./templates/partials/multiple-elections.hbs'),

  events : {
    '#plus-icon click' : 'openAboutModal',
    '#close-button click' : 'closeAboutModal',
  },

  hasSubmitted: false,

  address : '',

  onAfterRender : function(apiCallback) {
    var $address = this.find('#address-input');
    var $aboutModal = this.find('#about');
    var $notFoundModal = this.find('#address-not-found');
    var $currentLocationModal = this.find('#current-location');
    // var electionChoiceTemplate = require('./templates/elections.hbs');

    this.$container.css('max-width', 800);

    if (this.$container.width() > 600) {
      $('#user-image').css('max-width', '450px');
    }

    $('body').on('click', function(e) {
      if (e.target !== $aboutModal) $aboutModal.hide();
      if (e.target !== $notFoundModal) $notFoundModal.hide();
      if (e.target !== $currentLocationModal) $currentLocationModal.hide();
      if (e.target !== this.find('#fade')) this.find('#fade').hide();
    }.bind(this));
    this.autocomplete = new google.maps.places.Autocomplete($address[0]);
    console.log('in on after render', this.autocompleteListener)
    this.hasSubmitted = false;
    this.autocompleteListener = function () {
      if (this.hasSubmitted) return;
      enteredAddress = this.autocomplete.getPlace().formatted_address;
      if (typeof enteredAddress === 'undefined') return;
      this.address = enteredAddress;
      console.log('autocomplete triggered for address: ' + this.address);
      this.hasSubmitted = true;

      api({
        address: enteredAddress, 
        success: this.handleElectionData.bind(this), 
        error: this.handleAddressNotFound.bind(this)
      });
    }.bind(this);
    console.log(this.keypressListener)
    this.keypressListener = function(e) {
      console.log(this.find('#address-input').val(), this.hasSubmitted)
      if (this.hasSubmitted) return;
      var key = e.which || e.keyCode;
      if (key === 13) {
        var address = this.find('#address-input').val();
        this.address = address;
        console.log('enter key pressed for address: ' + this.address);
        this.hasSubmitted = true;

        api({
          address: address, 
          success: this.handleElectionData.bind(this), 
          error: this.handleAddressNotFound.bind(this)
        });
      }
    }

    $(window).on('keypress.keypressListener', this.keypressListener.bind(this));

    google.maps.event.addListener(this.autocomplete, 'place_changed', this.autocompleteListener);
  },

  onRemove: function() {
    google.maps.event.clearInstanceListeners(this.autocomplete);
    $(window).unbind('keypress.keypressListener')
  },

  handleElectionData: function(response) {
    var that = this;
    // if response has multiple elections, select which election
    // console.log('handling electiondata')
    // if (!response.otherElections) {
    //   response.otherElections = [{
    //     name: "VIP Test Election",
    //     date: "01/25/1900",
    //     id: "2000"
    //   }];
    // }
    var stateName = response.state[0].name;
    console.log(stateName)
    if (stateName === 'Washington' || stateName === 'Oregon') {
      $('#current-location, #fade')
        .show();

      $('#use-current-location').one('click', function() {
        if ('geolocation' in navigator) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var lat = position.coords.latitude
              , lng = position.coords.longitude;
            that._reverseGeocode(
{'fraction_non_alphanumeric': 0.1608953258722844, 'fraction_numerical': 0.0065832784726793945, 'mean_word_length': 2.9832197168327217, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '2959337', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2198, 'n_words': 409}