UMass Lowell Dept. of Computer Science

91.461 GUI Programming I

Fall 2015 Semester, Section 201

Prof. Jesse M. Heines

Notes for Class No. 20

Introduction to the jQuery UI Library

Tuesday, November 10, 2015

A video of this class is (or will be) posted at:   http://echo360.uml.edu/heines201516/guiprogramming1.html


Handouts and Materials


Openings / Announcements / Reminders

Reminder:  Friday, November 13, is that last day to withdraw from courses with a grade of “W”


Class Notes

Related reading for this class:  JS&jQ:TMM Ch. 9


Using the jQuery UI Library

Two approaches


What’s available in the jQuery UI Library

Widget Demonstrations

Interactions

Interactions add basic mouse-based behaviors to any element. 
You can create sortable lists, resizable elements, drag & drop behaviors and more with just a few lines or code. 
Interactions also make great building blocks for more complex widgets and applications.

Widgets

Widgets are full-featured UI controls that bring the richness of desktop applications to the Web. 
All widgets provide a solid core with plenty of extension points for customizing behavior, as well as full theming support.

Effects

Effects add support for animating colors and class transitions, as well as providing several additional easings. 
In addition, a full suite of custom effects are available for use when showing and hiding elements or just to add some visual appeal.

Utilities

Utilities used by jQuery UI to build interactions and widgets.

As you can quickly see, some of these widgets are also available in HTML5, but support in HTML5 is more limited.

The jQuery UI library also supplies themes, which you can construct with the ThemeRoller.


jQuery UI Library Structure  (source: jQuery UI 1.8 by Dan Wellman, p. 12)

To install the jQuery UI library, all you have to do is:

(1)  Download the jQuery UI library as a ZIP file from jqueryui.com/download

(2)  Unzip that ZIP file (jquery-ui-1.11.2.zip as of this writing) and you should have a directory named jquery-ui-1.11.2 that contains 8 jquery-ui files and an index.html file that provides demos


Introduction to the jQuery UI API  (source: jQuery UI 1.8 pp. 19-23, jQueryUI 1.10 pp. 23-27)

First, a note on the jQuery function syntax used in Wellman’s book:
(function($) {
  // JavaScript code
})(jQuery)

Source:  jquery-howto.blogspot.com/2008/12/what-heck-is-function-jquery.html

Thus, the following structures are equivalent

// Version 1
(function($) { 
  // JavaScript code 
})(jQuery)

// Version 2
var myFunction = function($) { 
  // JavaScript code
};
myFunction(jQuery);

And by the way, there is an incredible wealth of jQuery information posted at jquery-howto.blogspot.com


OK, on to the jQuery UI API

The API for each component consists of a series of different methods.

While these are all technically methods, it is useful to categorize them based on their particular function.

Method Type Description
The plugin
method
This method is used to initialize the component and is simply the name of the component followed by parentheses.  This is also referred to as the “widget” method.
Shared API methods
  • The destroy method can be used with any of the components, to completely disable the widget being used and in most cases, returns the underlying HTML to its original state.
  • The option method is used by all components to get or set any configuration option after initialization.
  • The enable and disable methods are used by most library components to enable or disable the component.
  • The widget method, exposed by all widgets, returns a reference to the current widget.
Specialized methods Each component has one or more methods unique to that particular component that perform specialized functions.

Methods are consistently called throughout each of the different components by passing the method that we'd like to call, as a simple string to the component's plugin method, with any arguments that the method accepts passed as strings after the method name.

Some methods, like standard JavaScript functions, accept arguments that trigger different behavior in the component.

The disable method, when used in conjunction with the tabs widget, accepts an integer, which refers to the index of the individual tab within the widget.

Sometimes the arguments that are passed to the method vary between components.

The option method is slightly more complex than the other common methods, but it’s also more powerful.

Events and Callbacks  (source: jQuery UI 1.8 pp. 22-23, jQuery UI 1.10 pp. 25-26)

The API for each component also contains a rich event model that allows us to easily react to different interactions.

We have two ways of working with events in jQuery UI.

(1)  Each component allows us to add callback functions that are executed when the specified event is fired, as values for configuration options.

(2)  The other way of working with events is to use jQuery's bind() method.

