Slider: Some more refactoring and cleanup

This commit is contained in:
jzaefferer 2010-10-26 12:44:32 +02:00
parent 134205840b
commit f667bb3c8d
2 changed files with 106 additions and 107 deletions

View File

@ -35,7 +35,7 @@ test("keydown UP on input, increases value not greater than max", function() {
} }
equals(el.val(), 100); equals(el.val(), 100);
el.val(50); el.spinner("value", 50);
simulateKeyDownUp(el, $.ui.keyCode.UP); simulateKeyDownUp(el, $.ui.keyCode.UP);
equals(el.val(), 60); equals(el.val(), 60);
}); });
@ -55,7 +55,7 @@ test("keydown DOWN on input, decreases value not less than min", function() {
} }
equals(el.val(), -100); equals(el.val(), -100);
el.val(50); el.spinner("value", 50);
simulateKeyDownUp(el, $.ui.keyCode.DOWN); simulateKeyDownUp(el, $.ui.keyCode.DOWN);
equals(el.val(), 40); equals(el.val(), 40);
}); });
@ -75,7 +75,7 @@ test("keydown PGUP on input, increases value not greater than max", function() {
} }
equal(el.val(), 500); equal(el.val(), 500);
el.val(0); el.spinner("value", 0);
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP); simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
equals(el.val(), 100); equals(el.val(), 100);
}); });
@ -95,7 +95,7 @@ test("keydown PGDN on input, decreases value not less than min", function() {
} }
equals(el.val(), -500); equals(el.val(), -500);
el.val(0); el.spinner("value", 0);
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN); simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
equals(el.val(), -100); equals(el.val(), -100);
}); });
@ -110,11 +110,11 @@ test("mouse click on buttons", function() {
$(".ui-spinner-down").trigger("mousedown").trigger("mouseup"); $(".ui-spinner-down").trigger("mousedown").trigger("mouseup");
equals(el.val(), --val, "mouse click to down"); equals(el.val(), --val, "mouse click to down");
el.val(50); el.spinner("value", 50);
$(".ui-spinner-up").trigger("mousedown").trigger("mouseup"); $(".ui-spinner-up").trigger("mousedown").trigger("mouseup");
equals(el.val(), 51); equals(el.val(), 51);
el.val(50); el.spinner("value", 50);
$(".ui-spinner-down").trigger("mousedown").trigger("mouseup"); $(".ui-spinner-down").trigger("mousedown").trigger("mouseup");
equals(el.val(), 49); equals(el.val(), 49);
}); });

View File

