How Close PrimeFaces PhotoCam? - image

I'm using photocam for take picture with camera but capture option is always open. I am using photocam autostarts property with created to button which invite a method like that:
public void pcFotoStarter()
{
if (pcFoto == Boolean.FALSE)
pcFoto = Boolean.TRUE;
else
pcFoto = Boolean.FALSE;
}
This method makes my method's pcFoto field true or false , and then I defined <f:ajax execute="button" render="PhotoCam" />
According to the pcFoto Boolean Value(true or false), photocam is working or not. Problem is, photocam starts at once and I can close only 1 time photocam.Ant than I am unable to restart photocam. And my webcam usage yellow led is flashing. It's like webcam is already in usage.

try execute this Javascript code when you want to off camera
Webcam.stream.getTracks().forEach(x=> x.stop());
In my case i turn on camera while open p:photoCam in p:dialog (dynamic) and has p:ajax callback on "close" event. Like this
<p:dialog id="modalDialog" widgetVar="photoEditDialog"
modal="true" onHide="PF('photoEditDialog').toggleMaximize();"
resizable="false" closeOnEscape="true"
dynamic="true"
>
<p:ajax event="close" update="photo #this" onsuccess="Webcam.stream.getTracks().forEach(x=> x.stop());"/>
<div style="display: flex;">
<section>
<h1>Camera</h1>
<p:photoCam widgetVar="photoCam" listener="#{cc.attrs.value.onPhotoCapture}" update="preview"
width="400" height="300" photoWidth="1024" photoHeight="768"
/>
<p:commandButton type="button" value="Capture" onclick="PF('photoCam').capture()"/>
</section>
<section>
<h1>Preview</h1>
<p:graphicImage id="preview" value="#{cc.attrs.value.photo}" stream="false"/>
</section>
</div>
</p:dialog>

Related

JSF: Datatable excecuting remove method twice

I'm facing a strange behavior in my jsf datatable component and couldn't figure out what`s going on. This is the scenario: I have a datatable with a remove button on each row. When I click on the button of the last row, it is correctly deleted. When I click on the button in the row before the last row, it deletes the row itself and also the next row. No row above it is affected.
My environment: Eclipse Neon + Tomcat 8 + Primefaces 6.0
I'm running the project from inside Eclipse.
The datasource of the p:dataTable is being modified by data selected from a p:autoComplete component.
In debbuging mode I could check that the remove method is being called multiple times with different parameters each time. Bellow is a part of the code.
It seems (looking at the browser developer tool) that the Ajax is calling the server method just once, so the multiple execution is happening at the server side.
I've already googled for similar issues in links like Link 1 and Link 2.
The page.xhtml:
<p:autoComplete id="ddl" dropdown="false"
value="#{bean.chosenItem}" var="item"
itemLabel="#{item.name}" itemValue="#{item}"
converter="convertItem"
emptyMessage="No item found" queryDelay="1000"
minQueryLength="3" autocomplete="off"
completeMethod="#{bean.findItemsCompleteText}"
forceSelection="true" placeholder="Type item name"
cache="true" widgetVar="dllWGV">
<p:ajax event="itemSelect"
listener="#{bean.addToDataTable}"
update="dtb ddl" />
</p:autoComplete>
<p:dataTable id="dtb"
value="#{bean.chosenItems}" var="rowItem"
sortBy="#{rowItem.name}" scrollable="true" scrollHeight="90"
scrollWidth="337" tableStyle="#{bean.dtbStyle}"
widgetVar="dtbWGV">
<p:column style="border: none; padding-left: 2px;">
<p:commandButton actionListener="#{bean.removeFromDataTable(rowItem)}"
update="dtb"
style="margin-right: 2px;" icon="fa fa-trash" />
<h:outputText value="#{rowItem.name}" />
</p:column>
<p:column style="border: none; width: 28%;">
<p:spinner suffix="%" min="0" max="100" stepFactor="0.25"
value="#{rowItem.dAux}" size="5" />
</p:column>
</p:dataTable>
The managed bean (ViewScoped):
private ItemClass chosenItem;
private List<ItemClass> chosenItems;
private String dtbStyle;
//getters and setters with no logic inside
//beyond getting and setting the properties
#PostConstruct
public void init(){
if (chosenItems == null) {
chosenItems = new ArrayList<ItemClass>();
}
setDtbStyle("visibility: hidden; width: 320px;");
}
//each time, during the multiple executions,
//the parameter localItem has a different value
public void removeFromDataTable(ItemClass localItem) {
this.chosenItems.remove(localItem);
if(chosenItems.size() == 0) {
setDtbStyle("visibility: hidden; width: 320px;");
}
}
public void addToDataTable(SelectEvent event) {
this.chosenItems.add(this.chosenItem);
setDtbStyle("visibility: visible; width: 320px;");
this.chosenItem = null;
}
Thanks for any clue about what could be happening.
Juliano
After reading here and here, I tried put process="#this" on the remove p:commandButton in the p:dataTable. It has made the trick, but I don`t know excactly how. I've understood that #this tells the server to process just the methods in the actionLinstener of the p:commandButton, ignoring any other actions in other components of the page. Well, the fact that this modification has worked points that the problem is in the overall logic of my page. Actually, I don't know where exactly my mistake is, but as the page is pretty heavy, I'm almost sure there really is a silly mistake of mine somewhere in it.
[1]:Clicking p:commandButton causes the value of p:selectOneMenu being set twice
[2]:Understanding process and update attributes of PrimeFaces
The page.xhtml was modified to:
<p:dataTable id="dtb"
value="#{bean.chosenItems}" var="rowItem"
sortBy="#{rowItem.name}" scrollable="true" scrollHeight="90"
scrollWidth="337" tableStyle="#{bean.dtbStyle}"
widgetVar="dtbWGV">
<p:column style="border: none; padding-left: 2px;">
<p:commandButton actionListener="#{bean.removeFromDataTable(rowItem)}"
update="dtb"
process="#this" <!--solution: adding this attribute-->
style="margin-right: 2px;" icon="fa fa-trash" />
<h:outputText value="#{rowItem.name}" />
</p:column>
<p:column style="border: none; width: 28%;">
<p:spinner suffix="%" min="0" max="100" stepFactor="0.25"
value="#{rowItem.dAux}" size="5" />
</p:column>
</p:dataTable>

