Using Pop-up in Liferay

When developing portlets in Liferay there are two ways to implement pop-ups. We can use a Floating Div or the traditional new browser Window without the toolbars. Each of them has got their advantages so we will have to decide which one fits us better. For example, if you are using a pop up with a form, you won't be able to send files attached to the form if you are using a floating div.

In this article there are some tips to develop both types of pop-ups.

Floating Div Pop-Up #

Liferay provides a class called Expanse.Popup to implement this type of pop ups. Here is how this type of pop up would look like:

One advantage of this type of pop up is that they are invisible to pop up blockers (although that could change as they get smarter). This javascript code will make an asynchronous call to the url we give it and will place the content in our page. Below there is an example which will load the url 'url' in a pop up called 'our title':

var popup = new Expanse.Popup(
				{
					header: 'our title',
					position:[150,150],
					modal:true,
					width:500,
					height:300,
					xy: ['center', 100],
					url: 'url',
					urlData: {
						parameter1: 'value1',
                                                parameter2: 'value2'
					}
				}
			);

It's important to note that if the url is a portlet url it must have the windowState parameter set to LiferayWindowState.EXCLUSIVE, because we just want the content produced by the portlet and not the extra portlet decoration produced by the portal.

To implement a button or link to close the pop up use the following code:

Expanse.Popup.close(this);

This type of pop-up is used in several Liferay core portlets like the image gallery (show pictures), journal (edit template) or my communities (publish to live). Look at them for further examples of usage.

Window Pop up #

This type of pop ups will load in a new window. In order to do that we must set the windowState parameter in the portlet url to LiferayWindowState.POP_UP. This windowState will produce that the result of calling a portlet url will be shown in another window, like a real pop up. There are no visual effects asociated, it's a different window.

However, from this window we can still make javascript calls to the parent page javascript functions as the javascript object opener refers to the parent window. There is an example below:

<input type="button" value="select" onClick="
	var organizationWindow = window.open('
			<portlet:renderURL 
				windowState="<%= LiferayWindowState.POP_UP.toString() %>">
				<portlet:param name="struts_action" value="/enterprise_admin/select_organization" />
		 		<portlet:param name="tabs1" value="organizations" />
			</portlet:renderURL>',
			 'title',
			'directories=no, height=640, location=no, menubar=no, resizable=yes, 
                                                                               scrollbars=yes, status=no, toolbar=no, width=680');
	 
	organizationWindow.focus();" />

If we want to close the pop up we just need to do the following:

 window.close(); 

If we want to close the parent page we can do the following:

opener.close(); 

This is used in several portlets like the image gallery (show slideShow), enterprse admin (select organizations for users...)

Floating Div Pop-up in 6.1 #

In 6.1, Liferay uses pop-ups extensively. Here is the code you can simply put in your jsp to load a portlet or any other content asynchronously (Ajax) using Alloy.

<aui:script>

Liferay.provide(
	window,
	'showSiteManagement',
	function() {
		var A = AUI(); 	
		var portletURL="<%= themeDisplay.getURLManageSiteMemberships().toString() %>";
		var dialog = new A.Dialog(
			{
				destroyOnClose: true,
				modal: false,
				title: 'My title',
				width: 900
			}
		).plug(
			A.Plugin.IO,
				{
					uri: portletURL.toString()
				}
		).render();
	},
	['aui-dialog']
); 
</aui:script>

Now, making a simple javascript call to the "showSiteManagement()" will open a floating window and load in the portles content. However as you will see, this does not work so very well if your portlet has actions and url. Any action/urls will result in reload of the whole page.

For those cases Liferay has another tool witch opens an IFrame inside the Alloy Dialog. The result looks the same however it works much better for displaying portlets and such.

<aui:script>
	Liferay.provide(
		window,
		'showSiteManagement',
		function() {
			var instance = this;

			var url='<%= themeDisplay.getURLManageSiteMemberships().toString() %>';

				Liferay.Util.openWindow(
					{
						cache: false,
						dialog: {
							align: Liferay.Util.Window.ALIGN_CENTER,
							after: {
								render: function(event) {
									this.set('y', this.get('y') + 50);
								}
							},
							width: 820
						},
						dialogIframe: {
							id: 'siteManagementIframe',
							uri: url
						},
						title: 'Site Management',
						uri: url
					}
				);
		},
		['liferay-util-window']
	);
	</aui:script>

In both cases I have used the "themeDisplay.getURLManageSiteMemberships().toString()" to get the URL to the portlet. However this code can be used to render a JSP page.

<portlet:renderURL var="somePageURL" windowState="<%= LiferayWindowState.POP_UP.toString() %>">
	<portlet:param name="jspPage" value="/jsp/some/page.jsp"/>
	<portlet:param name="redirect" value="<%= currentURL %>"/>
</portlet:renderURL>

Or this to render another portlet, you can generate an URL like this :

		var url = Liferay.PortletURL.createRenderURL();
                url.setPortletId("3");
                url.setWindowState("pop_up");

PS! In this last code : 1) Not all portlets can be viewed without being white-listet. 2) To use createRenderURL, you must add this 'liferay-portlet-url' to the last parameter of Liferay.provide