@ -14,10 +14,7 @@
(function($) { (function($) {
// shortcut constants // shortcut constants
var hover = 'ui-state-hover', var pageModifier = 10;
active = 'ui-state-active',
namespace = '.spinner',
pageModifier = 10;
$.widget('ui.spinner', { $.widget('ui.spinner', {
options: { options: {
@ -49,6 +46,7 @@ $.widget('ui.spinner', {
this._mousewheel(); this._mousewheel();
this._aria(); this._aria();
}, },
_draw: function() { _draw: function() {
var self = this, var self = this,
options = self.options; options = self.options;
@ -63,11 +61,11 @@ $.widget('ui.spinner', {
// add behaviours // add behaviours
.hover(function() { .hover(function() {
if (!options.disabled) { if (!options.disabled) {
$(this).addClass(hover); $(this).addClass('ui-state-hover');
} }
self.hovered = true; self.hovered = true;
}, function() { }, function() {
$(this).removeClass(hover); $(this).removeClass('ui-state-hover');
self.hovered = false; self.hovered = false;
}); });
@ -78,7 +76,7 @@ $.widget('ui.spinner', {
} }
this.element this.element
.bind('keydown'+namespace, function(event) { .bind('keydown.spinner', function(event) {
if (self.options.disabled) { if (self.options.disabled) {
return; return;
} }
@ -87,7 +85,7 @@ $.widget('ui.spinner', {
} }
return true; return true;
}) })
.bind('keyup'+namespace, function(event) { .bind('keyup.spinner', function(event) {
if (self.options.disabled) { if (self.options.disabled) {
return; return;
} }
@ -96,14 +94,14 @@ $.widget('ui.spinner', {
self._change(event); self._change(event);
} }
}) })
.bind('focus'+namespace, function() { .bind('focus.spinner', function() {
uiSpinner.addClass(active); uiSpinner.addClass('ui-state-active');
self.focused = true; self.focused = true;
}) })
.bind('blur'+namespace, function(event) { .bind('blur.spinner', function(event) {
self.value(self.element.val()); self.value(self.element.val());
if (!self.hovered) { if (!self.hovered) {
uiSpinner.removeClass(active); uiSpinner.removeClass('ui-state-active');
} }
self.focused = false; self.focused = false;
}); });
@ -163,66 +161,7 @@ $.widget('ui.spinner', {
this.disable(); this.disable();
} }
}, },
_uiSpinnerHtml: function() {
return '<div role="spinbutton" class="ui-spinner ui-state-default ui-widget ui-widget-content ui-corner-all"></div>';
},
_buttonHtml: function() {
return '<a class="ui-spinner-button ui-spinner-up ui-corner-tr"><span class="ui-icon ui-icon-triangle-1-n">&#9650;</span></a>' +
'<a class="ui-spinner-button ui-spinner-down ui-corner-br"><span class="ui-icon ui-icon-triangle-1-s">&#9660;</span></a>';
},
_start: function(event) {
if (!this.spinning && this._trigger('start', event, { value: this.value()}) !== false) {
if (!this.counter) {
this.counter = 1;
}
this.spinning = true;
return true;
}
return false;
},
_spin: function(step, event) {
if (!this.counter) {
this.counter = 1;
}
var newVal = this.value() + step * (this.options.incremental && this.counter > 100
? this.counter > 200
? 100
: 10
: 1);
// cancelable
if (this._trigger('spin', event, { value: newVal }) !== false) {
this.value(newVal);
this.counter++;
}
},
_stop: function(event) {
this.counter = 0;
if (this.timer) {
window.clearTimeout(this.timer);
}
this.element[0].focus();
this.spinning = false;
this._trigger('stop', event);
},
_change: function(event) {
this._trigger('change', event);
},
_repeat: function(i, steps, event) {
var self = this;
i = i || 100;
if (this.timer) {
window.clearTimeout(this.timer);
}
this.timer = window.setTimeout(function() {
self._repeat(self.options.incremental && self.counter > 20 ? 20 : i, steps, event);
}, i);
self._spin(steps*self.options.step, event);
},
_keydown: function(event) { _keydown: function(event) {
var o = this.options, var o = this.options,
KEYS = $.ui.keyCode; KEYS = $.ui.keyCode;
@ -247,9 +186,10 @@ $.widget('ui.spinner', {
return true; return true;
}, },
_mousewheel: function() { _mousewheel: function() {
var self = this; var self = this;
this.element.bind("mousewheel", function(event, delta) { this.element.bind("mousewheel.spinner", function(event, delta) {
if (self.options.disabled) { if (self.options.disabled) {
return; return;
} }
@ -270,22 +210,75 @@ $.widget('ui.spinner', {
event.preventDefault(); event.preventDefault();
}); });
}, },
value: function(newVal) {
if (!arguments.length) { _uiSpinnerHtml: function() {
return this._parse(this.element.val()); return '<div role="spinbutton" class="ui-spinner ui-state-default ui-widget ui-widget-content ui-corner-all"></div>';
}
this._setOption('value', newVal);
}, },
_getData: function(key) {
switch (key) { _buttonHtml: function() {
case 'min': return '<a class="ui-spinner-button ui-spinner-up ui-corner-tr"><span class="ui-icon ui-icon-triangle-1-n">&#9650;</span></a>' +
case 'max': '<a class="ui-spinner-button ui-spinner-down ui-corner-br"><span class="ui-icon ui-icon-triangle-1-s">&#9660;</span></a>';
case 'step':
return this['_'+key]();
break;
}
return this.options[key];
}, },
_start: function(event) {
if (!this.spinning && this._trigger('start', event, { value: this.options.value}) !== false) {
if (!this.counter) {
this.counter = 1;
}
this.spinning = true;
return true;
}
return false;
},
// TODO tune repeat behaviour, see http://jsbin.com/aruki4/2 for reference
_repeat: function(i, steps, event) {
var self = this;
i = i || 100;
if (this.timer) {
window.clearTimeout(this.timer);
}
this.timer = window.setTimeout(function() {
self._repeat(self.options.incremental && self.counter > 20 ? 20 : i, steps, event);
}, i);
self._spin(steps*self.options.step, event);
},
_spin: function(step, event) {
if (!this.counter) {
this.counter = 1;
}
var newVal = this.options.value + step * (this.options.incremental && this.counter > 100
? this.counter > 200
? 100
: 10
: 1);
// cancelable
if (this._trigger('spin', event, { value: newVal }) !== false) {
this.value(newVal);
this.counter++;
}
},
_stop: function(event) {
this.counter = 0;
if (this.timer) {
window.clearTimeout(this.timer);
}
this.element[0].focus();
this.spinning = false;
this._trigger('stop', event);
},
_change: function(event) {
this._trigger('change', event);
},
_setOption: function(key, value) { _setOption: function(key, value) {
if (key == 'value') { if (key == 'value') {
value = this._parse(value); value = this._parse(value);
@ -299,31 +292,24 @@ $.widget('ui.spinner', {
$.Widget.prototype._setOption.call( this, key, value ); $.Widget.prototype._setOption.call( this, key, value );
this._afterSetData(key, value); this._afterSetData(key, value);
}, },
_afterSetData: function(key, value) { _afterSetData: function(key, value) {
switch(key) { switch(key) {
case 'value':
this._format(this.options.value);
case 'max': case 'max':
case 'min': case 'min':
case 'step': case 'step':
if (value != null) {
// write attributes back to element if original exist
if (this.element.attr(key)) {
this.element.attr(key, value);
}
}
this._aria(); this._aria();
break;
case 'value':
this._format(this._parse(this.options.value));
this._aria();
break;
} }
}, },
_aria: function() { _aria: function() {
// TODO remove check, as soon as this method doesn't get called anymore before uiSpinner is initalized // TODO remove check, as soon as this method doesn't get called anymore before uiSpinner is initalized
this.uiSpinner && this.uiSpinner this.uiSpinner && this.uiSpinner
.attr('aria-valuemin', this.options.min) .attr('aria-valuemin', this.options.min)
.attr('aria-valuemax', this.options.max) .attr('aria-valuemax', this.options.max)
.attr('aria-valuenow', this.value()); .attr('aria-valuenow', this.options.value);
}, },
_parse: function(val) { _parse: function(val) {
@ -337,9 +323,9 @@ $.widget('ui.spinner', {
} }
val = window.Globalization && this.options.numberformat ? Globalization.parseFloat(val) : +val; val = window.Globalization && this.options.numberformat ? Globalization.parseFloat(val) : +val;
} }
console.log("input", input, "parsed", val)
return isNaN(val) ? null : val; return isNaN(val) ? null : val;
}, },
_format: function(num) { _format: function(num) {
this.element.val( window.Globalization && this.options.numberformat ? Globalization.format(num, this.options.numberformat) : num ); this.element.val( window.Globalization && this.options.numberformat ? Globalization.format(num, this.options.numberformat) : num );
}, },
@ -354,10 +340,11 @@ $.widget('ui.spinner', {
.removeAttr('disabled') .removeAttr('disabled')
.removeAttr('autocomplete') .removeAttr('autocomplete')
.removeData('spinner') .removeData('spinner')
.unbind(namespace); .unbind(".spinner");
this.uiSpinner.replaceWith(this.element); this.uiSpinner.replaceWith(this.element);
}, },
enable: function() { enable: function() {
this.element this.element
.removeAttr('disabled') .removeAttr('disabled')
@ -366,6 +353,7 @@ $.widget('ui.spinner', {
this.options.disabled = false; this.options.disabled = false;
this.buttons.button("enable"); this.buttons.button("enable");
}, },
disable: function() { disable: function() {
this.element this.element
.attr('disabled', true) .attr('disabled', true)
@ -374,21 +362,32 @@ $.widget('ui.spinner', {
this.options.disabled = true; this.options.disabled = true;
this.buttons.button("disable"); this.buttons.button("disable");
}, },
stepUp: function(steps) { stepUp: function(steps) {
this._spin((steps || 1) * this.options.step, null); this._spin((steps || 1) * this.options.step, null);
return this; return this;
}, },
stepDown: function(steps) { stepDown: function(steps) {
this._spin((steps || 1) * -this.options.step, null); this._spin((steps || 1) * -this.options.step, null);
return this; return this;
}, },
pageUp: function(pages) { pageUp: function(pages) {
return this.stepUp((pages || 1) * pageModifier); return this.stepUp((pages || 1) * pageModifier);
}, },
pageDown: function(pages) { pageDown: function(pages) {
return this.stepDown((pages || 1) * pageModifier); return this.stepDown((pages || 1) * pageModifier);
}, },
value: function(newVal) {
if (!arguments.length) {
return this.options.value;
}
this._setOption('value', newVal);
},
widget: function() { widget: function() {
return this.uiSpinner; return this.uiSpinner;
} }