JSF input file tag + ajax

I lost whole day on this issue and got nothing. I know that the question was asked but it was never answered. BalusC said that this issue is fixed in JSF 2.2.5 but either it is not or i do something wrong.
I use h:inputFile tag with ajax to render choosen image in preview div after chosing one from disk. However it's rendered only once and i have to reload page to see next chosen image. It causes more errors. For example when I use SUBMIT button to send chosen image to DB, ajax from commandLink is not executed (image is saved and rendered after page after refresh).
It looks to me like ajax is broken after chosing an image from disk and followed ajax actions are not executed, even they dont belong to h:inputfile.
This is my JSF form (one of its versions because i tried so many tricks)
<h:panelGroup class="photo-update" rendered="#{profileBean.page eq 'photo'}">
Dodaj lub zmień swoje zdjęcie profilowe[...]
<h:form enctype="multipart/form-data">
<h:panelGroup id="form-content">
<h:panelGroup id="current-image" class="current-image">
<p:graphicImage value="#{imageBean.getUserImage()}"/>
</h:panelGroup>
<h:panelGroup id="accept-container-ajax">
<h:panelGroup id="accept-container" class="accept-container"
rendered="true">
<div class="arrow">
<img src="resources/images/arrow.png"/>
<div class="change-text">Zamień na</div>
</div>
<h:panelGroup id="new-image" class="new-image">
<p:graphicImage value="#{profileBean.getConvertedImage()}"/>
</h:panelGroup>
<h:commandLink class="btn btn-default ok-button" value="zatwierdź"
action="#{profileBean.uploadPhoto}">
<f:ajax render="accept-container-ajax current-image"
execute="#form"/>
</h:commandLink>
</h:panelGroup>
</h:panelGroup>
<div class="btn btn-default change-image-container">
Zmień zdjęcie
<h:inputFile immediate="true" id="change-image"
class="change-image" value="#{profileBean.image}">
<f:ajax render="accept-container-ajax" execute="#form"/>
</h:inputFile>
</div>
</h:panelGroup>
</h:form>
</h:panelGroup>
This is fragment of the bean:
public StreamedContent getConvertedImage() throws IOException {
return userBo.convertPartToStream(image);
}
public void uploadPhoto() {
this.renderChangeContainer = false;
if (userBo.addImage(this)) {
FacesContext.getCurrentInstance().addMessage(
null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "...", null));
} else {
FacesContext.getCurrentInstance().addMessage(
null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "...", null));
}
}
injected BO methods are OK.
Please give me a clue what is going on.
EDIT:
I also see ugly iframe with some response content i guess.

