Close ModalWindow on keypress (ESC)

2012-08-17 Thread Marco Di Sabatino Di Diodoro
Hi, 

I need close the ModalWindows of my application when I press ESC on the 
keyboard.

Suggestions?

Regards
M

--

Dott. Marco Di Sabatino Di Diodoro
Tel. +39 3939065570

Tirasa S.r.l.
Viale D'Annunzio 267 - 65127 Pescara
Tel +39 0859116307 / FAX +39 085973
http://www.tirasa.net

Apache Syncope PPMC Member
http://people.apache.org/~mdisabatino






Re: Close ModalWindow on keypress (ESC)

2012-08-17 Thread Cedric Gatay
Hi,
I actually use this in my project :


/**
 * Behavior allowing to close the window on esc, requires jQuery
 * Slighlty modified from
http://stackoverflow.com/questions/5042426/wicket-close-modalwindow-on-keypress
 */
private class CloseOnESCBehavior extends AbstractDefaultAjaxBehavior {

@NonNls
private static final String PRE_JS = $(document).ready(function()
{\n +
   $(document).bind('keyup',
function(evt) {\n +
 if (evt.keyCode == 27)
{\n;
@NonNls
private static final String POST_JS = \n evt.preventDefault();\n +
  }\n +
});\n +
  });;

/**
 * {@inheritDoc}
 */
@Override
protected void respond(AjaxRequestTarget target) {
close(target);
}

/**
 * {@inheritDoc}
 *
 * @return always null to hide the indicator (it does not flicker)
 */
@Override
protected String findIndicatorId() {
return null;
}

/**
 * {@inheritDoc}
 */
@Override
public void renderHead(Component component, IHeaderResponse
response) {
response.renderJavaScript(new
StringBuilder(PRE_JS).append(getCallbackScript())
  .append(POST_JS).toString(),
closeModalOnEsc);
}
}

You'll need to adapt it if you're not using JQuery in your project. All you
have to do is add this behavior on your modal window.

Regards,
__
Cedric Gatay
http://www.bloggure.info | http://cedric.gatay.fr |
@Cedric_Gatayhttp://twitter.com/Cedric_Gatay


On Fri, Aug 17, 2012 at 12:05 PM, Marco Di Sabatino Di Diodoro 
marco.disabat...@tirasa.net wrote:

 Hi,

 I need close the ModalWindows of my application when I press ESC on the
 keyboard.

 Suggestions?

 Regards
 M

 --

 Dott. Marco Di Sabatino Di Diodoro
 Tel. +39 3939065570

 Tirasa S.r.l.
 Viale D'Annunzio 267 - 65127 Pescara
 Tel +39 0859116307 / FAX +39 085973
 http://www.tirasa.net

 Apache Syncope PPMC Member
 http://people.apache.org/~mdisabatino







Re: Close ModalWindow on keypress (ESC)

2012-08-17 Thread Marco Di Sabatino Di Diodoro
Hi, 

thanks for the help.
I integrate your code in my application, but the Modal Window closes only when 
I clicked on it with the mouse first.

How can I set the focus when the ModalWindow opens?

M

On Aug 17, 2012, at 12:31 PM, Cedric Gatay wrote:

 Hi,
 I actually use this in my project :
 
 
/**
 * Behavior allowing to close the window on esc, requires jQuery
 * Slighlty modified from
 http://stackoverflow.com/questions/5042426/wicket-close-modalwindow-on-keypress
 */
private class CloseOnESCBehavior extends AbstractDefaultAjaxBehavior {
 
@NonNls
private static final String PRE_JS = $(document).ready(function()
 {\n +
   $(document).bind('keyup',
 function(evt) {\n +
 if (evt.keyCode == 27)
 {\n;
@NonNls
private static final String POST_JS = \n evt.preventDefault();\n +
  }\n +
});\n +
  });;
 
/**
 * {@inheritDoc}
 */
@Override
protected void respond(AjaxRequestTarget target) {
close(target);
}
 
/**
 * {@inheritDoc}
 *
 * @return always null to hide the indicator (it does not flicker)
 */
@Override
protected String findIndicatorId() {
return null;
}
 
/**
 * {@inheritDoc}
 */
@Override
public void renderHead(Component component, IHeaderResponse
 response) {
response.renderJavaScript(new
 StringBuilder(PRE_JS).append(getCallbackScript())
  .append(POST_JS).toString(),
 closeModalOnEsc);
}
}
 
 You'll need to adapt it if you're not using JQuery in your project. All you
 have to do is add this behavior on your modal window.
 
 Regards,
 __
 Cedric Gatay
 http://www.bloggure.info | http://cedric.gatay.fr |
 @Cedric_Gatayhttp://twitter.com/Cedric_Gatay
 
 
 On Fri, Aug 17, 2012 at 12:05 PM, Marco Di Sabatino Di Diodoro 
 marco.disabat...@tirasa.net wrote:
 
 Hi,
 
 I need close the ModalWindows of my application when I press ESC on the
 keyboard.
 
 Suggestions?
 
 Regards
 M
 
 --
 
 Dott. Marco Di Sabatino Di Diodoro
 Tel. +39 3939065570
 
 Tirasa S.r.l.
 Viale D'Annunzio 267 - 65127 Pescara
 Tel +39 0859116307 / FAX +39 085973
 http://www.tirasa.net
 
 Apache Syncope PPMC Member
 http://people.apache.org/~mdisabatino
 
 
 
 
 

--

Dott. Marco Di Sabatino Di Diodoro
Tel. +39 3939065570

Tirasa S.r.l.
Viale D'Annunzio 267 - 65127 Pescara
Tel +39 0859116307 / FAX +39 085973
http://www.tirasa.net

Apache Syncope PPMC Member
http://people.apache.org/~mdisabatino