$("#someElement").bind("tabsselect", function() {
  // JavaScript code
});

Some of the custom events fired by jQuery UI components are cancellable and if stopped, can be used to prevent certain actions from taking place.


Callback Arguments  (source: jQuery UI 1.8 p. 23, jQuery UI 1.10 p. 26)

Any anonymous functions that we supply as callback functions to the different events, automatically pass two arguments:

To use these two objects we just specify them as arguments to the function:


The jQuery UI Tabs Widget


Source:  jQuery UI 1.8 p. 62, jQuery UI 1.10 p. 60


First, let’s look at how the textbook sets up a tabbed panel interface  (JS&jQ:TMM, pp. 301-306)

The HTML  (JS&jQ:TMM, p. 302)

The CSS  (JS&jQ:TMM, p. 304)

The JavaScript  (JS&jQ:TMM, p. 306)

Running the Textbook’s Tabbed Panels Example

Run the tabbed panels example that is built in the tutorial on pages 307-311 of JS&jQ:TMM.  Browse the source code to see how everything fits together.

If you have the book’s sample programs on your system, you can find the completed tabbed panels tutorial at:

{Your-Book-Root-Directory}\MM_JAVASCRIPT2E\chapter10\complete_tabs.html

If you do not have these programs, you can download them from:

http://examples.oreilly.com/9780596515898/MM_JAVASCRIPT.zip

Alternatively, you can run this program from:

http://jesseheines.com/~heines/91.461/resources/Book-McFarland-JavaScript_jQuery_2nd/MM_JAVASCRIPT2E/chapter10/complete_tabs.html

View the source code and look up any part of it that you don’t understand in the book.

Now let’s look at how you would do this with the jQuery UI library

runnable web page of the above code


A Minimal Tabbed Panel Program


Tabs Widget CSS Framework Classes  (jQuery UI 1.8 by Dan Wellman, pp. 64-66)

The following built-in classes are use by the Tabs Widget Framework to style the individual components

For the outer container <div>

Classname Purpose
ui-tabs Allows tab-specific structural CSS to be applied.
ui-widget Sets generic font styles that are inherited by nested elements.
ui-widget-content Provides theme-specific styles.
ui-corner-all Applies rounded corners to the container.

For the <ul> element within the container <div>

Classname Purpose
ui-tabs-nav Allows tab-specific structural CSS to be applied.
ui-helper-reset Neutralizes browser-specific styles applied to <ul> elements.
ui-helper-clearfix Applies the clear-fix as this element has children that are floated.
ui-widget-header Provides theme-specific styles.
ui-corner-all Applies rounded corners.

For the individual <li> elements that form a part of the tab headings

Classname Purpose
ui-state-default Applies the standard, non-active, non-selected, non-hovered state to the tab headings.
ui-corner-top Applies rounded corners to the top edges of the elements.
ui-tabs-selected This is only applied to the active tab.  On page-load of the default implementation, this will be the first tab.  Selecting another tab will remove this class from the currently selected tab and apply it to the newly selected tab.
ui-state-active Applies theme-specific styles to the currently selected tab.  This class name will be added to the tab that is currently selected, just like the previous classname.  The reason there are two class names is that ui-tabs-selected provides the functional CSS, while ui-state-active provides the visual, decorative styles.

For the panel elements that hold each tab’s content

Classname Purpose
ui-tabs-panel Applies structural CSS to the content panels.
ui-widget-content Applies theme-specific styles.
ui-corner-bottom Applies rounded corners to the bottom edges of the content panels.


Tabs Widget Options  (jQuery UI 1.8 by Dan Wellman, pp. 68-69)

Each components in the UI library also has a series of options that control the widget features hat are enabled by default.

