Confirmation Box UI Component
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.
Image of the confirmation box in action:
Or click this link to see one live!
Usage
- behavior
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).
Example:
onYes: function() {
alert("Yes!");
},
onNo: function() {
alert("Oh no :(");
}
};
- interactionParameters
Object that defines the different text elements displayed within 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).
Example :
confirmationText: "Are you sure you want to engage the wiki's self-destruction process?",
yesButtonText: "Yes, please do that",
noButtonText: "No thank you"
};
Source Code
if(typeof(XWiki) == "undefined" || typeof(XWiki.widgets) == "undefined" || typeof(XWiki.widgets.ModalPopup) == "undefined") {
if (typeof console != "undefined" && typeof console.warn == "function") {
console.warn("[MessageBox widget] Required class missing: XWiki.widgets.ModalPopup");
}
} else {
XWiki.widgets.ConfirmationBox = Class.create(XWiki.widgets.ModalPopup, {
/** Default displayed texts */
defaultInteractionParameters : {
confirmationText: "$msg.get('core.widgets.confirmationBox.defaultQuestion')",
yesButtonText: "$msg.get('core.widgets.confirmationBox.button.yes')",
noButtonText: "$msg.get('core.widgets.confirmationBox.button.no')"
},
/** Constructor. Registers the key listener that pops up the dialog. */
initialize : function($super, behavior, interactionParameters) {
this.interactionParameters = Object.extend(Object.clone(this.defaultInteractionParameters), interactionParameters || {});
$super(
this.createContent(this.interactionParameters),
{
"show" : { method : this.showDialog, keys : [] },
"yes" : { method : this.onYes, keys : ['Enter', 'Space'] },
"no" : { method : this.onNo, keys : ['Esc'] },
"close" : { method : this.closeDialog, keys : [] }
},
{
displayCloseButton : false,
removeOnClose : true
}
);
this.showDialog();
this.setClass("confirmation");
this.behavior = behavior || { };
},
/** Create the content of the confirmation dialog: icon + question text, buttons */
createContent : function (data) {
var question = new Element("div", {"class" : "question"}).update(data.confirmationText);
var buttons = new Element("div", {"class" : "buttons"});
var yesButton = this.createButton("button", data.yesButtonText, "(Enter)", "");
var noButton = this.createButton("button", data.noButtonText, "(Esc)", "");
buttons.insert(yesButton);
buttons.insert(noButton);
var content = new Element("div");
content.insert(question).insert(buttons);
Event.observe(yesButton, "click", this.onYes.bindAsEventListener(this));
Event.observe(noButton, "click", this.onNo.bindAsEventListener(this));
return content;
},
onYes : function() {
this.closeDialog();
if (typeof (this.behavior.onYes) == 'function') {
this.behavior.onYes();
}
},
onNo : function() {
this.closeDialog();
if (typeof (this.behavior.onNo) == 'function') {
this.behavior.onNo();
}
}
});
} // if the parent widget is defined
Tips
Check out the Javascript code: