Close all Dialogboxes when click ESCAPE(ESC) in primefaces - java

I am working with 4-5 primefaces dialogboxes. When click ESC i want close all dialogboxes which are opened.

Call the dialog's hide() function through the client side object specified with widgetVar. So if you defined your p:dialog like:
<p:dialog widgetVar="dialog1" header="Dialog 1"/>
<p:dialog widgetVar="dialog2" header="Dialog 2"/>
Your ESC button should look like:
<p:commandButton value="ESC" onclick="dialog1.hide();dialog2.hide()"/>
You can also create a reusable p:remoteCommand to close all your dialog and use that in your p:commandButton or in p:hotkey - if by "click ESCAPE" you mean hittig the Escape button:
<p:remoteCommand name="closeAll" onsuccess="dialog1.hide();dialog2.hide()"/>
then in your components refer to the closeAll() command:
<p:hotkey bind="esc" handler="closeAll()"/>
<p:commandButton value="ESC" onclick="closeAll()"/>

Although the post is old, but the answer is rather a static solution, here's a dynamic one using jQuery.
function escDialog() {
$(document).keyup(function(e) {
if (e.keyCode == 27) { // esc code is 27
closeAllDialog() ;
}
});
}
function closeAllDialog() {
for (var propertyName in PrimeFaces.widgets) {
if (PrimeFaces.widgets[propertyName] instanceof PrimeFaces.widget.Dialog ||
PrimeFaces.widgets[propertyName] instanceof PrimeFaces.widget.LightBox) {
PrimeFaces.widgets[propertyName].hide();
}
}
}
Then in your document.ready you would call escDialog()
Hope this helps.

Related

Primefaces open dialog into dialog programmatically

I can open a dialog programmatically in primefaces like this post :
Primefaces open closable dialog programmatically
I can open another dialog into my dialog too ! but when I want to close it with this code it won't close !
RequestContext.getCurrentInstance().closeDialog(null);
and I can't click on anything
it happen only when I open 2 dialogs into each other
index.xhtml
<p:commandButton value="Open First Dialog" process="#form" update="#form" actionListener="#{mybean.openFirstDialog}"/>
mybean.java
#Component
#Scope("session")
public class MyBean{
public void openFirstDialog() {
RequestContext requestContext = RequestContext.getCurrentInstance();
Map<String, Object> options = new HashMap<>();
options.put("modal", true);
options.put("width", "850");
options.put("contentWidth", "520");
options.put("closable", false);
options.put("resizable", false);
requestContext.openDialog("/page/firstdialog", options, null);
}
}
firstdialog.xhtml
<p:commandButton value="Open Second Dialog" process="#form" update="#form" actionListener="#{firstDialogBean.openSecondDialog}"/>
FirstDialogBean.java
#Component
#Scope("view")
public class FirstDialogBean{
public void openSecondDialog() {
RequestContext requestContext = RequestContext.getCurrentInstance();
Map<String, Object> options = new HashMap<>();
options.put("modal", true);
options.put("width", "850");
options.put("contentWidth", "520");
options.put("closable", false);
options.put("resizable", false);
requestContext.openDialog("/page/seconddialog", options, null);
}
}
seconddialog.xhtml
<p:commandButton value="Close Second Dialog" process="#form" update="#form" actionListener="#{secondDialogBean.close}"/>
secondDialogBean.java
#Component
#Scope("view")
public class SecondDialogBean{
public void close() {
RequestContext.getCurrentInstance().closeDialog(null);
}
}
actually i don't pass NULL while I'm closing dialog ! I do stuff and pass an object ! but I can't write full code here
then while I close second dialog I can get that Object in first Dialog and again do some stuff
I hope I could say what I mean
and after clicking "Close Second Dialog" it stuck !
I tried this too but no use :
RequestContext.getCurrentInstance().closeDialog("/page/seconddialog");
You need to name what dialog you are closing like...
RequestContext.getCurrentInstance().openDialog("titleEdit", options,
null);
RequestContext.getCurrentInstance().closeDialog("titleEdit");
See this example for having two dialogs open at once and how to close them.

Call JSP method from javascript

I have the above js and JSP code, I want to invoke procedureCall() while clicking on ok button on confirm window poopup (inside "if" statement). Is it possible and if yes, how it can be done?
<script type="text/javascript">
function confirmDelete() {
var answer = confirm("Are you sure you want to delete");
if (answer == true) {
return true;
} else {
return false;
}
}
</script>
JSP piece:
<%! public void procedureCall(int cId) {
String procedureCall = "{call NEW_PORTING_PRC.delete_album_metadata(cId)}";
try (CallableStatement cal = conn.prepareCall(procedureCall);) {
} catch (Exception e) {
e.printStackTrace();
}
%>
javascript runs in your browser, your java code is deployed in your container(Tomcat).
So only way to invoke this is via a Web call. Your javascript should invoke an ajax call to one servlet(configured in web.xml) and this servlet should invoke your java method.
like from JS you write as
$.get('URLofServlet');
OR
what you can try is submit the JSp with some parameters and reloading the JSP..
if (answer == true)
{
document.getElementById("param1").value='paramvalue';
document.getElementById("myForm").action='yourJspPath.jsp';
document.getElementById("myForm").submit();
}
Also I assume id and name will both be 'param1' for simplicity..
and in scriptlet you can do like
<%
if(request.getParamter("param1")=='paramvalue'){procedureCall();}
%>
Just to add I will recommend to remove scriplets and use ajax as in first approach.
Use form submission or ajax request or framework action to go back end and process the things needed for you.In your approach its not possible I think.
And please dont use scriplets in jsp,its not a good practice.

How to restrict a user to login in only one tab/window using java and javascript?

My requirement is to give access like one login per user, For that I have updated the login Status to true in db when user login, and false when user logout.
But the problem is when user close the window without logout.
To handle window close I have implemented the following js code
var validNavigation = false;
function wireUpEvents() {
var dont_confirm_leave = 0;
var leave_message = 'You sure you want to leave?'
function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave!==1) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
window.location = "logout.jsp";
}
}
window.onbeforeunload=goodbye;
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type='button']").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
Here it works for window close, means if user close the window it goes to logout page, but problem is it was going to logout page when user reloads the page.
So I need bind the reload event also like the above js code for f5, submit and anchor tags.
Pleae help me in this regard.
Thanks in Advance...
Two things came into my mind:
First of all the fact that the user is logged in, doesnt mean that theres any activity going on, some people leave there pc turned on with the browser running.
Second is that even if the user closes a page it doesnt necesserly means that he/she wanted to logout and its especially true if the user use two tab to navigate on your site.
So instead of trying to check when they close the tab I suggest considering the idea of logging them out only when they login from somewhere else and/or setting up a timer that automatically log them out after a certain amount of inactivity.

