diff options
Diffstat (limited to 'web/js/fixmystreet.js')
-rw-r--r-- | web/js/fixmystreet.js | 171 |
1 files changed, 88 insertions, 83 deletions
diff --git a/web/js/fixmystreet.js b/web/js/fixmystreet.js index 834aa4ed3..36d47b5b2 100644 --- a/web/js/fixmystreet.js +++ b/web/js/fixmystreet.js @@ -1,51 +1,31 @@ /* * fixmystreet.js - * FixMyStreet JavaScript + * FixMyStreet JavaScript used by all cobrands. + * With the JavaScript written more proper like. */ -function form_category_onchange() { - var cat = $('#form_category'); - var args = { - category: cat.val() - }; - - if ( typeof fixmystreet !== 'undefined' ) { - args.latitude = fixmystreet.latitude; - args.longitude = fixmystreet.longitude; - } else { - args.latitude = $('input[name="latitude"]').val(); - args.longitude = $('input[name="longitude"]').val(); - } - - $.getJSON('/report/new/category_extras', args, function(data) { - if ( data.category_extra ) { - if ( $('#category_meta').size() ) { - $('#category_meta').html( data.category_extra); - } else { - $('#form_category_row').after( data.category_extra ); - } - } else { - $('#category_meta').empty(); - } - }); -} +(function($){ +/* + Deal with changes to category by asking for details from the server. + */ $(function(){ - $('html').removeClass('no-js').addClass('js'); + var $html = $('html'); + + // Add a class to the whole page saying JavaScript is enabled (for CSS and so on) + $html.removeClass('no-js').addClass('js'); + // Preload the new report pin + document.createElement('img').src = '/i/pin-green.png'; + + // Focus on postcode box on front page $('#pc').focus(); + // In case we've come here by clicking back to a form that disabled a submit button $('input[type=submit]').removeAttr('disabled'); - /* - $('#mapForm').submit(function() { - if (this.submit_problem) { - $('input[type=submit]', this).prop("disabled", true); - } - return true; - }); - */ + // Questionnaire hide/showings if (!$('#been_fixed_no').prop('checked') && !$('#been_fixed_unknown').prop('checked')) { $('#another_qn').hide(); } @@ -59,10 +39,7 @@ $(function(){ $('#another_qn').hide('fast'); }); - var timer; - function email_alert_close() { - $('#email_alert_box').hide('fast'); - } + // Form validation // FIXME - needs to use translated string jQuery.validator.addMethod('validCategory', function(value, element) { @@ -85,12 +62,18 @@ $(function(){ }, messages: validation_strings, onkeyup: false, + onfocusout: false, errorElement: 'div', errorClass: 'form-error', // we do this to stop things jumping around on blur success: function (err) { if ( form_submitted ) { err.addClass('label-valid').removeClass('label-valid-hidden').html( ' ' ); } else { err.addClass('label-valid-hidden'); } }, errorPlacement: function( error, element ) { - element.parent('div').before( error ); + // Different for old/new style design + if ($('.form-field').length) { + element.parent('div.form-field').before( error ); + } else { + element.before( error ); + } }, submitHandler: function(form) { if (form.submit_problem) { @@ -102,7 +85,7 @@ $(function(){ // make sure we can see the error message when we focus on invalid elements showErrors: function( errorMap, errorList ) { if ( submitted && errorList.length ) { - $(window).scrollTop( $(errorList[0].element).offset().top - 40 ); + $(window).scrollTop( $(errorList[0].element).offset().top - 120 ); } this.defaultShowErrors(); submitted = false; @@ -112,83 +95,105 @@ $(function(){ $('input[type=submit]').click( function(e) { form_submitted = 1; } ); - /* set correct required status depending on what we submit - * NB: need to add things to form_category as the JS updating + /* set correct required status depending on what we submit + * NB: need to add things to form_category as the JS updating * of this we do after a map click removes them */ $('#submit_sign_in').click( function(e) { $('#form_category').addClass('required validCategory').removeClass('valid'); $('#form_name').removeClass(); + $('#form_first_name').removeClass(); + $('#form_last_name').removeClass(); + $('#form_fms_extra_title').removeClass(); } ); - $('#submit_register').click( function(e) { + $('#submit_register').click( function(e) { $('#form_category').addClass('required validCategory').removeClass('valid'); $('#form_name').addClass('required validName'); + $('#form_first_name').addClass('required'); + $('#form_last_name').addClass('required'); + $('#form_fms_extra_title').addClass('required'); } ); - $('#problem_submit > input[type="submit"]').click( function(e) { + $('#problem_submit > input[type="submit"]').click( function(e) { $('#form_category').addClass('required validCategory').removeClass('valid'); $('#form_name').addClass('required validName'); + $('#form_first_name').addClass('required'); + $('#form_last_name').addClass('required'); + $('#form_fms_extra_title').addClass('required'); } ); - $('#update_post').click( function(e) { + $('#update_post').click( function(e) { $('#form_name').addClass('required').removeClass('valid'); } ); - $('#email_alert').click(function(e) { - if (!$('#email_alert_box').length) { - return true; - } - e.preventDefault(); - if ($('#email_alert_box').is(':visible')) { - email_alert_close(); - } else { - var pos = $(this).position(); - $('#email_alert_box').css( { 'left': ( pos.left - 20 ) + 'px', 'top': ( pos.top + 20 ) + 'px' } ); - $('#email_alert_box').show('fast'); - $('#alert_rznvy').focus(); - } - }).hover(function() { - window.clearTimeout(timer); - }, function() { - timer = window.setTimeout(email_alert_close, 2000); - }); - - $('#email_alert_box').hover(function() { - window.clearTimeout(timer); - }, function() { - timer = window.setTimeout(email_alert_close, 2000); - }); - - // Using delegate here because we *might* be running under a cobrand which isn't jQuery 1.7+ - // Delegation is necessary because #form_category may be replaced during the lifetime of the page - $("#problem_form").delegate("select#form_category", "change", form_category_onchange ); - // Geolocation if (geo_position_js.init()) { - $('#postcodeForm').append('<p id="geolocate_para">Or <a href="#" id="geolocate_link">locate me automatically</a>').css({ "padding-bottom": "0.5em" }); + if ($('body.frontpage').length) { + $('#postcodeForm').after('<a href="#" id="geolocate_link">… or locate me automatically</a>'); + } else{ + $('#postcodeForm').append('<a href="#" id="geolocate_link">… or locate me automatically</a>'); + } $('#geolocate_link').click(function(e) { + var $link = $(this); e.preventDefault(); // Spinny thing! - $('#geolocate_para').append(' <img src="/i/flower.gif" alt="" align="bottom">'); + if($('.mobile').length){ + $link.append(' <img src="/cobrands/fixmystreet/images/spinner-black.gif" alt="" align="bottom">'); + }else{ + $link.append(' <img src="/cobrands/fixmystreet/images/spinner-yellow.gif" alt="" align="bottom">'); + } geo_position_js.getCurrentPosition(function(pos) { - $('#geolocate_para img').remove(); + $link.find('img').remove(); var latitude = pos.coords.latitude; var longitude = pos.coords.longitude; location.href = '/around?latitude=' + latitude + ';longitude=' + longitude; }, function(err) { - $('#geolocate_para img').remove(); + $link.find('img').remove(); if (err.code == 1) { // User said no + $link.html("You declined; please fill in the box above"); } else if (err.code == 2) { // No position - $('#geolocate_para').html("Could not look up location"); + $link.html("Could not look up location"); } else if (err.code == 3) { // Too long - $('#geolocate_para').html("No result returned"); + $link.html("No result returned"); } else { // Unknown - $('#geolocate_para').html("Unknown error"); + $link.html("Unknown error"); } }, { + enableHighAccuracy: true, timeout: 10000 }); }); } + // Delegation is necessary because #form_category may be replaced during the lifetime of the page + $("#problem_form").on("change.category", "select#form_category", function(){ + var args = { + category: $(this).val() + }; + + if ( typeof fixmystreet !== 'undefined' ) { + args.latitude = fixmystreet.latitude; + args.longitude = fixmystreet.longitude; + } else { + args.latitude = $('input[name="latitude"]').val(); + args.longitude = $('input[name="longitude"]').val(); + } + + $.getJSON('/report/new/category_extras', args, function(data) { + var $category_meta = $('#category_meta'); + if ( data.category_extra ) { + if ( $category_meta.length ) { + $category_meta.html( data.category_extra ); + } else { + $('#form_category_row').after( data.category_extra ); + } + } else { + $category_meta.empty(); + } + }); + }); + }); + +})(jQuery); + |