Option Default Value Used to...
ajaxOptions null Specify additional Ajax options.  We can use any of the options exposed by jQuery’s $.ajax() method such as data, type, url, and so on.
cache false Control whether the remote data is loaded only once when the page initializes, or is reloaded every time the corresponding tab is clicked.
collapsible false Allow an active tab to be unselected if it is clicked, so that all of the content panels are hidden and only the tab headings are visible.
cookie null Show the active tab using cookie data on page load.  The cookie plugin is required for this option to be used.
disabled false Disable the widget on page load.  We can also pass an array of tab indices (zero-based) in order to disable specific tabs.
event "click" Specify the event that triggers the display of content panels.
fx null Specify an animation effect when changing tabs.  Supply an object literal or an array of animation effects.
idPrefix "ui-tabs-" Generate a unique ID and fragment identifier when a remote tab heading’s <a> element has no title attribute.
panelTemplate "<div></div>" Specifying the elements used for the content section of a tab panel.
selected 0 Show a tab panel other than the first one on page load (overrides the cookie property).
spinner "Loading…" Specify the loading spinner for remote tabs.
tabTemplate
<li><a
 href="#{href}">
  <span>#{label}
  </span>
 </a>
</li>
Specify the elements used when creating new tabs.  The #{href} and #{label} strings are replaced by the widget internally when the new tab is created.


Tabs Widget Events  (jQuery UI 1.8 by Dan Wellman, pp. 72-73)

Various events are exposed by the tabs widget.

Event Fired when...
add A new tab is added.
disable A tab is disabled.
enable A tab is enabled.
load A tab’s remote data has loaded.
remove A tab is removed.
select A tab is selected.
show A tab is shown.
(function($){
  var handleSelect = function(e, tab) {
    $( "<p></p>" , 
       { 
         "text": "Tab at index " + tab.index + " selected",
         "class": "status-message ui-corner-all"
       }).appendTo(".ui-tabs-nav", "#myTabs").fadeOut( 5000, 
         function() {
           $(this).remove();
         }
       );
     } ,
   tabOpts = {
     select: handleSelect
   }
  $("#myTabs").tabs(tabOpts);
})(jQuery);

Binding to Tabs Widget Events  (jQuery UI 1.8 by Dan Wellman, p. 74)

Event Fired when...
tabsadd A tab has been added to the interface.
tabsdisable A tab has been disabled.
tabsenable A tab has been enabled.
tabsload A remote tab has loaded.
tabsremove A tab has been removed from the interface.
tabsselect A tab is selected.
tabsshow A tab is shown.
(function($) {
  $("#myTabs").tabs();
  $("#myTabs").bind("tabsselect", function(e, tab) {
    alert("The tab at index " + tab.index + " was selected");
  });
})(jQuery);


Tabs Widget Methods  (jQuery UI 1.8 by Dan Wellman, p. 75)

The tabs widget also provides a rich set of methods that we can use to control its behavior programmatically.

Method Used to...
abort Stop any animations or Ajax requests that are currently in progress.
add Add a new tab programmatically, specifying the URL of the tab's content, a label, and optionally its index number as arguments.
destroy Completely remove the tabs widget.
disable Disable a specific tab by passing a zero-based index number to the method, or pass nothing to the method to disable the entire set of tabs.
enable Enable a disable tab by passing a zero-based index number to the method, or enable the entire widget by passing nothing to the method.
length Return the number of tabs in the widget.
load Reload an Ajax tab’s content, specifying the index number of the tab.
option Get or set any property after the widget has been initialized.
remove Remove a tab programmatically, specifying the index of the tab to remove.  If no index is supplied, the first tab is removed.  Tabs can also be removed using their href value.
rotate Automatically changes the active tab after a specified number of milliseconds have passed, either once or repeatedly.
select Select a tab programmatically, which has the same effect as when a visitor clicks a tab, based on index number.
url Change the URL of content given to an Ajax tab.  The method expects the index number of the tab and the new URL.  See also load (earlier in this table).
widget Return the element that the tabs() widget method is called on.



Using Ajax with the Tabs Widget  (jQuery UI 1.8 by Dan Wellman, p. 82)

Simply add an href attribute to the underlying HTML

<li><a href="CourseDescriptionPanel.html">Information</a></li>


An Extended Tabs Widget Example of Using Styles and Options

The Website

The HTML to set up the tabs widget interface

The modified CSS file to style the website as I wished



This is document http://jesseheines.com:8080/~heines/91.461/91.461-2015-16f/461-lecs/lecture20.jsp.  It was last modified on Friday, August 26, 2022 at 4:09 PM.
Copyright © 2022 by Jesse M. Heines.  All rights reserved.  May be freely copied or excerpted for educational purposes with credit to the author.