Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / resources / lib / jquery.ui / jquery.ui.button.js
blob53cf69922f8fa69cbfbb7d75e9a07e6d8ba6530d
1 /*!
2  * jQuery UI Button 1.9.2
3  * http://jqueryui.com
4  *
5  * Copyright 2012 jQuery Foundation and other contributors
6  * Released under the MIT license.
7  * http://jquery.org/license
8  *
9  * http://api.jqueryui.com/button/
10  *
11  * Depends:
12  *      jquery.ui.core.js
13  *      jquery.ui.widget.js
14  */
15 (function( $, undefined ) {
17 var lastActive, startXPos, startYPos, clickDragged,
18         baseClasses = "ui-button ui-widget ui-state-default ui-corner-all",
19         stateClasses = "ui-state-hover ui-state-active ",
20         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",
21         formResetHandler = function() {
22                 var buttons = $( this ).find( ":ui-button" );
23                 setTimeout(function() {
24                         buttons.button( "refresh" );
25                 }, 1 );
26         },
27         radioGroup = function( radio ) {
28                 var name = radio.name,
29                         form = radio.form,
30                         radios = $( [] );
31                 if ( name ) {
32                         if ( form ) {
33                                 radios = $( form ).find( "[name='" + name + "']" );
34                         } else {
35                                 radios = $( "[name='" + name + "']", radio.ownerDocument )
36                                         .filter(function() {
37                                                 return !this.form;
38                                         });
39                         }
40                 }
41                 return radios;
42         };
44 $.widget( "ui.button", {
45         version: "1.9.2",
46         defaultElement: "<button>",
47         options: {
48                 disabled: null,
49                 text: true,
50                 label: null,
51                 icons: {
52                         primary: null,
53                         secondary: null
54                 }
55         },
56         _create: function() {
57                 this.element.closest( "form" )
58                         .unbind( "reset" + this.eventNamespace )
59                         .bind( "reset" + this.eventNamespace, formResetHandler );
61                 if ( typeof this.options.disabled !== "boolean" ) {
62                         this.options.disabled = !!this.element.prop( "disabled" );
63                 } else {
64                         this.element.prop( "disabled", this.options.disabled );
65                 }
67                 this._determineButtonType();
68                 this.hasTitle = !!this.buttonElement.attr( "title" );
70                 var that = this,
71                         options = this.options,
72                         toggleButton = this.type === "checkbox" || this.type === "radio",
73                         activeClass = !toggleButton ? "ui-state-active" : "",
74                         focusClass = "ui-state-focus";
76                 if ( options.label === null ) {
77                         options.label = (this.type === "input" ? this.buttonElement.val() : this.buttonElement.html());
78                 }
80                 this._hoverable( this.buttonElement );
82                 this.buttonElement
83                         .addClass( baseClasses )
84                         .attr( "role", "button" )
85                         .bind( "mouseenter" + this.eventNamespace, function() {
86                                 if ( options.disabled ) {
87                                         return;
88                                 }
89                                 if ( this === lastActive ) {
90                                         $( this ).addClass( "ui-state-active" );
91                                 }
92                         })
93                         .bind( "mouseleave" + this.eventNamespace, function() {
94                                 if ( options.disabled ) {
95                                         return;
96                                 }
97                                 $( this ).removeClass( activeClass );
98                         })
99                         .bind( "click" + this.eventNamespace, function( event ) {
100                                 if ( options.disabled ) {
101                                         event.preventDefault();
102                                         event.stopImmediatePropagation();
103                                 }
104                         });
106                 this.element
107                         .bind( "focus" + this.eventNamespace, function() {
108                                 // no need to check disabled, focus won't be triggered anyway
109                                 that.buttonElement.addClass( focusClass );
110                         })
111                         .bind( "blur" + this.eventNamespace, function() {
112                                 that.buttonElement.removeClass( focusClass );
113                         });
115                 if ( toggleButton ) {
116                         this.element.bind( "change" + this.eventNamespace, function() {
117                                 if ( clickDragged ) {
118                                         return;
119                                 }
120                                 that.refresh();
121                         });
122                         // if mouse moves between mousedown and mouseup (drag) set clickDragged flag
123                         // prevents issue where button state changes but checkbox/radio checked state
124                         // does not in Firefox (see ticket #6970)
125                         this.buttonElement
126                                 .bind( "mousedown" + this.eventNamespace, function( event ) {
127                                         if ( options.disabled ) {
128                                                 return;
129                                         }
130                                         clickDragged = false;
131                                         startXPos = event.pageX;
132                                         startYPos = event.pageY;
133                                 })
134                                 .bind( "mouseup" + this.eventNamespace, function( event ) {
135                                         if ( options.disabled ) {
136                                                 return;
137                                         }
138                                         if ( startXPos !== event.pageX || startYPos !== event.pageY ) {
139                                                 clickDragged = true;
140                                         }
141                         });
142                 }
144                 if ( this.type === "checkbox" ) {
145                         this.buttonElement.bind( "click" + this.eventNamespace, function() {
146                                 if ( options.disabled || clickDragged ) {
147                                         return false;
148                                 }
149                                 $( this ).toggleClass( "ui-state-active" );
150                                 that.buttonElement.attr( "aria-pressed", that.element[0].checked );
151                         });
152                 } else if ( this.type === "radio" ) {
153                         this.buttonElement.bind( "click" + this.eventNamespace, function() {
154                                 if ( options.disabled || clickDragged ) {
155                                         return false;
156                                 }
157                                 $( this ).addClass( "ui-state-active" );
158                                 that.buttonElement.attr( "aria-pressed", "true" );
160                                 var radio = that.element[ 0 ];
161                                 radioGroup( radio )
162                                         .not( radio )
163                                         .map(function() {
164                                                 return $( this ).button( "widget" )[ 0 ];
165                                         })
166                                         .removeClass( "ui-state-active" )
167                                         .attr( "aria-pressed", "false" );
168                         });
169                 } else {
170                         this.buttonElement
171                                 .bind( "mousedown" + this.eventNamespace, function() {
172                                         if ( options.disabled ) {
173                                                 return false;
174                                         }
175                                         $( this ).addClass( "ui-state-active" );
176                                         lastActive = this;
177                                         that.document.one( "mouseup", function() {
178                                                 lastActive = null;
179                                         });
180                                 })
181                                 .bind( "mouseup" + this.eventNamespace, function() {
182                                         if ( options.disabled ) {
183                                                 return false;
184                                         }
185                                         $( this ).removeClass( "ui-state-active" );
186                                 })
187                                 .bind( "keydown" + this.eventNamespace, function(event) {
188                                         if ( options.disabled ) {
189                                                 return false;
190                                         }
191                                         if ( event.keyCode === $.ui.keyCode.SPACE || event.keyCode === $.ui.keyCode.ENTER ) {
192                                                 $( this ).addClass( "ui-state-active" );
193                                         }
194                                 })
195                                 .bind( "keyup" + this.eventNamespace, function() {
196                                         $( this ).removeClass( "ui-state-active" );
197                                 });
199                         if ( this.buttonElement.is("a") ) {
200                                 this.buttonElement.keyup(function(event) {
201                                         if ( event.keyCode === $.ui.keyCode.SPACE ) {
202                                                 // TODO pass through original event correctly (just as 2nd argument doesn't work)
203                                                 $( this ).click();
204                                         }
205                                 });
206                         }
207                 }
209                 // TODO: pull out $.Widget's handling for the disabled option into
210                 // $.Widget.prototype._setOptionDisabled so it's easy to proxy and can
211                 // be overridden by individual plugins
212                 this._setOption( "disabled", options.disabled );
213                 this._resetButton();
214         },
216         _determineButtonType: function() {
217                 var ancestor, labelSelector, checked;
219                 if ( this.element.is("[type=checkbox]") ) {
220                         this.type = "checkbox";
221                 } else if ( this.element.is("[type=radio]") ) {
222                         this.type = "radio";
223                 } else if ( this.element.is("input") ) {
224                         this.type = "input";
225                 } else {
226                         this.type = "button";
227                 }
229                 if ( this.type === "checkbox" || this.type === "radio" ) {
230                         // we don't search against the document in case the element
231                         // is disconnected from the DOM
232                         ancestor = this.element.parents().last();
233                         labelSelector = "label[for='" + this.element.attr("id") + "']";
234                         this.buttonElement = ancestor.find( labelSelector );
235                         if ( !this.buttonElement.length ) {
236                                 ancestor = ancestor.length ? ancestor.siblings() : this.element.siblings();
237                                 this.buttonElement = ancestor.filter( labelSelector );
238                                 if ( !this.buttonElement.length ) {
239                                         this.buttonElement = ancestor.find( labelSelector );
240                                 }
241                         }
242                         this.element.addClass( "ui-helper-hidden-accessible" );
244                         checked = this.element.is( ":checked" );
245                         if ( checked ) {
246                                 this.buttonElement.addClass( "ui-state-active" );
247                         }
248                         this.buttonElement.prop( "aria-pressed", checked );
249                 } else {
250                         this.buttonElement = this.element;
251                 }
252         },
254         widget: function() {
255                 return this.buttonElement;
256         },
258         _destroy: function() {
259                 this.element
260                         .removeClass( "ui-helper-hidden-accessible" );
261                 this.buttonElement
262                         .removeClass( baseClasses + " " + stateClasses + " " + typeClasses )
263                         .removeAttr( "role" )
264                         .removeAttr( "aria-pressed" )
265                         .html( this.buttonElement.find(".ui-button-text").html() );
267                 if ( !this.hasTitle ) {
268                         this.buttonElement.removeAttr( "title" );
269                 }
270         },
272         _setOption: function( key, value ) {
273                 this._super( key, value );
274                 if ( key === "disabled" ) {
275                         if ( value ) {
276                                 this.element.prop( "disabled", true );
277                         } else {
278                                 this.element.prop( "disabled", false );
279                         }
280                         return;
281                 }
282                 this._resetButton();
283         },
285         refresh: function() {
286                 //See #8237 & #8828
287                 var isDisabled = this.element.is( "input, button" ) ? this.element.is( ":disabled" ) : this.element.hasClass( "ui-button-disabled" );
289                 if ( isDisabled !== this.options.disabled ) {
290                         this._setOption( "disabled", isDisabled );
291                 }
292                 if ( this.type === "radio" ) {
293                         radioGroup( this.element[0] ).each(function() {
294                                 if ( $( this ).is( ":checked" ) ) {
295                                         $( this ).button( "widget" )
296                                                 .addClass( "ui-state-active" )
297                                                 .attr( "aria-pressed", "true" );
298                                 } else {
299                                         $( this ).button( "widget" )
300                                                 .removeClass( "ui-state-active" )
301                                                 .attr( "aria-pressed", "false" );
302                                 }
303                         });
304                 } else if ( this.type === "checkbox" ) {
305                         if ( this.element.is( ":checked" ) ) {
306                                 this.buttonElement
307                                         .addClass( "ui-state-active" )
308                                         .attr( "aria-pressed", "true" );
309                         } else {
310                                 this.buttonElement
311                                         .removeClass( "ui-state-active" )
312                                         .attr( "aria-pressed", "false" );
313                         }
314                 }
315         },
317         _resetButton: function() {
318                 if ( this.type === "input" ) {
319                         if ( this.options.label ) {
320                                 this.element.val( this.options.label );
321                         }
322                         return;
323                 }
324                 var buttonElement = this.buttonElement.removeClass( typeClasses ),
325                         buttonText = $( "<span></span>", this.document[0] )
326                                 .addClass( "ui-button-text" )
327                                 .html( this.options.label )
328                                 .appendTo( buttonElement.empty() )
329                                 .text(),
330                         icons = this.options.icons,
331                         multipleIcons = icons.primary && icons.secondary,
332                         buttonClasses = [];
334                 if ( icons.primary || icons.secondary ) {
335                         if ( this.options.text ) {
336                                 buttonClasses.push( "ui-button-text-icon" + ( multipleIcons ? "s" : ( icons.primary ? "-primary" : "-secondary" ) ) );
337                         }
339                         if ( icons.primary ) {
340                                 buttonElement.prepend( "<span class='ui-button-icon-primary ui-icon " + icons.primary + "'></span>" );
341                         }
343                         if ( icons.secondary ) {
344                                 buttonElement.append( "<span class='ui-button-icon-secondary ui-icon " + icons.secondary + "'></span>" );
345                         }
347                         if ( !this.options.text ) {
348                                 buttonClasses.push( multipleIcons ? "ui-button-icons-only" : "ui-button-icon-only" );
350                                 if ( !this.hasTitle ) {
351                                         buttonElement.attr( "title", $.trim( buttonText ) );
352                                 }
353                         }
354                 } else {
355                         buttonClasses.push( "ui-button-text-only" );
356                 }
357                 buttonElement.addClass( buttonClasses.join( " " ) );
358         }
361 $.widget( "ui.buttonset", {
362         version: "1.9.2",
363         options: {
364                 items: "button, input[type=button], input[type=submit], input[type=reset], input[type=checkbox], input[type=radio], a, :data(button)"
365         },
367         _create: function() {
368                 this.element.addClass( "ui-buttonset" );
369         },
371         _init: function() {
372                 this.refresh();
373         },
375         _setOption: function( key, value ) {
376                 if ( key === "disabled" ) {
377                         this.buttons.button( "option", key, value );
378                 }
380                 this._super( key, value );
381         },
383         refresh: function() {
384                 var rtl = this.element.css( "direction" ) === "rtl";
386                 this.buttons = this.element.find( this.options.items )
387                         .filter( ":ui-button" )
388                                 .button( "refresh" )
389                         .end()
390                         .not( ":ui-button" )
391                                 .button()
392                         .end()
393                         .map(function() {
394                                 return $( this ).button( "widget" )[ 0 ];
395                         })
396                                 .removeClass( "ui-corner-all ui-corner-left ui-corner-right" )
397                                 .filter( ":first" )
398                                         .addClass( rtl ? "ui-corner-right" : "ui-corner-left" )
399                                 .end()
400                                 .filter( ":last" )
401                                         .addClass( rtl ? "ui-corner-left" : "ui-corner-right" )
402                                 .end()
403                         .end();
404         },
406         _destroy: function() {
407                 this.element.removeClass( "ui-buttonset" );
408                 this.buttons
409                         .map(function() {
410                                 return $( this ).button( "widget" )[ 0 ];
411                         })
412                                 .removeClass( "ui-corner-left ui-corner-right" )
413                         .end()
414                         .button( "destroy" );
415         }
418 }( jQuery ) );