/* * jQuery UI Spinner @VERSION * * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about) * Dual licensed under the MIT or GPL Version 2 licenses. * http://jquery.org/license * * http://docs.jquery.com/UI/Spinner * * Depends: * jquery.ui.core.js * jquery.ui.widget.js */ (function($) { $.widget('ui.spinner', { defaultElement: "", options: { incremental: true, max: null, min: null, numberformat: null, page: 10, step: null, value: null }, _create: function() { this._draw(); this._markupOptions(); this._mousewheel(); this._aria(); }, _markupOptions: function() { var _this = this; $.each({ min: -Number.MAX_VALUE, max: Number.MAX_VALUE, step: 1 }, function(attr, defaultValue) { if (_this.options[attr] === null) { var value = _this.element.attr(attr); _this.options[attr] = typeof value == "string" && value.length > 0 ? _this._parse(value) : defaultValue; } }); this.value(this.options.value !== null ? this.options.value : this.element.val() || 0); }, _draw: function() { var self = this, options = self.options; var uiSpinner = this.uiSpinner = self.element .addClass('ui-spinner-input') .attr('autocomplete', 'off') .wrap(self._uiSpinnerHtml()) .parent() // add buttons .append(self._buttonHtml()) // add behaviours .hover(function() { if (!options.disabled) { $(this).addClass('ui-state-hover'); } self.hovered = true; }, function() { $(this).removeClass('ui-state-hover'); self.hovered = false; }); this.element .attr( "role", "spinbutton" ) .bind('keydown.spinner', function(event) { if (self.options.disabled) { return; } if (self._start(event)) { return self._keydown(event); } return true; }) .bind('keyup.spinner', function(event) { if (self.options.disabled) { return; } if (self.spinning) { self._stop(event); self._change(event); } }) .bind('focus.spinner', function() { uiSpinner.addClass('ui-state-active'); self.focused = true; }) .bind('blur.spinner', function(event) { self.value(self.element.val()); if (!self.hovered) { uiSpinner.removeClass('ui-state-active'); } self.focused = false; }); // button bindings this.buttons = uiSpinner.find('.ui-spinner-button') .attr("tabIndex", -1) .button() .removeClass("ui-corner-all") .bind('mousedown', function(event) { if (self.options.disabled) { return; } if (self._start(event) === false) { return false; } self._repeat(null, $(this).hasClass('ui-spinner-up') ? 1 : -1, event); }) .bind('mouseup', function(event) { if (self.options.disabled) { return; } if (self.spinning) { self._stop(event); self._change(event); } }) .bind("mouseenter", function() { if (self.options.disabled) { return; } // button will add ui-state-active if mouse was down while mouseleave and kept down if ($(this).hasClass("ui-state-active")) { if (self._start(event) === false) { return false; } self._repeat(null, $(this).hasClass('ui-spinner-up') ? 1 : -1, event); } }) .bind("mouseleave", function() { if (self.spinning) { self._stop(event); self._change(event); } }); // disable spinner if element was already disabled if (options.disabled) { this.disable(); } }, _keydown: function(event) { var o = this.options, KEYS = $.ui.keyCode; switch (event.keyCode) { case KEYS.UP: this._repeat(null, 1, event); return false; case KEYS.DOWN: this._repeat(null, -1, event); return false; case KEYS.PAGE_UP: this._repeat(null, this.options.page, event); return false; case KEYS.PAGE_DOWN: this._repeat(null, -this.options.page, event); return false; case KEYS.ENTER: this.value(this.element.val()); } return true; }, _mousewheel: function() { // need the delta normalization that mousewheel plugin provides if (!$.fn.mousewheel) { return; } var self = this; this.element.bind("mousewheel.spinner", function(event, delta) { if (self.options.disabled || !delta) { return; } if (!self.spinning && !self._start(event)) { return false; } $("