The Dialog Widget

Exclusive offer: get 50% off this eBook here
Building UIs with Wijmo

Building UIs with Wijmo — Save 50%

Build user interfaces quickly using widgets with this book and ebook

$17.99    $9.00
by Yuguang Zhang | October 2013 | Open Source Web Development

This article by Yuguang Zhang, the author of the book Building UIs with Wijmo, discusses the dialog widget, which is present in the Wijmo Open set. It is an enhancement of the jQuery UI dialog with more features. Wijmo dialogs can be maximized, minimized, pinned to a location, and display external content from a URL in the dialog window. This article discusses the options, methods, and events that are added in Wijmo, and how to use them to change the appearance and behavior of the dialog.

(For more resources related to this topic, see here.)

Wijmo additions to the dialog widget at a glance

By default, the dialog window includes the pin, toggle, minimize, maximize, and close buttons. Pinning the dialog to a location on the screen disables the dragging feature on the title bar. The dialog can still be resized. Maximizing the dialog makes it take up the area inside the browser window. Toggling it expands or collapses it so that the dialog contents are shown or hidden with the title bar remaining visible. If these buttons cramp your style, they can be turned off with the captionButtons option. You can see how the dialog is presented in the browser from the following screenshot:

Wijmo features additional API compared to jQuery UI for changing the behavior of the dialog. The new API is mostly for the buttons in the title bar and managing window stacking. Window stacking determines which windows are drawn on top of other ones. Clicking on a dialog raises it above other dialogs and changes their window stacking settings. The following table shows the options added in Wijmo.

Options

Events

Methods

captionButtons

contentUrl

disabled

expandingAnimation

stack

zIndex

blur

buttonCreating

stateChanged

disable

enable

getState

maximize

minimize

pin

refresh

reset

restore

toggle

widget

  • The contentUrl option allows you to specify a URL to load within the window.
  • The expandingAnimation option is applied when the dialog is toggled from a collapsed state to an expanded state.
  • The stack and zIndex options determine whether the dialog sits on top of other dialogs.
  • Similar to the blur event on input elements, the blur event for dialog is fired when the dialog loses focus.
  • The buttonCreating method is called when buttons are created and can modify the buttons on the title bar.
  • The disable method disables the event handlers for the dialog. It prevents the default button actions and disables dragging and resizing.
  • The widget method returns the dialog HTML element.
  • The methods maximize, minimize, pin, refresh, reset, restore, and toggle, are available as buttons on the title bar. The best way to see what they do is play around with them.
  • In addition, the getState method is used to find the dialog state and returns either maximized, minimized, or normal.
  • Similarly, the stateChanged event is fired when the state of the dialog changes.

The methods are called as a parameter to the wijdialog method. To disable button interactions, pass the string disable:

$("#dialog").wijdialog ("disable");

Many of the methods come as pairs, and enable and disable are one of them. Calling enable enables the buttons again. Another pair is restore/minimize. minimize hides the dialog in a tray on the left bottom of the screen. restore sets the dialog back to its normal size and displays it again.

The most important option for usability is the captionButtons option. Although users are likely familiar with the minimize, resize, and close buttons; the pin and toggle buttons are not featured in common desktop environments. Therefore, you will want to choose the buttons that are visible depending on your use of the dialog box in your project. To turn off a button on the title bar, set the visible option to false. A default jQuery UI dialog window with only the close button can be created with:

$("#dialog").wijdialog({captionButtons: { pin: { visible: false }, refresh: { visible: false }, toggle: { visible: false }, minimize: { visible: false }, maximize: { visible: false } } });

The other options for each button are click, iconClassOff, and iconClassOn. The click option specifies an event handler for the button. Nevertheless, the buttons come with default actions and you will want to use different icons for custom actions. That's where iconClass comes in. iconClassOn defines the CSS class for the button when it is loaded. iconClassOff is the class for the button icon after clicking. For a list of available jQuery UI icons and their classes, see http://jquery-ui.googlecode.com/svn/tags/1.6rc5/tests/static/icons.html.

Our next example uses ui-icon-zoomin, ui-icon-zoomout, and ui-icon-lightbulb. They can be found by toggling the text for the icons on the web page as shown in the preceding screenshot.

Adding custom buttons