JSF update component on every ajax request

Hi i want to render the JSF messages as bootstrap alerts
<ui:repeat value="#{facesContext.messageList}" var="facesMessage">
<div class="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Warning!</strong> #{facesMessage.summary}
</div>
</ui:repeat>
in primefaces there is
<p:messages autoUpdate=true>
means it is rerendered on every ajax request without explicit adding it to the render-attribute of a commandLink
is there a way to achieve this with plain JSF-ajax tags?
You might try something like this during your page load:
jsf.ajax.addOnEvent(autoRender);
And in autoRender you would be able to trigger a kind of action in order to render the target component. For instance:
function autoRender() {
$(":form:autoRenderHiddenButton").click();
}
And in the button:
<h:commandButton id="autoRenderHiddenButton"
style="display: none">
<f:ajax render="theComponentIdYouWantToRender"/>
</h:commandButton>
Or you can easily achieve the same behavior by using Omniface's commandScript:
<o:commandScript name="autoRender" render="theComponentIdYouWantToRender"/>

How to customize the presentation of faces messages

I would like to customize the presentation of faces messages.
For this,
<h:inputText id="name" required="true" />
when validation is failed, then it will be shown in a
<h:message for="name" />
However, I would like to customize the presentation call JS as follows:
<div class="notification"></div>
function showNotification(msg){
$(".notification").html(msg);
$(".notification").fadeIn(1000, function(){
timeout = setTimeout(function(){
$(".notification").fadeOut(1000);
}, 5000);
});
}
How can I achieve this?
You can use FacesContext#getMessageList() to get the messages in the view, if necessary the ones for a specific client ID. You can iterate over them in a ui:repeat. Each item is a FacesMessage which has several getters. You can display any HTML in the message unescaped by using <h:outputText escape="false">.
So, in a nutshell:
<ui:repeat value="#{facesContext.messageList('form:name')}" var="message">
<div><h:outputText value="#{message.summary}" escape="false" /></div>
</ui:repeat>
(in the above example, I assume that your form has id="form")
Or, if that HTML help link is actually not part of the message, then so:
<ui:repeat value="#{facesContext.messageList('form:name')}" var="message">
<div>#{message.summary} help</div>
</ui:repeat>

Command button inside jQuery SimpleModal window does not invoke action

I have created a DIV section in my page in the following way:
<div id="modalAlert" style="display: none;">
<h:panelGroup layout="block">
<p>My Heading </p>
</h:panelGroup>
<h:panelGroup layout="block">
</h:panelGroup><h:panelGroup layout="block">
<h:commandButton id="btnSaveConfirmation" value="save" action="#{myBean.method}"> </h:commandButton>
</h:panelGroup>
</div>
And I am using the following JS script to display a SimpleModal window using jQuery:
function showAlert() {
fundpickerdialog = $('#modalAlert').modal({
opacity : 100,
overlayCss: {backgroundColor:"#fff"},
modal : true,
dataId : 'modalAlert',
classname: 'ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable',
closeOnEscape : false,
width : 50,
height : 50
});
}
$(document).ready(function() {
showAlert();
}
});
The modal window is appearing but on clicking the Save button it's not calling the backing bean method.
You forgot the form tags (h:commandButton must be located inside a form), wrap the content of the div with tags
try something like this:
<div id="modalAlert" style="display: none;">
<h:form>
<h:panelGroup layout="block">
<p>My Heading </p>
</h:panelGroup>
<h:panelGroup layout="block">
</h:panelGroup><h:panelGroup layout="block">
<h:commandButton id="btnSaveConfirmation" value="save" action="#{myBean.method}"></h:commandButton>
</h:panelGroup>
</h:form>
</div>
Some JavaScript/jQuery modal dialog approaches will remove the element representing the modal dialog and all of its children from its current position in the HTML DOM tree and re-insert it as immediate child of the <body> in order to prevent potential positioning and layering issues across various browsers. Since you didn't put the <form> inside the element representing the modal dialog, it will end up being form-less.
You really need to put the <h:form> inside the element representing the modal dialog.
Further, I recommend to use a JSF component library instead of an arbitrary jQuery plugin. For example PrimeFaces has a <p:dialog> component for exactly this purpose. It has already taken potential nasty JSF-related side effects into account for you without that you need to write any additional line of JS/jQuery code.

Resources