Blame view
static/plugins/webshim-gh-pages/js-webshim/dev/shims/geolocation.js
4.59 KB
831eac332 add file |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
(function($){ var webshims = window.webshims; setTimeout(function(){ webshims.isReady('geolocation', true); }); var domWrite = function(){ setTimeout(function(){ throw('document.write is overwritten by geolocation shim. This method is incompatible with this plugin'); }, 1); }, id = 0 ; var geoOpts = webshims.cfg.geolocation || {}; if(!navigator.geolocation){ navigator.geolocation = {}; } $.extend(navigator.geolocation, (function(){ var pos; var api = { getCurrentPosition: function(success, error, opts){ var locationAPIs = 2, errorTimer, googleTimer, calledEnd, createAjax, endCallback = function(){ if(calledEnd){return;} if(pos){ calledEnd = true; success($.extend({timestamp: new Date().getTime()}, pos)); resetCallback(); if(window.JSON && window.sessionStorage){ try{ sessionStorage.setItem('storedGeolocationData654321', JSON.stringify(pos)); } catch(e){} } } else if(error && !locationAPIs) { calledEnd = true; resetCallback(); error({ code: 2, message: "POSITION_UNAVAILABLE"}); } }, googleCallback = function(){ locationAPIs--; getGoogleCoords(); endCallback(); }, resetCallback = function(){ $(document).off('google-loader', resetCallback); clearTimeout(googleTimer); clearTimeout(errorTimer); }, getGoogleCoords = function(){ if(pos || !window.google || !google.loader || !google.loader.ClientLocation){return false;} var cl = google.loader.ClientLocation; pos = { coords: { latitude: cl.latitude, longitude: cl.longitude, altitude: null, accuracy: 43000, altitudeAccuracy: null, heading: parseInt('NaN', 10), velocity: null }, //extension similiar to FF implementation address: $.extend({streetNumber: '', street: '', premises: '', county: '', postalCode: ''}, cl.address) }; return true; }, getInitCoords = function(){ if(pos){return;} getGoogleCoords(); if(pos || !window.JSON || !window.sessionStorage){return;} try{ pos = sessionStorage.getItem('storedGeolocationData654321'); pos = (pos) ? JSON.parse(pos) : false; if(!pos.coords){pos = false;} } catch(e){ pos = false; } } ; getInitCoords(); if(!pos){ if(geoOpts.confirmText && !confirm(geoOpts.confirmText.replace('{location}', location.hostname))){ if(error){ error({ code: 1, message: "PERMISSION_DENIED"}); } return; } $.ajax({ url: 'http://freegeoip.net/json/', dataType: 'jsonp', cache: true, jsonp: 'callback', success: function(data){ locationAPIs--; if(!data){return;} pos = pos || { coords: { latitude: data.latitude, longitude: data.longitude, altitude: null, accuracy: 43000, altitudeAccuracy: null, heading: parseInt('NaN', 10), velocity: null }, //extension similiar to FF implementation address: { city: data.city, country: data.country_name, countryCode: data.country_code, county: "", postalCode: data.zipcode, premises: "", region: data.region_name, street: "", streetNumber: "" } }; endCallback(); }, error: function(){ locationAPIs--; endCallback(); } }); clearTimeout(googleTimer); if (!window.google || !window.google.loader) { googleTimer = setTimeout(function(){ //destroys document.write!!! if (geoOpts.destroyWrite) { document.write = domWrite; document.writeln = domWrite; } $(document).one('google-loader', googleCallback); webshims.loader.loadScript('http://www.google.com/jsapi', false, 'google-loader'); }, 800); } else { locationAPIs--; } } else { setTimeout(endCallback, 1); return; } if(opts && opts.timeout){ errorTimer = setTimeout(function(){ resetCallback(); if(error) { error({ code: 3, message: "TIMEOUT"}); } }, opts.timeout); } else { errorTimer = setTimeout(function(){ locationAPIs = 0; endCallback(); }, 10000); } }, clearWatch: $.noop }; api.watchPosition = function(a, b, c){ api.getCurrentPosition(a, b, c); id++; return id; }; return api; })()); webshims.isReady('geolocation', true); })(webshims.$); |