Wiki source code of Modal Popup
Last modified by Simon Urli on 2023/10/10
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{warning}} | ||
2 | This tutorial is a work in progress. | ||
3 | {{/warning}} | ||
4 | |||
5 | {{toc/}} | ||
6 | |||
7 | {{info}} | ||
8 | This is a Javascript widget bundled by default with the XWiki platform. | ||
9 | {{/info}} | ||
10 | |||
11 | = Usage = | ||
12 | |||
13 | The Modal Popup is a widget used as a base class for different modal widgets in XWiki, like the [[Confirmation Box>>xwiki:Documentation.DevGuide.FrontendResources.ConfirmationBox]] or the Jump To Page widget. It will not display a dialog box since it should not be used by itself. | ||
14 | |||
15 | = Constructor fields for the **ModalPopup** Javascript class = | ||
16 | |||
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. It is a map of the form { "action" : actionObject, etc. } , where actionObject has a //method// to execute, a //keys// list of [[keyboard shortcut strings>>xwiki:Documentation.UserGuide.Features.KeyboardShortcuts||anchor="HAddingyourownshortcuts"]] and a common map of [[keyboard shortcut>>http://www.openjs.com/scripts/events/keyboard_shortcuts/#add_docs]] //options// to be used for all the defined shortcuts. | ||
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 [[xwiki:Documentation.DevGuide.FrontendResources.AutoSuggestWidget]]) and just call the new widget from the wiki page: | ||
81 | |||
82 | {{code}} | ||
83 | #set($pageURL="$xwiki.getURL('DevGuide.AutoSuggestWidgetExample','view','xpage=plain')") | ||
84 | <a href="#" onclick="new XWiki.widgets.MyModalPopup({pageURL: '$pageURL'});"> | ||
85 | {{/code}} | ||
86 | |||
87 | {{html wiki="false"}} | ||
88 | <script type="text/javascript" language="javascript"> | ||
89 | // Make sure the XWiki 'namespace' and the ModalPopup class exist. | ||
90 | if(typeof(XWiki) == "undefined" || typeof(XWiki.widgets) == "undefined" || typeof(XWiki.widgets.ModalPopup) == "undefined") { | ||
91 | if (typeof console != "undefined" && typeof console.warn == "function") { | ||
92 | console.warn("[MessageBox widget] Required class missing: XWiki.widgets.ModalPopup"); | ||
93 | } | ||
94 | } else { | ||
95 | |||
96 | XWiki.widgets.MyModalPopup = Class.create(XWiki.widgets.ModalPopup, { | ||
97 | /** Default parameters can be added to the custom class. */ | ||
98 | defaultInteractionParameters : { | ||
99 | }, | ||
100 | /** Constructor. Registers the key listener that pops up the dialog. */ | ||
101 | initialize : function($super, interactionParameters) { | ||
102 | this.interactionParameters = Object.extend(Object.clone(this.defaultInteractionParameters), interactionParameters || {}); | ||
103 | // call constructor from ModalPopup with params content, shortcuts, options | ||
104 | $super( | ||
105 | this.createContent(this.interactionParameters), | ||
106 | { | ||
107 | "show" : { method : this.showDialog, keys : [] }, | ||
108 | "close" : { method : this.closeDialog, keys : ['Esc'] } | ||
109 | }, | ||
110 | { | ||
111 | displayCloseButton : true, | ||
112 | extraClassName : 'mydialog-box', | ||
113 | verticalPosition : "top", | ||
114 | backgroundColor : "#FFF" | ||
115 | } | ||
116 | ); | ||
117 | this.showDialog(); | ||
118 | this.setClass("my-modal-popup"); | ||
119 | }, | ||
120 | /** Get the content of the modal dialog using ajax */ | ||
121 | createContent : function (data) { | ||
122 | var content = new Element('div', {'class': 'modal-popup'}); | ||
123 | // get page content for the pageURL | ||
124 | new Ajax.Request(data.pageURL, | ||
125 | { | ||
126 | method:'get', | ||
127 | onSuccess: function(transport){ | ||
128 | var response = transport.responseText || "no response text"; | ||
129 | content.insert(response); | ||
130 | }.bind(this), | ||
131 | onFailure: function(){ content.insert('Something went wrong...'); | ||
132 | } | ||
133 | }); | ||
134 | |||
135 | return content; | ||
136 | } | ||
137 | }); | ||
138 | } // if the parent widget is defined | ||
139 | </script> | ||
140 | {{/html}} | ||
141 | |||
142 | {{velocity}} | ||
143 | {{html wiki="false"}} | ||
144 | #set($pageURL="$xwiki.getURL('DevGuide.AutoSuggestWidgetExample','view','xpage=plain')") | ||
145 | Check out the example above live: <a href="#" onclick="new XWiki.widgets.MyModalPopup({pageURL: '$pageURL'});">My Modal Popup</a>. | ||
146 | {{/html}} | ||
147 | {{/velocity}} | ||
148 | |||
149 | Widgets bundled by default with XWiki build on top of the Model Popup class: | ||
150 | |||
151 | * [[Confirmation Box>>xwiki:Documentation.DevGuide.FrontendResources.ConfirmationBox]] widget | ||
152 | * {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/js/xwiki/widgets/jumpToPage.js"}}Jump to page{{/scm}} widget | ||
153 | * {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/uicomponents/widgets/confirmedAjaxRequest.js"}}Confirmed Ajax Request Box{{/scm}} | ||
154 | |||
155 | = Tips = | ||
156 | |||
157 | Check out the Javascript code: | ||
158 | |||
159 | * for your wiki instance: **{{{http://localhost:8080/xwiki/bin/skin/resources/js/xwiki/widgets/modalPopup.js}}}** | ||
160 | * from GitHub: {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/js/xwiki/widgets/modalPopup.js"/}}. | ||
161 | |||
162 | Check out the CSS code: | ||
163 | |||
164 | * for your wiki instance: **{{{http://localhost:8080/xwiki/bin/skin/resources/js/xwiki/widgets/modalPopup.css}}}** | ||
165 | * from GitHub: {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/js/xwiki/widgets/modalPopup.css"/}}. |