cfpb/owning-a-home

View on GitHub
src/static/js/modules/geolocation.js

Summary

Maintainability
A
25 mins
Test Coverage
'use strict';

var guessState = require( 'fuzzy-state-search' );

module.exports.getState = function( options, callback ) {

  if ( !window.navigator.geolocation ) {
    var closestState = { name: 'AL' };
    return closestState;
  }

  var opts = options || {};
  var timeout = opts.timeout || 10000;
  var cb = typeof options === 'function' ? options : callback;
  var reallyIndecisive = true;

  function success( pos ) {
    var state = guessState( pos );
    reallyIndecisive = false;
    if ( cb ) {
      cb( state );
    }
  }

  function fail() {
    cb();
  }

  // Get their state using the HTML5 gelocation API.
  navigator.geolocation.getCurrentPosition( success, fail );

  // For users who don't see the geolocate permission bar in their
  // browser, fail after X milliseconds.
  setTimeout( function() {
    if ( reallyIndecisive ) {
      fail();
    }
  }, timeout );
};