|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 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 | + * Restricts input of number fields to digits, decimal separator, and minus. |
| 20 | + */ |
| 21 | +(function (Drupal, once) { |
| 22 | + Drupal.behaviors.json_forms_numberInput = { |
| 23 | + attach: function (context, settings) { |
| 24 | + // Decimal separator of browser. |
| 25 | + const decimalSeparator = Intl.NumberFormat() |
| 26 | + .formatToParts(1.1) |
| 27 | + .find(part => part.type === 'decimal') |
| 28 | + .value; |
| 29 | + const digits = '0123456789'; |
| 30 | + |
| 31 | + function getElementLang(element) { |
| 32 | + if (element.lang) { |
| 33 | + return element.lang; |
| 34 | + } |
| 35 | + |
| 36 | + return element.parentElement ? getElementLang(element.parentElement) : null; |
| 37 | + } |
| 38 | + |
| 39 | + once('json-forms-number-input', 'input[type="number"]', context).forEach((element) => { |
| 40 | + let decimalSeparators = decimalSeparator; |
| 41 | + const lang = getElementLang(element); |
| 42 | + if (lang) { |
| 43 | + // Decimal separator of element's language might be different from |
| 44 | + // the browser's separator. We allow both in that case. It depends on |
| 45 | + // the browser which one is preferred, i.e. the one that is used when |
| 46 | + // pressing the buttons to change a number. |
| 47 | + decimalSeparators += Intl.NumberFormat(lang) |
| 48 | + .formatToParts(1.1) |
| 49 | + .find(part => part.type === 'decimal') |
| 50 | + .value; |
| 51 | + } |
| 52 | + |
| 53 | + element.addEventListener('keydown', function (event) { |
| 54 | + if (event.key === 'Backspace' || '0123456789'.indexOf(event.key) !== -1) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if ('-' === event.key) { |
| 59 | + if (element.value.indexOf('-') === -1 && (element.min < 0 || element.min === '' || element.min == null)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + } |
| 63 | + else if (decimalSeparators.indexOf(event.key) !== -1) { |
| 64 | + if (element.value.indexOf('.') === -1 && (element.step === 'any' || element.step < 1)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + event.preventDefault(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | +})(Drupal, once); |
0 commit comments