diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-03-09 18:51:13 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-03-10 16:10:15 +0000 |
commit | f2756c4c825ba73c76fafd84acc3663ea410858a (patch) | |
tree | b32d957d96541aba24b597eb60a6d607e72de469 | |
parent | 7074b5713d08636589be1682dcd31cd3d7542638 (diff) |
Move staff-only JavaScript to separate file.
-rw-r--r-- | templates/web/base/common_scripts.html | 8 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/admin.js (renamed from web/js/fixmystreet-admin.js) | 0 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/fixmystreet.js | 358 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/staff.js | 342 |
4 files changed, 362 insertions, 346 deletions
diff --git a/templates/web/base/common_scripts.html b/templates/web/base/common_scripts.html index 1d53f1d51..42c04f11f 100644 --- a/templates/web/base/common_scripts.html +++ b/templates/web/base/common_scripts.html @@ -16,6 +16,12 @@ scripts.push( version('/cobrands/fixmystreet/fixmystreet.js'), ); +IF c.user_exists AND (c.user.from_body OR c.user.is_superuser); + scripts.push( + version('/cobrands/fixmystreet/staff.js') + ); +END; + FOR script IN map_js; scripts.push(script); END; @@ -28,7 +34,7 @@ scripts.push( IF admin; scripts.push( version('/js/jquery-ui/js/jquery-ui-1.10.3.custom.min.js'), - version('/js/fixmystreet-admin.js'), + version('/cobrands/fixmystreet/admin.js'), ); END; diff --git a/web/js/fixmystreet-admin.js b/web/cobrands/fixmystreet/admin.js index 02eb30766..02eb30766 100644 --- a/web/js/fixmystreet-admin.js +++ b/web/cobrands/fixmystreet/admin.js diff --git a/web/cobrands/fixmystreet/fixmystreet.js b/web/cobrands/fixmystreet/fixmystreet.js index a0d830a65..df8285942 100644 --- a/web/cobrands/fixmystreet/fixmystreet.js +++ b/web/cobrands/fixmystreet/fixmystreet.js @@ -222,6 +222,14 @@ fixmystreet.update_list_item_buttons = function($list) { $list.children(':last-child').find('[name="shortlist-down"]').prop('disabled', true); }; +// A tiny helper to call a function only if it exists (so we can +// call this with staff-only functions and they won't error). +fixmystreet.run = function(fn) { + if (fn) { + fn.call(this); + } +}; + fixmystreet.set_up = fixmystreet.set_up || {}; $.extend(fixmystreet.set_up, { basics: function() { @@ -406,199 +414,6 @@ $.extend(fixmystreet.set_up, { }); }, - manage_duplicates: function() { - // Deal with changes to report state by inspector/other staff, specifically - // displaying nearby reports if it's changed to 'duplicate'. - function refresh_duplicate_list() { - var report_id = $("#report_inspect_form .js-report-id").text(); - var args = { - filter_category: $("#report_inspect_form select#category").val(), - latitude: $('input[name="latitude"]').val(), - longitude: $('input[name="longitude"]').val() - }; - $("#js-duplicate-reports ul").html("<li>Loading...</li>"); - var nearby_url = '/report/'+report_id+'/nearby.json'; - $.getJSON(nearby_url, args, function(data) { - var duplicate_of = $("#report_inspect_form [name=duplicate_of]").val(); - var $reports = $(data.current) - .filter("li") - .not("[data-report-id="+report_id+"]") - .slice(0, 5); - $reports.filter("[data-report-id="+duplicate_of+"]").addClass("item-list--reports__item--selected"); - - (function() { - var timeout; - $reports.on('mouseenter', function(){ - clearTimeout(timeout); - fixmystreet.maps.markers_highlight(parseInt($(this).data('reportId'), 10)); - }).on('mouseleave', function(){ - timeout = setTimeout(fixmystreet.maps.markers_highlight, 50); - }); - })(); - - $("#js-duplicate-reports ul").empty().prepend($reports); - - $reports.find("a").click(function() { - var report_id = $(this).closest("li").data('reportId'); - $("#report_inspect_form [name=duplicate_of]").val(report_id); - $("#js-duplicate-reports ul li").removeClass("item-list--reports__item--selected"); - $(this).closest("li").addClass("item-list--reports__item--selected"); - return false; - }); - - show_nearby_pins(data, report_id); - }); - } - - function show_nearby_pins(data, report_id) { - var markers = fixmystreet.maps.markers_list( data.pins, true ); - // We're replacing all the features in the markers layer with the - // possible duplicates, but the list of pins from the server doesn't - // include the current report. So we need to extract the feature for - // the current report and include it in the list of features we're - // showing on the layer. - var report_marker = fixmystreet.maps.get_marker_by_id(parseInt(report_id, 10)); - if (report_marker) { - markers.unshift(report_marker); - } - fixmystreet.markers.removeAllFeatures(); - fixmystreet.markers.addFeatures( markers ); - } - - function state_change() { - // The duplicate report list only makes sense when state is 'duplicate' - if ($(this).val() !== "duplicate") { - $("#js-duplicate-reports").addClass("hidden"); - return; - } else { - $("#js-duplicate-reports").removeClass("hidden"); - } - // If this report is already marked as a duplicate of another, then - // there's no need to refresh the list of duplicate reports - var duplicate_of = $("#report_inspect_form [name=duplicate_of]").val(); - if (!!duplicate_of) { - return; - } - - refresh_duplicate_list(); - } - - $("#report_inspect_form").on("change.state", "select#state", state_change); - $("#js-change-duplicate-report").click(refresh_duplicate_list); - }, - - list_item_actions: function() { - function toggle_shortlist(btn, sw, id) { - btn.attr('class', 'item-list__item__shortlist-' + sw); - btn.attr('title', btn.data('label-' + sw)); - if (id) { - sw += '-' + id; - } - btn.attr('name', 'shortlist-' + sw); - } - - $('.item-list--reports').on('click', ':submit', function(e) { - e.preventDefault(); - - var $submitButton = $(this); - var whatUserWants = $submitButton.prop('name'); - var data; - var $item; - var $list; - var $hiddenInput; - var report_id; - if (fixmystreet.page === 'around') { - // Deal differently because one big form - var parts = whatUserWants.split('-'); - whatUserWants = parts[0] + '-' + parts[1]; - report_id = parts[2]; - var token = $('[name=token]').val(); - data = whatUserWants + '=1&token=' + token + '&id=' + report_id; - } else { - var $form = $(this).parents('form'); - $item = $form.parent('.item-list__item'); - $list = $item.parent('.item-list'); - - // The server expects to be told which button/input triggered the form - // submission. But $form.serialize() doesn't know that. So we inject a - // hidden input into the form, that can pass the name and value of the - // submit button to the server, as it expects. - $hiddenInput = $('<input>').attr({ - type: 'hidden', - name: whatUserWants, - value: $submitButton.prop('value') - }).appendTo($form); - data = $form.serialize() + '&ajax=1'; - } - - // Update UI while the ajax request is sent in the background. - if ('shortlist-down' === whatUserWants) { - $item.insertAfter( $item.next() ); - } else if ('shortlist-up' === whatUserWants) { - $item.insertBefore( $item.prev() ); - } else if ('shortlist-remove' === whatUserWants) { - toggle_shortlist($submitButton, 'add', report_id); - } else if ('shortlist-add' === whatUserWants) { - toggle_shortlist($submitButton, 'remove', report_id); - } - - // Items have moved around. We need to make sure the "up" button on the - // first item, and the "down" button on the last item, are disabled. - fixmystreet.update_list_item_buttons($list); - - $.ajax({ - url: '/my/planned/change', - type: 'POST', - data: data - }).fail(function() { - // Undo the UI changes we made. - if ('shortlist-down' === whatUserWants) { - $item.insertBefore( $item.prev() ); - } else if ('shortlist-up' === whatUserWants) { - $item.insertAfter( $item.next() ); - } else if ('shortlist-remove' === whatUserWants) { - toggle_shortlist($submitButton, 'remove', report_id); - } else if ('shortlist-add' === whatUserWants) { - toggle_shortlist($submitButton, 'add', report_id); - } - fixmystreet.update_list_item_buttons($list); - }).complete(function() { - if ($hiddenInput) { - $hiddenInput.remove(); - } - }); - }); - }, - - contribute_as: function() { - $('.content').on('change', '.js-contribute-as', function(){ - var opt = this.options[this.selectedIndex], - val = opt.value, - txt = opt.text; - var $emailInput = $('input[name=email]').add('input[name=rznvy]'); - var $nameInput = $('input[name=name]'); - var $showNameCheckbox = $('input[name=may_show_name]'); - var $addAlertCheckbox = $('#form_add_alert'); - if (val === 'myself') { - $emailInput.val($emailInput.prop('defaultValue')).prop('disabled', true); - $nameInput.val($nameInput.prop('defaultValue')).prop('disabled', false); - $showNameCheckbox.prop('checked', false).prop('disabled', false); - $addAlertCheckbox.prop('checked', true).prop('disabled', false); - } else if (val === 'another_user') { - $emailInput.val('').prop('disabled', false); - $nameInput.val('').prop('disabled', false); - $showNameCheckbox.prop('checked', false).prop('disabled', true); - $addAlertCheckbox.prop('checked', true).prop('disabled', false); - } else if (val === 'body') { - $emailInput.val('-').prop('disabled', true); - $nameInput.val(txt).prop('disabled', true); - $showNameCheckbox.prop('checked', true).prop('disabled', true); - $addAlertCheckbox.prop('checked', false).prop('disabled', true); - } - }); - $('.js-contribute-as').change(); - }, - on_resize: function() { var last_type; $(window).on('resize', function() { @@ -737,86 +552,6 @@ $.extend(fixmystreet.set_up, { make_multi('filter_categories'); }, - report_page_inspect: function() { - if (!$('form#report_inspect_form').length) { - return; - } - - // Focus on form - $('html,body').scrollTop($('#report_inspect_form').offset().top); - - // On the manage/inspect report form, we already have all the extra inputs - // in the DOM, we just need to hide/show them as appropriate. - $('form#report_inspect_form [name=category]').change(function() { - var category = $(this).val(), - selector = "[data-category='" + category + "']"; - $("form#report_inspect_form [data-category]:not(" + selector + ")").addClass("hidden"); - $("form#report_inspect_form " + selector).removeClass("hidden"); - // And update the associated priority list - var priorities = $("form#report_inspect_form " + selector).data('priorities'); - var $select = $('#problem_priority'), - curr_pri = $select.val(); - $select.find('option:gt(0)').remove(); - $.each(priorities.split('&'), function(i, kv) { - if (!kv) { - return; - } - kv = kv.split('=', 2); - $select.append($('<option>', { value: kv[0], text: decodeURIComponent(kv[1]) })); - }); - $select.val(curr_pri); - }); - - // The inspect form submit button can change depending on the selected state - $("#report_inspect_form [name=state]").change(function(){ - var state = $(this).val(); - var $inspect_form = $("#report_inspect_form"); - var $submit = $inspect_form.find("input[type=submit]"); - var value = $submit.attr('data-value-'+state); - if (value !== undefined) { - $submit.val(value); - } else { - $submit.val($submit.data('valueOriginal')); - } - - // We might also have a response template to preselect for the new state - var $select = $inspect_form.find("select.js-template-name"); - var $option = $select.find("option[data-problem-state='"+state+"']").first(); - if ($option.length) { - $select.val($option.val()).change(); - } - }).change(); - - $('.js-toggle-public-update').each(function() { - var $checkbox = $(this); - var toggle_public_update = function() { - if ($checkbox.prop('checked')) { - $('#public_update').parents('p').show(); - } else { - $('#public_update').parents('p').hide(); - } - }; - $checkbox.on('change', function() { - toggle_public_update(); - }); - toggle_public_update(); - }); - - if (geo_position_js.init()) { - fixmystreet.geolocate.setup(function(pos) { - var latlon = new OpenLayers.LonLat(pos.coords.longitude, pos.coords.latitude); - var bng = latlon.clone().transform( - new OpenLayers.Projection("EPSG:4326"), - new OpenLayers.Projection("EPSG:27700") // TODO: Handle other projections - ); - $("#problem_northing").text(bng.lat.toFixed(1)); - $("#problem_easting").text(bng.lon.toFixed(1)); - $("form#report_inspect_form input[name=latitude]").val(latlon.lat); - $("form#report_inspect_form input[name=longitude]").val(latlon.lon); - }); - } - }, - mobile_ui_tweaks: function() { //move 'skip this step' link on mobile $('.mobile #skip-this-step').addClass('chevron').wrap('<li>').parent().appendTo('#key-tools'); @@ -1064,75 +799,8 @@ $.extend(fixmystreet.set_up, { } }); }); - }, - - moderation: function() { - function toggle_original ($input, revert) { - $input.prop('disabled', revert); - if (revert) { - $input.data('currentValue', $input.val()); - } - $input.val($input.data(revert ? 'originalValue' : 'currentValue')); - } - - function add_handlers (elem, word) { - elem.each( function () { - var $elem = $(this); - $elem.find('.js-moderate').on('click', function () { - $elem.find('.moderate-display').hide(); - $elem.find('.moderate-edit').show(); - }); - - $elem.find('.revert-title').change( function () { - toggle_original($elem.find('input[name=problem_title]'), $(this).prop('checked')); - }); - - $elem.find('.revert-textarea').change( function () { - toggle_original($elem.find('textarea'), $(this).prop('checked')); - }); - - var hide_document = $elem.find('.hide-document'); - hide_document.change( function () { - $elem.find('input[name=problem_title]').prop('disabled', $(this).prop('checked')); - $elem.find('textarea').prop('disabled', $(this).prop('checked')); - $elem.find('input[type=checkbox]').prop('disabled', $(this).prop('checked')); - $(this).prop('disabled', false); // in case disabled above - }); - - $elem.find('.cancel').click( function () { - $elem.find('.moderate-display').show(); - $elem.find('.moderate-edit').hide(); - }); - - $elem.find('form').submit( function () { - if (hide_document.prop('checked')) { - return confirm('This will hide the ' + word + ' completely! (You will not be able to undo this without contacting support.)'); - } - return true; - }); - }); - } - add_handlers( $('.problem-header'), 'problem' ); - add_handlers( $('.item-list__item--updates'), 'update' ); - }, - - response_templates: function() { - // If the user has manually edited the contents of an update field, - // mark it as dirty so it doesn't get clobbered if we select another - // response template. If the field is empty, it's not considered dirty. - $('.js-template-name').each(function() { - var $input = $('#' + $(this).data('for')); - $input.change(function() { $(this).data('dirty', !/^\s*$/.test($(this).val())); }); - }); - - $('.js-template-name').change(function() { - var $this = $(this); - var $input = $('#' + $this.data('for')); - if (!$input.data('dirty')) { - $input.val($this.val()); - } - }); } + }); // The new location will be saved to a history state unless @@ -1328,8 +996,8 @@ fixmystreet.display = { if ($twoColReport.length) { $twoColReport.appendTo('#map_sidebar'); $('body').addClass('with-actions'); - fixmystreet.set_up.report_page_inspect(); - fixmystreet.set_up.manage_duplicates(); + fixmystreet.run(fixmystreet.set_up.report_page_inspect); + fixmystreet.run(fixmystreet.set_up.manage_duplicates); } else { $sideReport.appendTo('#map_sidebar'); } @@ -1371,8 +1039,8 @@ fixmystreet.display = { fixmystreet.set_up.fancybox_images(); fixmystreet.set_up.dropzone($sideReport); fixmystreet.set_up.form_focus_triggers(); - fixmystreet.set_up.moderation(); - fixmystreet.set_up.response_templates(); + fixmystreet.run(fixmystreet.set_up.moderation); + fixmystreet.run(fixmystreet.set_up.response_templates); window.selected_problem_id = reportId; var marker = fixmystreet.maps.get_marker_by_id(reportId); diff --git a/web/cobrands/fixmystreet/staff.js b/web/cobrands/fixmystreet/staff.js new file mode 100644 index 000000000..9825a37ea --- /dev/null +++ b/web/cobrands/fixmystreet/staff.js @@ -0,0 +1,342 @@ +$.extend(fixmystreet.set_up, { + manage_duplicates: function() { + // Deal with changes to report state by inspector/other staff, specifically + // displaying nearby reports if it's changed to 'duplicate'. + function refresh_duplicate_list() { + var report_id = $("#report_inspect_form .js-report-id").text(); + var args = { + filter_category: $("#report_inspect_form select#category").val(), + latitude: $('input[name="latitude"]').val(), + longitude: $('input[name="longitude"]').val() + }; + $("#js-duplicate-reports ul").html("<li>Loading...</li>"); + var nearby_url = '/report/'+report_id+'/nearby.json'; + $.getJSON(nearby_url, args, function(data) { + var duplicate_of = $("#report_inspect_form [name=duplicate_of]").val(); + var $reports = $(data.current) + .filter("li") + .not("[data-report-id="+report_id+"]") + .slice(0, 5); + $reports.filter("[data-report-id="+duplicate_of+"]").addClass("item-list--reports__item--selected"); + + (function() { + var timeout; + $reports.on('mouseenter', function(){ + clearTimeout(timeout); + fixmystreet.maps.markers_highlight(parseInt($(this).data('reportId'), 10)); + }).on('mouseleave', function(){ + timeout = setTimeout(fixmystreet.maps.markers_highlight, 50); + }); + })(); + + $("#js-duplicate-reports ul").empty().prepend($reports); + + $reports.find("a").click(function() { + var report_id = $(this).closest("li").data('reportId'); + $("#report_inspect_form [name=duplicate_of]").val(report_id); + $("#js-duplicate-reports ul li").removeClass("item-list--reports__item--selected"); + $(this).closest("li").addClass("item-list--reports__item--selected"); + return false; + }); + + show_nearby_pins(data, report_id); + }); + } + + function show_nearby_pins(data, report_id) { + var markers = fixmystreet.maps.markers_list( data.pins, true ); + // We're replacing all the features in the markers layer with the + // possible duplicates, but the list of pins from the server doesn't + // include the current report. So we need to extract the feature for + // the current report and include it in the list of features we're + // showing on the layer. + var report_marker = fixmystreet.maps.get_marker_by_id(parseInt(report_id, 10)); + if (report_marker) { + markers.unshift(report_marker); + } + fixmystreet.markers.removeAllFeatures(); + fixmystreet.markers.addFeatures( markers ); + } + + function state_change() { + // The duplicate report list only makes sense when state is 'duplicate' + if ($(this).val() !== "duplicate") { + $("#js-duplicate-reports").addClass("hidden"); + return; + } else { + $("#js-duplicate-reports").removeClass("hidden"); + } + // If this report is already marked as a duplicate of another, then + // there's no need to refresh the list of duplicate reports + var duplicate_of = $("#report_inspect_form [name=duplicate_of]").val(); + if (!!duplicate_of) { + return; + } + + refresh_duplicate_list(); + } + + $("#report_inspect_form").on("change.state", "select#state", state_change); + $("#js-change-duplicate-report").click(refresh_duplicate_list); + }, + + list_item_actions: function() { + function toggle_shortlist(btn, sw, id) { + btn.attr('class', 'item-list__item__shortlist-' + sw); + btn.attr('title', btn.data('label-' + sw)); + if (id) { + sw += '-' + id; + } + btn.attr('name', 'shortlist-' + sw); + } + + $('.item-list--reports').on('click', ':submit', function(e) { + e.preventDefault(); + + var $submitButton = $(this); + var whatUserWants = $submitButton.prop('name'); + var data; + var $item; + var $list; + var $hiddenInput; + var report_id; + if (fixmystreet.page === 'around') { + // Deal differently because one big form + var parts = whatUserWants.split('-'); + whatUserWants = parts[0] + '-' + parts[1]; + report_id = parts[2]; + var token = $('[name=token]').val(); + data = whatUserWants + '=1&token=' + token + '&id=' + report_id; + } else { + var $form = $(this).parents('form'); + $item = $form.parent('.item-list__item'); + $list = $item.parent('.item-list'); + + // The server expects to be told which button/input triggered the form + // submission. But $form.serialize() doesn't know that. So we inject a + // hidden input into the form, that can pass the name and value of the + // submit button to the server, as it expects. + $hiddenInput = $('<input>').attr({ + type: 'hidden', + name: whatUserWants, + value: $submitButton.prop('value') + }).appendTo($form); + data = $form.serialize() + '&ajax=1'; + } + + // Update UI while the ajax request is sent in the background. + if ('shortlist-down' === whatUserWants) { + $item.insertAfter( $item.next() ); + } else if ('shortlist-up' === whatUserWants) { + $item.insertBefore( $item.prev() ); + } else if ('shortlist-remove' === whatUserWants) { + toggle_shortlist($submitButton, 'add', report_id); + } else if ('shortlist-add' === whatUserWants) { + toggle_shortlist($submitButton, 'remove', report_id); + } + + // Items have moved around. We need to make sure the "up" button on the + // first item, and the "down" button on the last item, are disabled. + fixmystreet.update_list_item_buttons($list); + + $.ajax({ + url: '/my/planned/change', + type: 'POST', + data: data + }).fail(function() { + // Undo the UI changes we made. + if ('shortlist-down' === whatUserWants) { + $item.insertBefore( $item.prev() ); + } else if ('shortlist-up' === whatUserWants) { + $item.insertAfter( $item.next() ); + } else if ('shortlist-remove' === whatUserWants) { + toggle_shortlist($submitButton, 'remove', report_id); + } else if ('shortlist-add' === whatUserWants) { + toggle_shortlist($submitButton, 'add', report_id); + } + fixmystreet.update_list_item_buttons($list); + }).complete(function() { + if ($hiddenInput) { + $hiddenInput.remove(); + } + }); + }); + }, + + contribute_as: function() { + $('.content').on('change', '.js-contribute-as', function(){ + var opt = this.options[this.selectedIndex], + val = opt.value, + txt = opt.text; + var $emailInput = $('input[name=email]').add('input[name=rznvy]'); + var $nameInput = $('input[name=name]'); + var $showNameCheckbox = $('input[name=may_show_name]'); + var $addAlertCheckbox = $('#form_add_alert'); + if (val === 'myself') { + $emailInput.val($emailInput.prop('defaultValue')).prop('disabled', true); + $nameInput.val($nameInput.prop('defaultValue')).prop('disabled', false); + $showNameCheckbox.prop('checked', false).prop('disabled', false); + $addAlertCheckbox.prop('checked', true).prop('disabled', false); + } else if (val === 'another_user') { + $emailInput.val('').prop('disabled', false); + $nameInput.val('').prop('disabled', false); + $showNameCheckbox.prop('checked', false).prop('disabled', true); + $addAlertCheckbox.prop('checked', true).prop('disabled', false); + } else if (val === 'body') { + $emailInput.val('-').prop('disabled', true); + $nameInput.val(txt).prop('disabled', true); + $showNameCheckbox.prop('checked', true).prop('disabled', true); + $addAlertCheckbox.prop('checked', false).prop('disabled', true); + } + }); + $('.js-contribute-as').change(); + }, + + report_page_inspect: function() { + if (!$('form#report_inspect_form').length) { + return; + } + + // Focus on form + $('html,body').scrollTop($('#report_inspect_form').offset().top); + + // On the manage/inspect report form, we already have all the extra inputs + // in the DOM, we just need to hide/show them as appropriate. + $('form#report_inspect_form [name=category]').change(function() { + var category = $(this).val(), + selector = "[data-category='" + category + "']"; + $("form#report_inspect_form [data-category]:not(" + selector + ")").addClass("hidden"); + $("form#report_inspect_form " + selector).removeClass("hidden"); + // And update the associated priority list + var priorities = $("form#report_inspect_form " + selector).data('priorities'); + var $select = $('#problem_priority'), + curr_pri = $select.val(); + $select.find('option:gt(0)').remove(); + $.each(priorities.split('&'), function(i, kv) { + if (!kv) { + return; + } + kv = kv.split('=', 2); + $select.append($('<option>', { value: kv[0], text: decodeURIComponent(kv[1]) })); + }); + $select.val(curr_pri); + }); + + // The inspect form submit button can change depending on the selected state + $("#report_inspect_form [name=state]").change(function(){ + var state = $(this).val(); + var $inspect_form = $("#report_inspect_form"); + var $submit = $inspect_form.find("input[type=submit]"); + var value = $submit.attr('data-value-'+state); + if (value !== undefined) { + $submit.val(value); + } else { + $submit.val($submit.data('valueOriginal')); + } + + // We might also have a response template to preselect for the new state + var $select = $inspect_form.find("select.js-template-name"); + var $option = $select.find("option[data-problem-state='"+state+"']").first(); + if ($option.length) { + $select.val($option.val()).change(); + } + }).change(); + + $('.js-toggle-public-update').each(function() { + var $checkbox = $(this); + var toggle_public_update = function() { + if ($checkbox.prop('checked')) { + $('#public_update').parents('p').show(); + } else { + $('#public_update').parents('p').hide(); + } + }; + $checkbox.on('change', function() { + toggle_public_update(); + }); + toggle_public_update(); + }); + + if (geo_position_js.init()) { + fixmystreet.geolocate.setup(function(pos) { + var latlon = new OpenLayers.LonLat(pos.coords.longitude, pos.coords.latitude); + var bng = latlon.clone().transform( + new OpenLayers.Projection("EPSG:4326"), + new OpenLayers.Projection("EPSG:27700") // TODO: Handle other projections + ); + $("#problem_northing").text(bng.lat.toFixed(1)); + $("#problem_easting").text(bng.lon.toFixed(1)); + $("form#report_inspect_form input[name=latitude]").val(latlon.lat); + $("form#report_inspect_form input[name=longitude]").val(latlon.lon); + }); + } + }, + + moderation: function() { + function toggle_original ($input, revert) { + $input.prop('disabled', revert); + if (revert) { + $input.data('currentValue', $input.val()); + } + $input.val($input.data(revert ? 'originalValue' : 'currentValue')); + } + + function add_handlers (elem, word) { + elem.each( function () { + var $elem = $(this); + $elem.find('.js-moderate').on('click', function () { + $elem.find('.moderate-display').hide(); + $elem.find('.moderate-edit').show(); + }); + + $elem.find('.revert-title').change( function () { + toggle_original($elem.find('input[name=problem_title]'), $(this).prop('checked')); + }); + + $elem.find('.revert-textarea').change( function () { + toggle_original($elem.find('textarea'), $(this).prop('checked')); + }); + + var hide_document = $elem.find('.hide-document'); + hide_document.change( function () { + $elem.find('input[name=problem_title]').prop('disabled', $(this).prop('checked')); + $elem.find('textarea').prop('disabled', $(this).prop('checked')); + $elem.find('input[type=checkbox]').prop('disabled', $(this).prop('checked')); + $(this).prop('disabled', false); // in case disabled above + }); + + $elem.find('.cancel').click( function () { + $elem.find('.moderate-display').show(); + $elem.find('.moderate-edit').hide(); + }); + + $elem.find('form').submit( function () { + if (hide_document.prop('checked')) { + return confirm('This will hide the ' + word + ' completely! (You will not be able to undo this without contacting support.)'); + } + return true; + }); + }); + } + add_handlers( $('.problem-header'), 'problem' ); + add_handlers( $('.item-list__item--updates'), 'update' ); + }, + + response_templates: function() { + // If the user has manually edited the contents of an update field, + // mark it as dirty so it doesn't get clobbered if we select another + // response template. If the field is empty, it's not considered dirty. + $('.js-template-name').each(function() { + var $input = $('#' + $(this).data('for')); + $input.change(function() { $(this).data('dirty', !/^\s*$/.test($(this).val())); }); + }); + + $('.js-template-name').change(function() { + var $this = $(this); + var $input = $('#' + $this.data('for')); + if (!$input.data('dirty')) { + $input.val($this.val()); + } + }); + } +}); |