From d21a056f1c750086ab88c7b55fce4989a816a1ea Mon Sep 17 00:00:00 2001 From: tuomaspelkonen Date: Mon, 23 May 2011 14:33:54 -0700 Subject: [PATCH] Multiple comment submissions for a diff is prevented now. Summary: It was possible to submit a comment multiple times if the submit button was pressed more than once quickly. Added javascript code that disables the button when it is clicked. Test Plan: Tried to click the button multiple times very quickly, but the button was disabled after the first click. Reviewed By: epriestley Reviewers: epriestley, jungejason Commenters: aran CC: aran, epriestley, tuomaspelkonen Differential Revision: 337 --- src/__celerity_resource_map__.php | 12 +++++ src/view/form/base/AphrontFormView.php | 5 ++ src/view/form/base/__init__.php | 1 + .../rsrc/js/application/core/behavior-form.js | 48 +++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 webroot/rsrc/js/application/core/behavior-form.js diff --git a/src/__celerity_resource_map__.php b/src/__celerity_resource_map__.php index 712e5e0f82..19541c41d5 100644 --- a/src/__celerity_resource_map__.php +++ b/src/__celerity_resource_map__.php @@ -284,6 +284,18 @@ celerity_register_resource_map(array( ), 'disk' => '/rsrc/js/application/core/behavior-tokenizer.js', ), + 'javelin-behavior-aphront-form-disable-on-submit' => + array( + 'uri' => '/res/6c659ede/rsrc/js/application/core/behavior-form.js', + 'type' => 'js', + 'requires' => + array( + 0 => 'javelin-behavior', + 1 => 'javelin-stratcom', + 2 => 'javelin-dom', + ), + 'disk' => '/rsrc/js/application/core/behavior-form.js', + ), 'javelin-behavior-dark-console' => array( 'uri' => '/res/044c171f/rsrc/js/application/core/behavior-dark-console.js', diff --git a/src/view/form/base/AphrontFormView.php b/src/view/form/base/AphrontFormView.php index 710b223638..5d8a251c44 100644 --- a/src/view/form/base/AphrontFormView.php +++ b/src/view/form/base/AphrontFormView.php @@ -64,6 +64,11 @@ final class AphrontFormView extends AphrontView { public function render() { require_celerity_resource('aphront-form-view-css'); + + Javelin::initBehavior( + 'aphront-form-disable-on-submit', + array()); + return javelin_render_tag( 'form', array( diff --git a/src/view/form/base/__init__.php b/src/view/form/base/__init__.php index b4cba977cd..4ff1b13a67 100644 --- a/src/view/form/base/__init__.php +++ b/src/view/form/base/__init__.php @@ -7,6 +7,7 @@ phutil_require_module('phabricator', 'infrastructure/celerity/api'); +phutil_require_module('phabricator', 'infrastructure/javelin/api'); phutil_require_module('phabricator', 'infrastructure/javelin/markup'); phutil_require_module('phabricator', 'view/base'); diff --git a/webroot/rsrc/js/application/core/behavior-form.js b/webroot/rsrc/js/application/core/behavior-form.js new file mode 100644 index 0000000000..06994a39ee --- /dev/null +++ b/webroot/rsrc/js/application/core/behavior-form.js @@ -0,0 +1,48 @@ +/** + * @requires javelin-behavior javelin-stratcom javelin-dom + * @provides javelin-behavior-aphront-form-disable-on-submit + */ + +JX.behavior('aphront-form-disable-on-submit', function(config) { + + var restore = []; + var root = null; + + JX.Stratcom.listen('submit', 'tag:form', function(e) { + if (e.getNode('workflow')) { + // Don't activate for forms with workflow, the workflow behavior will + // handle it. + return; + } + + root = e.getNode('tag:form'); + if (root._disabled) { + e.kill(); + } + root._disabled = true; + var buttons = JX.DOM.scry(root, 'button'); + for (var ii = 0; ii < buttons.length; ii++) { + if (!buttons[ii].disabled) { + buttons[ii].disabled = 'disabled'; + JX.DOM.alterClass(buttons[ii], 'disabled', true); + restore.push(buttons[ii]); + } + } + }); + + JX.Stratcom.listen('unload', null, function(e) { + // Reenable everything on page unload so we don't bfcache back to a page + // that has disabled forms. + for (var ii = 0; ii < restore.length; ii++) { + restore[ii].disabled = ''; + JX.DOM.alterClass(restore[ii], 'disabled', false); + root._disabled = false; + } + if (root) { + delete root._disabled; + } + restore = []; + root = null; + }); + +});