/* * fixmystreet.js * FixMyStreet JavaScript */ 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( CONFIG.FMS_URL + '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(); } }); } /* * general height fixing function * * elem1: element to check against * elem2: target element * offset: this will be added (if present) to the final value, useful for height errors */ function heightFix(elem1, elem2, offset){ var h1 = $(elem1).height(), h2 = $(elem2).height(); if(offset === undefined){ offset = 0; } if(h1 > h2){ $(elem2).css({'min-height':h1+offset}); } } /* * very simple tab function * * elem: trigger element, must have an href attribute (so probably needs to be an ) */ function tabs(elem, indirect) { var href = elem.attr('href'); //stupid IE sometimes adds the full uri into the href attr, so trim var start = href.indexOf('#'), target = href.slice(start, href.length); if (indirect) { elem = $(target + '_tab'); } if(!$(target).hasClass('open')) { //toggle class on nav $('.tab-nav .active').removeClass('active'); elem.addClass('active'); //hide / show the right tab $('.tab.open').hide().removeClass('open'); $(target).show().addClass('open'); } } $(function(){ var $html = $('html'); $html.removeClass('no-js').addClass('js'); // Preload the new report pin document.createElement('img').src = '../i/pin-green.png'; var last_type; $(window).resize(function(){ var type = $('#site-header').css('borderTopWidth'); if (type == '4px') { type = 'mobile'; } else if (type == '0px') { type = 'desktop'; } else { return; } if (last_type == type) { return; } if (type == 'mobile') { $html.addClass('mobile'); $('#map_box').prependTo('.content').css({ zIndex: '', position: '', top: '', left: '', right: '', bottom: '', width: '', height: '10em', margin: '' }); if (typeof fixmystreet !== 'undefined') { fixmystreet.state_map = ''; // XXX } if (typeof fixmystreet !== 'undefined' && fixmystreet.page == 'around') { // Immediately go full screen map if on around page $('#site-header').hide(); $('#map_box').prependTo('.wrapper').css({ position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, height: 'auto', margin: 0 }); $('#fms_pan_zoom').css({ top: '2.75em !important' }); $('.big-green-banner') .addClass('mobile-map-banner') .appendTo('#map_box') .text('Place pin on map') .prepend('home'); } $('span.report-a-problem-btn').on('click.reportBtn', function(){ $('html, body').animate({scrollTop:0}, 500); }).css({ cursor:'pointer' }).on('hover.reportBtn', function(){ $(this).toggleClass('hover'); }); } else { // Make map full screen on non-mobile sizes. $html.removeClass('mobile'); var map_pos = 'fixed', map_height = '100%'; if ($html.hasClass('ie6')) { map_pos = 'absolute'; map_height = $(window).height(); } $('#map_box').prependTo('.wrapper').css({ zIndex: 0, position: map_pos, top: 0, left: 0, right: 0, bottom: 0, width: '100%', height: map_height, margin: 0 }); if (typeof fixmystreet !== 'undefined') { fixmystreet.state_map = 'full'; } if (typeof fixmystreet !== 'undefined' && fixmystreet.page == 'around') { // Remove full-screen-ness $('#site-header').show(); $('#fms_pan_zoom').css({ top: '4.75em !important' }); $('.big-green-banner') .removeClass('mobile-map-banner') .prependTo('#side') .text('Click map to report a problem'); } $('span.report-a-problem-btn').css({ cursor:'' }).off('.reportBtn'); } last_type = type; }); //add mobile class if small screen $(window).resize(); $('#pc').focus(); $('input[type=submit]').removeAttr('disabled'); /* $('#mapForm').submit(function() { if (this.submit_problem) { $('input[type=submit]', this).prop("disabled", true); } return true; }); */ if (!$('#been_fixed_no').prop('checked') && !$('#been_fixed_unknown').prop('checked')) { $('#another_qn').hide(); } $('#been_fixed_no').click(function() { $('#another_qn').show('fast'); }); $('#been_fixed_unknown').click(function() { $('#another_qn').show('fast'); }); $('#been_fixed_yes').click(function() { $('#another_qn').hide('fast'); }); // FIXME - needs to use translated string jQuery.validator.addMethod('validCategory', function(value, element) { return this.optional(element) || value != '-- Pick a category --'; }, validation_strings.category ); jQuery.validator.addMethod('validName', function(value, element) { var validNamePat = /\ba\s*n+on+((y|o)mo?u?s)?(ly)?\b/i; return this.optional(element) || value.length > 5 && value.match( /\S/ ) && !value.match( validNamePat ); }, validation_strings.category ); var form_submitted = 0; var submitted = false; $("form.validate").validate({ rules: { title: { required: true }, detail: { required: true }, email: { required: true }, update: { required: true }, rznvy: { required: true } }, 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.before( error ); }, submitHandler: function(form) { if (form.submit_problem) { $('input[type=submit]', form).prop("disabled", true); } // this needs to be disabled otherwise it submits the form normally rather than // over AJAX. This comment is to make this more likely to happen when updating // this code. //form.submit(); }, // 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 - 120 ); } this.defaultShowErrors(); submitted = false; }, invalidHandler: function(form, validator) { submitted = true; } }); $('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 * 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(); } ); $('#submit_register').click( function(e) { $('#form_category').addClass('required validCategory').removeClass('valid'); $('#form_name').addClass('required validName'); } ); $('#problem_submit > input[type="submit"]').click( function(e) { $('#form_category').addClass('required validCategory').removeClass('valid'); $('#form_name').addClass('required validName'); } ); $('#update_post').click( function(e) { $('#form_name').addClass('required').removeClass('valid'); } ); $('#form_category').change( form_category_onchange ); // Geolocation if (geo_position_js.init()) { $('#postcodeForm').after('… or locate me automatically'); $('#geolocate_link').click(getPosition); } /* * Report a problem page */ //desktop if ($('#report-a-problem-sidebar').is(':visible')) { heightFix('#report-a-problem-sidebar', '.content', 26); } //show/hide notes on mobile $('.mobile #report-a-problem-sidebar').after('How to send successful reports').hide(); $('.rap-notes-trigger').click(function(e){ e.preventDefault(); //check if we've already moved the notes if($('.rap-notes').length > 0){ //if we have, show and hide .content $('.content').hide(); $('.rap-notes').show(); }else{ //if not, move them and show, hiding .content $('.content').after('
').hide(); $('#report-a-problem-sidebar').appendTo('.rap-notes').show().after('Back'); } $('html, body').scrollTop($('#report-a-problem-sidebar').offset().top); location.hash = 'rap-notes'; }); $('.mobile').on('click', '.rap-notes-close', function(e){ e.preventDefault(); //hide notes, show .content $('.content').show(); $('.rap-notes').hide(); $('html, body').scrollTop($('#mob_ok').offset().top); location.hash = 'report'; }); //move 'skip this step' link on mobile $('.mobile #skip-this-step').hide(); $('.mobile #skip-this-step a').addClass('chevron').wrap('