From 03896940aca28799a54f7efc3da8cf99094e0eae Mon Sep 17 00:00:00 2001 From: David Molineus Date: Thu, 12 Oct 2017 12:06:51 +0200 Subject: [PATCH] Fix the icon toggling. --- src/Bundle/Resources/contao/config/config.php | 1 + .../Resources/contao/dca/tl_leaflet_layer.php | 3 +- src/Bundle/Resources/public/js/backend.js | 64 +++++++++++++++++++ src/Listener/Dca/LayerDcaListener.php | 16 ++++- 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 src/Bundle/Resources/public/js/backend.js diff --git a/src/Bundle/Resources/contao/config/config.php b/src/Bundle/Resources/contao/config/config.php index c7bf145..38abba6 100644 --- a/src/Bundle/Resources/contao/config/config.php +++ b/src/Bundle/Resources/contao/config/config.php @@ -43,6 +43,7 @@ array_insert( ), 'icon' => 'bundles/netzmachtcontaoleaflet/img/layers.png', 'stylesheet' => 'bundles/netzmachtcontaoleaflet/css/backend.css', + 'javascript' => 'bundles/netzmachtcontaoleaflet/js/backend.js' ), 'leaflet_about' => array ( diff --git a/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php b/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php index 275c9c9..303f954 100644 --- a/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php +++ b/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php @@ -111,7 +111,8 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [ 'toggle' => [ 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['toggle'], 'icon' => 'visible.gif', - 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', + 'attributes' => 'onclick="Backend.getScrollOffset(); + return ContaoLeafletAjaxRequest.toggleVisibility(this,%s)"', 'button_callback' => [ 'netzmacht.contao_toolkit.dca.listeners.state_button_callback', 'handleButtonCallback', diff --git a/src/Bundle/Resources/public/js/backend.js b/src/Bundle/Resources/public/js/backend.js new file mode 100644 index 0000000..1e5f383 --- /dev/null +++ b/src/Bundle/Resources/public/js/backend.js @@ -0,0 +1,64 @@ +/** + * Contao Open Source CMS + * + * Copyright (c) 2005-2017 Leo Feyer + * + * @license LGPL-3.0+ + */ + + +/** + * Provide methods to handle Ajax requests. + * + * @author Leo Feyer + */ +var ContaoLeafletAjaxRequest = { + /** + * Toggle the visibility of an element + * + * @param {object} el The DOM element + * @param {string} id The ID of the target element + * @param {string} table The table name + * + * @returns {boolean} + */ + toggleVisibility: function (el, id, table) { + el.blur(); + + var img = null, + image = $(el).getFirst('img'), + published = (image.get('data-state') == 1), + div = el.getParent('div'), + index, next, icon, icond, pa; + img = div.getParent('li').getFirst('.tl_left img.list-icon'); + + // Change the icon + if (img !== null && img.nodeName.toLowerCase() == 'img') { + icon = img.get('data-icon'); + icond = img.get('data-icon-disabled'); + + img.src = !published ? icon : icond; + } + + // Send request + if (!published) { + image.src = AjaxRequest.themePath + 'icons/visible.svg'; + image.set('data-state', 1); + new Request.Contao({'url': window.location.href, 'followRedirects': false}).get({ + 'tid': id, + 'state': 1, + 'rt': Contao.request_token + }); + } else { + image.src = AjaxRequest.themePath + 'icons/invisible.svg'; + image.set('data-state', 0); + new Request.Contao({'url': window.location.href, 'followRedirects': false}).get({ + 'tid': id, + 'state': 0, + 'rt': Contao.request_token + }); + } + + return false; + } +}; diff --git a/src/Listener/Dca/LayerDcaListener.php b/src/Listener/Dca/LayerDcaListener.php index 4a020c6..7df607e 100644 --- a/src/Listener/Dca/LayerDcaListener.php +++ b/src/Listener/Dca/LayerDcaListener.php @@ -168,12 +168,22 @@ class LayerDcaListener extends AbstractListener $src = 'iconPLAIN.gif'; } + $activeIcon = $src; + $disabledIcon = preg_replace('/(\.[^\.]+)$/', '_1$1', $src); + if (!$row['active']) { - $src = preg_replace('/(\.[^\.]+)$/', '_1$1', $src); + $src = $disabledIcon; } - $alt = $this->getFormatter()->formatValue('type', $row['type']); - $icon = Image::getHtml($src, $alt, sprintf('title="%s"', StringUtil::specialchars(strip_tags($alt)))); + $alt = $this->getFormatter()->formatValue('type', $row['type']); + $attributes = sprintf( + 'class="list-icon" title="%s" data-icon="%s" data-icon-disabled="%s"', + StringUtil::specialchars(strip_tags($alt)), + $activeIcon, + $disabledIcon + ); + + $icon = Image::getHtml($src, $alt, $attributes); $label = $this->labelRenderer->render($row, $label, $this->translator); return $icon . ' ' . $label;