diff options
author | Struan Donald <struan@exo.org.uk> | 2012-03-12 14:58:57 +0000 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2012-03-12 14:59:49 +0000 |
commit | b2ffaaa92e467a40dfb75eac4c87c852d10bca32 (patch) | |
tree | ced7796c3de128d4e0fb11448c51a093f67d1aaf /phonegap | |
parent | d14bdc65777a6347c419f29e355fa9f1745de739 (diff) |
send anonymous flag and fix jslint complaints
Diffstat (limited to 'phonegap')
-rw-r--r-- | phonegap/www/js/mobile.js | 189 |
1 files changed, 101 insertions, 88 deletions
diff --git a/phonegap/www/js/mobile.js b/phonegap/www/js/mobile.js index 7ce62d703..9a6bf22dc 100644 --- a/phonegap/www/js/mobile.js +++ b/phonegap/www/js/mobile.js @@ -6,16 +6,14 @@ var urlParams = {}; d = function (s) { return decodeURIComponent(s.replace(a, " ")); }, q = window.location.search.substring(1); - while (e = r.exec(q)) - urlParams[d(e[1])] = d(e[2]); + // do it this way to get round jslint complaints + for (;;) { + e = r.exec(q); + if (!e) { break; } + urlParams[d(e[1])] = d(e[2]); + } })(); -$(function(){ - $('#postcodeForm').submit(locate); - $('#mapForm').submit(postReport); - $('#ffo').click(getPosition); -}); - function show_around( lat, long ) { pc = $('#pc').val(); window.location='around.html?pc=' + pc + '&latitude=' + lat + '&longitude=' + long; @@ -27,77 +25,50 @@ function valid_postcode(pc) { var in_pattern = '[0-9][ABD-HJLNP-UW-Z]{2}'; var full_pattern = '^' + out_pattern + in_pattern + '$'; var postcode_regex = new RegExp(full_pattern); - + pc = pc.toUpperCase().replace(/\s+/, ''); if ( postcode_regex.test(pc) ) { return true; } - + return false; } function checkConnection() { var networkState = navigator.network.connection.type; if ( networkState == Connection.NONE || networkState == Connection.UNKNOWN ) { - $('#main').hide(); + $('#main').hide(); $('#noconnection').show(); - } + } } -function locate() { - $("#postcode_error").html(""); - $("#multiple").remove(); - var pc = $('#pc').val(); - - if (!pc) { - $('#postcode_error').html("Please enter location"); - return false; - } - - var loadingStart = function() {}; - var loadingStop = function() {}; - if (typeof navigator.notificationEx !== "undefined") { - loadingStart = navigator.notificationEx.loadingStart; - loadingStop = navigator.notificationEx.loadingStop; - } - loadingStart( { 'backgroundOpacity' : 0.5, labelText: 'Getting Location...', minDuration: 1 } ); - if ( valid_postcode( pc ) ) { - jQuery.get( 'http://mapit.mysociety.org/postcode/' + pc + '.json', function(data, status) { - if ( status == 'success' ) { - show_around( data['wgs84_lat'], data['wgs84_lon'] ); - - loadingStop(); - } else { - loadingStop(); - } - }); - } else { - lookup_string(pc ); - } - return false; +function use_lat_long( lat, long ) { + show_around( lat, long ); } + function lookup_string(q) { q = q.toLowerCase(); - q = q.replace(/[^-&\w ']/, ' '); + q = q.replace(/[^\-&\w ']/, ' '); q = q.replace(/\s+/, ' '); - + if (!q) { - $('#postcode_error').html("Please enter location"); + alert('no location'); + $('#postcodeForm').after("<p>Please enter location</p>"); return false; } - + var url = "http://dev.virtualearth.net/REST/v1/Locations?q=" + escape(q); url += '&c=en-GB&key=Au4rfRu-kpWgzddOjaTRlLhvB8wkTAr5_ky96BOxlmtjM-3FRJhjSsfauaWQnS0Z'; - jQuery.get( url, function(data, status) { + var x = jQuery.get( url, function(data, status) { if ( status == 'success' ) { var valid_locations = 0; var latitude = 0; var longitude = 0; - var multiple = Array(); + var multiple = []; - for ( result in data.resourceSets[0].resources ) { - var details = data.resourceSets[0].resources[result]; + for ( i = 0; i < data.resourceSets[0].resources.length; i++ ) { + var details = data.resourceSets[0].resources[i]; if ( details.address.countryRegion != 'United Kingdom' ) { continue; } var address = details.name; @@ -106,64 +77,93 @@ function lookup_string(q) { latitude = latitude.toPrecision(6); longitude = longitude.toPrecision(6); - multiple.push( { 'address': address, 'latitude': latitude, 'longitude': longitude } ) + multiple.push( { 'address': address, 'latitude': latitude, 'longitude': longitude } ); valid_locations += 1; } if ( valid_locations == 1 ) { show_around( latitude, longitude ); - } else if ( valid_locations == 0 ) { - $('#postcode_error').html('Location not found'); - $('#id_q').select(); + } else if ( valid_locations === 0 ) { + $('#postcodeForm').after('<p>Location not found</p>'); + $('#pc').select(); } else { var multiple_html = '<ul id="multiple"><li>Multiple locations found, please select one:'; - for ( i in multiple ) { + for ( i = 0; i < multiple.length; i++ ) { multiple_html += '<li><a href="#" onclick="use_lat_long( ' + multiple[i].latitude + ',' + multiple[i].longitude +')">' + multiple[i].address + '</a></li>'; } multiple_html += '</ul>'; - $('#postcode').after( multiple_html ); + $('#postcodeForm').after( multiple_html ); } } else { $('#postcode_error').html('<span>Lookup failed</span>'); } - if (navigator.notificationEx) navigator.notificationEx.loadingStop(); + if (navigator.notificationEx) { navigator.notificationEx.loadingStop(); } }); } - -function getPosition() { + +function locate() { + $("#postcode_error").html(""); + $("#multiple").remove(); + var pc = $('#pc').val(); + + if (!pc) { + $('#postcode_error').html("Please enter location"); + return false; + } + var loadingStart = function() {}; var loadingStop = function() {}; - if (typeof navigator.notificationEx !== "undefined") { loadingStart = navigator.notificationEx.loadingStart; loadingStop = navigator.notificationEx.loadingStop; } loadingStart( { 'backgroundOpacity' : 0.5, labelText: 'Getting Location...', minDuration: 1 } ); - - navigator.geolocation.getCurrentPosition(foundLocation, notFoundLocation); + if ( valid_postcode( pc ) ) { + jQuery.get( 'http://mapit.mysociety.org/postcode/' + pc + '.json', function(data, status) { + if ( status == 'success' ) { + show_around( data.wgs84_lat, data.wgs84_lon ); + loadingStop(); + } else { + loadingStop(); + } + }); + } else { + lookup_string(pc); + } + return false; } function foundLocation(myLocation) { - if (navigator.notificationEx) navigator.notificationEx.loadingStop(); + if (navigator.notificationEx) { navigator.notificationEx.loadingStop(); } var lat = myLocation.coords.latitude; var long = myLocation.coords.longitude; show_around( lat, long ); } - -function notFoundLocation() { alert( 'not found' ) } - -function takePhoto(type) { - navigator.camera.getPicture(onSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI, sourceType: type }); + +function notFoundLocation() { alert( 'not found' ); } + +function getPosition() { + var loadingStart = function() {}; + var loadingStop = function() {}; + + if (typeof navigator.notificationEx !== "undefined") { + loadingStart = navigator.notificationEx.loadingStart; + loadingStop = navigator.notificationEx.loadingStop; + } + loadingStart( { 'backgroundOpacity' : 0.5, labelText: 'Getting Location...', minDuration: 1 } ); + + navigator.geolocation.getCurrentPosition(foundLocation, notFoundLocation); } + function onSuccess(imageURI) { $('#form_photo').val(imageURI); $('#photo').attr('src', imageURI ); $('#add_photo').hide(); $('#display_photo').show(); } - + function delPhoto() { $('#form_photo').val(''); $('#photo').attr('src', '' ); @@ -174,7 +174,27 @@ function delPhoto() { function onFail(message) { // $('#photo').val('failed to get photo: ' + message ); } - + +function takePhoto(type) { + navigator.camera.getPicture(onSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI, sourceType: type }); +} + +function win(r) { + console.log( r.response ); + console.log( typeof r.response ); + if ( r.response.indexOf( 'success' ) >= 0 ) { + window.location = 'email_sent.html'; + } else { + alert('!woot'); + $('input[type=submit]').prop("disabled", false); + } +} + +function fail() { + alert('boo!'); +} + + function postReport(e) { e.preventDefault(); @@ -183,14 +203,15 @@ function postReport(e) { title: $('#form_title').val(), detail: $('#form_detail').val(), name: $('#form_name').val(), + may_show_name: $('#form_may_show_name').attr('checked') ? 1 : 0, email: $('#form_email').val(), category: $('#form_category').val(), lat: $('#fixmystreet\\.latitude').val(), lon: $('#fixmystreet\\.longitude').val(), - pc: $('#pc').val(), + pc: $('#pc').val() }; - if ( $('#form_photo').val() != '' ) { + if ( $('#form_photo').val() !== '' ) { fileURI = $('#form_photo').val(); var options = new FileUploadOptions(); @@ -220,23 +241,15 @@ function postReport(e) { error: function (data, status, errorThrown ) { console.log( data ); alert( 'There was a problem submitting your report, please try again: ' + data, function(){}, 'Submit report' ); - }, + } } ); } return false; } -function win(r) { - console.log( r.response ); - eval( 'var res = ' + r.response ); - if ( res.success == 1 ) { - window.location = 'email_sent.html'; - } else { - alert('!woot'); - $('input[type=submit]').prop("disabled", false); - } -} - -function fail() { - alert('boo!'); -} +$(function(){ + $('#postcodeForm').submit(locate); + $('#mapForm').submit(postReport); + $('#ffo').click(getPosition); +}); + |