Wiki source code of Modal Popup UI Component

Version 6.1 by Sergiu Dumitriu on 2012/07/10

Show last authors
1
2 {{warning}}
3 This tutorial is a work in progress.
4 {{/warning}}
5
6 {{toc/}}
7
8 {{info}}
9 This is a Javascript widget bundled by default with the XWiki platform.
10 {{/info}}
11
12 =Usage=
13
14 The Modal Popup is a widget used as a base class for different modal widgets in XWiki, like the [[Confirmation Box>>DevGuide.ConfirmationBox]] or the Jump To Page widget. It will not display a dialog box since it should not be used by itself.
15
16 =Constructor fields for the **ModalPopup** Javascript class=
17 ; content
18 : Object that defines the content of the modal dialog.
19 ; shortcuts
20 : Object that defines the shortcuts that will pop up the dialog.
21 ; options
22 : Object that defines the options for the modal dialog: //title//, //displayCloseButton//, //extraClassName//, //screenColor//, //borderColor//, //titleColor//, //backgroundColor//, //screenOpacity//, //verticalPosition// and //horizontalPosition//.
23
24 =Examples=
25
26 The Modal Popup Javascript class could be used as a base class for a widget that loads the content of another page:
27
28 {{code language="javascript"}}
29 // Make sure the XWiki 'namespace' and the ModalPopup class exist.
30 if(typeof(XWiki) == "undefined" || typeof(XWiki.widgets) == "undefined" || typeof(XWiki.widgets.ModalPopup) == "undefined") {
31 if (typeof console != "undefined" && typeof console.warn == "function") {
32 console.warn("[MessageBox widget] Required class missing: XWiki.widgets.ModalPopup");
33 }
34 } else {
35
36 XWiki.widgets.MyModalPopup = Class.create(XWiki.widgets.ModalPopup, {
37 /** Default parameters can be added to the custom class. */
38 defaultInteractionParameters : {
39 },
40 /** Constructor. Registers the key listener that pops up the dialog. */
41 initialize : function($super, interactionParameters) {
42 this.interactionParameters = Object.extend(Object.clone(this.defaultInteractionParameters), interactionParameters || {});
43 // call constructor from ModalPopup with params content, shortcuts, options
44 $super(
45 this.createContent(this.interactionParameters),
46 {
47 "show" : { method : this.showDialog, keys : [] },
48 "close" : { method : this.closeDialog, keys : ['Esc'] }
49 },
50 {
51 displayCloseButton : true,
52 verticalPosition : "top",
53 backgroundColor : "#FFF"
54 }
55 );
56 this.showDialog();
57 this.setClass("my-modal-popup");
58 },
59 /** Get the content of the modal dialog using ajax */
60 createContent : function (data) {
61 var content = new Element('div', {'class': 'modal-popup'});
62 // get page content for the pageURL
63 new Ajax.Request(data.pageURL,
64 {
65 method:'get',
66 onSuccess: function(transport){
67 var response = transport.responseText || "no response text";
68 content.insert(response);
69 },
70 onFailure: function(){ content.insert('Something went wrong...');
71 }
72 });
73
74 return content;
75 }
76 });
77 } // if the parent widget is defined
78 {{/code}}
79
80 Define the URL of the page to be loaded (in this case the [[DevGuide.AutoSuggestWidgetExample]]) and just call the new widget from the wiki page:
81 {{code}}
82 #set($pageURL="$xwiki.getURL('DevGuide.AutoSuggestWidgetExample','view','xpage=plain')")
83 <a href="#" onclick="new XWiki.widgets.MyModalPopup({pageURL: '$pageURL'});">
84 {{/code}}
85
86 {{html wiki="false"}}
87 <script type="text/javascript" language="javascript">
88 // Make sure the XWiki 'namespace' and the ModalPopup class exist.
89 if(typeof(XWiki) == "undefined" || typeof(XWiki.widgets) == "undefined" || typeof(XWiki.widgets.ModalPopup) == "undefined") {
90 if (typeof console != "undefined" && typeof console.warn == "function") {
91 console.warn("[MessageBox widget] Required class missing: XWiki.widgets.ModalPopup");
92 }
93 } else {
94
95 XWiki.widgets.MyModalPopup = Class.create(XWiki.widgets.ModalPopup, {
96 /** Default parameters can be added to the custom class. */
97 defaultInteractionParameters : {
98 },
99 /** Constructor. Registers the key listener that pops up the dialog. */
100 initialize : function($super, interactionParameters) {
101 this.interactionParameters = Object.extend(Object.clone(this.defaultInteractionParameters), interactionParameters || {});
102 // call constructor from ModalPopup with params content, shortcuts, options
103 $super(
104 this.createContent(this.interactionParameters),
105 {
106 "show" : { method : this.showDialog, keys : [] },
107 "close" : { method : this.closeDialog, keys : ['Esc'] }
108 },
109 {
110 displayCloseButton : true,
111 extraClassName : 'mydialog-box',
112 verticalPosition : "top",
113 backgroundColor : "#FFF"
114 }
115 );
116 this.showDialog();
117 this.setClass("my-modal-popup");
118 },
119 /** Get the content of the modal dialog using ajax */
120 createContent : function (data) {
121 var content = new Element('div', {'class': 'modal-popup'});
122 // get page content for the pageURL
123 new Ajax.Request(data.pageURL,
124 {
125 method:'get',
126 onSuccess: function(transport){
127 var response = transport.responseText || "no response text";
128 content.insert(response);
129 },
130 onFailure: function(){ content.insert('Something went wrong...');
131 }
132 });
133
134 return content;
135 }
136 });
137 } // if the parent widget is defined
138 </script>
139 {{/html}}
140
141 {{velocity}}
142 {{html wiki="false"}}
143 #set($pageURL="$xwiki.getURL('DevGuide.AutoSuggestWidgetExample','view','xpage=plain')")
144 Check out the example above live: <a href="#" onclick="new XWiki.widgets.MyModalPopup({pageURL: '$pageURL'});">My Modal Popup</a>.
145 {{/html}}
146 {{/velocity}}
147
148 Widgets bundled by default with XWiki build on top of the Model Popup class:
149 * [[Confirmation Box>>DevGuide.ConfirmationBox]] widget
150 * {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/js/xwiki/widgets/jumpToPage.js"}}Jump to page widget{{/scm}}
151 * {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/uicomponents/widgets/confirmedAjaxRequest.js"}}Confirmed Ajax Request Box{{/scm}}
152
153 =Tips=
154
155 Check out the Javascript code:
156 * for your wiki instance: **{{{http://localhost:8080/xwiki/bin/skin/resources/js/xwiki/widgets/modalPopup.js}}}**
157 * from GitHub: {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/js/xwiki/widgets/modalPopup.js"/}}.
158
159 Check out the CSS code:
160 * for your wiki instance: **{{{http://localhost:8080/xwiki/bin/skin/resources/js/xwiki/widgets/modalPopup.css}}}**
161 * from GitHub: {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/js/xwiki/widgets/modalPopup.css"/}}.

Get Connected