jQuery UI's dialog API lacks an option for configuring the buttons shown on the title bar. Wijmo not only comes with useful default buttons, but also lets you override them easily.

<!DOCTYPE HTML> <html> <head> ... <style> .plus { font-size: 150%; } </style> <script id="scriptInit" type="text/javascript"> $(document).ready(function () { $('#dialog').wijdialog({ autoOpen: true, captionButtons: { pin: { visible: false }, refresh: { visible: false }, toggle: {visible: true, click: function () { $('#dialog').toggleClass('plus') }, iconClassOn: 'ui-icon-zoomin', iconClassOff: 'ui-icon-zoomout'} , minimize: { visible: false }, maximize: {visible: true, click: function () { alert('To enloarge text, click the zoom icon.') }, iconClassOn: 'ui-icon-lightbulb' }, close: {visible: true, click: self.close, iconClassOn:'ui-icon-close'} } }); }); </script> </head> <body> <div id="dialog" title="Basic dialog"> <p>Loremipsum dolor sitamet, consectetueradipiscingelit.
Aeneancommodo ligula eget dolor.Aeneanmassa. Cum
sociisnatoquepenatibusetmagnis dis
parturient montes, nasceturridiculus mus. Donec
quam felis, ultriciesnec, pellentesqueeu, pretiumquis,
sem. Nullaconsequatmassaquisenim. Donecpedejusto,
fringillavel, aliquetnec, vulputate</p> </div> </body> </html>

We create a dialog window passing in the captionButtons option. The pin, refresh, and minimize buttons have visible set to false so that the title bar is initialized without them. The final output looks as shown in the following screenshot:

In addition, the toggle and maximize buttons are modified and given custom behaviors. The toggle button toggles the font size of the text by applying or removing a CSS class. Its default icon, set with iconClassOn, indicates that clicking on it will zoom in on the text. Once clicked, the icon changes to a zoom out icon. Likewise, the behavior and appearance of the maximize button have been changed. In the position where the maximize icon was displayed in the title bar previously, there is now a lightbulb icon with a tip.

Although this method of adding new buttons to the title bar seems clumsy, it is the only option that Wijmo currently offers. Adding buttons in the content area is much simpler. The buttons option specifies the buttons to be displayed in the dialog window content area below the title bar. For example, to display a simple confirmation button:

$('#dialog').wijdialog({buttons: {ok: function () { $(this).wijdialog('close') }}});

The text displayed on the button is ok and clicking on the button hides the dialog. Calling $('#dialog').wijdialog('open') will show the dialog again.

Configuring the dialog widget's appearance

Wijmo offers several options that change the dialog's appearance including title, height, width, and position. The title of the dialog can be changed either by setting the title attribute of the div element of the dialog, or by using the title option. To change the dialog's theme, you can use CSS styling on the wijmo-wijdialog and wijmo-wijdialog-captionbutton classes:

