Wiki source code of Confirmation Box UI Component
Version 13.1 by Oana Florea on 2010/03/18
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{toc start=2 /}} | ||
2 | |||
3 | The Confirmation Box UI is an alternative to the old-fashioned browsers confirm boxes. It displays a nice popup box asking the user to confirm an action. Functions hooks are available on both confirm and cancel actions. | ||
4 | |||
5 | //Image of the confirmation box in action : // | ||
6 | image:confirmbox.png | ||
7 | |||
8 | {{html}} | ||
9 | <a href="#" onclick="javascript:new XWiki.widgets.ConfirmationBox();"> | ||
10 | Or click this link to see one live! | ||
11 | </a> | ||
12 | {{/html}} | ||
13 | |||
14 | == Usage == | ||
15 | |||
16 | {{code language="javascript"}} | ||
17 | new XWiki.widgets.ConfirmationBox(behavior, interactionParameters); | ||
18 | {{/code}} | ||
19 | |||
20 | ; behavior | ||
21 | : Object that define confirm and cancel handlers. When the user chooses "Yes", its "onYes" method is triggered if it exists. When the user chooses "No", its "onNo" method is triggered, if it exists. The behavior parameter is not mandatory and is empty by default (It means nothing will happen upon confirmation or cancel). | ||
22 | |||
23 | Example : | ||
24 | |||
25 | {{code language="javascript"}} | ||
26 | var myBehavior = { | ||
27 | onYes: function() { | ||
28 | alert("Yes !"); | ||
29 | }, | ||
30 | onNo: function() { | ||
31 | alert("Oh no :("); | ||
32 | } | ||
33 | }; | ||
34 | {{/code}} | ||
35 | |||
36 | ; interactionParameters | ||
37 | : Object that defines the different text elements displayed with-in the confirmation box. Three variables are available for customization : **confirmationText**, the message to confirm (default value for the English language is //Are you sure ?//), **yesButtonText** the "yes" button text (or confirm button), **noButtonText** the "no" button text (or cancel button). | ||
38 | |||
39 | Example : | ||
40 | |||
41 | {{code language="javascript"}} | ||
42 | var myInteractionParameters = { | ||
43 | confirmationText: "Are you sure you want to engage the wiki's self-destruction process ?", | ||
44 | yesButtonText: "Yes, please do that", | ||
45 | noButtonText: "No thank you" | ||
46 | }; | ||
47 | {{/code}} | ||
48 | |||
49 | == Source Code (for tag xwiki-web-2.0) == | ||
50 | |||
51 | {{code language="javascript"}} | ||
52 | // Make sure the XWiki 'namespace' and the ModalPopup class exist. | ||
53 | if(typeof(XWiki) == "undefined" || typeof(XWiki.widgets) == "undefined" || typeof(XWiki.widgets.ModalPopup) == "undefined") { | ||
54 | if (typeof console != "undefined" && typeof console.warn == "function") { | ||
55 | console.warn("[MessageBox widget] Required class missing: XWiki.widgets.ModalPopup"); | ||
56 | } | ||
57 | } else { | ||
58 | |||
59 | XWiki.widgets.ConfirmationBox = Class.create(XWiki.widgets.ModalPopup, { | ||
60 | /** Default displayed texts */ | ||
61 | defaultInteractionParameters : { | ||
62 | confirmationText: "$msg.get('core.widgets.confirmationBox.defaultQuestion')", | ||
63 | yesButtonText: "$msg.get('core.widgets.confirmationBox.button.yes')", | ||
64 | noButtonText: "$msg.get('core.widgets.confirmationBox.button.no')" | ||
65 | }, | ||
66 | /** Constructor. Registers the key listener that pops up the dialog. */ | ||
67 | initialize : function($super, behavior, interactionParameters) { | ||
68 | this.interactionParameters = Object.extend(Object.clone(this.defaultInteractionParameters), interactionParameters || {}); | ||
69 | $super( | ||
70 | this.createContent(this.interactionParameters), | ||
71 | { | ||
72 | "show" : { method : this.showDialog, keys : [] }, | ||
73 | "yes" : { method : this.onYes, keys : ['Enter', 'Space'] }, | ||
74 | "no" : { method : this.onNo, keys : ['Esc'] }, | ||
75 | "close" : { method : this.closeDialog, keys : [] } | ||
76 | }, | ||
77 | { | ||
78 | displayCloseButton : false | ||
79 | } | ||
80 | ); | ||
81 | this.showDialog(); | ||
82 | this.setClass("confirmation"); | ||
83 | this.behavior = behavior || { }; | ||
84 | }, | ||
85 | /** Create the content of the confirmation dialog: icon + question text, buttons */ | ||
86 | createContent : function (data) { | ||
87 | var question = new Element("div", {"class" : "question"}).update(data.confirmationText); | ||
88 | var buttons = new Element("div", {"class" : "buttons"}); | ||
89 | var yesButton = this.createButton("button", data.yesButtonText, "(Enter)", ""); | ||
90 | var noButton = this.createButton("button", data.noButtonText, "(Esc)", ""); | ||
91 | buttons.insert(yesButton); | ||
92 | buttons.insert(noButton); | ||
93 | var content = new Element("div"); | ||
94 | content.insert(question).insert(buttons); | ||
95 | Event.observe(yesButton, "click", this.onYes.bindAsEventListener(this)); | ||
96 | Event.observe(noButton, "click", this.onNo.bindAsEventListener(this)); | ||
97 | return content; | ||
98 | }, | ||
99 | onYes : function() { | ||
100 | this.closeDialog(); | ||
101 | if (typeof (this.behavior.onYes) == 'function') { | ||
102 | this.behavior.onYes(); | ||
103 | } | ||
104 | }, | ||
105 | onNo : function() { | ||
106 | this.closeDialog(); | ||
107 | if (typeof (this.behavior.onNo) == 'function') { | ||
108 | this.behavior.onNo(); | ||
109 | } | ||
110 | } | ||
111 | }); | ||
112 | } // if the parent widget is defined | ||
113 | {{/code}} | ||
114 | |||
115 | ==Tips== | ||
116 | |||
117 | Check out the Javascript code: | ||
118 | * for your wiki instance: **{{{http://localhost:8080xwiki/bin/skin/resources/uicomponents/widgets/confirmationBox.js}}}** | ||
119 | * from svn (for tag xwiki-web-2.0): [[http://svn.xwiki.org/svnroot/xwiki/platform/web/tags/xwiki-web-2.0/standard/src/main/webapp/resources/uicomponents/widgets/confirmationBox.js]] |