|
| 1 | +/* |
| 2 | + * Copyright 2000-2022 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.component.dialog.tests; |
| 17 | + |
| 18 | +import com.vaadin.flow.component.dialog.Dialog; |
| 19 | +import com.vaadin.flow.component.html.Div; |
| 20 | +import com.vaadin.flow.component.html.Hr; |
| 21 | +import com.vaadin.flow.component.html.NativeButton; |
| 22 | +import com.vaadin.flow.router.Route; |
| 23 | + |
| 24 | +/** |
| 25 | + * Page created for testing purposes. Not suitable for demos. |
| 26 | + * |
| 27 | + * @author Vaadin Ltd. |
| 28 | + * |
| 29 | + */ |
| 30 | +@Route("vaadin-dialog/dialog-modality") |
| 31 | +public class ModalityDialogsPage extends Div { |
| 32 | + |
| 33 | + private Log log = new Log(); |
| 34 | + |
| 35 | + public ModalityDialogsPage() { |
| 36 | + Dialog modalDialog = createModalDialog(); |
| 37 | + |
| 38 | + Dialog nonModalDialog = setupNonModalDialog(); |
| 39 | + |
| 40 | + NativeButton modalButton = new NativeButton("Open modal dialog", |
| 41 | + event -> modalDialog.open()); |
| 42 | + modalButton.setId("open-modal-dialog"); |
| 43 | + |
| 44 | + NativeButton button = new NativeButton("Open non modal dialog", |
| 45 | + event -> nonModalDialog.open()); |
| 46 | + button.setId("open-dialog"); |
| 47 | + |
| 48 | + NativeButton logButton = new NativeButton("Log", |
| 49 | + event -> log.log("Clicked")); |
| 50 | + logButton.setId("log"); |
| 51 | + |
| 52 | + final NativeButton showModal = new NativeButton("Show Hidden Modal", |
| 53 | + e -> modalDialog.setVisible(true)); |
| 54 | + showModal.setId("show"); |
| 55 | + add(modalButton, showModal, button, logButton, new Hr(), log); |
| 56 | + } |
| 57 | + |
| 58 | + private Dialog setupNonModalDialog() { |
| 59 | + final Dialog nonModalDialog = createNonModalDialog(); |
| 60 | + nonModalDialog |
| 61 | + .add(new NativeButton("close", e -> nonModalDialog.close())); |
| 62 | + return nonModalDialog; |
| 63 | + } |
| 64 | + |
| 65 | + private Dialog createModalDialog() { |
| 66 | + Dialog modalDialog = new Dialog(); |
| 67 | + modalDialog.setCloseOnOutsideClick(false); |
| 68 | + final NativeButton modalClose = new NativeButton("close", |
| 69 | + e -> modalDialog.close()); |
| 70 | + modalClose.setId("close"); |
| 71 | + final NativeButton hide = new NativeButton("hide", |
| 72 | + e -> modalDialog.setVisible(false)); |
| 73 | + hide.setId("hide"); |
| 74 | + modalDialog.add(modalClose, hide); |
| 75 | + |
| 76 | + Dialog nonModalDialog = createNonModalDialog(); |
| 77 | + |
| 78 | + final NativeButton closeSub = new NativeButton("close", |
| 79 | + e -> nonModalDialog.close()); |
| 80 | + closeSub.setId("close-sub"); |
| 81 | + final NativeButton logSub = new NativeButton("log", |
| 82 | + e -> log.log("sub-click")); |
| 83 | + logSub.setId("log-sub"); |
| 84 | + nonModalDialog.add(closeSub, logSub); |
| 85 | + |
| 86 | + final NativeButton openSubDialog = new NativeButton("open dialog", |
| 87 | + e -> nonModalDialog.open()); |
| 88 | + openSubDialog.setId("open-sub"); |
| 89 | + modalDialog.add(openSubDialog); |
| 90 | + |
| 91 | + return modalDialog; |
| 92 | + } |
| 93 | + |
| 94 | + private Dialog createNonModalDialog() { |
| 95 | + Dialog nonModalDialog = new Dialog(); |
| 96 | + |
| 97 | + nonModalDialog.setCloseOnOutsideClick(false); |
| 98 | + nonModalDialog.setModal(false); |
| 99 | + |
| 100 | + return nonModalDialog; |
| 101 | + } |
| 102 | + |
| 103 | + public static class Log extends Div { |
| 104 | + |
| 105 | + public static final String LOG_ID = "log-output"; |
| 106 | + |
| 107 | + private int logCount; |
| 108 | + |
| 109 | + public Log() { |
| 110 | + setId(LOG_ID); |
| 111 | + } |
| 112 | + |
| 113 | + public void log(String msg) { |
| 114 | + Div div = new Div(); |
| 115 | + div.addClassName("log"); |
| 116 | + logCount++; |
| 117 | + div.setText(logCount + ". " + msg); |
| 118 | + add(div); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments