Confirmation Box UI Component

Version 17.2 by Vincent Massol on 2017/09/01

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:
confirmbox.png

Or click this link to see one live!

Usage

new XWiki.widgets.ConfirmationBox(behavior, interactionParameters);
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. When the user chooses "Cancel", if the cancel button is enabled, its "onCancel" 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:

var myBehavior = {
   onYes: function() {
      alert("Yes!");
   },
   onNo: function() {
      alert("Oh no :(");
   }
};
interactionParameters

Object that defines the different text elements displayed within the confirmation box. Five 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, cancelButtonText - the "cancel" button text, if enabled, and showCancelButton - whether to show the cancel button, disabled by default.

Example :

var myInteractionParameters = {
  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

// Make sure the XWiki 'namespace' and the ModalPopup class exist.
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: "$services.localization.render('core.widgets.confirmationBox.defaultQuestion')",
   yesButtonText: "$services.localization.render('core.widgets.confirmationBox.button.yes')",
   noButtonText: "$services.localization.render('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:

Tags:
   

Get Connected