Skip to content
Open
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 @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Add Transifex translations link to README

### Changed

- Make the plugin compatible with the Content Security Policy

### Fixed

- Fix search crash when the "Tags - Entity" column is displayed
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));
});
}
};

// 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
37 changes: 3 additions & 34 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"><i class="far fa-save"></i></button>'
'<button type="submit" name="save_tag" id="save_tag" class="btn btn-outline-primary" 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,36 +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': $('#dropdown__plugin_tag_tag_values{{ rand }}').val(),
'_plugin_tag_tag_process_form': 1,
}
});
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>