ZK showBusy called before long operation, but it is showed after it

Using Zk 6.5.11CE.
In a modal window I got a button which clicked send an email. It is a long operation, and in waiting time I want to use Clients.showBusy to block the user to click/modify my modal window.
Here's the ZUL
<window apply="org.zkoss.bind.BindComposer"
viewModel="#id('vm') #init('viewmodel.EventView')">
...fill the form...
<button label="SEND" onClick="#command('send')" autodisable="self" />
</window>
Here's Java EventView.java
#Command
#NotifyChange("*")
public void send() {
Clients.showBusy(winFather.getModalWin(), "Please wait...");
// ... do something ...
sendMail(); // it takes 2/3 seconds
Clients.clearBusy(winFather.getWinEvent());
}
Where winFather is the win (my home page) that called the modal window and getModalWin() get the modal window, in which i got the showBusy problem. Hope it is clear :)
However, i'm searching the web and found something interesting here and here. So I got I have to use Echo Events.
For those had not clicked links:
<window id="w" width="200px" title="Test echoEvent" border="normal">
<attribute name="onLater">
doLongOperation(); //take long to execute
Clients.clearBusy(); //remove the busy message
</attribute>
<button label="Echo Event">
<attribute name="onClick">
Clients.showBusy("Execute..."); //show a busy message to user
Events.echoEvent("onLater", w, null); //echo an event back
</attribute>
</button>
</window>
Question(s):
is Echo Events the only chance to resolve the problem, or maybe I forgot to do something to make showBusy properly works?
because I really don't want code in my zul page, how can I define the stuff in my viewModel?
If i assign with binded value
disabled="#load('vm.busy')"
to all components I want to disable during email sending, and substitute Clients showBusy and clearBusy with
busy = true; // Clients.showBusy(winFather.getModalWin(), "Please wait...");
...
busy = false; // Clients.clearBusy(winFather.getModalWin());
I got the same problem, the email is sending before zul components were disabled. It seems to be a sync problem.
hi
i had same problem before , in my code you can see that I've override onClientInfo on my target component,so if you call this event , you can reach your goal.
#Listen("onClick = #addRejectDocumentBtn")
public void openWaiting() {
Clients.showBusy(saleRejectMainPageWin,"please wait...");
saleRejectMainPageWin.addEventListener(Events.ON_CLIENT_INFO, new EventListener<Event>() {
#Override
public void onEvent(Event event) throws Exception {
LongOperationExample(); <- here is my long operation
Clients.clearBusy(saleRejectMainPageWin);
}
});
"Here you can call the event"
Events.echoEvent("onClientInfo", saleRejectMainPageWin, null);
}

Open url inside a grid in a new browser window

I have a list of urls in a grid, And I need that when a user click in a url a new browser windows open with the same url
I read some threads but in my case I believe my situation is a little different. In my controller
I'm using the following code
UrlListCollection.generateListUrl();
dataGrid.setRowRenderer(new RowRenderer() {
public void render(Row row, Object data) throws Exception {
UrlObj url = (UrlObj) data;
row.getChildren().add(new Label("Some data"));
row.getChildren().add(new Toolbarbutton(url.getUrlApp())); // url.getUrlApp() will be return a link like http://www.google.com
}
});
In my view(zul) I have
<grid id="dataGrid" width="100%">
<columns>
<column label="Some Data" sort="auto(FIELD_NAME)" width="200px" />
<column label="URL LINK" sort="auto(URL)" width="630px" />
</columns>
</grid>
But the common way to set an event an component in java can be:
Toolbarbutton button = new Toolbarbutton(url.getUrlApp()));
button.addEventListener(Events.ON_CLICK, new EventListener() {
public void onEvent(evt) {
// what I put here to open a Link in another web browser window ????
// and I need to be able to open every object value retrieved by url.getUrlApp() ???
}
});
I don't now what is necessary to make my code works..for me the way to apply a Event to toolbar button inside a grid that use RowRenderer method is strange. I can't see a solution by myself.
You can use the following example,
Executions.getCurrent().sendRedirect("http://www.google.com", "_blank");
Or you may use the A component with the setHref() method instead of the Toolbarbutton component.
This works fine for me, thanks!
UrlObj url = (UrlObj) data;
Toolbarbutton tb = new Toolbarbutton(url.getUrlApp());
tb.setHref(url.getUrlApp());
tb.setTarget("_blank");
row.getChildren().add(new Label("Some data"));
row.getChildren().add(tb);

Categories