diff --git a/CHANGELOG.md b/CHANGELOG.md index d3b78d8..c5d2464 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed + +- Make the plugin compatible with the Content Security Policy + ### Fixed - CI: fix Psalm cache directory, declare a unique composer autoloader suffix diff --git a/public/js/dropdown.js b/public/js/dropdown.js new file mode 100644 index 0000000..33c299f --- /dev/null +++ b/public/js/dropdown.js @@ -0,0 +1,79 @@ +/** + * ------------------------------------------------------------------------- + * Tag plugin for GLPI + * ------------------------------------------------------------------------- + * + * LICENSE + * + * This file is part of Tag. + * + * Tag 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 2 of the License, or + * (at your option) any later version. + * + * Tag 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 Tag. If not, see . + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2014-2023 by Teclib'. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/tag + * ------------------------------------------------------------------------- + */ + +(function () { + 'use strict'; + + // Loaded once per page through ADD_JAVASCRIPT (setup.php), in the page footer. + // dropdown_init.js handles markers inserted later (AJAX); markers already in the page are handled below. + window.PluginTagDropdown = { + colorize: function (marker, $container) { + var colors = JSON.parse(marker.getAttribute('data-tag-colors')); + var selector = 'select[data-glpi-plugin-tag-dropdown-uuid="' + marker.getAttribute('data-tag-dropdown-uuid') + '"]'; + import(GLPI_PLUGINS_PATH.tag + '/js/modules/TagDropdownColorizer.js').then(function (m) { + new m.GlpiPluginTagTagDropdownColorizer(colors, selector, $container); + }); + }, + initMarkers: function (scope) { + scope.querySelectorAll('[data-tag-colorizer]:not([data-tag-colorizer-ready])').forEach(function (marker) { + marker.setAttribute('data-tag-colorizer-ready', ''); + window.PluginTagDropdown.colorize(marker, $(document)); + }); + } + }; + + // Markers rendered inline before the footer (e.g. /Form/Render) ran dropdown_init.js before this file existed. + window.PluginTagDropdown.initMarkers(document); + + $(document).on('glpi-form-editor-question-type-changed', function (event, question, type) { + if (type !== 'PluginTagQuestionType') { + return; + } + $(question).find('[data-tag-colorizer]').each(function () { + window.PluginTagDropdown.colorize(this, question); + }); + }); + + $(document).on('click', '#save_tag', function (e) { + e.preventDefault(); + var $button = $(this); + $.ajax({ + url: $button.attr('data-tag-url'), + type: 'POST', + data: { + 'itemtype': $button.attr('data-tag-itemtype'), + 'items_id': $button.data('tagItemsId'), + '_plugin_tag_tag_values': $('select[data-glpi-plugin-tag-dropdown-uuid="' + $button.attr('data-tag-dropdown-uuid') + '"]').val(), + '_plugin_tag_tag_process_form': 1, + } + }).done(function () { + displayAjaxMessageAfterRedirect(); + window.glpiUnsavedFormChanges = false; + }); + }); +})(); diff --git a/public/js/dropdown_init.js b/public/js/dropdown_init.js new file mode 100644 index 0000000..604357a --- /dev/null +++ b/public/js/dropdown_init.js @@ -0,0 +1,29 @@ +/** + * ------------------------------------------------------------------------- + * Tag plugin for GLPI + * ------------------------------------------------------------------------- + * + * LICENSE + * + * This file is part of Tag. + * + * Tag 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 2 of the License, or + * (at your option) any later version. + * + * Tag 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 Tag. If not, see . + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2014-2023 by Teclib'. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/tag + * ------------------------------------------------------------------------- + */ + +window.PluginTagDropdown?.initMarkers(document); diff --git a/setup.php b/setup.php index 0931f52..db53983 100644 --- a/setup.php +++ b/setup.php @@ -156,6 +156,7 @@ function plugin_init_tag() // add needed javascript & css files $PLUGIN_HOOKS[Hooks::ADD_JAVASCRIPT]['tag'][] = 'js/common.js'; $PLUGIN_HOOKS[Hooks::ADD_JAVASCRIPT]['tag'][] = 'js/kanban.js'; + $PLUGIN_HOOKS[Hooks::ADD_JAVASCRIPT]['tag'][] = 'js/dropdown.js'; $PLUGIN_HOOKS[Hooks::ADD_CSS]['tag'][] = 'css/tag.css'; if (Session::isMultiEntitiesMode() && Session::haveRight(PluginTagTag::$rightname, READ)) { $PLUGIN_HOOKS[Hooks::ADD_JAVASCRIPT]['tag'][] = 'js/entity.js'; @@ -187,6 +188,7 @@ function plugin_version_tag() 'author' => 'Teclib\' - Infotel conseil', 'homepage' => 'https://github.com/pluginsGLPI/tag', 'license' => 'GPLv2+', + 'csp_compliant' => true, 'requirements' => [ 'glpi' => [ 'min' => PLUGIN_TAG_MIN_GLPI, diff --git a/templates/dropdown.html.twig b/templates/dropdown.html.twig index 45f4570..0e443c5 100644 --- a/templates/dropdown.html.twig +++ b/templates/dropdown.html.twig @@ -64,7 +64,7 @@ {% if show_save_button is defined and show_save_button %} {% set tooltip_parts = tooltip_parts|merge([ - '' + '' ]) %} {% endif %} @@ -90,37 +90,5 @@ }) ) }} - - -{% if show_save_button is defined and show_save_button %} - -{% endif %} + +{{ call('Html::script', ['/plugins/tag/js/dropdown_init.js', {'version': constant('PLUGIN_TAG_VERSION')}, false])|raw }} diff --git a/templates/question_dropdown.html.twig b/templates/question_dropdown.html.twig index b624283..b72523a 100644 --- a/templates/question_dropdown.html.twig +++ b/templates/question_dropdown.html.twig @@ -41,17 +41,3 @@ 'dropdown_params' : dropdown_params, 'condition' : condition, } %} - - diff --git a/tests/Units/TagItemTest.php b/tests/Units/TagItemTest.php index b2f81f4..a1f61ab 100644 --- a/tests/Units/TagItemTest.php +++ b/tests/Units/TagItemTest.php @@ -127,6 +127,8 @@ public function testShowForTagEncodesLinkedItemValues(): void $this->assertStringNotContainsString($raw_name, $output); $this->assertStringContainsString(htmlescape($raw_name), $output); + } + public function testTagAssociationCreatesLink(): void { $this->loginAs(self::TECH_USER); diff --git a/tests/Units/TagTest.php b/tests/Units/TagTest.php index a180cc8..e4195b7 100644 --- a/tests/Units/TagTest.php +++ b/tests/Units/TagTest.php @@ -39,7 +39,6 @@ use function Safe\ob_get_clean; use function Safe\ob_start; -use function Safe\preg_match; final class TagTest extends TagTestCase { @@ -175,9 +174,9 @@ public function testSaveButtonOnClosedTicketTargetsTagDropdown(): void $crawler = new Crawler($html); $this->assertCount(1, $crawler->filter('#save_tag')); - // The save button JS must read the values from the rendered tags select - $this->assertSame(1, preg_match('/\'_plugin_tag_tag_values\': \$\(\'([^\']+)\'\)\.val\(\)/', $html, $matches)); - $select = $crawler->filter($matches[1]); + // The save button JS reads the values from the tags select matching its dropdown uuid + $uuid = $crawler->filter('#save_tag')->attr('data-tag-dropdown-uuid'); + $select = $crawler->filter('select[data-glpi-plugin-tag-dropdown-uuid="' . $uuid . '"]'); $this->assertCount(1, $select); $this->assertSame('_plugin_tag_tag_values[]', $select->attr('name')); }