diff --git a/CHANGELOG.md b/CHANGELOG.md index c0e8f96..53fbbf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [UNRELEASED] - Fix Composer autoloader collisions with other plugins +- Make the plugin compatible with the Content Security Policy ## [3.0.3] - 2026-08-11 diff --git a/public/js/migration_status.js b/public/js/migration_status.js new file mode 100644 index 0000000..a880ca1 --- /dev/null +++ b/public/js/migration_status.js @@ -0,0 +1,116 @@ +/** + * ------------------------------------------------------------------------- + * GenericObject plugin for GLPI + * ------------------------------------------------------------------------- + * + * LICENSE + * + * This file is part of GenericObject. + * + * GenericObject is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GenericObject is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GenericObject. If not, see . + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2009-2023 by GenericObject plugin team. + * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/pluginsGLPI/genericobject + * ------------------------------------------------------------------------- + */ + +/* global CFG_GLPI */ + +const glpiGenericobjectDebounceTimers = new WeakMap(); + +function glpiGenericobjectUpdateCounter(input) { + clearTimeout(glpiGenericobjectDebounceTimers.get(input)); + glpiGenericobjectDebounceTimers.set(input, setTimeout(() => glpiGenericobjectFetchSystemName(input), 300)); +} + +function glpiGenericobjectFetchSystemName(input) { + const maxLen = parseInt(input.getAttribute('maxlength')); + const counterId = input.dataset.counterTarget; + const warningId = input.dataset.warningTarget; + const resultId = input.dataset.resultTarget; + const counter = counterId ? document.getElementById(counterId) : null; + const warning = warningId ? document.getElementById(warningId) : null; + const result = resultId ? document.getElementById(resultId) : null; + + fetch( + `${CFG_GLPI.root_doc}/plugins/genericobject/front/migration_status.php?action=get_system_name&name=${encodeURIComponent(input.value)}`, + ) + .then(r => r.json()) + .then(data => { + const currentLen = data.system_name.length; + if (counter) { + counter.textContent = currentLen + '/' + maxLen; + counter.className = 'small'; + if (currentLen >= maxLen) { + counter.classList.add('text-danger', 'fw-bold'); + } else { + counter.classList.add('text-muted'); + } + } + + if (warning) { + if (currentLen >= maxLen) { + warning.classList.remove('d-none'); + } else { + warning.classList.add('d-none'); + } + } + + if (result) { + result.textContent = data.system_name; + } + }) + .catch((e) => console.error(e.message)); +} + +document.addEventListener('shown.bs.modal', function (event) { + const input = event.target.querySelector('[data-counter-target]'); + if (input) { + glpiGenericobjectUpdateCounter(input); + } +}); + +function copyCode() { + const codeBlock = document.getElementById('codeBlock'); + const textArea = document.createElement('textarea'); + textArea.value = codeBlock.textContent; + document.body.appendChild(textArea); + textArea.select(); + document.execCommand('copy'); + document.body.removeChild(textArea); + + const copyBtn = document.querySelector('.copy-btn'); + copyBtn.innerHTML = ` ${copyBtn.dataset.genericobjectCopiedLabel} !`; + copyBtn.classList.remove('btn-outline-secondary'); + copyBtn.classList.add('btn-success'); + + setTimeout(() => { + copyBtn.innerHTML = ` ${copyBtn.dataset.genericobjectCopyLabel}`; + copyBtn.classList.remove('btn-success'); + copyBtn.classList.add('btn-outline-secondary'); + }, 2000); +} + +document.addEventListener('input', (e) => { + if (e.target.matches('[data-genericobject-update-counter]')) { + glpiGenericobjectUpdateCounter(e.target); + } +}); + +document.addEventListener('click', (e) => { + if (e.target.closest('[data-genericobject-copy-code]') !== null) { + copyCode(); + } +}); diff --git a/setup.php b/setup.php index c460bbb..2672b22 100644 --- a/setup.php +++ b/setup.php @@ -134,6 +134,7 @@ function plugin_version_genericobject() 'author' => "Teclib' & siprossii", 'homepage' => 'https://github.com/pluginsGLPI/genericobject', 'license' => 'GPLv2+', + 'csp_compliant' => true, 'requirements' => [ 'glpi' => [ 'min' => PLUGIN_GENERICOBJECT_MIN_GLPI, diff --git a/templates/migration_status.html.twig b/templates/migration_status.html.twig index bee5cf3..fe54266 100644 --- a/templates/migration_status.html.twig +++ b/templates/migration_status.html.twig @@ -256,7 +256,7 @@ data-counter-target="{{ modal_id }}-counter" data-warning-target="{{ modal_id }}-length-warning" data-result-target="{{ modal_id }}-result" - oninput="glpiGenericobjectUpdateCounter(this)" + data-genericobject-update-counter required >
@@ -331,7 +331,7 @@
php bin/console migration:genericobject_plugin_to_core - @@ -430,79 +430,4 @@ right: 0.5rem; } - +{{ call('Html::script', ['/plugins/genericobject/js/migration_status.js', {'version': constant('PLUGIN_GENERICOBJECT_VERSION')}, false])|raw }}