|
| 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 ($, Drupal, once) { |
| 26 | + |
| 27 | + $(document).on('ajaxStart', () => { |
| 28 | + const buttons = document.querySelectorAll('input[type="submit"]:enabled, input[type="button"]:enabled'); |
| 29 | + buttons.forEach((button) => { |
| 30 | + button.setAttribute('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.removeAttribute('disabled'); |
| 39 | + }); |
| 40 | + |
| 41 | + }); |
| 42 | + |
| 43 | +})(jQuery); |
0 commit comments