Skip to content

Commit

Permalink
Make sure moderation works on AJAX-loaded report.
Browse files Browse the repository at this point in the history
This refactors the moderation JS into a fixmystreet.set_up function, and
ensures the button handlers are attached when a new report is loaded
over AJAX. A side effect of bringing it into fixmystreet.js instead of
its own moderate.js file is the few extra kb each user will have to
download, but hopefully gzip and caching will help ameliorate this.
  • Loading branch information
davea authored and dracos committed Aug 26, 2016
1 parent 0e2e741 commit 4f7664c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
5 changes: 0 additions & 5 deletions templates/web/base/common_footer_tags.html
Expand Up @@ -22,11 +22,6 @@
<script type="text/javascript" src="[% version('/js/fixmystreet-admin.js') %]"></script>
[% END %]

[% moderating = c.user && c.user.has_permission_to('moderate', problem.bodies_str) %]
[% IF moderating %]
<script type="text/javascript" src="[% version('/js/moderate.js') %]"></script>
[% END %]

[% extra_js %]

[% TRY %][% PROCESS 'footer_extra_js.html' %][% CATCH file %][% END %]
51 changes: 51 additions & 0 deletions web/cobrands/fixmystreet/fixmystreet.js
Expand Up @@ -785,6 +785,56 @@ $.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('.moderate').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' );
}
});

Expand Down Expand Up @@ -1007,6 +1057,7 @@ fixmystreet.display = {
fixmystreet.set_up.fancybox_images();
fixmystreet.set_up.dropzone($sideReport);
fixmystreet.set_up.form_focus_triggers();
fixmystreet.set_up.moderation();

window.selected_problem_id = reportId;
var marker = fixmystreet.maps.get_marker_by_id(reportId);
Expand Down
51 changes: 0 additions & 51 deletions web/js/moderate.js

This file was deleted.

0 comments on commit 4f7664c

Please sign in to comment.