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-08-19 07:18:04 +00:00
|
|
|
$.widget('ui.spinner', {
|
2008-08-16 14:45:28 +00:00
|
|
|
_init: function() {
|
2008-08-26 07:28:14 +00:00
|
|
|
// terminate initialization if spinner already applied to current element
|
|
|
|
if($.data(this.element[0], 'spinner')) return;
|
2008-08-19 07:18:04 +00:00
|
|
|
|
|
|
|
// check for decimals in steppinng and set _decimals as internal (needs cleaning up)
|
2008-08-19 17:03:59 +00:00
|
|
|
this._decimals = 0;
|
2008-08-19 07:18:04 +00:00
|
|
|
if (this.options.stepping.toString().indexOf('.') != -1) {
|
|
|
|
var s = this.options.stepping.toString();
|
2008-08-19 15:52:35 +00:00
|
|
|
this._decimals = s.slice(s.indexOf('.')+1, s.length).length;
|
2008-08-19 07:18:04 +00:00
|
|
|
}
|
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
//Initialize needed constants
|
|
|
|
var self = this;
|
2008-08-19 07:18:04 +00:00
|
|
|
this.element
|
|
|
|
.addClass('ui-spinner-box')
|
|
|
|
.attr('autocomplete', 'off'); // switch off autocomplete in opera
|
|
|
|
|
|
|
|
this._setValue( isNaN(this._getValue()) ? this.options.start : this._getValue() );
|
2008-07-10 02:50:51 +00:00
|
|
|
|
|
|
|
this.element
|
2008-08-19 07:18:04 +00:00
|
|
|
.wrap('<div>')
|
2008-07-10 02:50:51 +00:00
|
|
|
.parent()
|
2008-08-19 07:18:04 +00:00
|
|
|
.addClass('ui-spinner')
|
2008-08-24 23:33:16 +00:00
|
|
|
.append('<button class="ui-spinner-up" type="button">▲</button>')
|
2008-08-19 07:18:04 +00:00
|
|
|
.find('.ui-spinner-up')
|
|
|
|
.bind('mousedown', function(e) {
|
|
|
|
$(this).addClass('ui-spinner-pressed');
|
|
|
|
if(!self.counter) self.counter = 1;
|
|
|
|
self._mousedown(100, '_up', e);
|
|
|
|
})
|
|
|
|
.bind('mouseup', function(e) {
|
|
|
|
$(this).removeClass('ui-spinner-pressed');
|
|
|
|
if(self.counter == 1) self._up(e);
|
|
|
|
self._mouseup(e);
|
|
|
|
})
|
|
|
|
.bind('mouseout', function(e) {
|
|
|
|
$(this).removeClass('ui-spinner-pressed');
|
|
|
|
self._mouseup(e);
|
|
|
|
})
|
|
|
|
// mousedown/mouseup capture first click, now handle second click
|
|
|
|
.bind('dblclick', function(e) {
|
|
|
|
$(this).removeClass('ui-spinner-pressed');
|
|
|
|
self._up(e);
|
|
|
|
})
|
|
|
|
.bind('keydown.spinner', function(e) {
|
2008-08-24 15:32:22 +00:00
|
|
|
var KEYS = $.keyCode;
|
2008-08-24 19:13:16 +00:00
|
|
|
if (e.keyCode == KEYS.SPACE || e.keyCode == KEYS.ENTER) {
|
2008-08-19 07:18:04 +00:00
|
|
|
$(this).addClass('ui-spinner-pressed');
|
|
|
|
if(!self.counter) self.counter = 1;
|
|
|
|
self._up.call(self, e);
|
|
|
|
} else if (e.keyCode == KEYS.DOWN || e.keyCode == KEYS.RIGHT) {
|
|
|
|
self.element.siblings('.ui-spinner-down').focus();
|
|
|
|
} else if (e.keyCode == KEYS.LEFT) {
|
|
|
|
self.element.focus();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.bind('keyup.spinner', function(e) {
|
|
|
|
$(this).removeClass('ui-spinner-pressed');
|
|
|
|
self.counter = 0;
|
|
|
|
self._propagate('change', e);
|
|
|
|
})
|
2008-07-10 02:50:51 +00:00
|
|
|
.end()
|
2008-08-24 23:33:16 +00:00
|
|
|
.append('<button class="ui-spinner-down" type="button">▼</button>')
|
2008-08-19 07:18:04 +00:00
|
|
|
.find('.ui-spinner-down')
|
|
|
|
.bind('mousedown', function(e) {
|
|
|
|
$(this).addClass('ui-spinner-pressed');
|
|
|
|
if(!self.counter) self.counter = 1;
|
|
|
|
self._mousedown(100, '_down', e);
|
|
|
|
})
|
|
|
|
.bind('mouseup', function(e) {
|
|
|
|
$(this).removeClass('ui-spinner-pressed');
|
|
|
|
if(self.counter == 1) self._down();
|
|
|
|
self._mouseup(e);
|
|
|
|
})
|
|
|
|
.bind('mouseout', function(e) {
|
|
|
|
$(this).removeClass('ui-spinner-pressed');
|
|
|
|
self._mouseup(e);
|
|
|
|
})
|
|
|
|
// mousedown/mouseup capture first click, now handle second click
|
|
|
|
.bind('dblclick', function(e) {
|
|
|
|
$(this).removeClass('ui-spinner-pressed');
|
|
|
|
self._down(e);
|
|
|
|
})
|
|
|
|
.bind('keydown.spinner', function(e) {
|
2008-08-24 15:32:22 +00:00
|
|
|
var KEYS = $.keyCode;
|
2008-08-24 19:13:16 +00:00
|
|
|
if (e.keyCode == KEYS.SPACE || e.keyCode == KEYS.ENTER) {
|
2008-08-19 07:18:04 +00:00
|
|
|
$(this).addClass('ui-spinner-pressed');
|
|
|
|
if(!self.counter) self.counter = 1;
|
|
|
|
self._down.call(self, e);
|
|
|
|
} else if (e.keyCode == KEYS.UP || e.keyCode == KEYS.LEFT) {
|
|
|
|
self.element.siblings('.ui-spinner-up').focus();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.bind('keyup.spinner', function(e) {
|
|
|
|
$(this).removeClass('ui-spinner-pressed');
|
|
|
|
self.counter = 0;
|
|
|
|
self._propagate('change', e);
|
|
|
|
})
|
|
|
|
.end();
|
2008-07-10 02:50:51 +00:00
|
|
|
|
2008-08-30 20:01:23 +00:00
|
|
|
// DataList: Set contraints for object length and step size.
|
|
|
|
// Manipulate height of spinner.
|
|
|
|
this._items = this.element.children().length;
|
|
|
|
if (this._items > 1) {
|
|
|
|
this.element
|
|
|
|
.addClass('ui-spinner-list')
|
|
|
|
.css('height', this.element.outerHeight()/this._items)
|
|
|
|
.children()
|
|
|
|
.addClass('ui-spinner-listitem')
|
|
|
|
.end()
|
|
|
|
.parent()
|
|
|
|
.css('height', this.element.outerHeight())
|
|
|
|
.end();
|
|
|
|
this.options.stepping = 1;
|
|
|
|
this.options.min = 0;
|
|
|
|
this.options.max = this._items-1;
|
2008-08-26 07:28:14 +00:00
|
|
|
}
|
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
this.element
|
2008-08-19 07:18:04 +00:00
|
|
|
.bind('keydown.spinner', function(e) {
|
2008-07-10 02:50:51 +00:00
|
|
|
if(!self.counter) self.counter = 1;
|
2008-08-19 07:18:04 +00:00
|
|
|
return self._keydown.call(self, e);
|
2008-07-10 02:50:51 +00:00
|
|
|
})
|
2008-08-19 07:18:04 +00:00
|
|
|
.bind('keyup.spinner', function(e) {
|
2008-07-10 02:50:51 +00:00
|
|
|
self.counter = 0;
|
2008-08-19 07:18:04 +00:00
|
|
|
self._propagate('change', e);
|
2008-07-10 02:50:51 +00:00
|
|
|
})
|
2008-08-19 07:18:04 +00:00
|
|
|
.bind('blur.spinner', function(e) {
|
2008-08-23 02:38:28 +00:00
|
|
|
self._cleanUp();
|
2008-08-19 07:18:04 +00:00
|
|
|
});
|
|
|
|
|
2008-08-10 07:28:44 +00:00
|
|
|
if ($.fn.mousewheel) {
|
2008-08-19 07:18:04 +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
|
|
|
},
|
2008-08-19 07:18:04 +00:00
|
|
|
|
2008-08-16 05:55:17 +00:00
|
|
|
_constrain: function() {
|
2008-08-19 07:18:04 +00:00
|
|
|
if(this.options.min != undefined && this._getValue() < this.options.min) this._setValue(this.options.min);
|
|
|
|
if(this.options.max != undefined && this._getValue() > this.options.max) this._setValue(this.options.max);
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_cleanUp: function() {
|
2008-08-19 07:18:04 +00:00
|
|
|
this._setValue(this._getValue());
|
2008-08-16 05:55:17 +00:00
|
|
|
this._constrain();
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
2008-08-19 07:18:04 +00:00
|
|
|
_spin: function(d, e) {
|
|
|
|
if (this.disabled) return;
|
|
|
|
|
|
|
|
if(isNaN(this._getValue())) this._setValue(this.options.start);
|
|
|
|
this._setValue(this._getValue() + (d == 'up' ? 1:-1) * (this.options.incremental && this.counter > 100 ? (this.counter > 200 ? 100 : 10) : 1) * this.options.stepping);
|
2008-08-26 07:28:14 +00:00
|
|
|
this._animate(d);
|
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-19 07:18:04 +00:00
|
|
|
this._propagate('spin', e);
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
2008-08-18 11:37:19 +00:00
|
|
|
_down: function(e) {
|
2008-08-19 07:18:04 +00:00
|
|
|
this._spin('down', e);
|
|
|
|
this._propagate('down', e);
|
2008-08-18 11:37:19 +00:00
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_up: function(e) {
|
2008-08-19 07:18:04 +00:00
|
|
|
this._spin('up', e);
|
|
|
|
this._propagate('up', e);
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
2008-08-19 07:18:04 +00:00
|
|
|
_mousedown: function(i, d, e) {
|
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() {
|
2008-08-19 07:18:04 +00:00
|
|
|
self[d](e);
|
|
|
|
if(self.counter > 20) self._mousedown(20, d, e);
|
2008-07-10 02:50:51 +00:00
|
|
|
}, i);
|
|
|
|
},
|
2008-08-19 07:18:04 +00:00
|
|
|
_mouseup: function(e) {
|
|
|
|
this.counter = 0;
|
|
|
|
if(this.timer) window.clearInterval(this.timer);
|
|
|
|
this.element[0].focus();
|
|
|
|
this._propagate('change', e);
|
|
|
|
},
|
2008-08-16 05:55:17 +00:00
|
|
|
_keydown: function(e) {
|
2008-08-24 15:32:22 +00:00
|
|
|
var KEYS = $.keyCode;
|
2008-08-19 07:18:04 +00:00
|
|
|
|
|
|
|
if(e.keyCode == KEYS.UP) this._up(e);
|
|
|
|
if(e.keyCode == KEYS.DOWN) this._down(e);
|
|
|
|
if(e.keyCode == KEYS.HOME) this._setValue(this.options.min || this.options.start); //Home key goes to min, if defined, else to start
|
|
|
|
if(e.keyCode == KEYS.END && this.options.max != undefined) this._setValue(this.options.max); //End key goes to maximum
|
|
|
|
return (e.keyCode == KEYS.TAB || e.keyCode == KEYS.BACKSPACE ||
|
|
|
|
e.keyCode == KEYS.LEFT || e.keyCode == KEYS.RIGHT || e.keyCode == KEYS.PERIOD ||
|
2008-08-21 15:31:11 +00:00
|
|
|
e.keyCode == KEYS.NUMPAD_DECIMAL || e.keyCode == KEYS.NUMPAD_SUBTRACT ||
|
|
|
|
(e.keyCode >= 96 && e.keyCode <= 105) || // add support for numeric keypad 0-9
|
2008-08-19 07:18:04 +00:00
|
|
|
(/[0-9\-\.]/).test(String.fromCharCode(e.keyCode))) ? true : false;
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
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-08-19 07:18:04 +00:00
|
|
|
_getValue: function() {
|
2008-08-26 07:28:14 +00:00
|
|
|
return parseFloat(this.element.val().replace(/[^0-9\-\.]/g, ''));
|
2008-08-19 07:18:04 +00:00
|
|
|
},
|
|
|
|
_setValue: function(newVal) {
|
|
|
|
if(isNaN(newVal)) newVal = this.options.start;
|
2008-08-26 07:28:14 +00:00
|
|
|
this.element.val(
|
2008-08-19 07:18:04 +00:00
|
|
|
this.options.currency ?
|
|
|
|
$.ui.spinner.format.currency(newVal, this.options.currency) :
|
2008-08-19 15:52:35 +00:00
|
|
|
$.ui.spinner.format.number(newVal, this._decimals)
|
2008-08-19 07:18:04 +00:00
|
|
|
);
|
|
|
|
},
|
2008-08-26 07:28:14 +00:00
|
|
|
_animate: function(d) {
|
2008-08-30 19:15:08 +00:00
|
|
|
if (this.element.hasClass('ui-spinner-list') && ((d == 'up' && this._getValue() <= this.options.max) || (d == 'down' && this._getValue() >= this.options.min)) ) {
|
2008-08-26 07:28:14 +00:00
|
|
|
this.element.animate({marginTop: '-' + this._getValue() * this.element.outerHeight() }, {
|
|
|
|
duration: 'fast',
|
|
|
|
queue: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2008-08-19 07:18:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
plugins: {},
|
2008-07-10 02:50:51 +00:00
|
|
|
ui: function(e) {
|
|
|
|
return {
|
|
|
|
options: this.options,
|
2008-08-19 07:18:04 +00:00
|
|
|
element: this.element,
|
|
|
|
value: this._getValue()
|
2008-07-10 02:50:51 +00:00
|
|
|
};
|
|
|
|
},
|
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()]);
|
2008-08-19 07:18:04 +00:00
|
|
|
return this.element.triggerHandler(n == 'spin' ? n : 'spin'+n, [e, this.ui()], this.options[n]);
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
|
|
|
destroy: function() {
|
|
|
|
if(!$.data(this.element[0], 'spinner')) return;
|
2008-08-19 16:31:42 +00:00
|
|
|
if ($.fn.mousewheel) {
|
|
|
|
this.element.unmousewheel();
|
|
|
|
}
|
2008-07-10 02:50:51 +00:00
|
|
|
this.element
|
2008-08-30 19:15:08 +00:00
|
|
|
.removeClass('ui-spinner-box ui-spinner-list')
|
2008-08-19 07:18:04 +00:00
|
|
|
.removeAttr('disabled')
|
2008-08-26 07:28:14 +00:00
|
|
|
.removeAttr('autocomplete')
|
2008-08-19 07:18:04 +00:00
|
|
|
.removeData('spinner')
|
|
|
|
.unbind('.spinner')
|
|
|
|
.siblings()
|
|
|
|
.remove()
|
|
|
|
.end()
|
2008-08-30 19:15:08 +00:00
|
|
|
.children()
|
|
|
|
.removeClass('ui-spinner-listitem')
|
|
|
|
.end()
|
2008-08-19 07:18:04 +00:00
|
|
|
.parent()
|
|
|
|
.removeClass('ui-spinner ui-spinner-disabled')
|
|
|
|
.before(this.element.clone())
|
|
|
|
.remove()
|
|
|
|
.end();
|
|
|
|
},
|
|
|
|
enable: function() {
|
|
|
|
this.element
|
|
|
|
.removeAttr('disabled')
|
|
|
|
.siblings()
|
|
|
|
.removeAttr('disabled')
|
|
|
|
.parent()
|
|
|
|
.removeClass('ui-spinner-disabled');
|
|
|
|
this.disabled = false;
|
|
|
|
},
|
|
|
|
disable: function() {
|
|
|
|
this.element
|
|
|
|
.attr('disabled', true)
|
|
|
|
.siblings()
|
|
|
|
.attr('disabled', true)
|
|
|
|
.parent()
|
|
|
|
.addClass('ui-spinner-disabled');
|
|
|
|
this.disabled = true;
|
2008-07-10 02:50:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2008-08-19 07:18:04 +00:00
|
|
|
$.extend($.ui.spinner, {
|
|
|
|
defaults: {
|
|
|
|
stepping: 1,
|
|
|
|
start: 0,
|
|
|
|
incremental: true,
|
|
|
|
currency: false
|
|
|
|
},
|
|
|
|
format: {
|
|
|
|
number: function(num, dec) {
|
|
|
|
return this.round(num, dec);
|
|
|
|
},
|
|
|
|
currency: function(num, sym) {
|
|
|
|
return (num !== Math.abs(num) ? '-' : '') + sym + this.round(Math.abs(num), 2);
|
|
|
|
},
|
|
|
|
round: function(num, dec) {
|
2008-08-19 17:03:59 +00:00
|
|
|
var s = Math.round(parseFloat(num)*Math.pow(10, dec)) / Math.pow(10, dec); // round off weird decimals
|
2008-08-19 07:18:04 +00:00
|
|
|
if (dec > 0) {
|
2008-08-19 17:03:59 +00:00
|
|
|
s = s + ((s.toString().indexOf('.') == -1) ? '.' : '') + '0000000001';
|
2008-08-19 07:18:04 +00:00
|
|
|
s = s.substr(0, s.indexOf('.')+1+dec);
|
|
|
|
} else {
|
2008-08-23 02:38:28 +00:00
|
|
|
s = Math.round(s);
|
2008-08-19 07:18:04 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
})(jQuery);
|