2 Attachments
112380 Views
Average (3 Votes)
The average rating is 3.3333333333333335 stars out of 5.
Comments
Threaded Replies Author Date
In Liferay-trunk at revision 33901 "Expanse" is... Oliver Bayer July 23, 2009 5:45 AM
Looks like it was renamed again and in my case... Alexey Kakunin September 19, 2009 7:12 AM
Sorry - Liferay.Popup - rest of attributes... Alexey Kakunin September 19, 2009 7:12 AM
This mechanism doesn't work with IceFaces... Alex Wallace January 25, 2010 10:56 AM
After playing with it, loading via ajax is an... Alex Wallace January 25, 2010 11:58 AM
Hi, Can you tell me how can I display a pop-up... Madhura Raut January 18, 2011 9:16 PM
hi Bozhidar Dedov September 3, 2012 4:15 AM
Hi, how could I show a jsp page in popup? I... Mahdi Lashkari September 29, 2012 6:32 AM
Hi Alex I am having the same issue I... dominick campbell February 26, 2013 6:01 AM
i did liferay popup and how do i can localize... delang j May 9, 2010 11:42 PM
can anyone tell me how can i use the code???... ankit yakkundi August 27, 2010 4:22 AM
Hi Ankit, You can create a jsp which will... Faris Abdulla September 6, 2010 8:38 AM
hi, i try using Liferay.popup and... Brahim TARNAOUI January 12, 2011 10:04 AM
hi, In Liferay-trunk at revision 33901... Madhura Raut January 18, 2011 9:14 PM
Here it is what I've done to render a jsp page... Riccardo Rotondo August 7, 2013 7:09 AM
I know this thread is quite old but maybe this... Luca Bacco November 8, 2014 12:29 AM
Hi newbie here how am i supposed to run... Edgar Trania December 22, 2013 6:12 PM
Hi, in the jsp page where the button is... Riccardo Rotondo February 4, 2014 1:50 AM

In Liferay-trunk at revision 33901 "Expanse" is renamed to "Alloy" (LPS-3987).
So to use the "Popup"-function in trunk you must change "new Expanse.Popup" to "new Alloy.Popup()"
Posted on 7/23/09 5:45 AM.
Looks like it was renamed again and in my case (Liferay 5.2.3) I had to use Liferay.POPUP to make it working
Posted on 9/19/09 7:12 AM in reply to Oliver Bayer.
Sorry - Liferay.Popup - rest of attributes looks like stay same
Posted on 9/19/09 7:12 AM in reply to Alexey Kakunin.
This mechanism doesn't work with IceFaces portlets.

For one, IceFaces portlet don't wokr (actions don't work) when using EXCLUSIVE mode.

Second, these popups use Ajax to load their content and this breaks IceFaces as well... I wish there was an option to not use Ajax to load the content of the floating div popup.

I will have to look for other Floating divs that don't...

Thanks!
Posted on 1/25/10 10:56 AM.
After playing with it, loading via ajax is an option, commonly seen in liferay, where originally message is set to a div with a loading animation, and then jQuery.ajax is used to insert the portlet action url...

If instead, the message is substituted with an iframe that has as src the url where my IceFaces portlet exists, it works...

However the theme displays completly, not just the portlet... I've customized the theme to only show the portlet and nothing else conditionally via http params.. That way the whole thing seems to work: IceFaces portlet in a Liferay.Popup.
Posted on 1/25/10 11:58 AM in reply to Alex Wallace.
i did liferay popup and how do i can localize it content.
thanks.
Posted on 5/9/10 11:42 PM.
can anyone tell me how can i use the code???
can i get the source code which i can copy into view.jsp. do need to create another jsp portlet ,whose content will be displayed on the popup??? if yes then how can show the portlet in popup and how to use the above code ie Floating Div Pop-Up .
Posted on 8/27/10 4:22 AM in reply to delang j.
Hi Ankit,

