Hi,

I want to close a modal window when the user presses the ESC key.

Right now I have overridden the show method in our ModalWindow subclass
and added the keypress event listener (using jquery), the javascript
close function is just copied from the ModalWindow.

Opening the modal window and closing it with ESC works once,
unfortunately, I cannot open any more the modal window after that.

Can anyone hint on what's the problem here, or perhaps someone already
has a working solution?

This is the implementation of the show method:

    @Override
    public void show(final AjaxRequestTarget target) {
        if (!isShown()) {
            final AppendingStringBuffer buffer = new AppendingStringBuffer(500);
            buffer.append("function mwClose(ev) {\n" +
                    "var code = ev.keyCode || ev.which;\n" +
                        "if (code == 27) { " +
                            getCloseJavascript() +
                        "};" +
                    "}");
            buffer.append("jQuery(document).keypress(mwClose);\n");
            target.appendJavascript(buffer.toString());
        }
        super.show(target);
    }

The getCloseJavascript method (copied from ModalWindow, as it's private 
therein):

    /**
     * @return javascript that closes current modal window
     */
    private static String getCloseJavacript() {
        return "var win;\n" //
            + "try {\n"
            + " win = window.parent.Wicket.Window;\n"
            + "} catch (ignore) {\n"
            + "}\n"
            + "if (typeof(win) == \"undefined\" || typeof(win.current) == 
\"undefined\") {\n"
            + "  try {\n"
            + "     win = window.Wicket.Window;\n"
            + "  } catch (ignore) {\n"
            + "  }\n"
            + "}\n"
            + "if (typeof(win) != \"undefined\" && typeof(win.current) != 
\"undefined\") {\n"
            + " var close = function(w) { w.setTimeout(function() {\n"
            + "     win.current.close();\n"
            + " }, 0);  } \n"
            + " try { close(window.parent); } catch (ignore) { close(window); 
};\n" + "}";
    }

Thanx for your help,
cheers,
Martin

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to