diff options
Diffstat (limited to 'www/js/mobile.js')
-rw-r--r-- | www/js/mobile.js | 210 |
1 files changed, 144 insertions, 66 deletions
diff --git a/www/js/mobile.js b/www/js/mobile.js index c44501d..9da0ecb 100644 --- a/www/js/mobile.js +++ b/www/js/mobile.js @@ -1,38 +1,13 @@ -function touchmove(e) { - e.preventDefault(); -} - -function loadingSpinner(method){ - if (method == "on") { - //Adjust to screen size - var pHeight = window.innerHeight; - var pWidth = window.innerWidth; - var sH = parseInt($('#loadingSpinner').css('height'),10); - var sW = parseInt($('#loadingSpinner').css('width'),10); - $('#loadingSpinner').css('top',(pHeight-sH)/2+$('body').scrollTop()); - $('#loadingSpinner').css('left',(pWidth-sW)/2); - $('#loadingSpinner').css('z-index',1000); - //Show - $('#loadingSpinner').show(); - } else if (method == 'off'){ - $('#loadingSpinner').hide(); - } -} +Storage.prototype.setObject = function(key, value) { + this.setItem(key, JSON.stringify(value)); +}; -function showBusy( title, msg ) { - if ( navigator && navigator.notification && typeof navigator.notification.activityStart !== "undefined") { - navigator.notification.activityStart( title, msg ); - } else { - loadingSpinner('on'); - } -} +Storage.prototype.getObject = function(key) { + return JSON.parse(this.getItem(key)); +}; -function hideBusy() { - if ( navigator && navigator.notification && navigator.notification.activityStop) { - navigator.notification.activityStop(); - } else { - loadingSpinner('off'); - } +function touchmove(e) { + e.preventDefault(); } function show_around( lat, long ) { @@ -40,8 +15,7 @@ function show_around( lat, long ) { localStorage.latitude = lat; localStorage.longitude = long; localStorage.pc = pc; - hideBusy(); - window.location='around.html'; + $.mobile.changePage('around.html'); return false; } @@ -72,7 +46,6 @@ function use_lat_long( lat, long ) { } function location_error( msg ) { - hideBusy(); if ( msg === '' ) { $('#location_error').remove(); return; @@ -93,7 +66,6 @@ function lookup_string(q) { q = q.replace(/\s+/, ' '); if (!q) { - hideBusy(); location_error("Please enter location"); return false; } @@ -140,7 +112,6 @@ function lookup_string(q) { } else { location_error("Could not find your location"); } - hideBusy(); }); return false; } @@ -154,8 +125,6 @@ function locate() { return false; } - showBusy('Locating', 'Looking up location'); - if ( valid_postcode( pc ) ) { jQuery.get( CONFIG.MAPIT_URL + 'postcode/' + pc + '.json', function(data, status) { if ( status == 'success' ) { @@ -182,7 +151,6 @@ function foundLocation(myLocation) { function notFoundLocation() { location_error( 'Could not find location' ); } function getPosition() { - showBusy( 'Locating', 'Looking up location' ); navigator.geolocation.getCurrentPosition(foundLocation, notFoundLocation); } @@ -224,7 +192,11 @@ function check_name( name, msg ) { $('#form_name').val( name ); if ( msg ) { $('#form_name').focus(); - $('#form_name').before('<div class="form-error">' + msg + '</div>' ); + if ( $('#form_name_error').length ) { + $('#form_name_error').text(msg); + } else { + $('#form_name').before('<div class="form-error" id="form_name_error">' + msg + '</div>' ); + } } } @@ -240,11 +212,9 @@ function fileUploadSuccess(r) { if ( data.success ) { if ( data.report ) { localStorage.report = data.report; - hideBusy(); - window.location = 'report_created.html'; + $.mobile.changePage('report_created.html'); } else { - hideBusy(); - window.location = 'email_sent.html'; + $.mobile.changePage('email_sent.html'); } } else { if ( data.check_name ) { @@ -255,14 +225,12 @@ function fileUploadSuccess(r) { $('input[type=submit]').prop("disabled", false); } } else { - hideBusy(); alert('Could not submit report'); $('input[type=submit]').prop("disabled", false); } } function fileUploadFail() { - hideBusy(); alert('Could not submit report'); $('input[type=submit]').prop("disabled", false); } @@ -306,7 +274,6 @@ function postReport(e) { } } - showBusy( 'Sending Report', 'Please wait while your report is sent' ); if ( $('#form_photo').val() !== '' ) { fileURI = $('#form_photo').val(); @@ -332,11 +299,9 @@ function postReport(e) { localStorage.long = null; if ( data.report ) { localStorage.report = data.report; - hideBusy(); - window.location = 'report_created.html'; + $.mobile.changePage('report_created.html'); } else { - hideBusy(); - window.location = 'email_sent.html'; + $.mobile.changePage('email_sent.html'); } if ( !localStorage.name && $('#password_sign_in').val() ) { localStorage.name = $('#form_name').val(); @@ -348,11 +313,9 @@ function postReport(e) { check_name( data.check_name, data.errors.name ); } $('input[type=submit]').prop("disabled", false); - hideBusy(); } }, error: function (data, status, errorThrown ) { - hideBusy(); alert( 'There was a problem submitting your report, please try again (' + status + '): ' + JSON.stringify(data), function(){}, 'Submit report' ); $('input[type=submit]').prop("disabled", false); } @@ -362,7 +325,6 @@ function postReport(e) { } function sign_in() { - showBusy( 'Signing In', 'Please wait while you are signed in' ); $('#form_email').blur(); $('#password_sign_in').blur(); jQuery.ajax( { @@ -374,19 +336,16 @@ function sign_in() { remember_me: 1 }, success: function(data) { - console.log(data); if ( data.name ) { localStorage.name = data.name; localStorage.username = $('#form_email').val(); localStorage.password = $('#password_sign_in').val(); - hideBusy(); $('#user-meta').html('<p>You are signed in as ' + localStorage.username + '.</p>'); $('#form_sign_in_only').hide(); $('#forget_button').show(); $('#form_email').val(''); $('#password_sign_in').val(''); } else { - hideBusy(); $('#form_email').before('<div class="form-error">There was a problem with your email/password combination.</div>'); } } @@ -394,7 +353,7 @@ function sign_in() { } -function display_signed_out_msg() { +function display_account_page() { if ( localStorage.signed_out == 1 ) { $('#user-meta').html('<p>You’ve been signed out.</p>'); $('#form_sign_in_only').show(); @@ -418,8 +377,7 @@ function sign_out() { if ( data.signed_out ) { localStorage.signed_out = 1; localStorage.name = null; - hideBusy(); - document.location = 'sign_in.html'; + $.mobile.changePage('sign_in.html'); } } } ); @@ -466,7 +424,7 @@ function forget() { delete localStorage.username; delete localStorage.password; localStorage.signed_out = 1; - display_signed_out_msg(); + display_account_page(); } function onDeviceReady() { @@ -476,14 +434,134 @@ function onDeviceReady() { navigator.network.connection.type == Connection.UNKNOWN ) ) { document.location = 'no_connection.html'; } +} + +function get_report_params () { + var params = { + service: 'iphone', + title: $('#form_title').val(), + detail: $('#form_detail').val(), + may_show_name: $('#form_may_show_name').attr('checked') ? 1 : 0, + category: $('#form_category').val(), + lat: $('#fixmystreet\\.latitude').val(), + lon: $('#fixmystreet\\.longitude').val(), + phone: $('#form_phone').val(), + pc: $('#pc').val(), + time: new Date() + }; + + if ( localStorage.username && localStorage.password && localStorage.name ) { + params.name = localStorage.name; + params.email = localStorage.username; + params.password_sign_in = localStorage.password; + } else { + params.name = $('#form_name').val(); + params.email = $('#form_email').val(); + params.password_sign_in = $('#password_sign_in').val(); + } + + if ( $('#form_photo').val() !== '' ) { + fileURI = $('#form_photo').val(); + params.file = fileURI; + } + + return params; + +} + +function save_report() { + var params = get_report_params(); + + var r; + if ( localStorage.getObject( 'reports' ) ) { + r = localStorage.getObject( 'reports' ); + } else { + r = []; + } + r.push( params ); + localStorage.setObject('reports', r); + $.mobile.changePage('my_reports.html'); +} + +function display_saved_reports() { + if ( localStorage.getObject( 'reports' ) ) { + var r = localStorage.getObject('reports'); + var list = $('<ul id="current" class="issue-list-a tab open"></ul>'); + for ( i = 0; i < r.length; i++ ) { + if ( r[i] && r[i].title ) { + var item = $('<li class="saved-report" id="' + i + '"></li>'); + var date; + if ( r[i].time ) { + var date = new Date( r[i].time ); + date = date.getDate() + '-' + ( date.getMonth() + 1 ) + '-' + date.getFullYear(); + date += ' ' + date.getHour() + ':' + date.getMinute(); + } else { + date = ''; + } + var content = $('<a class="text"><h4>' + r[i].title + '</h4><small>' + date + '</small></a>'); + if ( r[i].file ) { + $('<img class="img" src="' + r[i].file + '" height="60" width="90">').prependTo(content); + } + content.appendTo(item); + item.appendTo(list); + } + } + list.appendTo('#reports'); + } else { + $("#reports").innerHTML('No Reports'); + } +} + +function open_saved_report_page(e) { + localStorage.currentReport = this.id; + $.mobile.changePage('report.html'); +} + +function display_saved_report() { + var r = localStorage.getObject('reports')[localStorage.currentReport]; + fixmystreet.latitude = r.lat; + fixmystreet.longitude = r.lon; + fixmystreet.pins = [ [ r.lat, r.lon, 'yellow', '', "", 'big' ] ]; + + $('#title').text(r.title); + $('#details').text(r.detail); + if ( r.file ) { + $('#photo').attr('src', r.file); + $('#photo_link').attr('href', r.file); + $('#report-img').show(); + } else { + $('#report-img').hide(); + } +} + +function submit_problem_show() { + if ( localStorage.name ) { + $('.form-focus-hidden').show(); + } else { + $('.form-focus-hidden').hide(); + } +} + +$(document).bind('pageinit', function() { $('#postcodeForm').submit(locate); $('#mapForm').submit(postReport); $('#signInForm').submit(sign_in); $('#ffo').click(getPosition); - $('#forget').click(forget); + $('#forget').on('click', forget); + $('#save_report').on('click', save_report); $('#mapForm :input[type=submit]').on('click', function() { submit_clicked = $(this); }); account(); - hideBusy(); -} +}); document.addEventListener("deviceready", onDeviceReady, false); + +$(document).delegate('#report-created', 'pageshow',function() { + var uri = CONFIG.FMS_URL + 'report/' + localStorage.report; + $('#report_url').html( '<a href="' + uri + '">' + uri + '</a>' ); +}); + +$(document).delegate('#account-page', 'pageshow', display_account_page); +$(document).delegate('#my-reports-page', 'pageshow', display_saved_reports); +$(document).delegate('#report-page', 'pageshow', display_saved_report); +$(document).delegate('#submit-problem', 'pageshow', submit_problem_show); +$(document).delegate('.saved-report', 'click', open_saved_report_page); |