mozdev.org

JSLib
Page Updated: August 14, 2001
Latest Version: 0.1.375
Debug | Install | File I/O | RDF | Network | Sound | Utils | XUL | Zip
Home | Mailing List | Install | Source Code | Downloads | Bugs | docs | clients

CommonDialog

Files:
commonDialog.js

Description:
  This is a class to make it easier to produce simple dialogs.  One of the things that I found about creating an app in mozilla, is that you have to create every dialog (with a couple of exceptions).  So this is an attempt at reusing some common code.

  You can display five types of dialogs:  Message, Alert, Error, Question and None.  The only difference between them is the icon that is displayed on the upper left.  The "none" just doesn't display an icon.  The message is displayed in the upper right, and the buttons are on the bottom.  You can customize how many buttons there are, and what the labels are, or you can select a "prebuilt" button type.  You can also align the buttons to the left, center or right.

Lets take a look at an example:

  var dialog = new CommonDialog(JS_CD_ERROR, JS_CD_YESNO, JS_CD_RIGHT);
  dialog.title = "Start over?";
  dialog.message = "An error has occured processing your entry.  Would you like to start over?";
  dialog.show();
  var rv = dialog.result;
  if(rv.text == "yes") {
    // lets start over...
  }
This code produces the following dialog:


 

Let's say you wanted to do something a little more customized:
 

  var dialog = new CommonDialog(JS_CD_MESSAGE);
  dialog.buttonAlign = JS_CD_RIGHT;
  dialog.title = "Which one?";
  dialog.message = "What is your favorite color?";
  dialog.appendButton("red", true);
  dialog.appendButton("blue");
  dialog.show();
  var rv = dialog.result;
  if(rv.text == "red") {
    // red?...
  }
This code produces the following dialog:


 
 
 

Function List

    CommonDialog(aType, aButtonType, aButtonAlign)
Constructor.
aType A dialog type:  JS_CD_MESSAGE, JS_CD_ALERT, JS_CD_ERROR or JS_CD_QUESTION
aButtonType A "pre-built" set of buttons:  JS_CD_YESNO, JS_CD_YES, JS_CD_NO, JS_CD_OKCANCEL, JS_CD_OK, JS_CD_CANCEL
aButtonAlign An alignment for the buttons: JS_CD_LEFT, JS_CD_CENTER, JS_CD_RIGHT
   (get) type
Returns the "type" of the dialog:  JS_CD_MESSAGE, JS_CD_ALERT, JS_CD_ERROR or JS_CD_QUESTION.  This can be "undefined" if not passed into the constructor.
   (set) type(aType)
Sets the "type" of the dialog:  JS_CD_MESSAGE, JS_CD_ALERT, JS_CD_ERROR or JS_CD_QUESTION.
   buttonType(aType)
Sets the buttons to one of the "pre-built" buttons:  JS_CD_MESSAGE, JS_CD_ALERT, JS_CD_ERROR or JS_CD_QUESTION.
   (get) defaultButton
Returns the number coresponding to the default button.  Buttons are numbered from left to right starting with zero.
   (set) defaultButton(aType)
Sets the number coresponding to the default button.  Buttons are numbered from left to right starting with zero.
   (get) message
Returns the current message.
   (set) message(aMessage)
Sets the current message.
   (get) buttonAlign
Returns the current alignment:  JS_CD_LEFT, JS_CD_CENTER, JS_CD_RIGHT.
   (set) buttonAlign(aMessage)
Sets the current alignment:  JS_CD_LEFT, JS_CD_CENTER, JS_CD_RIGHT.
   appendButton(aName, aDefault)
Creates a button and appends it to the right side of the existing buttons.  The first argument is the label for the button, and the second is optional and sets it to the default button.
   (get) title
Returns the current dialog title.
   (set) title(aMessage)
Sets the current dialog title.
    show()
Creates the dialog and waits for a button push.
   (get) result
Returns an object with two members:  object.id and object.text.  "id" is the number ot the button that was pushed and "text" was the buttons label.

 
 

The jslib project can be contacted through the mailing list or the member list.
Copyright © 2000-2017. All rights reserved. Terms of Use & Privacy Policy.