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 附件
172347 查看
平均 (3 票)
满分为 5,平均得分为 3.3333333333333335。
评论
讨论主题回复 作者 日期
In Liferay-trunk at revision 33901 "Expanse" is... Oliver Bayer 2009年7月23日 上午5:45
Looks like it was renamed again and in my case... Alexey Kakunin 2009年9月19日 上午7:12
Sorry - Liferay.Popup - rest of attributes... Alexey Kakunin 2009年9月19日 上午7:12
This mechanism doesn't work with IceFaces... Alex Wallace 2010年1月25日 上午10:56
After playing with it, loading via ajax is an... Alex Wallace 2010年1月25日 上午11:58
Hi, Can you tell me how can I display a pop-up... Madhura Raut 2011年1月18日 下午9:16
hi Bozhidar Dedov 2012年9月3日 上午4:15
Hi, how could I show a jsp page in popup? I... Mahdi Lashkari 2012年9月29日 上午6:32
Hi Alex I am having the same issue I... dominick campbell 2013年2月26日 上午6:01
i did liferay popup and how do i can localize... delang j 2010年5月9日 下午11:42
can anyone tell me how can i use the code???... ankit yakkundi 2010年8月27日 上午4:22
Hi Ankit, You can create a jsp which will... Faris Abdulla 2010年9月6日 上午8:38
hi, i try using Liferay.popup and... Brahim TARNAOUI 2011年1月12日 上午10:04
hi, In Liferay-trunk at revision 33901... Madhura Raut 2011年1月18日 下午9:14
Here it is what I've done to render a jsp page... Riccardo Rotondo 2013年8月7日 上午7:09
I know this thread is quite old but maybe this... Luca Bacco 2014年11月8日 上午12:29
Hi newbie here how am i supposed to run... Edgar Trania 2013年12月22日 下午6:12
Hi, in the jsp page where the button is... Riccardo Rotondo 2014年2月4日 上午1:50

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()"
在 09-7-23 上午5:45 发帖。
Looks like it was renamed again and in my case (Liferay 5.2.3) I had to use Liferay.POPUP to make it working
在 09-9-19 上午7:12 发帖以回复 Oliver Bayer
Sorry - Liferay.Popup - rest of attributes looks like stay same
在 09-9-19 上午7:12 发帖以回复 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!
在 10-1-25 上午10:56 发帖。
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.
在 10-1-25 上午11:58 发帖以回复 Alex Wallace
i did liferay popup and how do i can localize it content.
thanks.
在 10-5-9 下午11:42 发帖。
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 .
在 10-8-27 上午4:22 发帖以回复 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.
在 10-9-6 上午8:38 发帖以回复 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?
在 11-1-12 上午10:04 发帖以回复 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)
在 11-1-18 下午9:14 发帖以回复 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
在 11-1-18 下午9:16 发帖以回复 Alex Wallace
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!
在 12-9-29 上午6:32 发帖以回复 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
在 13-2-26 上午6:01 发帖以回复 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.
在 13-8-7 上午7:09 发帖。
Hi newbie here how am i supposed to run Floating Div Pop-up in 6.1 assuming that i have a button to click
在 13-12-22 下午6:12 发帖。
Hi,

in the jsp page where the button is located.

Regards,

Riccardo
在 14-2-4 上午1:50 发帖以回复 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
在 14-11-8 上午12:29 发帖以回复 Riccardo Rotondo