Skip to content

Commit e2cc150

Browse files
authored
Merge pull request #80 from systopia/disable-buttons-on-ajax
Disable buttons during AJAX calls to prevent inconsistent state
2 parents 27c80bd + dac05f1 commit e2cc150

File tree

4 files changed

+48
-60
lines changed

4 files changed

+48
-60
lines changed

js/disable-buttons-on-ajax.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2024 SYSTOPIA GmbH
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
/**
19+
* Adding/removing entries to on array before a previous AJAX call has been
20+
* finished might lead to an inconsistent state. Thus, buttons are disabled
21+
* during AJAX calls. Additionally, form submit is not possible during AJAX
22+
* calls. Fields that initiate an AJAX calls are disabled until the call is
23+
* finished and would be missing in the submitted data.
24+
*/
25+
(function ($) {
26+
27+
$(document).on('ajaxStart', () => {
28+
const buttons = document.querySelectorAll('input[type="submit"]:enabled, input[type="button"]:enabled');
29+
buttons.forEach((button) => {
30+
button.disabled = true;
31+
button.setAttribute('data-ajax-disabled', 'true');
32+
});
33+
});
34+
35+
$(document).on('ajaxStop', () => {
36+
const buttons = document.querySelectorAll('input[data-ajax-disabled="true"]');
37+
buttons.forEach((button) => {
38+
button.disabled = false;
39+
buttom.removeAttribute('data-ajax-disabled');
40+
});
41+
42+
});
43+
44+
})(jQuery);

js/submit.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

json_forms.libraries.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ confirm:
99
- core/jquery
1010
- core/once
1111

12-
submit:
12+
disable_buttons_on_ajax:
1313
version: 0.1.0
1414
js:
15-
js/submit.js: {}
15+
js/disable-buttons-on-ajax.js: {}
1616
dependencies:
17-
- core/drupal
1817
- core/jquery
19-
- core/once
2018

2119
vertical_tabs:
2220
version: 0.1.0

src/Form/AbstractJsonFormsForm.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ protected function buildJsonFormsForm(
116116
// @phpstan-ignore-next-line
117117
$form['#attributes']['class'][] = 'json-forms';
118118

119-
if ($recalculateOnChange) {
120-
$form['#attached']['library'][] = 'json_forms/submit';
121-
$form['#attached']['library'][] = 'json_forms/vertical_tabs';
122-
}
119+
$form['#attached']['library'][] = 'json_forms/disable_buttons_on_ajax';
120+
$form['#attached']['library'][] = 'json_forms/vertical_tabs';
123121

124122
return $form;
125123
}

0 commit comments

Comments
 (0)