Skip to content

Commit 3b452b4

Browse files
committed
[Copyright] Add "Dont show again" option
1 parent 13a153a commit 3b452b4

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed

src/Mapbender/CoreBundle/Element/Copyright.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Mapbender\Component\Element\AbstractElementService;
55
use Mapbender\Component\Element\StaticView;
6+
use Mapbender\Component\Element\TemplateView;
67
use Mapbender\CoreBundle\Entity\Element;
78
use Mapbender\Utils\HtmlUtil;
89
use Twig;
@@ -72,6 +73,7 @@ public static function getDefaultConfiguration()
7273
return array(
7374
'autoOpen' => false,
7475
'content' => null,
76+
'dontShowAgain' => false,
7577
'popupWidth' => 300,
7678
'popupHeight' => null,
7779
);
@@ -88,19 +90,19 @@ public function getWidgetName(Element $element)
8890
public function getView(Element $element)
8991
{
9092
$config = $element->getConfiguration();
93+
94+
$view = new TemplateView('@MapbenderCore/Element/copyright.html.twig');
95+
$view->attributes['class'] = 'mb-element-copyright';
96+
$view->attributes['data-title'] = $element->getTitle();
97+
$view->variables['content'] = $config['content'];
98+
$view->variables['dontShowAgain'] = $config['dontShowAgain'];
99+
$view->variables['dontShowAgainLabel'] = $config['dontShowAgainLabel'];
100+
91101
// Do not cache if content contains any twig expressions or flow control ("{{" or "{%")
92-
$content = $this->templateEngine->createTemplate($config['content'] ?: '')->render(array(
93-
'configuration' => $config,
94-
));
95-
$wrapped = HtmlUtil::renderTag('div', $content, array(
96-
'class' => 'hidden -js-popup-content',
97-
));
98-
$view = new StaticView($wrapped);
99102
if (!empty($config['content']) && false !== strpos($config['content'], '{')) {
100103
$view->cacheable = false;
101104
}
102-
$view->attributes['class'] = 'mb-element-copyright';
103-
$view->attributes['data-title'] = $element->getTitle();
105+
104106
return $view;
105107
}
106108

src/Mapbender/CoreBundle/Element/Type/CopyrightAdminType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
class CopyrightAdminType extends AbstractType
1010
{
11+
1112
/**
1213
* @inheritdoc
1314
*/
@@ -43,6 +44,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
4344
new HtmlTwigConstraint(),
4445
),
4546
))
47+
->add('dontShowAgain', 'Symfony\Component\Form\Extension\Core\Type\CheckboxType', array(
48+
'required' => false,
49+
'label' => 'mb.core.copyright.admin.dontShowAgain',
50+
))
51+
->add('dontShowAgainLabel', 'Symfony\Component\Form\Extension\Core\Type\TextType', array(
52+
'required' => false,
53+
'label' => 'mb.core.copyright.admin.dontShowAgainLabel',
54+
'data' => 'mb.core.copyright.admin.dontShowAgainDefaultLabel',
55+
))
4656
;
4757
}
4858
}

src/Mapbender/CoreBundle/Resources/public/elements/MbCopyright.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@
55
super(configuration, $element);
66

77
this.content_ = $('.-js-popup-content', this.$element).remove().removeClass('hidden');
8-
if (this.checkAutoOpen && this.checkAutoOpen()) {
8+
9+
if (this.options.autoOpen) {
10+
const currentText = this.content_.text().trim();
11+
const storedText = localStorage.getItem('mbCopyrightText-' + Mapbender.configuration.application.slug);
12+
this.options.autoOpen = (storedText === null) || (currentText !== storedText);
13+
}
14+
15+
if (this.checkAutoOpen(this.element, this.options)) {
916
this.activateByButton();
1017
}
18+
1119
this._setup();
1220
}
1321

@@ -35,6 +43,10 @@
3543
if (this.notifyWidgetActivated) {
3644
this.notifyWidgetActivated();
3745
}
46+
47+
if (this.options.dontShowAgain) {
48+
this.initListeners();
49+
}
3850
}
3951

4052
closeByButton() {
@@ -43,6 +55,23 @@
4355
this.notifyWidgetDeactivated();
4456
}
4557
}
58+
59+
initListeners() {
60+
const currentText = this.content_.text().trim();
61+
const localStorageKey = 'mbCopyrightText-' + Mapbender.configuration.application.slug;
62+
const storedText = localStorage.getItem(localStorageKey);
63+
const $checkbox = this.content_.find('.copyright-dont-show-again');
64+
65+
$checkbox.prop('checked', (storedText !== null) && (storedText === currentText));
66+
$checkbox.off('change.mbCopyright')
67+
.on('change.mbCopyright', function() {
68+
if (this.checked) {
69+
localStorage.setItem(localStorageKey, currentText);
70+
} else {
71+
localStorage.removeItem(localStorageKey);
72+
}
73+
});
74+
}
4675
}
4776

4877
window.Mapbender.Element = window.Mapbender.Element || {};

src/Mapbender/CoreBundle/Resources/translations/messages.de.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ mb:
356356
dialog: Dialog
357357
admin:
358358
content: Inhalt
359+
dontShowAgain: '"Nicht mehr anzeigen"-Option'
360+
dontShowAgainLabel: '"Nicht mehr anzeigen"-Text'
361+
dontShowAgainDefaultLabel: 'Nicht mehr anzeigen'
359362
map:
360363
class:
361364
title: Karte

src/Mapbender/CoreBundle/Resources/translations/messages.en.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ mb:
356356
dialog: dialog
357357
admin:
358358
content: Content
359+
dontShowAgain: "\"Don't show again\"-Option"
360+
dontShowAgainLabel: "\"Don't show again\"-Label"
361+
dontShowAgainDefaultLabel: "Don't show again"
359362
map:
360363
class:
361364
title: Map
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div class="hidden -js-popup-content">
2+
{{- content | raw -}}
3+
{% if dontShowAgain %}
4+
<div class="form-check mt-5">
5+
<input type="checkbox" class="form-check-input copyright-dont-show-again">
6+
<label class="form-check-label">{{ dontShowAgainLabel | trans }}</label>
7+
</div>
8+
{% endif %}
9+
</div>

0 commit comments

Comments
 (0)