Skip to content

Commit 7215db7

Browse files
committed
Added utility method for Dialogs - isOpen() and easily setCloseOnBrowserBackNavigation().
1 parent 3e7da2e commit 7215db7

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialDialog.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,41 @@ public class MaterialDialog extends MaterialWidget implements HasType<DialogType
8181

8282
private CssTypeMixin<DialogType, MaterialDialog> typeMixin;
8383
private FullscreenMixin fullscreenMixin;
84+
private boolean open;
85+
private boolean closeOnBrowserBackNavigation = true;
8486

8587
public MaterialDialog() {
8688
super(Document.get().createDivElement(), CssName.MODAL);
8789
}
8890

91+
@Override
92+
protected void onLoad() {
93+
super.onLoad();
94+
95+
setupBackNavigation();
96+
}
97+
98+
protected void setupBackNavigation() {
99+
if (closeOnBrowserBackNavigation) {
100+
window().on("hashchange", (e, param1) -> {
101+
e.preventDefault();
102+
if (isOpen()) {
103+
close();
104+
}
105+
return true;
106+
});
107+
} else {
108+
window().off("hashchange");
109+
}
110+
}
111+
112+
@Override
113+
protected void onUnload() {
114+
super.onUnload();
115+
116+
window().off("hashchange");
117+
}
118+
89119
@Override
90120
public void setType(DialogType type) {
91121
getTypeMixin().setType(type);
@@ -126,6 +156,17 @@ public boolean isDismissible() {
126156
return options.dismissible;
127157
}
128158

159+
public boolean isCloseOnBrowserBackNavigation() {
160+
return closeOnBrowserBackNavigation;
161+
}
162+
163+
/**
164+
* A property wherein if enabled - upon updating the browser url will automatically close the dialog
165+
*/
166+
public void setCloseOnBrowserBackNavigation(boolean closeOnBrowserBackNavigation) {
167+
this.closeOnBrowserBackNavigation = closeOnBrowserBackNavigation;
168+
}
169+
129170
@Override
130171
public void setOpacity(double opacity) {
131172
options.opacity = opacity;
@@ -206,6 +247,7 @@ public void open(boolean fireEvent) {
206247
* @param fireEvent - Flag whether this component fires Open Event
207248
*/
208249
protected void open(Element e, boolean fireEvent) {
250+
this.open = true;
209251
options.complete = () -> onNativeClose(true, true);
210252
options.ready = () -> onNativeOpen(fireEvent);
211253
$(e).openModal(options);
@@ -265,12 +307,17 @@ public void close(boolean autoClosed, boolean fireEvent) {
265307
}
266308

267309
protected void close(Element e, boolean autoClosed, boolean fireEvent) {
310+
this.open = false;
268311
if (options != null) {
269312
options.complete = () -> onNativeClose(autoClosed, fireEvent);
270313
$(e).closeModal(options);
271314
}
272315
}
273316

317+
public boolean isOpen() {
318+
return open;
319+
}
320+
274321
@Override
275322
public HandlerRegistration addCloseHandler(CloseHandler<MaterialDialog> handler) {
276323
return addHandler(handler, CloseEvent.getType());

0 commit comments

Comments
 (0)