Commits

Jon Nylander committed 560aca0

Added non-blocking geolocation support through the navigator.geolocation API.

Comments (0)

Files changed (1)

detect_timezone.js

     'use strict';
     var HEMISPHERE_SOUTH = 'SOUTH',
         preliminary_result = null,
-        callback = null,
+        config = null,
         
         /** 
          * Gets the offset in minutes from UTC for a certain date.
             return (base_offset - date_offset) !== 0;
         },
     
-        /**
-         * This function does some basic calculations to create information about 
-         * the user's timezone.
-         * 
-         * Returns a key that can be used to do lookups in jstz.olson.timezones.
-         * 
-         * @returns {String}  
-         */
-        
         lookup_key = function () {
             var january_offset = get_january_offset(), 
                 june_offset = get_june_offset(), 
             return january_offset + ",0";
         },
     
-        doIpLookup = function (preliminary_result, callback) {
-            var url = "http://www.pageloom.com/ip-to-tz/?callback=jstz.handleIpLookup";
+        ip_lookup = function () {
+            var url = "http://www.pageloom.com/timezone/api/?callback=jstz.tz_lookup_callback";
             var script = document.createElement('script');
             script.src = url;
             document.body.appendChild(script);
         },
         
-        handleIpLookup = function (ip_timezone_data) {
+        geo_lookup_fallback = function (error) {
+            if (config.ip) {
+                ip_lookup();
+            }
+            else {
+                config.callback(preliminary_result);
+            }
+        },
+        
+        geo_lookup_callback = function (position) {
+            var url = "http://www.pageloom.com/timezone/api/?callback=jstz.tz_lookup_callback&latitude=" + position.coords.latitude + "&longitude=" + position.coords.longitude;
+            var script = document.createElement('script');
+            script.src = url;
+            document.body.appendChild(script);
+        },
+        
+        geo_lookup = function () {
+            if (navigator.geolocation) {
+                navigator.geolocation.getCurrentPosition(geo_lookup_callback, geo_lookup_fallback);
+            }
+            
+            geo_lookup_fallback();
+        },
+        
+        tz_lookup_callback = function (ip_timezone_data) {
             if (ip_timezone_data.timezone !== preliminary_result.name()) {
                 preliminary_result.compare_with(ip_timezone_data);
-                callback(preliminary_result);
+                config.callback(preliminary_result);
                 return;
             }
-            callback(preliminary_result);
+            config.callback(preliminary_result);
         },
         
-        /**
-         * Uses get_timezone_info() to formulate a key to use in the olson.timezones dictionary.
-         * 
-         * Returns a primitive object on the format:
-         * {'timezone': TimeZone, 'key' : 'the key used to find the TimeZone object'}
-         * 
-         * @returns Object 
-         */
-        determine_timezone = function (return_function) {
+        determine = function (settings) {
             var key = lookup_key();
+            config = settings;
             
+            // Result via pure JavaScript
             preliminary_result = new jstz.TimeZone(jstz.olson.timezones[key]);
             
-            if (!return_function) {
-                return preliminary_result;
+            if (!config.geoloc) {
+                if (config.ip) {
+                    ip_lookup();
+                } else {
+                    config.callback(preliminary_result);
+                }                
+            } else {
+                geo_lookup();
             }
-            
-            callback = return_function;
-            
-            doIpLookup(preliminary_result, callback);
         };
     
     return {
-        determine_timezone : determine_timezone,
-        handleIpLookup : handleIpLookup,
+        determine : determine,
+        tz_lookup_callback : tz_lookup_callback,
         date_is_dst : date_is_dst
     };
 }());
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.