Wiki source code of Modal Popup
Last modified by Simon Urli on 2023/10/10
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
1.1 | 1 | {{warning}} |
2 | This tutorial is a work in progress. | ||
3 | {{/warning}} | ||
![]() |
5.2 | 4 | |
![]() |
1.1 | 5 | {{toc/}} |
![]() |
5.2 | 6 | |
![]() |
1.1 | 7 | {{info}} |
8 | This is a Javascript widget bundled by default with the XWiki platform. | ||
9 | {{/info}} | ||
![]() |
5.2 | 10 | |
![]() |
8.1 | 11 | = Usage = |
![]() |
1.1 | 12 | |
![]() |
9.2 | 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. |
![]() |
1.1 | 14 | |
![]() |
8.1 | 15 | = Constructor fields for the **ModalPopup** Javascript class = |
16 | |||
![]() |
1.1 | 17 | ; content |
18 | : Object that defines the content of the modal dialog. | ||
19 | ; shortcuts | ||
![]() |
9.2 | 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. |
![]() |
1.1 | 21 | ; options |
![]() |
6.1 | 22 | : Object that defines the options for the modal dialog: //title//, //displayCloseButton//, //extraClassName//, //screenColor//, //borderColor//, //titleColor//, //backgroundColor//, //screenOpacity//, //verticalPosition// and //horizontalPosition//. |
![]() |
1.1 | 23 | |
![]() |
8.1 | 24 | = Examples = |
![]() |
1.1 | 25 | |
![]() |
2.1 | 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 | |||
![]() |
9.2 | 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: |
![]() |
7.2 | 81 | |
![]() |
2.1 | 82 | {{code}} |
![]() |
3.1 | 83 | #set($pageURL="$xwiki.getURL('DevGuide.AutoSuggestWidgetExample','view','xpage=plain')") |
![]() |
2.1 | 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, | ||
![]() |
6.1 | 112 | extraClassName : 'mydialog-box', |
![]() |
2.1 | 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); | ||
![]() |
7.1 | 130 | }.bind(this), |
![]() |
2.1 | 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"}} | ||
![]() |
3.1 | 144 | #set($pageURL="$xwiki.getURL('DevGuide.AutoSuggestWidgetExample','view','xpage=plain')") |
![]() |
4.1 | 145 | Check out the example above live: <a href="#" onclick="new XWiki.widgets.MyModalPopup({pageURL: '$pageURL'});">My Modal Popup</a>. |
![]() |
2.1 | 146 | {{/html}} |
147 | {{/velocity}} | ||
148 | |||
![]() |
1.1 | 149 | Widgets bundled by default with XWiki build on top of the Model Popup class: |
![]() |
7.2 | 150 | |
![]() |
9.2 | 151 | * [[Confirmation Box>>xwiki:Documentation.DevGuide.FrontendResources.ConfirmationBox]] widget |
![]() |
7.2 | 152 | * {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/js/xwiki/widgets/jumpToPage.js"}}Jump to page{{/scm}} widget |
![]() |
5.3 | 153 | * {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/uicomponents/widgets/confirmedAjaxRequest.js"}}Confirmed Ajax Request Box{{/scm}} |
![]() |
1.1 | 154 | |
![]() |
8.1 | 155 | = Tips = |
![]() |
1.1 | 156 | |
157 | Check out the Javascript code: | ||
![]() |
8.1 | 158 | |
![]() |
1.1 | 159 | * for your wiki instance: **{{{http://localhost:8080/xwiki/bin/skin/resources/js/xwiki/widgets/modalPopup.js}}}** |
![]() |
5.3 | 160 | * from GitHub: {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/js/xwiki/widgets/modalPopup.js"/}}. |
![]() |
1.1 | 161 | |
162 | Check out the CSS code: | ||
![]() |
8.1 | 163 | |
![]() |
5.3 | 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"/}}. |