diff options
author | Brenda Wallace <shiny@cpan.org> | 2010-01-18 11:48:11 +1300 |
---|---|---|
committer | Brenda Wallace <shiny@cpan.org> | 2010-01-18 11:48:11 +1300 |
commit | 02a6006bafd663443b512c5c283b64c7dacfbbb1 (patch) | |
tree | 6d2ac830a8be8330df124da3056f891a2065eedb /js | |
parent | c1f1d712bd4d775b24ce99b44f469ec7dcd2342e (diff) | |
parent | 4978810c81c8d7ba75daa795d66965d6b43331f3 (diff) |
Merge commit 'mainline/0.9.x' into 0.9.x
Diffstat (limited to 'js')
-rw-r--r-- | js/geometa.js | 133 | ||||
-rw-r--r-- | js/util.js | 137 |
2 files changed, 161 insertions, 109 deletions
diff --git a/js/geometa.js b/js/geometa.js index 87e3c99a1..bba59b448 100644 --- a/js/geometa.js +++ b/js/geometa.js @@ -1,5 +1,5 @@ -// A shim to implement the W3C Geolocation API Specification using Gears -if (typeof navigator.geolocation == "undefined" || navigator.geolocation.shim ) (function(){ +// A shim to implement the W3C Geolocation API Specification using Gears or the Ajax API +if (typeof navigator.geolocation == "undefined" || navigator.geolocation.shim ) { (function(){ // -- BEGIN GEARS_INIT (function() { @@ -23,8 +23,7 @@ if (typeof navigator.geolocation == "undefined" || navigator.geolocation.shim ) } } catch (e) { // Safari - if ((typeof navigator.mimeTypes != 'undefined') - && navigator.mimeTypes["application/x-googlegears"]) { + if ((typeof navigator.mimeTypes != 'undefined') && navigator.mimeTypes["application/x-googlegears"]) { factory = document.createElement("object"); factory.style.display = "none"; factory.width = 0; @@ -64,8 +63,8 @@ var GearsGeoLocation = (function() { return function(position) { callback(position); self.lastPosition = position; - } - } + }; + }; // -- PUBLIC return { @@ -96,9 +95,123 @@ var GearsGeoLocation = (function() { }; }); -// If you have Gears installed use that -if (window.google && google.gears) { - navigator.geolocation = GearsGeoLocation(); -} +var AjaxGeoLocation = (function() { + // -- PRIVATE + var loading = false; + var loadGoogleLoader = function() { + if (!hasGoogleLoader() && !loading) { + loading = true; + var s = document.createElement('script'); + s.src = (document.location.protocol == "https:"?"https://":"http://") + 'www.google.com/jsapi?callback=_google_loader_apiLoaded'; + s.type = "text/javascript"; + document.getElementsByTagName('body')[0].appendChild(s); + } + }; + + var queue = []; + var addLocationQueue = function(callback) { + queue.push(callback); + }; + + var runLocationQueue = function() { + if (hasGoogleLoader()) { + while (queue.length > 0) { + var call = queue.pop(); + call(); + } + } + }; + + window['_google_loader_apiLoaded'] = function() { + runLocationQueue(); + }; + + var hasGoogleLoader = function() { + return (window['google'] && google['loader']); + }; + + var checkGoogleLoader = function(callback) { + if (hasGoogleLoader()) { return true; } + + addLocationQueue(callback); + + loadGoogleLoader(); + + return false; + }; + + loadGoogleLoader(); // start to load as soon as possible just in case + + // -- PUBLIC + return { + shim: true, + + type: "ClientLocation", + + lastPosition: null, + + getCurrentPosition: function(successCallback, errorCallback, options) { + var self = this; + if (!checkGoogleLoader(function() { + self.getCurrentPosition(successCallback, errorCallback, options); + })) { return; } + + if (google.loader.ClientLocation) { + var cl = google.loader.ClientLocation; + + var position = { + coords: { + latitude: cl.latitude, + longitude: cl.longitude, + altitude: null, + accuracy: 43000, // same as Gears accuracy over wifi? + altitudeAccuracy: null, + heading: null, + speed: null + }, + // extra info that is outside of the bounds of the core API + address: { + city: cl.address.city, + country: cl.address.country, + country_code: cl.address.country_code, + region: cl.address.region + }, + timestamp: new Date() + }; + + successCallback(position); + + this.lastPosition = position; + } else if (errorCallback === "function") { + errorCallback({ code: 3, message: "Using the Google ClientLocation API and it is not able to calculate a location."}); + } + }, + + watchPosition: function(successCallback, errorCallback, options) { + this.getCurrentPosition(successCallback, errorCallback, options); + + var self = this; + var watchId = setInterval(function() { + self.getCurrentPosition(successCallback, errorCallback, options); + }, 10000); + + return watchId; + }, + + clearWatch: function(watchId) { + clearInterval(watchId); + }, + + getPermission: function(siteName, imageUrl, extraMessage) { + // for now just say yes :) + return true; + } + + }; +}); + +// If you have Gears installed use that, else use Ajax ClientLocation +navigator.geolocation = (window.google && google.gears) ? GearsGeoLocation() : AjaxGeoLocation(); })(); +} diff --git a/js/util.js b/js/util.js index 0314668d9..a10e9d15a 100644 --- a/js/util.js +++ b/js/util.js @@ -289,6 +289,7 @@ var SN = { // StatusNet } } $('#'+form_id).resetForm(); + $('#'+form_id+' #'+SN.C.S.NoticeInReplyTo).val(''); $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove(); SN.U.FormNoticeEnhancements($('#'+form_id)); } @@ -480,8 +481,9 @@ var SN = { // StatusNet var NDGe = $('#'+SN.C.S.NoticeDataGeo); function removeNoticeDataGeo() { - $('label[for='+SN.C.S.NoticeDataGeo+']').removeClass('checked').attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text())); - $('#'+SN.C.S.NoticeDataGeoSelected).hide(); + $('label[for='+SN.C.S.NoticeDataGeo+']') + .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text())) + .removeClass('checked'); $('#'+SN.C.S.NoticeLat).val(''); $('#'+SN.C.S.NoticeLon).val(''); @@ -492,7 +494,7 @@ var SN = { // StatusNet $.cookie(SN.C.S.NoticeDataGeoCookie, 'disabled'); } - function getJSONgeocodeURL(geocodeURL, data) { + function getJSONgeocodeURL(geocodeURL, data, position) { $.getJSON(geocodeURL, data, function(location) { var lns, lid; @@ -513,17 +515,8 @@ var SN = { // StatusNet NLN_text = location.name; } - $('#'+SN.C.S.NoticeGeoName) - .replaceWith('<a id="notice_data-geo_name"/>'); - - $('#'+SN.C.S.NoticeGeoName) - .attr('href', location.url) - .text(NLN_text) - .click(function() { - window.open(location.url); - - return false; - }); + $('label[for='+SN.C.S.NoticeDataGeo+']') + .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + NLN_text + ')'); $('#'+SN.C.S.NoticeLat).val(data.lat); $('#'+SN.C.S.NoticeLon).val(data.lon); @@ -532,14 +525,13 @@ var SN = { // StatusNet $('#'+SN.C.S.NoticeDataGeo).attr('checked', true); var cookieValue = { - 'NLat': data.lat, - 'NLon': data.lon, - 'NLNS': lns, - 'NLID': lid, - 'NLN': NLN_text, - 'NLNU': location.url, - 'NDG': true, - 'NDGSM': false + NLat: data.lat, + NLon: data.lon, + NLNS: lns, + NLID: lid, + NLN: NLN_text, + NLNU: location.url, + NDG: true }; $.cookie(SN.C.S.NoticeDataGeoCookie, JSON.stringify(cookieValue)); }); @@ -557,62 +549,14 @@ var SN = { // StatusNet var geocodeURL = NGW.attr('title'); NGW.removeAttr('title'); - $('label[for='+SN.C.S.NoticeDataGeo+']').attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text())); + $('label[for='+SN.C.S.NoticeDataGeo+']') + .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text())); NDGe.change(function() { - var NLN = $('#'+SN.C.S.NoticeGeoName); - if (NLN.length > 0) { - NLN.remove(); - } - if ($('#'+SN.C.S.NoticeDataGeo).attr('checked') === true || $.cookie(SN.C.S.NoticeDataGeoCookie) === null) { - $('label[for='+SN.C.S.NoticeDataGeo+']').addClass('checked').attr('title', NoticeDataGeoShareDisable_text); - - var S = '<div id="'+SN.C.S.NoticeDataGeoSelected+'" class="'+SN.C.S.Success+'"/>'; - var NDGS = $('#'+SN.C.S.NoticeDataGeoSelected); - - if (NDGS.length > 0) { - NDGS.replaceWith(S); - } - else { - $('#'+SN.C.S.FormNotice).append(S); - } - - NDGS = $('#'+SN.C.S.NoticeDataGeoSelected); - NDGS.prepend('<span id="'+SN.C.S.NoticeGeoName+'">Geo</span> <button class="minimize" title="'+NoticeDataGeoInfoMinimize_text+'">_</button> <button class="close" title="'+NoticeDataGeoShareDisable_text+'">×</button>'); - - var NLN = $('#'+SN.C.S.NoticeGeoName); - NLN.addClass('processing'); - - $('#'+SN.C.S.NoticeDataGeoSelected+' button.close').click(function(){ - removeNoticeDataGeo(); - - $('#'+SN.C.S.NoticeDataGeoSelected).remove(); - - $('#'+SN.C.S.NoticeDataText).focus(); - - return false; - }); - - $('#'+SN.C.S.NoticeDataGeoSelected+' button.minimize').click(function(){ - $('#'+SN.C.S.NoticeDataGeoSelected).hide(); - - var cookieValue = { - 'NLat': $('#'+SN.C.S.NoticeLat).val(), - 'NLon': $('#'+SN.C.S.NoticeLat).val(), - 'NLNS': $('#'+SN.C.S.NoticeLocationNs).val(), - 'NLID': $('#'+SN.C.S.NoticeLocationId).val(), - 'NLN': $('#'+SN.C.S.NoticeGeoName).text(), - 'NLNU': $('#'+SN.C.S.NoticeGeoName).attr('href'), - 'NDG': true, - 'NDGSM': true - }; - $.cookie(SN.C.S.NoticeDataGeoCookie, JSON.stringify(cookieValue)); - - $('#'+SN.C.S.NoticeDataText).focus(); - - return false; - }); + $('label[for='+SN.C.S.NoticeDataGeo+']') + .attr('title', NoticeDataGeo_text.ShareDisable) + .addClass('checked'); if ($.cookie(SN.C.S.NoticeDataGeoCookie) === null || $.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') { if (navigator.geolocation) { @@ -622,18 +566,27 @@ var SN = { // StatusNet $('#'+SN.C.S.NoticeLon).val(position.coords.longitude); var data = { - 'lat': position.coords.latitude, - 'lon': position.coords.longitude, - 'token': $('#token').val() + lat: position.coords.latitude, + lon: position.coords.longitude, + token: $('#token').val() }; - getJSONgeocodeURL(geocodeURL, data); + getJSONgeocodeURL(geocodeURL, data, position); }, function(error) { - if (error.PERMISSION_DENIED == 1) { - removeNoticeDataGeo(); + switch(error.code) { + case error.PERMISSION_DENIED: + removeNoticeDataGeo(); + break; + case error.TIMEOUT: + $('#'+SN.C.S.NoticeDataGeo).attr('checked', false); + break; } + }, + + { + timeout: 10000 } ); } @@ -645,7 +598,7 @@ var SN = { // StatusNet 'token': $('#token').val() }; - getJSONgeocodeURL(geocodeURL, data); + getJSONgeocodeURL(geocodeURL, data, position); } else { removeNoticeDataGeo(); @@ -657,34 +610,20 @@ var SN = { // StatusNet else { var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeoCookie)); - if (cookieValue.NDGSM === true) { - $('#'+SN.C.S.NoticeDataGeoSelected).hide(); - } - $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat); $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon); $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS); $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID); $('#'+SN.C.S.NoticeDataGeo).attr('checked', cookieValue.NDG); - $('#'+SN.C.S.NoticeGeoName) - .replaceWith('<a id="notice_data-geo_name"/>'); - - $('#'+SN.C.S.NoticeGeoName) - .attr('href', cookieValue.NLNU) - .text(cookieValue.NLN) - .click(function() { - window.open($(this).attr('href')); - - return false; - }); + $('label[for='+SN.C.S.NoticeDataGeo+']') + .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + cookieValue.NLN + ')') + .addClass('checked'); } } else { removeNoticeDataGeo(); } - - $('#'+SN.C.S.NoticeDataText).focus(); }).change(); } }, |