annotate jquery-ui/development-bundle/ui/jquery.ui.button.js @ 0:b2e4605f20b2

beta version
author dwinter
date Thu, 30 Jun 2011 09:07:49 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
1 /*
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
2 * jQuery UI Button 1.8.11
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
3 *
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
4 * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
5 * Dual licensed under the MIT or GPL Version 2 licenses.
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
6 * http://jquery.org/license
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
7 *
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
8 * http://docs.jquery.com/UI/Button
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
9 *
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
10 * Depends:
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
11 * jquery.ui.core.js
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
12 * jquery.ui.widget.js
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
13 */
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
14 (function( $, undefined ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
15
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
16 var lastActive,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
17 baseClasses = "ui-button ui-widget ui-state-default ui-corner-all",
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
18 stateClasses = "ui-state-hover ui-state-active ",
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
19 typeClasses = "ui-button-icons-only ui-button-icon-only ui-button-text-icons ui-button-text-icon-primary ui-button-text-icon-secondary ui-button-text-only",
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
20 formResetHandler = function( event ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
21 $( ":ui-button", event.target.form ).each(function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
22 var inst = $( this ).data( "button" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
23 setTimeout(function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
24 inst.refresh();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
25 }, 1 );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
26 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
27 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
28 radioGroup = function( radio ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
29 var name = radio.name,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
30 form = radio.form,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
31 radios = $( [] );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
32 if ( name ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
33 if ( form ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
34 radios = $( form ).find( "[name='" + name + "']" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
35 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
36 radios = $( "[name='" + name + "']", radio.ownerDocument )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
37 .filter(function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
38 return !this.form;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
39 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
40 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
41 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
42 return radios;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
43 };
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
44
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
45 $.widget( "ui.button", {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
46 options: {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
47 disabled: null,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
48 text: true,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
49 label: null,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
50 icons: {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
51 primary: null,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
52 secondary: null
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
53 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
54 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
55 _create: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
56 this.element.closest( "form" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
57 .unbind( "reset.button" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
58 .bind( "reset.button", formResetHandler );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
59
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
60 if ( typeof this.options.disabled !== "boolean" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
61 this.options.disabled = this.element.attr( "disabled" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
62 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
63
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
64 this._determineButtonType();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
65 this.hasTitle = !!this.buttonElement.attr( "title" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
66
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
67 var self = this,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
68 options = this.options,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
69 toggleButton = this.type === "checkbox" || this.type === "radio",
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
70 hoverClass = "ui-state-hover" + ( !toggleButton ? " ui-state-active" : "" ),
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
71 focusClass = "ui-state-focus";
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
72
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
73 if ( options.label === null ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
74 options.label = this.buttonElement.html();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
75 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
76
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
77 if ( this.element.is( ":disabled" ) ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
78 options.disabled = true;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
79 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
80
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
81 this.buttonElement
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
82 .addClass( baseClasses )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
83 .attr( "role", "button" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
84 .bind( "mouseenter.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
85 if ( options.disabled ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
86 return;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
87 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
88 $( this ).addClass( "ui-state-hover" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
89 if ( this === lastActive ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
90 $( this ).addClass( "ui-state-active" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
91 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
92 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
93 .bind( "mouseleave.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
94 if ( options.disabled ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
95 return;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
96 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
97 $( this ).removeClass( hoverClass );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
98 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
99 .bind( "focus.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
100 // no need to check disabled, focus won't be triggered anyway
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
101 $( this ).addClass( focusClass );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
102 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
103 .bind( "blur.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
104 $( this ).removeClass( focusClass );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
105 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
106
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
107 if ( toggleButton ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
108 this.element.bind( "change.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
109 self.refresh();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
110 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
111 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
112
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
113 if ( this.type === "checkbox" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
114 this.buttonElement.bind( "click.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
115 if ( options.disabled ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
116 return false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
117 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
118 $( this ).toggleClass( "ui-state-active" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
119 self.buttonElement.attr( "aria-pressed", self.element[0].checked );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
120 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
121 } else if ( this.type === "radio" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
122 this.buttonElement.bind( "click.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
123 if ( options.disabled ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
124 return false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
125 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
126 $( this ).addClass( "ui-state-active" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
127 self.buttonElement.attr( "aria-pressed", true );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
128
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
129 var radio = self.element[ 0 ];
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
130 radioGroup( radio )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
131 .not( radio )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
132 .map(function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
133 return $( this ).button( "widget" )[ 0 ];
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
134 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
135 .removeClass( "ui-state-active" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
136 .attr( "aria-pressed", false );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
137 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
138 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
139 this.buttonElement
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
140 .bind( "mousedown.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
141 if ( options.disabled ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
142 return false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
143 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
144 $( this ).addClass( "ui-state-active" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
145 lastActive = this;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
146 $( document ).one( "mouseup", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
147 lastActive = null;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
148 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
149 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
150 .bind( "mouseup.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
151 if ( options.disabled ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
152 return false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
153 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
154 $( this ).removeClass( "ui-state-active" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
155 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
156 .bind( "keydown.button", function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
157 if ( options.disabled ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
158 return false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
159 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
160 if ( event.keyCode == $.ui.keyCode.SPACE || event.keyCode == $.ui.keyCode.ENTER ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
161 $( this ).addClass( "ui-state-active" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
162 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
163 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
164 .bind( "keyup.button", function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
165 $( this ).removeClass( "ui-state-active" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
166 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
167
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
168 if ( this.buttonElement.is("a") ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
169 this.buttonElement.keyup(function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
170 if ( event.keyCode === $.ui.keyCode.SPACE ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
171 // TODO pass through original event correctly (just as 2nd argument doesn't work)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
172 $( this ).click();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
173 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
174 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
175 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
176 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
177
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
178 // TODO: pull out $.Widget's handling for the disabled option into
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
179 // $.Widget.prototype._setOptionDisabled so it's easy to proxy and can
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
180 // be overridden by individual plugins
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
181 this._setOption( "disabled", options.disabled );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
182 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
183
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
184 _determineButtonType: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
185
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
186 if ( this.element.is(":checkbox") ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
187 this.type = "checkbox";
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
188 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
189 if ( this.element.is(":radio") ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
190 this.type = "radio";
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
191 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
192 if ( this.element.is("input") ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
193 this.type = "input";
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
194 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
195 this.type = "button";
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
196 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
197 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
198 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
199
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
200 if ( this.type === "checkbox" || this.type === "radio" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
201 // we don't search against the document in case the element
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
202 // is disconnected from the DOM
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
203 var ancestor = this.element.parents().filter(":last"),
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
204 labelSelector = "label[for=" + this.element.attr("id") + "]";
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
205 this.buttonElement = ancestor.find( labelSelector );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
206 if ( !this.buttonElement.length ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
207 ancestor = ancestor.length ? ancestor.siblings() : this.element.siblings();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
208 this.buttonElement = ancestor.filter( labelSelector );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
209 if ( !this.buttonElement.length ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
210 this.buttonElement = ancestor.find( labelSelector );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
211 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
212 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
213 this.element.addClass( "ui-helper-hidden-accessible" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
214
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
215 var checked = this.element.is( ":checked" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
216 if ( checked ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
217 this.buttonElement.addClass( "ui-state-active" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
218 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
219 this.buttonElement.attr( "aria-pressed", checked );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
220 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
221 this.buttonElement = this.element;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
222 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
223 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
224
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
225 widget: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
226 return this.buttonElement;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
227 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
228
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
229 destroy: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
230 this.element
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
231 .removeClass( "ui-helper-hidden-accessible" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
232 this.buttonElement
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
233 .removeClass( baseClasses + " " + stateClasses + " " + typeClasses )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
234 .removeAttr( "role" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
235 .removeAttr( "aria-pressed" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
236 .html( this.buttonElement.find(".ui-button-text").html() );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
237
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
238 if ( !this.hasTitle ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
239 this.buttonElement.removeAttr( "title" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
240 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
241
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
242 $.Widget.prototype.destroy.call( this );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
243 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
244
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
245 _setOption: function( key, value ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
246 $.Widget.prototype._setOption.apply( this, arguments );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
247 if ( key === "disabled" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
248 if ( value ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
249 this.element.attr( "disabled", true );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
250 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
251 this.element.removeAttr( "disabled" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
252 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
253 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
254 this._resetButton();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
255 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
256
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
257 refresh: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
258 var isDisabled = this.element.is( ":disabled" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
259 if ( isDisabled !== this.options.disabled ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
260 this._setOption( "disabled", isDisabled );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
261 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
262 if ( this.type === "radio" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
263 radioGroup( this.element[0] ).each(function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
264 if ( $( this ).is( ":checked" ) ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
265 $( this ).button( "widget" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
266 .addClass( "ui-state-active" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
267 .attr( "aria-pressed", true );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
268 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
269 $( this ).button( "widget" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
270 .removeClass( "ui-state-active" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
271 .attr( "aria-pressed", false );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
272 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
273 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
274 } else if ( this.type === "checkbox" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
275 if ( this.element.is( ":checked" ) ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
276 this.buttonElement
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
277 .addClass( "ui-state-active" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
278 .attr( "aria-pressed", true );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
279 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
280 this.buttonElement
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
281 .removeClass( "ui-state-active" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
282 .attr( "aria-pressed", false );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
283 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
284 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
285 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
286
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
287 _resetButton: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
288 if ( this.type === "input" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
289 if ( this.options.label ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
290 this.element.val( this.options.label );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
291 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
292 return;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
293 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
294 var buttonElement = this.buttonElement.removeClass( typeClasses ),
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
295 buttonText = $( "<span></span>" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
296 .addClass( "ui-button-text" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
297 .html( this.options.label )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
298 .appendTo( buttonElement.empty() )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
299 .text(),
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
300 icons = this.options.icons,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
301 multipleIcons = icons.primary && icons.secondary,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
302 buttonClasses = [];
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
303
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
304 if ( icons.primary || icons.secondary ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
305 if ( this.options.text ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
306 buttonClasses.push( "ui-button-text-icon" + ( multipleIcons ? "s" : ( icons.primary ? "-primary" : "-secondary" ) ) );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
307 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
308
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
309 if ( icons.primary ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
310 buttonElement.prepend( "<span class='ui-button-icon-primary ui-icon " + icons.primary + "'></span>" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
311 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
312
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
313 if ( icons.secondary ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
314 buttonElement.append( "<span class='ui-button-icon-secondary ui-icon " + icons.secondary + "'></span>" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
315 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
316
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
317 if ( !this.options.text ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
318 buttonClasses.push( multipleIcons ? "ui-button-icons-only" : "ui-button-icon-only" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
319
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
320 if ( !this.hasTitle ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
321 buttonElement.attr( "title", buttonText );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
322 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
323 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
324 } else {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
325 buttonClasses.push( "ui-button-text-only" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
326 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
327 buttonElement.addClass( buttonClasses.join( " " ) );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
328 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
329 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
330
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
331 $.widget( "ui.buttonset", {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
332 options: {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
333 items: ":button, :submit, :reset, :checkbox, :radio, a, :data(button)"
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
334 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
335
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
336 _create: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
337 this.element.addClass( "ui-buttonset" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
338 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
339
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
340 _init: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
341 this.refresh();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
342 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
343
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
344 _setOption: function( key, value ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
345 if ( key === "disabled" ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
346 this.buttons.button( "option", key, value );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
347 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
348
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
349 $.Widget.prototype._setOption.apply( this, arguments );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
350 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
351
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
352 refresh: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
353 this.buttons = this.element.find( this.options.items )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
354 .filter( ":ui-button" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
355 .button( "refresh" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
356 .end()
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
357 .not( ":ui-button" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
358 .button()
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
359 .end()
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
360 .map(function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
361 return $( this ).button( "widget" )[ 0 ];
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
362 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
363 .removeClass( "ui-corner-all ui-corner-left ui-corner-right" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
364 .filter( ":first" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
365 .addClass( "ui-corner-left" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
366 .end()
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
367 .filter( ":last" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
368 .addClass( "ui-corner-right" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
369 .end()
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
370 .end();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
371 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
372
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
373 destroy: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
374 this.element.removeClass( "ui-buttonset" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
375 this.buttons
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
376 .map(function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
377 return $( this ).button( "widget" )[ 0 ];
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
378 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
379 .removeClass( "ui-corner-left ui-corner-right" )
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
380 .end()
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
381 .button( "destroy" );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
382
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
383 $.Widget.prototype.destroy.call( this );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
384 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
385 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
386
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
387 }( jQuery ) );