<!DOCTYPE HTML> <html> <head> ... <style> .wijmo-wijdialog { /*rounded corners*/ -webkit-border-radius: 12px; border-radius: 12px; background-clip: padding-box; /*shadow behind dialog window*/ -moz-box-shadow: 3px 3px 5px 6px #ccc; -webkit-box-shadow: 3px 3px 5px 6px #ccc; box-shadow: 3px 3px 5px 6px #ccc; /*fade contents from dark gray to gray*/ background-image: -webkit-gradient(linear, left top, left bottom,
from(#444444), to(#999999)); background-image: -webkit-linear-gradient(top, #444444, #999999); background-image: -moz-linear-gradient(top, #444444, #999999); background-image: -o-linear-gradient(top, #444444, #999999); background-image: linear-gradient(to bottom, #444444, #999999); background-color: transparent; text-shadow: 1px 1px 3px #888; } </style> <script id="scriptInit" type="text/javascript"> $(document).ready(function () { $('#dialog').wijdialog({width: 350}); }); </script> </head> <body> <div id="dialog" title="Subtle gradients"> <p>Loremipsum dolor sitamet, consectetueradipiscingelit.
Aeneancommodo ligula eget dolor.Aeneanmassa. Cum
sociisnatoquepenatibusetmagnis
dis parturient montes, nasceturridiculus mus. Donec
quam felis, ultriciesnec, pellentesqueeu, pretiumquis,
sem. Nullaconsequatmassaquisenim. Donecpedejusto,
fringillavel, aliquetnec, vulputate </p> </div> </body> </html>

We now add rounded boxes, a box shadow, and a text shadow to the dialog box. This is done with the .wijmo-wijdialog class. Since many of the CSS3 properties have different names on different browsers, the browser specific properties are used. For example, -webkit-box-shadow is necessary on Webkit-based browsers. The dialog width is set to 350 px when initialized so that the title text and buttons all fit on one line.

Loading external content

Wijmo makes it easy to load content in an iFrame. Simply pass a URL with the contentUrl option:

$(document).ready(function () { $("#dialog").wijdialog({captionButtons: { pin: { visible: false }, refresh: { visible: true }, toggle: { visible: false }, minimize: { visible: false }, maximize: { visible: true }, close: { visible: false } }, contentUrl: "http://wijmo.com/demo/themes/" }); });

This will load the Wijmo theme explorer in a dialog window with refresh and maximize/restore buttons. This output can be seen in the following screenshot:

The refresh button reloads the content in the iFrame, which is useful for dynamic content. The maximize button resizes the dialog window.

Form Components

Wijmo form decorator widgets for radio button, checkbox, dropdown, and textbox elements give forms a consistent visual style across all platforms. There are separate libraries for decorating the dropdown and other form elements, but Wijmo gives them a consistent theme. jQuery UI lacks form decorators, leaving the styling of form components to the designer. Using Wijmo form components saves time during development and presents a consistent interface across all browsers.

Checkbox

The checkbox widget is an excellent example of the style enhancements that Wijmo provides over default form controls. The checkbox is used if multiple choices are allowed. The following screenshot shows the different checkbox states:

Wijmo adds rounded corners, gradients, and hover highlighting to the checkbox. Also, the increased size makes it more usable. Wijmo checkboxes can be initialized to be checked. The code for this purpose is as follows:

<!DOCTYPE HTML> <html> <head> ... <script id="scriptInit" type="text/javascript"> $(document).ready(function () { $("#checkbox3").wijcheckbox({checked: true}); $(":input[type='checkbox']:not(:checked)").wijcheckbox(); }); </script> <style> div { display: block; margin-top: 2em; } </style> </head> <body> <div><input type='checkbox' id='checkbox1' /><label for='checkbox1'>
Unchecked</label></div> <div><input type='checkbox' id='checkbox2' /><label for='checkbox2'>
Hover</label></div> <div><input type='checkbox' id='checkbox3' /><label for='checkbox3'>
Checked</label></div> </body> </html>.

In this instance, checkbox3 is set to Checked as it is initialized.

You will not get the same result if one of the checkboxes is initialized twice. Here, we avoid that by selecting the checkboxes that are not checked after checkbox3 is set to be Checked.

Radio buttons

Radio buttons, in contrast with checkboxes, allow only one of the several options to be selected. In addition, they are customized through the HTML markup rather than a JavaScript API. To illustrate, the checked option is set by the checked attribute:

<input type="radio" checked />

jQuery UI offers a button widget for radio buttons, as shown in the following screenshot, which in my experience causes confusion as users think that they can select multiple options:

The Wijmo radio buttons are closer in appearance to regular radio buttons so that users would expect the same behavior, as shown in the following screenshot:

Wijmo radio buttons are initialized by calling the wijradiomethod method on radio button elements:

<!DOCTYPE html> <html> <head> ... <script id="scriptInit" type="text/javascript">
$(document).ready(function () { $(":input[type='radio']").wijradio({ changed: function (e, data) { if (data.checked) { alert($(this).attr('id') + ' is checked') } } }); }); </script> </head> <body> <div id="radio"> <input type="radio" id="radio1" name="radio"/><label for="radio1">Choice 1
</label> <input type="radio" id="radio2" name="radio" checked="checked"/>
<label for="radio2">Choice 2</label> <input type="radio" id="radio3" name="radio"/><label for="radio3">
Choice 3</label> </div> </body> </html>

In this example, the changed option, which is also available for checkboxes, is set to a handler. The handler is passed a jQuery.Event object as the first argument. It is just a JavaScript event object normalized for consistency across browsers. The second argument exposes the state of the widget. For both checkboxes and radio buttons, it is an object with only the checked property.

Dropdown

Styling a dropdown to be consistent across all browsers is notoriously difficult. Wijmo offers two options for styling the HTML select and option elements. When there are no option groups, the ComboBox is the better widget to use. For a dropdown with nested options under option groups, only the wijdropdown widget will work. As an example, consider a country selector categorized by continent:

<!DOCTYPE HTML> <html> <head> ... <script id="scriptInit" type="text/javascript"> $(document).ready(function () { $('select[name=country]').wijdropdown(); $('#reset').button().click(function(){ $('select[name=country]').wijdropdown('destroy') }); $('#refresh').button().click(function(){ $('select[name=country]').wijdropdown('refresh') }) }); </script> </head> <body> <button id="reset"> Reset </button> <button id="refresh"> Refresh </button> <select name="country" style="width:170px"> <optgroup label="Africa"> <option value="gam">Gambia</option> <option value="mad">Madagascar</option> <option value="nam">Namibia</option> </optgroup> <optgroup label="Europe"> <option value="fra">France</option> <option value="rus">Russia</option> </optgroup> <optgroup label="North America"> <option value="can">Canada</option> <option value="mex">Mexico</option> <option selected="selected" value="usa">United States</option> </optgroup> </select> </body> </html>

The select element's width is set to 170 pixels so that when the dropdown is initialized, both the dropdown menu and items have a width of 170 pixels. This allows the North America option category to be displayed on a single line, as shown in the following screenshot. Although the dropdown widget lacks a width option, it takes the select element's width when it is initialized. To initialize the dropdown, call the wijdropdown method on the select element:

$('select[name=country]').wijdropdown();

The dropdown element uses the blind animation to show the items when the menu is toggled.

Also, it applies the same click animation as on buttons to the slider and menu:

To reset the dropdown to a select box, I've added a reset button that calls the destroy method. If you have JavaScript code that dynamically changes the styling of the dropdown, the refresh method applies the Wijmo styles again.

Summary

The Wijmo dialog widget is an extension of the jQuery UI dialog. In this article, the features unique to Wijmo's dialog widget are explored and given emphasis. I showed you how to add custom buttons, how to change the dialog appearance, and how to load content from other URLs in the dialog.

We also learned about Wijmo's form components. A checkbox is used when multiple items can be selected. Wijmo's checkbox widget has style enhancements over the default checkboxes. Radio buttons are used when only one item is to be selected. While jQuery UI only supports button sets on radio buttons, Wijmo's radio buttons are much more intuitive. Wijmo's dropdown widget should only be used when there are nested or categorized <select> options. The ComboBox comes with more features when the structure of the options is flat.

Resources for Article:


Further resources on this subject:


Building UIs with Wijmo Build user interfaces quickly using widgets with this book and ebook
Published: September 2013
eBook Price: $17.99
Book Price: $29.99
See more
Select your format and quantity:

About the Author :


Yuguang Zhang

Yuguang Zhang has worked as a web developer in a number of small companies. Notable companies where he has worked at include SociaLabra, a social media company, and SmartPager, a startup in mobile paging. He has expertise in developing interactive client-side applications with Knockout. As a personal project, he built the first IDE that runs Python in the browser using Knockout and jQuery UI, that is pythonfiddle.com. In addition, he designed and programmed fiddlesalad.com, a multi-language playground for rapid frontend development built with CoffeeScript and Django.

Books From Packt


 Instant Wijmo Widgets How-to [Instant]
Instant Wijmo Widgets How-to [Instant]

 jQuery UI 1.8: The User Interface Library for jQuery
jQuery UI 1.8: The User Interface Library for jQuery

 jQuery UI 1.6: The User Interface Library for jQuery
jQuery UI 1.6: The User Interface Library for jQuery

Instant Ext JS Starter [Instant]
Instant Ext JS Starter [Instant]

Instant AngularJS Starter [Instant]
Instant AngularJS Starter [Instant]

Instant Node.js Starter [Instant]
Instant Node.js Starter [Instant]

 WordPress Multisite Administration
WordPress Multisite Administration

 WordPress 3 Plugin Development Essentials
WordPress 3 Plugin Development Essentials


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
x
B
f
r
f
7
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software