Skip to content

Commit d2e92c5

Browse files
committed
Fixes martin-g#381 - Add functionality to disable the enforcement of the focus so that inner components (like Select2) can acquire the focus
1 parent 3bef193 commit d2e92c5

File tree

1 file changed

+37
-2
lines changed
  • bootstrap-core/src/main/java/de/agilecoders/wicket/core/markup/html/bootstrap/dialog

1 file changed

+37
-2
lines changed

bootstrap-core/src/main/java/de/agilecoders/wicket/core/markup/html/bootstrap/dialog/Modal.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public static enum Backdrop {
7878
private final IModel<Boolean> closeOnEscapeKey = Model.of(true);
7979
private final IModel<Backdrop> backdrop = Model.of(Backdrop.TRUE);
8080

81+
/**
82+
* Disables the enforcement of the focus.
83+
* Needed in cases when a component inside the modal requires
84+
* the focus as well.
85+
* See <a href="https://github.com/l0rdn1kk0n/wicket-bootstrap/issues/381">#381</a>
86+
*/
87+
private final IModel<Boolean> disableEnforceFocus = Model.of(false);
88+
8189
private final Label headerLabel;
8290
private final List<Component> buttons = new ArrayList<Component>();
8391
private final WebMarkupContainer footer;
@@ -327,14 +335,29 @@ public Modal<T> close(final AjaxRequestTarget target) {
327335
/**
328336
* Append dialog show JavaScript to current request AJAX target.
329337
*
330-
* @param target
338+
* @param target The current {@link AjaxRequestTarget}
331339
* @return This
332340
*/
333341
public Modal<T> appendShowDialogJavaScript(final AjaxRequestTarget target) {
334342
target.appendJavaScript(createActionScript(getMarkupId(true), "show"));
343+
appendDisableEnforceFocus(target);
344+
335345
return this;
336346
}
337-
347+
348+
/**
349+
* Appends JavaScript snippet that disables the modal's enforceFocus functionality
350+
*
351+
* @param target The current {@link AjaxRequestTarget}
352+
* @return This
353+
*/
354+
protected Modal<T> appendDisableEnforceFocus(AjaxRequestTarget target) {
355+
if (disableEnforceFocus.getObject()) {
356+
target.appendJavaScript("$.fn.modal.Constructor.prototype.enforceFocus = function () {};");
357+
}
358+
return this;
359+
}
360+
338361
/**
339362
* A short alias for {@link Modal#appendShowDialogJavaScript}
340363
* @param target
@@ -532,4 +555,16 @@ public final Modal<T> setUseKeyboard(boolean keyboard) {
532555
return this;
533556
}
534557

558+
/**
559+
* Whether the modal should not enforce the focus.
560+
*
561+
* @param disable true, if the modal should not enforce the focus
562+
* @return This
563+
*/
564+
public final Modal<T> setDisableEnforceFocus(boolean disable) {
565+
this.disableEnforceFocus.setObject(disable);
566+
return this;
567+
}
568+
569+
535570
}

0 commit comments

Comments
 (0)