You can create a jsp which will display your details. No need to create a portlet.
1) Create a jsp.
2) Configure struts-config.xml and tiles-defs.xml for rendering the jsp.
3) Create a renderURL for calling the JSP inside your popup.
4) Paste the javascript function in your current jsp. Replace the 'url' with your renderURL.
Posted on 9/6/10 8:38 AM in reply to ankit yakkundi.
hi,

i try using Liferay.popup and Expanse.popup but no of this not work.
i'm using Liferay 6.0.5
can you help me please?
Posted on 1/12/11 10:04 AM in reply to Faris Abdulla.
hi,
In Liferay-trunk at revision 33901 "Expanse" is renamed to "Alloy" (LPS-3987).
So to use the "Popup"-function in trunk you must change "new Expanse.Popup" to "new Alloy.Popup()" (as commented by Oliver Bayer)
Posted on 1/18/11 9:14 PM in reply to Brahim TARNAOUI.
Hi,
Can you tell me how can I display a pop-up with dynamic data on it? I am using Liferay 6.0.4 with JSF 2.0(without ice-faces) and glassfish server v2.1.1
Posted on 1/18/11 9:16 PM in reply to Alex Wallace.
Posted on 9/3/12 4:15 AM in reply to Madhura Raut.
Hi, how could I show a jsp page in popup? I have a jsp in my portlet for example test.jsp and I want to show it in popup. please guide me. thanks a lot!
Posted on 9/29/12 6:32 AM in reply to Bozhidar Dedov.
Hi Alex I am having the same issue

I understand how you suppressed the theme to only show the portlet using http params

but how did you build the liferay url? Here is what I am doing currently:
LiferayPortletURL url = PortletURLFactoryUtil.create(httpRequest, portletId, themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);

url.setWindowState(LiferayWindowState.POP_UP);
url.s­etPortletMode(PortletMode.VIEW);

Is this what you used?

Thank you
Posted on 2/26/13 6:01 AM in reply to Alex Wallace.
Here it is what I've done to render a jsp page in the popup (called delete.jsp):

 1
 2 <portlet:renderURL var="deleteURL"
 3    windowState= "<%= LiferayWindowState.POP_UP.toString() %>">
 4<portlet:param name="jspPage" value="/jsps/data/delete.jsp" />
 5       
 6    </portlet:renderURL>
 7   
 8    <liferay-ui:icon image="delete" message="file-entry-delete"
 9                     url="javascript:alertDelete();" />
10   
11    <aui:script use="aui-dialog,aui-overlay-manager">
12       Liferay.provide(
13        window,
14        'alertDelete',
15        function() {
16            var instance = this;
17            var url='${deleteURL}';
18
19                Liferay.Util.openWindow(
20                    {
21                        cache: false,
22                        dialog: {
23                            align: Liferay.Util.Window.ALIGN_CENTER,
24                            after: {
25                                render: function(event) {
26                                    this.set('y', this.get('y') + 50);
27                                }
28                            },
29                            width: 820
30                        },
31                        dialogIframe: {
32                            id: 'deleteIFrame',
33                            uri: url
34                        },
35                        title: Liferay.Language.get('cloud'),
36                        uri: url
37                    }
38                );
39        },
40        ['liferay-util-window']
41    );
42
43    </aui:script>


What I wanted to ask is: how I can make the windows to close? In the first example the author mentioned window.close()
What about the second example? You open with Liferay.Util.openWindow ... Is there anything like Liferay.Util.closeWindow to create a button to close? Any help is appreciated. Thank you.
Posted on 8/7/13 7:09 AM.
Hi newbie here how am i supposed to run Floating Div Pop-up in 6.1 assuming that i have a button to click
Posted on 12/22/13 6:12 PM.
Hi,

in the jsp page where the button is located.

Regards,

Riccardo
Posted on 2/4/14 1:50 AM in reply to Edgar Trania.
I know this thread is quite old but maybe this will be usefull for others...

I've added, in the opener page:

<aui:script>
Liferay.provide(
window,
'closeBookmarkPopup',
function() {

var A = AUI();

A.DialogManager.closeByChild('#popUpID');
},
['aui-base','aui-dialog','aui-d­ialog-iframe']
);
</aui:script>


where popupid is:
Liferay.Util.openWindow(
{
cache: false,
id: 'popUpID',
dialog: {
...

better late than never emoticon
Posted on 11/8/14 12:29 AM in reply to Riccardo Rotondo.