Code: Select all
.
.
.
<p:commandLink
update="portrait"
oncomplete="setTimeout(function(){PF('portrait-dialog').show();}, 50);">
<f:setPropertyActionListener
value="#{portrait}"
target="#{portraits.portrait}" />
<p:graphicImage
value="/files#{portrait.getPath('_162x288.jpg')}"
style="height: 192px; width: auto;" />
</p:commandLink>
.
.
.
<p:dialog
id="portrait-dialog"
showHeader="false"
widgetVar="portrait-dialog"
modal="true"
responsive="true"
onShow="positionDialog('portrait-dialog')">
<p:graphicImage
id="portrait"
style="max-height: 85vh;"
value="/files#{portraits.portrait.getPath('_810x1440.jpg')}"
onclick="PF('portrait-dialog').hide();" />
</p:dialog>
If I delay the call to the dialog show method 50 ms as shown in my code, everything works well. The dialog updates with the new graphic and then when displayed is centered on the screen.
If I don't delay 50 ms, the initial dialog is only 40px wide and the positionDialog function centers the dialog based on the 40px width. Soon after the dialog is displayed and centered incorrectly, the graphic finishes downloading and the dialog expands to the new width as dictated by the graphic but the dialog is not centered.
Why is the oncomplete being called before the graphic finishes downloading as part of the update?
Is there a better way for me to respond to the change in width of the dialog and recenter than delaying 50 ms before showing?