2008-07-10 02:50:51 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI Spinner
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 jQuery
|
|
|
|
* Dual licensed under the MIT (MIT-LICENSE.txt)
|
|
|
|
* and GPL (GPL-LICENSE.txt) licenses.
|
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Spinner
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* ui.core.js
|
|
|
|
*/
|
2008-07-09 11:09:40 +00:00
|
|
|
(function($) {
|
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
$.widget("ui.spinner", {
|
2008-08-16 14:45:28 +00:00
|
|
|
_init: function() {
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
//Initialize needed constants
|
|
|
|
var self = this;
|
|
|
|
this.element.addClass("ui-spinner");
|
|
|
|
this.element[0].value = this.options.start;
|
|
|
|
|
|
|
|
var pickerHeight = this.element.innerHeight() / 2 - parseInt(this.element.css("borderTopWidth"),10) - 2;
|
|
|
|
this.element
|
|
|
|
.wrap("<div>")
|
|
|
|
.parent()
|
|
|
|
.css({
|
|
|
|
position: this.element.css("position") == "static" ? "relative" : this.element.css("position"),
|
|
|
|
left: this.element.css("left"),
|
|
|
|
top: this.element.css("top"),
|
|
|
|
width: this.element.outerWidth(),
|
|
|
|
height: this.element.outerHeight()
|
2008-07-09 11:09:40 +00:00
|
|
|
})
|
2008-07-10 02:50:51 +00:00
|
|
|
.css("float", this.element.css("float"))
|
|
|
|
.prepend('<div class="ui-spinner-up"></div>')
|
|
|
|
.find("div.ui-spinner-up")
|
2008-08-16 05:55:17 +00:00
|
|
|
.bind("mousedown", function() { if(!self.counter) self.counter = 1; self._mousedown(100, "_up"); })
|
|
|
|
.bind("mouseup", function(e) { self.counter = 0; if(self.timer) window.clearInterval(self.timer); self.element[0].focus(); self._propagate("change", e); })
|
2008-07-10 02:50:51 +00:00
|
|
|
.css({ height: pickerHeight, top: parseInt(this.element.css("borderTopWidth"),10)+1, right: parseInt(this.element.css("borderRightWidth"),10)+1 })
|
|
|
|
.end()
|
|
|
|
.append('<div class="ui-spinner-down"></div>')
|
|
|
|
.find("div.ui-spinner-down")
|
2008-08-16 05:55:17 +00:00
|
|
|
.bind("mousedown", function() { if(!self.counter) self.counter = 1; self._mousedown(100, "_down"); })
|
|
|
|
.bind("mouseup", function(e) { self.counter = 0; if(self.timer) window.clearInterval(self.timer); self.element[0].focus(); self._propagate("change", e); })
|
2008-07-10 02:50:51 +00:00
|
|
|
.css({ height: pickerHeight, bottom: parseInt(this.element.css("borderBottomWidth"),10)+1, right: parseInt(this.element.css("borderRightWidth"),10)+1 })
|
|
|
|
.end()
|
|
|
|
;
|
|
|
|
|
|
|
|
this.element
|
|
|
|
.bind("keydown.spinner", function(e) {
|
|
|
|
if(!self.counter) self.counter = 1;
|
2008-08-16 05:55:17 +00:00
|
|
|
self._keydown.call(self, e);
|
2008-07-10 02:50:51 +00:00
|
|
|
})
|
|
|
|
.bind("keyup.spinner", function(e) {
|
|
|
|
self.counter = 0;
|
2008-08-16 05:55:17 +00:00
|
|
|
self._cleanUp();
|
|
|
|
self._propagate("change", e);
|
2008-07-10 02:50:51 +00:00
|
|
|
})
|
|
|
|
;
|
2008-08-10 07:28:44 +00:00
|
|
|
if ($.fn.mousewheel) {
|
2008-08-16 05:55:17 +00:00
|
|
|
this.element.mousewheel(function(e, delta) { self._mousewheel(e, delta); });
|
2008-08-10 07:28:44 +00:00
|
|
|
}
|
2008-07-10 02:50:51 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
plugins: {},
|
2008-08-16 05:55:17 +00:00
|
|
|
_constrain: function() {
|
2008-07-10 02:50:51 +00:00
|
|
|
if(this.options.min != undefined && this.element[0].value < this.options.min) this.element[0].value = this.options.min;
|
|
|
|
if(this.options.max != undefined && this.element[0].value > this.options.max) this.element[0].value = this.options.max;
|
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_cleanUp: function() {
|
2008-07-10 02:50:51 +00:00
|
|
|
this.element[0].value = this.element[0].value.replace(/[^0-9\-]/g, '');
|
2008-08-16 05:55:17 +00:00
|
|
|
this._constrain();
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_down: function(e) {
|
2008-07-10 02:50:51 +00:00
|
|
|
if(isNaN(parseInt(this.element[0].value,10))) this.element[0].value = this.options.start;
|
|
|
|
this.element[0].value -= (this.options.incremental && this.counter > 100 ? (this.counter > 200 ? 100 : 10) : 1) * this.options.stepping;
|
2008-08-16 05:55:17 +00:00
|
|
|
this._constrain();
|
2008-07-10 02:50:51 +00:00
|
|
|
if(this.counter) this.counter++;
|
2008-08-16 05:55:17 +00:00
|
|
|
this._propagate("spin", e);
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_up: function(e) {
|
2008-07-10 02:50:51 +00:00
|
|
|
if(isNaN(parseInt(this.element[0].value,10))) this.element[0].value = this.options.start;
|
|
|
|
this.element[0].value = parseFloat(this.element[0].value) + (this.options.incremental && this.counter > 100 ? (this.counter > 200 ? 100 : 10) : 1) * this.options.stepping;
|
2008-08-16 05:55:17 +00:00
|
|
|
this._constrain();
|
2008-07-10 02:50:51 +00:00
|
|
|
if(this.counter) this.counter++;
|
2008-08-16 05:55:17 +00:00
|
|
|
this._propagate("spin", e);
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_mousedown: function(i, d) {
|
2008-07-10 02:50:51 +00:00
|
|
|
var self = this;
|
|
|
|
i = i || 100;
|
|
|
|
if(this.timer) window.clearInterval(this.timer);
|
|
|
|
this.timer = window.setInterval(function() {
|
|
|
|
self[d]();
|
2008-08-16 05:55:17 +00:00
|
|
|
if(self.counter > 20) self._mousedown(20, d);
|
2008-07-10 02:50:51 +00:00
|
|
|
}, i);
|
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_keydown: function(e) {
|
|
|
|
if(e.keyCode == 38 || e.keyCode == 39) this._up(e);
|
|
|
|
if(e.keyCode == 40 || e.keyCode == 37) this._down(e);
|
2008-07-10 02:50:51 +00:00
|
|
|
if(e.keyCode == 36) this.element[0].value = this.options.min || this.options.start; //Home key goes to min, if defined, else to start
|
|
|
|
if(e.keyCode == 35 && this.options.max != undefined) this.element[0].value = this.options.max; //End key goes to maximum
|
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_mousewheel: function(e, delta) {
|
2008-08-10 07:28:44 +00:00
|
|
|
delta = ($.browser.opera ? -delta / Math.abs(delta) : delta);
|
2008-08-16 05:55:17 +00:00
|
|
|
delta > 0 ? this._up(e) : this._down(e);
|
2008-08-10 07:28:44 +00:00
|
|
|
e.preventDefault();
|
|
|
|
},
|
2008-07-10 02:50:51 +00:00
|
|
|
ui: function(e) {
|
|
|
|
return {
|
|
|
|
instance: this,
|
|
|
|
options: this.options,
|
|
|
|
element: this.element
|
|
|
|
};
|
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_propagate: function(n,e) {
|
2008-07-10 02:50:51 +00:00
|
|
|
$.ui.plugin.call(this, n, [e, this.ui()]);
|
|
|
|
return this.element.triggerHandler(n == "spin" ? n : "spin"+n, [e, this.ui()], this.options[n]);
|
|
|
|
},
|
|
|
|
destroy: function() {
|
|
|
|
if(!$.data(this.element[0], 'spinner')) return;
|
|
|
|
this.element
|
|
|
|
.removeClass("ui-spinner ui-spinner-disabled")
|
|
|
|
.removeData("spinner")
|
|
|
|
.unbind(".spinner");
|
2008-08-10 07:28:44 +00:00
|
|
|
if ($.fn.mousewheel) {
|
|
|
|
this.element.unmousewheel();
|
|
|
|
}
|
2008-07-10 02:50:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.ui.spinner.defaults = {
|
|
|
|
stepping: 1,
|
|
|
|
start: 0,
|
|
|
|
incremental: true
|
|
|
|
};
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
})(jQuery);
|