Created
November 27, 2013 20:39
-
-
Save tjvantoll/7682814 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<entries> | |
<entry type="method" name="jQuery.widget"> | |
<title>Widget Factory</title> | |
<signature> | |
<argument name="name" type="String"> | |
<desc>The name of the widget to create, including the namespace.</desc> | |
</argument> | |
<argument name="base" type="Function" optional="true"> | |
<desc>The base widget to inherit from. This must be a constructor that can be instantiated with the `new` keyword. Defaults to <code>jQuery.Widget</code>.</desc> | |
</argument> | |
<argument name="prototype" type="PlainObject"> | |
<desc>The object to use as a prototype for the widget.</desc> | |
</argument> | |
</signature> | |
<desc>Create stateful jQuery plugins using the same abstraction as all jQuery UI widgets.</desc> | |
<longdesc> | |
<p>You can create new widgets from scratch, using just the <code>$.Widget</code> object as a base to inherit from, or you can explicitly inherit from existing jQuery UI or third-party widgets. Defining a widget with the same name as you inherit from even allows you to extend widgets in place.</p> | |
<p>jQuery UI contains many widgets that maintain state and therefore have a slightly different usage pattern than typical jQuery plugins. All of jQuery UI's widgets use the same patterns, which is defined by the widget factory. So if you learn how to use one widget, then you'll know how to use all of them.</p> | |
<p><em>Note: This documentation shows examples using the <a href="/progressbar/">progressbar widget</a> but the syntax is the same for every widget.</em></p> | |
<h3>Initialization</h3> | |
<p>In order to track the state of the widget, we must introduce a full life cycle for the widget. The life cycle starts when the widget is initialized. To initialize a widget, we simply call the plugin on one or more elements.</p> | |
<pre><code> | |
$( "#elem" ).progressbar(); | |
</code></pre> | |
<p>This will initialize each element in the jQuery object, in this case the element with an id of <code>"elem"</code>.</p> | |
<h3>Options</h3> | |
<p>Because <code>progressbar()</code> was called with no parameters, the widget was initialized with its default options. We can pass a set of options during initialization to override the defaults:</p> | |
<pre><code> | |
$( "#elem" ).progressbar({ value: 20 }); | |
</code></pre> | |
<p>We can pass as many or as few options as we want during initialization. Any options that we don't pass will just use their default values.</p> | |
<p>You can pass multiple options arguments. Those arguments will be merged into one object (similar to <a href="http://api.jquery.com/jQuery.extend/"><code>$.extend( true, target, object1, objectN )</code></a>). This is useful for sharing options between instances, while overriding some settings for each one:</p> | |
<pre><code> | |
var options = { modal: true, show: "slow" }; | |
$( "#dialog1" ).dialog( options ); | |
$( "#dialog2" ).dialog( options, { autoOpen: false }); | |
</code></pre> | |
<p>All options passed on init are deep-copied to ensure the objects can be modified later without affecting the widget. Arrays are the only exception, they are referenced as-is. This exception is in place to support data-binding, where the data source has to be kept as a reference.</p> | |
<p>The default values are stored on the widget's prototype, therefore we have the ability to override the values that jQuery UI sets. For example, after setting the following, all future progressbar instances will default to a value of 80:</p> | |
<pre><code> | |
$.ui.progressbar.prototype.options.value = 80; | |
</code></pre> | |
<p>The options are part of the widget's state, so we can set options after initialization as well. We'll see this later with the option method.</p> | |
<h3>Methods</h3> | |
<p>Now that the widget is initialized, we can query its state or perform actions on the widget. All actions after initialization take the form of a method call. To call a method on a widget, we pass the name of the method to the jQuery plugin. For example, to call the <code>value()</code> method on our progressbar widget, we would use:</p> | |
<pre><code> | |
$( "#elem" ).progressbar( "value" ); | |
</code></pre> | |
<p>If the method accepts parameters, we can pass them after the method name. For example, to pass the parameter <code>40</code> to the <code>value()</code> method, we can use:</p> | |
<pre><code> | |
$( "#elem" ).progressbar( "value", 40 ); | |
</code></pre> | |
<p>Just like other methods in jQuery, most widget methods return the jQuery object for chaining.</p> | |
<pre><code> | |
$( "#elem" ) | |
.progressbar( "value", 90 ) | |
.addClass( "almost-done" ); | |
</code></pre> | |
<p>Each widget will have its own set of methods based on the functionality that the widget provides. However, there are a few methods that exist on all widgets, which are documented below.</p> | |
<h3>Events</h3> | |
<p>All widgets have events associated with their various behaviors to notify you when the state is changing. For most widgets, when the events are triggered, the names are prefixed with the widget name. For example, we can bind to progressbar's <code>change</code> event which is triggered whenever the value changes.</p> | |
<pre><code> | |
$( "#elem" ).bind( "progressbarchange", function() { | |
alert( "The value has changed!" ); | |
}); | |
</code></pre> | |
<p>Each event has a corresponding callback, which is exposed as an option. We can hook into progressbar's <code>change</code> callback instead of binding to the <code>progressbarchange</code> event, if we want to.</p> | |
<pre><code> | |
$( "#elem" ).progressbar({ | |
change: function() { | |
alert( "The value has changed!" ); | |
} | |
}); | |
</code></pre> | |
<p>All widgets have a <code>create</code> event which is triggered upon instantiation.</p> | |
<h3>Instance</h3> | |
<p>The widget's instance can be retrieved from a given element using the <a href="#method-instance"><code>instance()</code></a> method.</p> | |
<pre><code> | |
$( "#elem" ).progressbar( "instance" ); | |
</code></pre> | |
<p>If the <code>instance()</code> method is called on an element that is not associated with the widget, <code>undefined</code> is returned.</p> | |
<pre><code> | |
$( "#not-a-progressbar" ).progressbar( "instance" ); // undefined | |
</code></pre> | |
<p>The instance is stored using <a href="http://api.jquery.com/jQuery.data/"><code>jQuery.data()</code></a> with the widget's full name as the key. Therefore, the <a href="/data-selector/"><code>:data</code></a> selector can also determine whether an element has a given widget bound to it.</p> | |
<pre><code> | |
$( "#elem" ).is( ":data( 'ui-progressbar' )" ); // true | |
$( "#elem" ).is( ":data( 'ui-draggable' )" ); // false | |
</code></pre> | |
<p>Unlike <code>instance()</code>, <code>:data</code> can be used even if the widget being tested for has not loaded.</p> | |
<pre><code> | |
$( "#elem" ).nonExistentWidget( "instance" ); // TypeError | |
$( "#elem" ).is( ":data( 'ui-nonExistentWidget' )" ); // false | |
</code></pre> | |
<p>You can also use <code>:data</code> to get a list of all elements that are instances of a given widget.</p> | |
<pre><code> | |
$( ":data( 'ui-progressbar' )" ); | |
</code></pre> | |
<h3>Properties</h3> | |
<p>All widgets have the following set of properties:</p> | |
<ul> | |
<li> | |
<strong>document</strong>: The <code>document</code> that the widget's element is within. Useful if you need to interact with widgets within iframes. | |
</li> | |
<li> | |
<strong>element</strong>: A jQuery object containing the element used to instantiate the widget. If you select multiple elements and call <code>.myWidget()</code>, a separate widget instance will be created for each element. Therefore, this property will always contain one element. | |
</li> | |
<li> | |
<strong>namespace</strong>: The location on the global jQuery object that the widget's prototype is stored on. For example a <code>namespace</code> of <code>"ui"</code> indicates that the widget's prototype is stored on <code>$.ui</code>. | |
</li> | |
<li> | |
<strong>options</strong>: An object containing the options currently being used by the widget. On instantiation, any options provided by the user will automatically be merged with any default values defined in <code>$.myNamespace.myWidget.prototype.options</code>. User specified options override the defaults. | |
</li> | |
<li> | |
<strong>uuid</strong>: A unique integer identifier for the widget. | |
</li> | |
<li> | |
<strong>version</strong>: The string version of the widget. For jQuery UI widgets this will be set to the version of jQuery UI the widget is using. Widget developers have to set this property in their prototype explicitly. | |
</li> | |
<li> | |
<strong>widgetEventPrefix</strong>: The prefix prepended to the name of events fired from this widget. For example the <code>widgetEventPrefix</code> of the <a href="/draggable/">draggable widget</a> is <code>"drag"</code>, therefore when a draggable is created, the name of the event fired is <code>"dragcreate"</code>. By default the <code>widgetEventPrefix</code> of a widget is its name. <em>Note: This property is deprecated and will be removed in a later release. Event names will be changed to widgetName:eventName (e.g. <code>"draggable:create"</code>.</em> | |
</li> | |
<li> | |
<strong>widgetFullName</strong>: The full name of the widget including the namespace. For <code>$.widget( "myNamespace.myWidget", {} )</code>, <code>widgetFullName</code> will be <code>"myNamespace-myWidget"</code>. | |
</li> | |
<li> | |
<strong>widgetName</strong>: The name of the widget. For <code>$.widget( "myNamespace.myWidget", {} )</code>, <code>widgetName</code> will be <code>"myWidget"</code>. | |
</li> | |
<li> | |
<strong>window</strong>: The <code>window</code> that the widget's element is within. Useful if you need to interact with widgets within iframes. | |
</li> | |
</ul> | |
</longdesc> | |
<category slug="utilities"/> | |
<category slug="widgets"/> | |
</entry> | |
<entry type="widget" name="jQuery.Widget" widget-name="widget" animated-element="element" | |
widget-element="original element or other relevant generated element"> | |
<title>Base Widget</title> | |
<desc>The base widget used by the widget factory.</desc> | |
<options> | |
<xi:include href="../includes/widget-option-disabled.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
<xi:include href="../includes/widget-option-hide.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
<xi:include href="../includes/widget-option-show.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
</options> | |
<methods suppress-examples="true"> | |
<xi:include href="../includes/widget-method-destroy.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
<xi:include href="../includes/widget-method-disable.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
<xi:include href="../includes/widget-method-enable.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
<xi:include href="../includes/widget-method-instance.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
<xi:include href="../includes/widget-method-option.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
<xi:include href="../includes/widget-method-widget.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
<method name="_create"> | |
<desc> | |
The <code>_create()</code> method is the widget's constructor. | |
There are no parameters, but <code>this.element</code> and <code>this.options</code> are already set. | |
</desc> | |
<example> | |
<desc>Set the background color of the widget's element based on an option.</desc> | |
<code><![CDATA[ | |
_create: function() { | |
this.element.css( "background-color", this.options.color ); | |
} | |
]]></code> | |
</example> | |
</method> | |
<method name="_destroy"> | |
<desc> | |
The public <a href="#method-destroy"><code>destroy()</code></a> method cleans up all common data, events, etc. and then delegates out to <code>_destroy()</code> for custom, widget-specific, cleanup. | |
</desc> | |
<example> | |
<desc>Remove a class from the widget's element when the wiget is destroyed.</desc> | |
<code><![CDATA[ | |
_destroy: function() { | |
this.element.removeClass( "my-widget" ); | |
} | |
]]></code> | |
</example> | |
</method> | |
<method name="_getCreateEventData" return="Object"> | |
<desc> | |
All widgets trigger the <a href="#event-create"><code>create</code></a> event. By default, no data is provided in the event, but this method can return an object which will be passed as the <code>create</code> event's data. | |
</desc> | |
<example> | |
<desc>Pass the widget's options to <code>create</code> event handlers as an argument.</desc> | |
<code><![CDATA[ | |
_getCreateEventData: function() { | |
return this.options; | |
} | |
]]></code> | |
</example> | |
</method> | |
<method name="_getCreateOptions" return="Object"> | |
<desc> | |
This method allows the widget to define a custom method for defining options during instantiation. The user-provided options override the options returned by this method, which override the default options. | |
</desc> | |
<example> | |
<desc>Make the widget element's id attribute available as an option.</desc> | |
<code><![CDATA[ | |
_getCreateOptions: function() { | |
return { id: this.element.attr( "id" ) }; | |
} | |
]]></code> | |
</example> | |
</method> | |
<method name="_init"> | |
<desc> | |
Widgets have the concept of initialization that is distinct from creation. Any time the plugin is called with no arguments or with only an option hash, the widget is initialized; this includes when the widget is created. | |
<p><em>Note: Initialization should only be handled if there is a logical action to perform on successive calls to the widget with no arguments.</em></p> | |
</desc> | |
<example> | |
<desc>Call the <code>open()</code> method if the <code>autoOpen</code> option is set.</desc> | |
<code><![CDATA[ | |
_init: function() { | |
if ( this.options.autoOpen ) { | |
this.open(); | |
} | |
} | |
]]></code> | |
</example> | |
</method> | |
<method name="_setOptions"> | |
<desc> | |
Called whenever the <a href="#method-option"><code>option()</code></a> method is called, regardless of the form in which the <code>option()</code> method was called. | |
<p>Overriding this is useful if you can defer processor-intensive changes for multiple option changes.</p> | |
</desc> | |
<example> | |
<desc>Call a <code>resize()</code> method if the <code>height</code> or <code>width</code> options change.</desc> | |
<code><![CDATA[ | |
_setOptions: function( options ) { | |
var that = this, | |
resize = false; | |
$.each( options, function( key, value ) { | |
that._setOption( key, value ); | |
if ( key === "height" || key === "width" ) { | |
resize = true; | |
} | |
}); | |
if ( resize ) { | |
this.resize(); | |
} | |
} | |
]]></code> | |
</example> | |
<argument name="options" type="Object"> | |
<desc>A map of option-value pairs to set.</desc> | |
</argument> | |
</method> | |
<method name="_setOption"> | |
<desc> | |
Called from the <a href="#method-_setOptions"><code>_setOptions()</code></a> method for each individual option. Widget state should be updated based on changes. | |
</desc> | |
<argument name="key" type="String"> | |
<desc>The name of the option to set.</desc> | |
</argument> | |
<argument name="value" type="Object"> | |
<desc>A value to set for the option.</desc> | |
</argument> | |
<example> | |
<desc>Update a widget's element when its <code>height</code> or <code>width</code> option changes.</desc> | |
<code><![CDATA[ | |
_setOption: function( key, value ) { | |
if ( key === "width" ) { | |
this.element.width( value ); | |
} | |
if ( key === "height" ) { | |
this.element.height( value ); | |
} | |
this._super( key, value ); | |
} | |
]]></code> | |
</example> | |
</method> | |
<method name="_on"> | |
<desc> | |
Binds event handlers to the specified element(s). Delegation is supported via selectors inside the event names, e.g., "<code>click .foo</code>". The <code>_on()</code> method provides several benefits of direct event binding: | |
<ul> | |
<li>Maintains proper <code>this</code> context inside the handlers.</li> | |
<li>Automatically handles disabled widgets: If the widget is disabled or the event occurs on an element with the <code>ui-state-disabled</code> class, the event handler is not invoked. Can be overridden with the <code>suppressDisabledCheck</code> parameter.</li> | |
<li>Event handlers are automatically namespaced and cleaned up on destroy.</li> | |
</ul> | |
</desc> | |
<argument name="suppressDisabledCheck" type="Boolean" optional="true" default="false"> | |
<desc>Whether or not to bypass the disabled check.</desc> | |
</argument> | |
<argument name="element" type="jQuery" optional="true"> | |
<desc>Which element(s) to bind the event handlers to. If no element is provided, <code>this.element</code> is used.</desc> | |
</argument> | |
<argument name="handlers" type="Object"> | |
<desc> | |
A map in which the string keys represent the event type and optional selector for delegation, and the values represent a handler function to be called for the event. | |
</desc> | |
</argument> | |
<example> | |
<desc>Prevent the default action of all links clicked within the widget's element.</desc> | |
<code><![CDATA[ | |
this._on( this.element, { | |
"click a": function( event ) { | |
event.preventDefault(); | |
} | |
}); | |
]]></code> | |
</example> | |
</method> | |
<method name="_off"> | |
<desc> | |
Unbinds event handlers from the specified element(s). | |
</desc> | |
<argument name="element" type="jQuery"> | |
<desc> | |
The element(s) to unbind the event handlers from. Unlike the <code>_on()</code> method, the elements are required for <code>_off()</code>. | |
</desc> | |
</argument> | |
<argument name="eventName" type="String"> | |
<desc>One or more space-separated event types.</desc> | |
</argument> | |
<example> | |
<desc>Unbind all click events from the widget's element.</desc> | |
<code><![CDATA[ | |
this._off( this.element, "click" ); | |
]]></code> | |
</example> | |
</method> | |
<method name="_super"> | |
<desc> | |
Invokes the method of the same name from the parent widget, with any specified arguments. Essentially <code>.call()</code>. | |
</desc> | |
<argument name="arg" type="Object" optional="true" rest="true"> | |
<desc> | |
Zero to many arguments to pass to the parent widget's method. | |
</desc> | |
</argument> | |
<example> | |
<desc>Handle <code>title</code> option updates and call the parent widget's <code>_setOption()</code> to update the internal storage of the option.</desc> | |
<code><![CDATA[ | |
_setOption: function( key, value ) { | |
if ( key === "title" ) { | |
this.element.find( "h3" ).text( value ); | |
} | |
this._super( key, value ); | |
} | |
]]></code> | |
</example> | |
</method> | |
<method name="_superApply"> | |
<desc> | |
Invokes the method of the same name from the parent widget, with the array of arguments. Essentially <code>.apply()</code>. | |
</desc> | |
<argument name="arguments" type="Array"> | |
<desc>Array of arguments to pass to the parent method.</desc> | |
</argument> | |
<example> | |
<desc>Handle <code>title</code> option updates and call the parent widget's <code>_setOption()</code> to update the internal storage of the option.</desc> | |
<code><![CDATA[ | |
_setOption: function( key, value ) { | |
if ( key === "title" ) { | |
this.element.find( "h3" ).text( value ); | |
} | |
this._superApply( arguments ); | |
} | |
]]></code> | |
</example> | |
</method> | |
<method name="_delay" return="Number"> | |
<desc> | |
Invokes the provided function after a specified delay. Keeps <code>this</code> context correct. Essentially <code>setTimeout()</code>. | |
<p>Returns the timeout ID for use with <code>clearTimeout()</code>.</p> | |
</desc> | |
<argument name="fn"> | |
<desc>The function to invoke. Can also be the name of a method on the widget.</desc> | |
<type name="Function"/> | |
<type name="String"/> | |
</argument> | |
<argument name="delay" type="Number" optional="true"> | |
<desc>The number of milliseconds to wait before invoking the function. Defaults to <code>0</code>.</desc> | |
</argument> | |
<example> | |
<desc>Call the <code>_foo()</code> method on the widget after 100 milliseconds.</desc> | |
<code><![CDATA[ | |
this._delay( this._foo, 100 ); | |
]]></code> | |
</example> | |
</method> | |
<method name="_hoverable"> | |
<desc> | |
Sets up <code>element</code> to apply the <code>ui-state-hover</code> class on hover. | |
<p>The event handlers are automatically cleaned up on destroy.</p> | |
</desc> | |
<argument name="element" type="jQuery"> | |
<desc>The element(s) to apply the hoverable behavior to.</desc> | |
</argument> | |
<example> | |
<desc>Apply hoverable styling to all <code><div></code>s within the element on hover.</desc> | |
<code><![CDATA[ | |
this._hoverable( this.element.find( "div" ) ); | |
]]></code> | |
</example> | |
</method> | |
<method name="_focusable"> | |
<desc> | |
Sets up <code>element</code> to apply the <code>ui-state-focus</code> class on focus. | |
<p>The event handlers are automatically cleaned up on destroy.</p> | |
</desc> | |
<argument name="element" type="jQuery"> | |
<desc>The element(s) to apply the focusable behavior to.</desc> | |
</argument> | |
<example> | |
<desc>Apply focusable styling to a set of elements within the widget.</desc> | |
<code><![CDATA[ | |
this._focusable( this.element.find( ".my-items" ) ); | |
]]></code> | |
</example> | |
</method> | |
<method name="_trigger" return="Boolean"> | |
<desc> | |
Triggers an event and its associated callback. | |
<p>The option with the name equal to type is invoked as the callback.</p> | |
<p>The event name is the widget name + type.</p> | |
<p><em>Note: When providing data, you must provide all three parameters. If there is no event to pass along, just pass <code>null</code>.</em></p> | |
<p>If the default action is prevented, <code>false</code> will be returned, otherwise <code>true</code>. Preventing the default action happens when the handler returns <code>false</code> or calls <code>event.preventDefault()</code>.</p> | |
</desc> | |
<argument name="type" type="String"> | |
<desc>The <code>type</code> should match the name of a callback option. The full event type will be generated automatically.</desc> | |
</argument> | |
<argument name="event" type="Event" optional="true"> | |
<desc>The original event that caused this event to occur; useful for providing context to the listener.</desc> | |
</argument> | |
<argument name="data" type="Object" optional="true"> | |
<desc>A hash of data associated with the event.</desc> | |
</argument> | |
<example> | |
<desc>Trigger a <code>search</code> event whenever a key is pressed.</desc> | |
<code><![CDATA[ | |
this._on( this.element, { | |
keydown: function( event ) { | |
// Pass the original event so that the custom search event has | |
// useful information, such as keyCode | |
this._trigger( "search", event, { | |
// Pass additional information unique to this event | |
value: this.element.val() | |
}); | |
} | |
}); | |
]]></code> | |
</example> | |
</method> | |
<method name="_show"> | |
<desc> | |
Shows an element immediately, using built-in animation methods, or using custom effects. | |
See the <a href="#option-show">show</a> option for possible <code>option</code> values. | |
</desc> | |
<argument name="element" type="jQuery"> | |
<desc>The element(s) to show.</desc> | |
</argument> | |
<argument name="option" type="Object"> | |
<desc>The settings defining how to show the element.</desc> | |
</argument> | |
<argument name="callback" type="Function" optional="true"> | |
<desc>Callback to invoke after the element has been fully shown.</desc> | |
</argument> | |
<example> | |
<desc>Pass along the <code>show</code> option for custom animations.</desc> | |
<code><![CDATA[ | |
this._show( this.element, this.options.show, function() { | |
// Focus the element when it's fully visible. | |
this.focus(); | |
}]]></code> | |
</example> | |
</method> | |
<method name="_hide"> | |
<desc> | |
Hides an element immediately, using built-in animation methods, or using custom effects. | |
See the <a href="#option-hide">hide</a> option for possible <code>option</code> values. | |
</desc> | |
<argument name="element" type="jQuery"> | |
<desc>The element(s) to hide.</desc> | |
</argument> | |
<argument name="option" type="Object"> | |
<desc>The settings defining how to hide the element.</desc> | |
</argument> | |
<argument name="callback" type="Function" optional="true"> | |
<desc>Callback to invoke after the element has been fully hidden.</desc> | |
</argument> | |
<example> | |
<desc>Pass along the <code>hide</code> option for custom animations.</desc> | |
<code><![CDATA[ | |
this._hide( this.element, this.options.hide, function() { | |
// Remove the element from the DOM when it's fully hidden. | |
$( this ).remove(); | |
}); | |
]]></code> | |
</example> | |
</method> | |
</methods> | |
<events> | |
<xi:include href="../includes/widget-event-create.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/> | |
</events> | |
<category slug="utilities"/> | |
<category slug="widgets"/> | |
</entry> | |
</entries> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment