You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

89 lines
2.3 KiB

(function($K)
{
$K.add('module', 'number', {
init: function(app, context)
{
this.app = app;
// context
this.context = context;
this.$element = context.getElement();
},
// public
start: function()
{
this.$input = this.$element.find('input[type="number"]');
this.$btnUp = this.$element.find('.is-up');
this.$btnDown = this.$element.find('.is-down');
this._buildStep();
this._buildMin();
this._buildMax();
if (!this._isDisabled())
{
this.$btnUp.on('click.kube.number', this._increase.bind(this));
this.$btnDown.on('click.kube.number', this._decrease.bind(this));
}
},
stop: function()
{
this.$btnUp.off('.kube.number');
this.$btnDown.off('.kube.number');
},
// private
_buildStep: function()
{
var step = this.$input.attr('step');
this.step = (step) ? parseFloat(step) : 1;
},
_buildMin: function()
{
var min = this.$input.attr('min');
this.min = (min) ? parseFloat(min) : false;
},
_buildMax: function()
{
var max = this.$input.attr('max');
this.max = (max) ? parseFloat(max) : false;
},
_isDisabled: function()
{
return this.$input.attr('disabled');
},
_getValue: function()
{
var value = parseFloat(this.$input.val());
var min = (this.min === false) ? 0 : this.min;
return (isNaN(value)) ? min : value;
},
_increase: function(e)
{
if (e)
{
e.preventDefault();
e.stopPropagation();
}
var oldValue = this._getValue();
var newVal = (this.max !== false && oldValue >= this.max) ? oldValue : oldValue + this.step;
this.$input.val(newVal);
},
_decrease: function(e)
{
if (e)
{
e.preventDefault();
e.stopPropagation();
}
var oldValue = this._getValue();
var newVal = (this.min !== false && oldValue <= this.min) ? oldValue : oldValue - this.step;
this.$input.val(newVal);
}
});
})(Kube);