Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions public/js/dropdown.js
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @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));
});
}
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested fix for dropdown init issue 2/3

Suggested change
// Markers rendered inline before the footer (e.g. /Form/Render) ran dropdown_init.js before this file existed.
window.PluginTagDropdown.initMarkers(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;
});
});
})();
29 changes: 29 additions & 0 deletions public/js/dropdown_init.js
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @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);
2 changes: 2 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -187,6 +188,7 @@ function plugin_version_tag()
'author' => '<a href="http://www.teclib.com">Teclib\'</a> - Infotel conseil',
'homepage' => 'https://github.com/pluginsGLPI/tag',
'license' => '<a href="' . plugin_tag_geturl() . '/LICENSE" target="_blank">GPLv2+</a>',
'csp_compliant' => true,
'requirements' => [
'glpi' => [
'min' => PLUGIN_TAG_MIN_GLPI,
Expand Down
38 changes: 3 additions & 35 deletions templates/dropdown.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

{% if show_save_button is defined and show_save_button %}
{% set tooltip_parts = tooltip_parts|merge([
'<button type="submit" name="save_tag" id="save_tag" class="btn btn-outline-primary" title="' ~ __('Save')|e('html_attr') ~ '" aria-label="' ~ __('Save')|e('html_attr') ~ '"><i class="far fa-save"></i></button>'
'<button type="submit" name="save_tag" id="save_tag" class="btn btn-outline-primary" title="' ~ __('Save')|e('html_attr') ~ '" aria-label="' ~ __('Save')|e('html_attr') ~ '" data-tag-url="' ~ url|e('html_attr') ~ '" data-tag-itemtype="' ~ itemtype|e('html_attr') ~ '" data-tag-items-id="' ~ items_id|e('html_attr') ~ '" data-tag-dropdown-uuid="' ~ rand|e('html_attr') ~ '"><i class="far fa-save"></i></button>'
]) %}
{% endif %}

Expand All @@ -90,37 +90,5 @@
})
) }}

<script>
import(GLPI_PLUGINS_PATH.tag + "/js/modules/TagDropdownColorizer.js").then((m) => {
$(document).ready(function() {
new m.GlpiPluginTagTagDropdownColorizer(
{{ tags_color|json_encode(constant('JSON_HEX_TAG') b-or constant('JSON_HEX_AMP'))|raw }},
'select[data-glpi-plugin-tag-dropdown-uuid="{{ rand }}"]',
$(document)
);
});
});
</script>

{% if show_save_button is defined and show_save_button %}
<script>
$(function() {
$('#save_tag').on('click', function (e) {
e.preventDefault();
$.ajax({
url: '{{ url }}',
type: 'POST',
data: {
'itemtype': '{{ itemtype }}',
'items_id': {{ items_id }},
'_plugin_tag_tag_values': $('select[data-glpi-plugin-tag-dropdown-uuid="{{ rand }}"]').val(),
'_plugin_tag_tag_process_form': 1,
}
}).done(function () {
displayAjaxMessageAfterRedirect();
window.glpiUnsavedFormChanges = false;
});
});
});
</script>
{% endif %}
<span data-tag-colorizer data-tag-dropdown-uuid="{{ rand }}" data-tag-colors="{{ tags_color|json_encode(constant('JSON_HEX_TAG') b-or constant('JSON_HEX_AMP')) }}" style="display: none;" aria-hidden="true"></span>
{{ call('Html::script', ['/plugins/tag/js/dropdown_init.js', {'version': constant('PLUGIN_TAG_VERSION')}, false])|raw }}
14 changes: 0 additions & 14 deletions templates/question_dropdown.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,3 @@
'dropdown_params' : dropdown_params,
'condition' : condition,
} %}

<script>
$(document).on('glpi-form-editor-question-type-changed', (event, question, type) => {
if (type === 'PluginTagQuestionType') {
import("plugins/js/modules/TagDropdownColorizer.js").then((m) => {
new m.GlpiPluginTagTagDropdownColorizer(
{{ tags_color|json_encode(constant('JSON_HEX_TAG') b-or constant('JSON_HEX_AMP'))|raw }},
'select[data-glpi-plugin-tag-dropdown-uuid="{{ rand }}"]',
question
);
});
}
});
</script>
2 changes: 2 additions & 0 deletions tests/Units/TagItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions tests/Units/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'));
}
Expand Down