|
| 1 | +package io.kyberorg.yalsee.ui.components; |
| 2 | + |
| 3 | +import com.vaadin.flow.component.ClickEvent; |
| 4 | +import com.vaadin.flow.component.Composite; |
| 5 | +import com.vaadin.flow.component.Tag; |
| 6 | +import com.vaadin.flow.component.button.Button; |
| 7 | +import com.vaadin.flow.component.button.ButtonVariant; |
| 8 | +import com.vaadin.flow.component.checkbox.Checkbox; |
| 9 | +import com.vaadin.flow.component.dialog.Dialog; |
| 10 | +import com.vaadin.flow.component.html.Anchor; |
| 11 | +import com.vaadin.flow.component.html.H3; |
| 12 | +import com.vaadin.flow.component.html.Span; |
| 13 | +import com.vaadin.flow.component.orderedlayout.FlexLayout; |
| 14 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 15 | +import io.kyberorg.yalsee.Endpoint; |
| 16 | +import io.kyberorg.yalsee.constants.App; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | + |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +/** |
| 22 | + * Cookie Banner. |
| 23 | + * |
| 24 | + * @since 3.5 |
| 25 | + */ |
| 26 | +@Slf4j |
| 27 | +@Tag("yalsee-cookie-banner") |
| 28 | +public class CookieBanner extends Composite<Dialog> { |
| 29 | + public static final String TAG = "[" + CookieBanner.class.getSimpleName() + "]"; |
| 30 | + |
| 31 | + private final VerticalLayout coreLayout = new VerticalLayout(); |
| 32 | + private final H3 title = new H3("Cookies Info"); |
| 33 | + |
| 34 | + private final Span textAndLinkSection = new Span(); |
| 35 | + private final Span text = new Span("This website uses cookies, because without cookies it works like shit. "); |
| 36 | + private final Anchor link = new Anchor(Endpoint.UI.APP_INFO_PAGE, "More info"); |
| 37 | + |
| 38 | + private final FlexLayout buttons = new FlexLayout(); |
| 39 | + private final Button onlyNecessaryButton = new Button("Only necessary cookies"); |
| 40 | + private final Button mySelectionButton = new Button("Allow selection"); |
| 41 | + private final Button allowAllButton = new Button("Allow all cookies"); |
| 42 | + |
| 43 | + private final FlexLayout checkboxes = new FlexLayout(); |
| 44 | + private final Checkbox onlyNecessaryBox = new Checkbox(); |
| 45 | + private final Checkbox analyticsBox = new Checkbox(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates Cookie Banner. |
| 49 | + */ |
| 50 | + public CookieBanner() { |
| 51 | + init(); |
| 52 | + applyStyle(); |
| 53 | + } |
| 54 | + |
| 55 | + private void init() { |
| 56 | + setId(IDs.CB_DIALOG); |
| 57 | + |
| 58 | + title.setId(IDs.CB_TITLE); |
| 59 | + textAndLinkSection.setId(IDs.CB_TEXT_AND_LINK_SECTION); |
| 60 | + text.setId(IDs.CB_TEXT); |
| 61 | + link.setId(IDs.CB_LINK); |
| 62 | + |
| 63 | + checkboxes.setId(IDs.CB_BOXES_SECTION); |
| 64 | + |
| 65 | + onlyNecessaryBox.setId(IDs.CB_ONLY_NECESSARY_BOX); |
| 66 | + onlyNecessaryBox.setLabel("Technical"); |
| 67 | + onlyNecessaryBox.setValue(true); |
| 68 | + onlyNecessaryBox.setEnabled(false); |
| 69 | + onlyNecessaryBox.setReadOnly(true); |
| 70 | + |
| 71 | + analyticsBox.setId(IDs.CB_ANALYTICS_BOX); |
| 72 | + analyticsBox.setLabel("Analytics"); |
| 73 | + |
| 74 | + buttons.setId(IDs.CB_BUTTONS_SECTION); |
| 75 | + |
| 76 | + onlyNecessaryButton.setId(IDs.CB_ONLY_NECESSARY_BUTTON); |
| 77 | + onlyNecessaryButton.addClickListener(this::onNecessaryButtonClicked); |
| 78 | + onlyNecessaryButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY, |
| 79 | + ButtonVariant.LUMO_CONTRAST); |
| 80 | + |
| 81 | + mySelectionButton.setId(IDs.CB_SELECTION_BUTTON); |
| 82 | + mySelectionButton.addClickListener(this::onMySelectionButtonClicked); |
| 83 | + mySelectionButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY); |
| 84 | + |
| 85 | + allowAllButton.setId(IDs.CB_ALLOW_ALL_BUTTON); |
| 86 | + allowAllButton.addClickListener(this::onAllowAllButtonClicked); |
| 87 | + allowAllButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY); |
| 88 | + |
| 89 | + textAndLinkSection.add(text, link); |
| 90 | + checkboxes.add(onlyNecessaryBox, analyticsBox); |
| 91 | + buttons.add(onlyNecessaryButton, mySelectionButton, allowAllButton); |
| 92 | + |
| 93 | + coreLayout.add(title, textAndLinkSection, checkboxes, buttons); |
| 94 | + |
| 95 | + getContent().add(coreLayout); |
| 96 | + } |
| 97 | + |
| 98 | + private void applyStyle() { |
| 99 | + Stream<Checkbox> boxStream = Stream.of(onlyNecessaryBox, analyticsBox); |
| 100 | + boxStream.forEach(box -> box.addClassName(Classes.CB_BOX)); |
| 101 | + |
| 102 | + buttons.setFlexWrap(FlexLayout.FlexWrap.WRAP); |
| 103 | + Stream<Button> buttonStream = Stream.of(onlyNecessaryButton, mySelectionButton, allowAllButton); |
| 104 | + buttonStream.forEach(button -> button.addClassName(Classes.CB_BUTTON)); |
| 105 | + } |
| 106 | + |
| 107 | + private void onNecessaryButtonClicked(final ClickEvent<Button> event) { |
| 108 | + // disable others options first |
| 109 | + analyticsBox.setValue(false); |
| 110 | + writeValuesToSessionAndClose(); |
| 111 | + } |
| 112 | + |
| 113 | + private void onMySelectionButtonClicked(final ClickEvent<Button> event) { |
| 114 | + writeValuesToSessionAndClose(); |
| 115 | + } |
| 116 | + |
| 117 | + private void onAllowAllButtonClicked(final ClickEvent<Button> event) { |
| 118 | + analyticsBox.setValue(true); |
| 119 | + writeValuesToSessionAndClose(); |
| 120 | + } |
| 121 | + |
| 122 | + private void writeValuesToSessionAndClose() { |
| 123 | + if (getContent().getUI().isPresent()) { |
| 124 | + final boolean uiHasSession = getContent().getUI().get().getSession() != null; |
| 125 | + if (uiHasSession) { |
| 126 | + final boolean isAnalyticsAllowed = analyticsBox.getValue(); |
| 127 | + getContent().getUI().get().getSession() |
| 128 | + .setAttribute(App.Session.COOKIE_BANNER_ANALYTICS_ALLOWED, isAnalyticsAllowed); |
| 129 | + } else { |
| 130 | + log.warn("{} UI has no session inside. Skipping further actions...", TAG); |
| 131 | + } |
| 132 | + } else { |
| 133 | + log.warn("{} UI is missing. Skipping further actions...", TAG); |
| 134 | + } |
| 135 | + getContent().close(); |
| 136 | + } |
| 137 | + |
| 138 | + public static final class IDs { |
| 139 | + public static final String CB_DIALOG = "cbDialog"; |
| 140 | + public static final String CB_TITLE = "cbTitle"; |
| 141 | + public static final String CB_TEXT_AND_LINK_SECTION = "cbTextAndLinkSection"; |
| 142 | + public static final String CB_TEXT = "cbText"; |
| 143 | + public static final String CB_LINK = "cbLink"; |
| 144 | + public static final String CB_BOXES_SECTION = "cbBoxes"; |
| 145 | + public static final String CB_ONLY_NECESSARY_BOX = "cbOnlyNecessaryBox"; |
| 146 | + public static final String CB_ANALYTICS_BOX = "cbAnalyticsBox"; |
| 147 | + public static final String CB_BUTTONS_SECTION = "cbButtons"; |
| 148 | + public static final String CB_ONLY_NECESSARY_BUTTON = "cbOnlyNecessaryButton"; |
| 149 | + public static final String CB_SELECTION_BUTTON = "cbSelectionButton"; |
| 150 | + public static final String CB_ALLOW_ALL_BUTTON = "cbAllowAllButton"; |
| 151 | + } |
| 152 | + |
| 153 | + public static final class Classes { |
| 154 | + public static final String CB_BUTTON = "cb-button"; |
| 155 | + public static final String CB_BOX = "cb-box"; |
| 156 | + } |
| 157 | +} |
0 commit comments