nikku / jquery-bootstrap-scripting

A number of jQuery plugins to ease scripting of bootstrap featured pages.
http://nikku.github.com/jquery-bootstrap-scripting
278 stars 49 forks source link

Contains some nice jQuery plugins to make working with the facilities provided by the bootstrap CSS framework more fun.

Dialog Two, a jQuery dialog plugin based on the bootstrap modal dialog

jquery.dialog2.js uses the modal dialog provided by bootstrap and provides a controlable dialog functionality for web pages as a jQuery plugin.

Features

Dependencies

Migrating from earlier versions

Controlling a dialog with JavaScript

Options

The dialog2() method accepts an options object:

{
  id: "my-dialog", // id which (if specified) will be added to the dialog to make it accessible later 
  autoOpen: true | false, // Should the dialog be automatically opened?
  title: "Title of the dialog", 
  buttons: {
    button1Label: callback | object, 
    button2Label: callback, 
    ...
  }, 
  closeOnOverlayClick: true | false, // Should the dialog be closed on overlay click?
  closeOnEscape: true | false, // Should the dialog be closed if [ESCAPE] key is pressed?
  removeOnClose: true | false, // Should the dialog be removed from the document when it is closed?
  showCloseHandle: true | false, // Should a close handle be shown?
  initialLoadText: "" // Text to be displayed when the dialogs contents are loaded
}

When adding buttons to a dialog, a button options object can be passed to it instead of a callback:

{
  click: function() { }, // callback to execute on button click (has this bound to the dialog)
  primary: true | false, // if this button is the primary button (will be styled accordingly)
  type: "info" // basically additional classes to be attached to the button
}

API

Let the markup rule

The dialog has some distinct features which make it totally controllable via html markup.

Changing the UI

Dialog width

Override the boostrap styles for .modal which are (as of version 1.3)

.modal {
    ...
    width: 560px;
    top: 50%;
    margin: -250px 0 0 -250px;
}

with your new styles for wide dialogs

.wide-modal {
    ...
    top: 20%;
    width: 800px;
    margin: 0 0 0 -400px; /* -1 * (width / 2) */ 
}

and add the class wide-modal to big dialogs.

Extending the dialog

A user may plug into the dialog behaviour by listening to events, namely

dialog2.ajax-start: fired right before an ajax request is started, 
dialog2.ajax-complete: fired when an ajax request was executed successfully, 
dialog2.content-update: when the dialogs contents are updated (after ajax-complete)

The preferred way to register handlers on these events is $().delegate. The code snipped shown below illustrates how this behaviour can be used to implement a auto-close ability for a dialog (see issue #19).


// If a a.auto-close is contained in the dialogs content, 
// the dialog will close itself automatically and (optionally)
// redirect to a new page
$(document).delegate(".modal", "dialog2.content-update", function() { 
     // got the dialog as this object. Do something with it!

    var e = $(this);

    var autoclose = e.find("a.auto-close");
    if (autoclose.length > 0) {
        e.dialog("close");

        var href = autoclose.attr('href');
        if (href) {
            window.location.href = href;
        }
    }
});

Check out some examples

Go to the plugins web page to check out a number of examples on usage.