From d9bd472beebd965aae9e9f5382bbeb4df41f122f Mon Sep 17 00:00:00 2001 From: Andrew Robert Nicols Date: Mon, 21 May 2012 11:49:26 +0100 Subject: [PATCH] MDL-33135 Prevent duplicate form submission on the course activity chooser --- lib/yui/chooserdialogue/chooserdialogue.js | 55 ++++++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/yui/chooserdialogue/chooserdialogue.js b/lib/yui/chooserdialogue/chooserdialogue.js index 1cbb0fe4073..71c77a30a24 100644 --- a/lib/yui/chooserdialogue/chooserdialogue.js +++ b/lib/yui/chooserdialogue/chooserdialogue.js @@ -14,6 +14,9 @@ YUI.add('moodle-core-chooserdialogue', function(Y) { // The chooserdialogue container container : null, + // Any event listeners we may need to cancel later + listenevents : [], + setup_chooser_dialogue : function(bodycontent, headercontent, config) { // Set Default options var params = { @@ -45,6 +48,7 @@ YUI.add('moodle-core-chooserdialogue', function(Y) { this.container = this.overlay.get('boundingBox').one('#choosercontainer'); this.options = this.container.all('.option input[type=radio]'); }, + /** * Display the module chooser * @@ -58,15 +62,33 @@ YUI.add('moodle-core-chooserdialogue', function(Y) { var bb = this.overlay.get('boundingBox'); var dialogue = this.container.one('.alloptions'); + var thisevent; + // These will trigger a check_options call to display the correct help - this.container.on('click', this.check_options, this); - this.container.on('key_up', this.check_options, this); - this.container.on('dblclick', function(e) { + thisevent = this.container.on('click', this.check_options, this); + this.listenevents.push(thisevent); + thisevent = this.container.on('key_up', this.check_options, this); + this.listenevents.push(thisevent); + thisevent = this.container.on('dblclick', function(e) { if (e.target.ancestor('div.option')) { this.check_options(); + + // Prevent duplicate submissions + this.submitbutton.setAttribute('disabled', 'disabled'); + this.options.setAttribute('disabled', 'disabled'); + this.cancel_listenevents(); + this.container.one('form').submit(); } }, this); + this.listenevents.push(thisevent); + + this.container.one('form').on('submit', function(e) { + // Prevent duplicate submissions on submit + this.submitbutton.setAttribute('disabled', 'disabled'); + this.options.setAttribute('disabled', 'disabled'); + this.cancel_listenevents(); + }, this); // Hook onto the cancel button to hide the form this.container.one('#addcancel').on('click', this.cancel_popup, this); @@ -81,6 +103,9 @@ YUI.add('moodle-core-chooserdialogue', function(Y) { // Disable the submit element until the user makes a selection this.submitbutton.set('disabled', 'true'); + // Ensure that the options are shown + this.options.removeAttribute('disabled'); + // Display the overlay this.overlay.show(); @@ -93,11 +118,28 @@ YUI.add('moodle-core-chooserdialogue', function(Y) { // Trigger check_options to set the initial jumpurl this.check_options(); }, + + /** + * Cancel any listen events in the listenevents queue + * + * Several locations add event handlers which should only be called before the form is submitted. This provides + * a way of cancelling those events. + * + * @return void + */ + cancel_listenevents : function () { + // Detach all listen events to prevent duplicate triggers + var thisevent; + while (thisevent = this.listenevents.shift()) { + thisevent.detach(); + } + }, + /** * Calculate the optimum height of the chooser dialogue * * This tries to set a sensible maximum and minimum to ensure that some options are always shown, and preferably - * all, whilst fitting the box within the current viewport + * all, whilst fitting the box within the current viewport. * * @param dialogue Y.Node The dialogue * @return void @@ -142,22 +184,26 @@ YUI.add('moodle-core-chooserdialogue', function(Y) { // out what we're setting at present -- shoud be the boudingBox bb.setStyle('top', dialoguetop + 'px'); }, + handle_key_press : function(e) { if (e.keyCode == 27) { this.cancel_popup(e); } }, + cancel_popup : function (e) { // Prevent normal form submission before hiding e.preventDefault(); this.hide(); }, + hide : function() { // Detach the global keypress handler before hiding Y.one('document').detach('keyup', this.handle_key_press, this); this.container.detachAll(); this.overlay.hide(); }, + check_options : function(e) { // Check which options are set, and change the parent class // to show/hide help as required @@ -179,6 +225,7 @@ YUI.add('moodle-core-chooserdialogue', function(Y) { } }, this); }, + option_selected : function(e) { } },