spinner.js
Summary
A spinner widget.
Copyright (c) 2004-2006 by Zapatec, Inc.
http://www.zapatec.com
1700 MLK Way, Berkeley, California,
94709, U.S.A.
All rights reserved.
Zapatec.Spinner = function(objArgs) {
if(arguments.length == 0){
objArgs = {};
}
Zapatec.Spinner.SUPERconstructor.call(this, objArgs);
};
Zapatec.Spinner.id = 'Zapatec.Spinner';
Zapatec.inherit(Zapatec.Spinner, Zapatec.Widget);
Zapatec.Spinner.prototype.init = function(objArgs)
{
Zapatec.Spinner.SUPERclass.init.call(this, objArgs);
this.createSpinner();
this.updateText();
};
Zapatec.Spinner.prototype.createSpinner = function () {
this.fireEvent("beforeCreate");
var content = [];
var tableId = Zapatec.Spinner.generateString(
"zpSpin", this.id, "Table");
var tableClass = this.getClassName({
prefix: 'zpSpinner',
suffix: 'Table'
});
content.push("<table id='");
content.push(tableId);
content.push("' class='");
content.push(tableClass);
content.push("' cellSpacing='0' cellPadding='0' >");
content.push("<tbody>");
var inputId = Zapatec.Spinner.generateString(
"zpSpin", this.id, "Input");
var inputClass = this.getClassName({
prefix: 'zpSpinner',
suffix: 'Input'
});
content.push("<tr><td rowspan='2' id='");
content.push(inputId);
content.push("' class='");
content.push(inputClass);
content.push("'></td>");
var upId = Zapatec.Spinner.generateString(
"zpSpin", this.id, "Up");
var upClass = this.getClassName({
prefix: 'zpSpinner',
suffix: 'Up'
});
content.push("<td id='");
content.push(upId);
content.push("' class='");
content.push(upClass);
content.push("'");
this.addEvents(content, "up");
content.push("></td></tr>");
var downId = Zapatec.Spinner.generateString(
"zpSpin", this.id, "Down");
var downClass = this.getClassName({
prefix: 'zpSpinner',
suffix: 'Down'
});
content.push("<tr><td id='");
content.push(downId);
content.push("' class='");
content.push(downClass);
content.push("'");
this.addEvents(content, "down");
content.push("></td></tr>");
content.push("<tr></tr></tbody></table>");
var html = content.join("");
this.fireEvent("afterCreate");
var wrapper = document.createElement("span");
wrapper.innerHTML = html;
this.config.parent.appendChild(wrapper);
this.createProperty(this, "input", wrapper.childNodes[0].
childNodes[0].childNodes[0].childNodes[0]);
this.createProperty(this, "upButton", wrapper.childNodes[0].
childNodes[0].childNodes[0].childNodes[1]);
this.createProperty(this, "downButton", wrapper.childNodes[0].
childNodes[0].childNodes[1].childNodes[0]);
}
Zapatec.Spinner.prototype.getValue = function() {
return this.config.value;
}
Zapatec.Spinner.prototype.setValue = function(value) {
this.config.value = value;
this.fireEvent("valueChanged");
this.updateText();
}
Zapatec.Spinner.prototype.updateText = function() {
var value = this.getValue();
var str = '';
if (null != value) {
str += value;
if (this.config.minTextLength) {
while (str.length < this.config.minTextLength) {
str = '0' + str;
}
}
}
this.input.innerHTML = str;
}
Zapatec.Spinner.prototype.configure = function(objArgs) {
this.defineConfigOption('parent', null);
this.defineConfigOption('value', null);
this.defineConfigOption('delta', 1);
this.defineConfigOption('min', null);
this.defineConfigOption('max', null);
this.defineConfigOption('minTextLength', null);
this.defineConfigOption('repeatTimeout', 500);
this.defineConfigOption('repeatAcceleration', 0.8);
Zapatec.Spinner.SUPERclass.configure.call(this, objArgs);
this.config.parent = Zapatec.Widget.getElementById(this.config.parent);
if (!this.config.parent) {
Zapatec.Log({description: "Can't find parent for spinner (\"parent\" config option)"});
return;
}
this.pressedArrow = 0;
this.repeatTimer = null;
this.repeatTimeout = this.config.repeatTimeout;
this.repeatAcceleration = this.config.repeatAcceleration;
};
Zapatec.Spinner.prototype.addEvents = function(content, arg) {
content.push(" onmouseover='Zapatec.Widget.callMethod(");
content.push(this.id);
content.push(", \"onMouseOver\", \"");
content.push(arg);
content.push("\")'");
content.push(" onmousedown='return Zapatec.Widget.callMethod(");
content.push(this.id);
content.push(", \"onMouseDown\", \"");
content.push(arg);
content.push("\");'");
content.push(" onmouseup='Zapatec.Widget.callMethod(");
content.push(this.id);
content.push(", \"onMouseUp\", \"");
content.push(arg);
content.push("\")'");
content.push(" onmouseout='Zapatec.Widget.callMethod(");
content.push(this.id);
content.push(", \"onMouseOut\", \"");
content.push(arg);
content.push("\")'");
if (Zapatec.is_ie) {
content.push(" ondblclick='Zapatec.Widget.callMethod(");
content.push(this.id);
content.push(", \"onMouseDblClick\", \"");
content.push(arg);
content.push("\")'");
}
}
Zapatec.Spinner.prototype.adjustValue = function(direction) {
var value = this.getValue();
if (0 < direction) {
value += this.config.delta;
}
else if (direction < 0) {
value -= this.config.delta;
}
if (this.config.min) {
if (value < this.config.min) {
value = this.config.min;
}
}
if (this.config.max) {
if (this.config.max < value) {
value = this.config.max;
}
}
this.setValue(value);
}
Zapatec.Spinner.prototype.onTimeout = function() {
if (!this.pressedArrow) {
this.resetTimer();
return;
}
this.adjustValue(this.pressedArrow);
this.invokeTimer();
}
Zapatec.Spinner.prototype.resetTimer = function() {
this.repeatTimeout = this.config.repeatTimeout;
if (this.repeatTimer) {
clearTimeout(this.repeatTimer);
}
}
Zapatec.Spinner.prototype.invokeTimer = function() {
var self = this;
this.repeatTimer = setTimeout(function() {
self.onTimeout();
}, this.repeatTimeout );
this.repeatTimeout = Math.round(
this.repeatTimeout * this.config.repeatAcceleration);
}
Zapatec.Spinner.prototype.getEventSource = function(arg) {
var source = null;
if (arg == "up") {
source = this.upButton;
}
else if (arg == "down") {
source = this.downButton;
}
return source;
}
Zapatec.Spinner.prototype.onMouseOver = function(arg) {
var source = this.getEventSource(arg);
Zapatec.Utils.addClass(source, this.getClassName({
prefix: 'zpSpinner',
suffix: 'Hover'
}));
}
Zapatec.Spinner.prototype.onMouseDown = function(arg) {
var source = this.getEventSource(arg);
Zapatec.Utils.addClass(source, this.getClassName({
prefix: 'zpSpinner',
suffix: 'Pressed'
}));
var direction;
if (arg == "up") {
direction = +1;
}
else if (arg == "down") {
direction = -1;
}
this.pressedArrow = direction;
this.adjustValue(direction);
this.resetTimer();
this.invokeTimer();
var self = this;
var mouseUpGlobalHandler = function(ev) {
self.onMouseUp(arg);
Zapatec.Utils.removeEvent(document.body, "mouseup", mouseUpGlobalHandler);
}
Zapatec.Utils.addEvent(document.body, "mouseup", mouseUpGlobalHandler);
return false;
}
Zapatec.Spinner.prototype.onMouseDblClick = function(arg) {
var source = this.getEventSource(arg);
var direction;
if (arg == "up") {
direction = +1;
}
else if (arg == "down") {
direction = -1;
}
this.adjustValue(direction);
return false;
}
Zapatec.Spinner.prototype.onMouseUp = function(arg) {
var source = this.getEventSource(arg);
Zapatec.Utils.removeClass(source, this.getClassName({
prefix: 'zpSpinner',
suffix: 'Pressed'
}));
this.pressedArrow = 0;
}
Zapatec.Spinner.prototype.onMouseOut = function(arg) {
var source = this.getEventSource(arg);
Zapatec.Utils.removeClass(source, this.getClassName({
prefix: 'zpSpinner',
suffix: 'Pressed'
}));
Zapatec.Utils.removeClass(source, this.getClassName({
prefix: 'zpSpinner',
suffix: 'Hover'
}));
}
Zapatec.Spinner.generateString = function(){
var arr = [];
for(var ii = 0; ii < arguments.length; ii++){
arr.push(arguments[ii]);
}
return arr.join("");
}
Documentation generated by
JSDoc on Thu Aug 16 12:18:39 2007