source: trunk/themes/default/js/ui/jquery.ui.spinner.js @ 28780

Last change on this file since 28780 was 28780, checked in by rvelices, 10 years ago

upgrade jquery ui from 1.10.1 to 1.10.4

File size: 12.2 KB
Line 
1/*!
2 * jQuery UI Spinner 1.10.4
3 * http://jqueryui.com
4 *
5 * Copyright 2014 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
8 *
9 * http://api.jqueryui.com/spinner/
10 *
11 * Depends:
12 *  jquery.ui.core.js
13 *  jquery.ui.widget.js
14 *  jquery.ui.button.js
15 */
16(function( $ ) {
17
18function modifier( fn ) {
19        return function() {
20                var previous = this.element.val();
21                fn.apply( this, arguments );
22                this._refresh();
23                if ( previous !== this.element.val() ) {
24                        this._trigger( "change" );
25                }
26        };
27}
28
29$.widget( "ui.spinner", {
30        version: "1.10.4",
31        defaultElement: "<input>",
32        widgetEventPrefix: "spin",
33        options: {
34                culture: null,
35                icons: {
36                        down: "ui-icon-triangle-1-s",
37                        up: "ui-icon-triangle-1-n"
38                },
39                incremental: true,
40                max: null,
41                min: null,
42                numberFormat: null,
43                page: 10,
44                step: 1,
45
46                change: null,
47                spin: null,
48                start: null,
49                stop: null
50        },
51
52        _create: function() {
53                // handle string values that need to be parsed
54                this._setOption( "max", this.options.max );
55                this._setOption( "min", this.options.min );
56                this._setOption( "step", this.options.step );
57
58                // Only format if there is a value, prevents the field from being marked
59                // as invalid in Firefox, see #9573.
60                if ( this.value() !== "" ) {
61                        // Format the value, but don't constrain.
62                        this._value( this.element.val(), true );
63                }
64
65                this._draw();
66                this._on( this._events );
67                this._refresh();
68
69                // turning off autocomplete prevents the browser from remembering the
70                // value when navigating through history, so we re-enable autocomplete
71                // if the page is unloaded before the widget is destroyed. #7790
72                this._on( this.window, {
73                        beforeunload: function() {
74                                this.element.removeAttr( "autocomplete" );
75                        }
76                });
77        },
78
79        _getCreateOptions: function() {
80                var options = {},
81                        element = this.element;
82
83                $.each( [ "min", "max", "step" ], function( i, option ) {
84                        var value = element.attr( option );
85                        if ( value !== undefined && value.length ) {
86                                options[ option ] = value;
87                        }
88                });
89
90                return options;
91        },
92
93        _events: {
94                keydown: function( event ) {
95                        if ( this._start( event ) && this._keydown( event ) ) {
96                                event.preventDefault();
97                        }
98                },
99                keyup: "_stop",
100                focus: function() {
101                        this.previous = this.element.val();
102                },
103                blur: function( event ) {
104                        if ( this.cancelBlur ) {
105                                delete this.cancelBlur;
106                                return;
107                        }
108
109                        this._stop();
110                        this._refresh();
111                        if ( this.previous !== this.element.val() ) {
112                                this._trigger( "change", event );
113                        }
114                },
115                mousewheel: function( event, delta ) {
116                        if ( !delta ) {
117                                return;
118                        }
119                        if ( !this.spinning && !this._start( event ) ) {
120                                return false;
121                        }
122
123                        this._spin( (delta > 0 ? 1 : -1) * this.options.step, event );
124                        clearTimeout( this.mousewheelTimer );
125                        this.mousewheelTimer = this._delay(function() {
126                                if ( this.spinning ) {
127                                        this._stop( event );
128                                }
129                        }, 100 );
130                        event.preventDefault();
131                },
132                "mousedown .ui-spinner-button": function( event ) {
133                        var previous;
134
135                        // We never want the buttons to have focus; whenever the user is
136                        // interacting with the spinner, the focus should be on the input.
137                        // If the input is focused then this.previous is properly set from
138                        // when the input first received focus. If the input is not focused
139                        // then we need to set this.previous based on the value before spinning.
140                        previous = this.element[0] === this.document[0].activeElement ?
141                                this.previous : this.element.val();
142                        function checkFocus() {
143                                var isActive = this.element[0] === this.document[0].activeElement;
144                                if ( !isActive ) {
145                                        this.element.focus();
146                                        this.previous = previous;
147                                        // support: IE
148                                        // IE sets focus asynchronously, so we need to check if focus
149                                        // moved off of the input because the user clicked on the button.
150                                        this._delay(function() {
151                                                this.previous = previous;
152                                        });
153                                }
154                        }
155
156                        // ensure focus is on (or stays on) the text field
157                        event.preventDefault();
158                        checkFocus.call( this );
159
160                        // support: IE
161                        // IE doesn't prevent moving focus even with event.preventDefault()
162                        // so we set a flag to know when we should ignore the blur event
163                        // and check (again) if focus moved off of the input.
164                        this.cancelBlur = true;
165                        this._delay(function() {
166                                delete this.cancelBlur;
167                                checkFocus.call( this );
168                        });
169
170                        if ( this._start( event ) === false ) {
171                                return;
172                        }
173
174                        this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
175                },
176                "mouseup .ui-spinner-button": "_stop",
177                "mouseenter .ui-spinner-button": function( event ) {
178                        // button will add ui-state-active if mouse was down while mouseleave and kept down
179                        if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
180                                return;
181                        }
182
183                        if ( this._start( event ) === false ) {
184                                return false;
185                        }
186                        this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
187                },
188                // TODO: do we really want to consider this a stop?
189                // shouldn't we just stop the repeater and wait until mouseup before
190                // we trigger the stop event?
191                "mouseleave .ui-spinner-button": "_stop"
192        },
193
194        _draw: function() {
195                var uiSpinner = this.uiSpinner = this.element
196                        .addClass( "ui-spinner-input" )
197                        .attr( "autocomplete", "off" )
198                        .wrap( this._uiSpinnerHtml() )
199                        .parent()
200                                // add buttons
201                                .append( this._buttonHtml() );
202
203                this.element.attr( "role", "spinbutton" );
204
205                // button bindings
206                this.buttons = uiSpinner.find( ".ui-spinner-button" )
207                        .attr( "tabIndex", -1 )
208                        .button()
209                        .removeClass( "ui-corner-all" );
210
211                // IE 6 doesn't understand height: 50% for the buttons
212                // unless the wrapper has an explicit height
213                if ( this.buttons.height() > Math.ceil( uiSpinner.height() * 0.5 ) &&
214                                uiSpinner.height() > 0 ) {
215                        uiSpinner.height( uiSpinner.height() );
216                }
217
218                // disable spinner if element was already disabled
219                if ( this.options.disabled ) {
220                        this.disable();
221                }
222        },
223
224        _keydown: function( event ) {
225                var options = this.options,
226                        keyCode = $.ui.keyCode;
227
228                switch ( event.keyCode ) {
229                case keyCode.UP:
230                        this._repeat( null, 1, event );
231                        return true;
232                case keyCode.DOWN:
233                        this._repeat( null, -1, event );
234                        return true;
235                case keyCode.PAGE_UP:
236                        this._repeat( null, options.page, event );
237                        return true;
238                case keyCode.PAGE_DOWN:
239                        this._repeat( null, -options.page, event );
240                        return true;
241                }
242
243                return false;
244        },
245
246        _uiSpinnerHtml: function() {
247                return "<span class='ui-spinner ui-widget ui-widget-content ui-corner-all'></span>";
248        },
249
250        _buttonHtml: function() {
251                return "" +
252                        "<a class='ui-spinner-button ui-spinner-up ui-corner-tr'>" +
253                                "<span class='ui-icon " + this.options.icons.up + "'>&#9650;</span>" +
254                        "</a>" +
255                        "<a class='ui-spinner-button ui-spinner-down ui-corner-br'>" +
256                                "<span class='ui-icon " + this.options.icons.down + "'>&#9660;</span>" +
257                        "</a>";
258        },
259
260        _start: function( event ) {
261                if ( !this.spinning && this._trigger( "start", event ) === false ) {
262                        return false;
263                }
264
265                if ( !this.counter ) {
266                        this.counter = 1;
267                }
268                this.spinning = true;
269                return true;
270        },
271
272        _repeat: function( i, steps, event ) {
273                i = i || 500;
274
275                clearTimeout( this.timer );
276                this.timer = this._delay(function() {
277                        this._repeat( 40, steps, event );
278                }, i );
279
280                this._spin( steps * this.options.step, event );
281        },
282
283        _spin: function( step, event ) {
284                var value = this.value() || 0;
285
286                if ( !this.counter ) {
287                        this.counter = 1;
288                }
289
290                value = this._adjustValue( value + step * this._increment( this.counter ) );
291
292                if ( !this.spinning || this._trigger( "spin", event, { value: value } ) !== false) {
293                        this._value( value );
294                        this.counter++;
295                }
296        },
297
298        _increment: function( i ) {
299                var incremental = this.options.incremental;
300
301                if ( incremental ) {
302                        return $.isFunction( incremental ) ?
303                                incremental( i ) :
304                                Math.floor( i*i*i/50000 - i*i/500 + 17*i/200 + 1 );
305                }
306
307                return 1;
308        },
309
310        _precision: function() {
311                var precision = this._precisionOf( this.options.step );
312                if ( this.options.min !== null ) {
313                        precision = Math.max( precision, this._precisionOf( this.options.min ) );
314                }
315                return precision;
316        },
317
318        _precisionOf: function( num ) {
319                var str = num.toString(),
320                        decimal = str.indexOf( "." );
321                return decimal === -1 ? 0 : str.length - decimal - 1;
322        },
323
324        _adjustValue: function( value ) {
325                var base, aboveMin,
326                        options = this.options;
327
328                // make sure we're at a valid step
329                // - find out where we are relative to the base (min or 0)
330                base = options.min !== null ? options.min : 0;
331                aboveMin = value - base;
332                // - round to the nearest step
333                aboveMin = Math.round(aboveMin / options.step) * options.step;
334                // - rounding is based on 0, so adjust back to our base
335                value = base + aboveMin;
336
337                // fix precision from bad JS floating point math
338                value = parseFloat( value.toFixed( this._precision() ) );
339
340                // clamp the value
341                if ( options.max !== null && value > options.max) {
342                        return options.max;
343                }
344                if ( options.min !== null && value < options.min ) {
345                        return options.min;
346                }
347
348                return value;
349        },
350
351        _stop: function( event ) {
352                if ( !this.spinning ) {
353                        return;
354                }
355
356                clearTimeout( this.timer );
357                clearTimeout( this.mousewheelTimer );
358                this.counter = 0;
359                this.spinning = false;
360                this._trigger( "stop", event );
361        },
362
363        _setOption: function( key, value ) {
364                if ( key === "culture" || key === "numberFormat" ) {
365                        var prevValue = this._parse( this.element.val() );
366                        this.options[ key ] = value;
367                        this.element.val( this._format( prevValue ) );
368                        return;
369                }
370
371                if ( key === "max" || key === "min" || key === "step" ) {
372                        if ( typeof value === "string" ) {
373                                value = this._parse( value );
374                        }
375                }
376                if ( key === "icons" ) {
377                        this.buttons.first().find( ".ui-icon" )
378                                .removeClass( this.options.icons.up )
379                                .addClass( value.up );
380                        this.buttons.last().find( ".ui-icon" )
381                                .removeClass( this.options.icons.down )
382                                .addClass( value.down );
383                }
384
385                this._super( key, value );
386
387                if ( key === "disabled" ) {
388                        if ( value ) {
389                                this.element.prop( "disabled", true );
390                                this.buttons.button( "disable" );
391                        } else {
392                                this.element.prop( "disabled", false );
393                                this.buttons.button( "enable" );
394                        }
395                }
396        },
397
398        _setOptions: modifier(function( options ) {
399                this._super( options );
400                this._value( this.element.val() );
401        }),
402
403        _parse: function( val ) {
404                if ( typeof val === "string" && val !== "" ) {
405                        val = window.Globalize && this.options.numberFormat ?
406                                Globalize.parseFloat( val, 10, this.options.culture ) : +val;
407                }
408                return val === "" || isNaN( val ) ? null : val;
409        },
410
411        _format: function( value ) {
412                if ( value === "" ) {
413                        return "";
414                }
415                return window.Globalize && this.options.numberFormat ?
416                        Globalize.format( value, this.options.numberFormat, this.options.culture ) :
417                        value;
418        },
419
420        _refresh: function() {
421                this.element.attr({
422                        "aria-valuemin": this.options.min,
423                        "aria-valuemax": this.options.max,
424                        // TODO: what should we do with values that can't be parsed?
425                        "aria-valuenow": this._parse( this.element.val() )
426                });
427        },
428
429        // update the value without triggering change
430        _value: function( value, allowAny ) {
431                var parsed;
432                if ( value !== "" ) {
433                        parsed = this._parse( value );
434                        if ( parsed !== null ) {
435                                if ( !allowAny ) {
436                                        parsed = this._adjustValue( parsed );
437                                }
438                                value = this._format( parsed );
439                        }
440                }
441                this.element.val( value );
442                this._refresh();
443        },
444
445        _destroy: function() {
446                this.element
447                        .removeClass( "ui-spinner-input" )
448                        .prop( "disabled", false )
449                        .removeAttr( "autocomplete" )
450                        .removeAttr( "role" )
451                        .removeAttr( "aria-valuemin" )
452                        .removeAttr( "aria-valuemax" )
453                        .removeAttr( "aria-valuenow" );
454                this.uiSpinner.replaceWith( this.element );
455        },
456
457        stepUp: modifier(function( steps ) {
458                this._stepUp( steps );
459        }),
460        _stepUp: function( steps ) {
461                if ( this._start() ) {
462                        this._spin( (steps || 1) * this.options.step );
463                        this._stop();
464                }
465        },
466
467        stepDown: modifier(function( steps ) {
468                this._stepDown( steps );
469        }),
470        _stepDown: function( steps ) {
471                if ( this._start() ) {
472                        this._spin( (steps || 1) * -this.options.step );
473                        this._stop();
474                }
475        },
476
477        pageUp: modifier(function( pages ) {
478                this._stepUp( (pages || 1) * this.options.page );
479        }),
480
481        pageDown: modifier(function( pages ) {
482                this._stepDown( (pages || 1) * this.options.page );
483        }),
484
485        value: function( newVal ) {
486                if ( !arguments.length ) {
487                        return this._parse( this.element.val() );
488                }
489                modifier( this._value ).call( this, newVal );
490        },
491
492        widget: function() {
493                return this.uiSpinner;
494        }
495});
496
497}( jQuery ) );
Note: See TracBrowser for help on using the repository browser.