/**
* DataCell is a wrapper object for all HTML DOM objects of one data item.
* All usable data units in the application should be of this type.
* Equivalent to a register
*/
/**
* DataCell constructor
* Optional arguments:
* showID - true if you want to show cell ID
* serialID - cell's serial ID you would like to show
* numDigits - number of digits to show in ID (with trailing zeros)
*/
function DataCell(isRegister, showID, serialID, numDigits, first) {
var itemDivName = "itemdiv" + getNewGUID();
var itemTextBoxName = itemDivName + "tb";
var itemSpanName = itemDivName + "sp";
var itemDiv = document.createElement('DIV');
itemDiv.id = itemDivName;
var itemSpan = document.createElement('SPAN');
itemSpan.id = itemSpanName;
itemDiv.appendChild(itemSpan);
var itemTextBox = document.createElement('INPUT');
itemTextBox.type = 'TEXT';
itemTextBox.id = itemTextBoxName;
itemDiv.appendChild(itemTextBox);
itemDiv.style.height = CELL_HEIGHT;
itemDiv.style.lineHeight = CELL_HEIGHT;
itemDiv.style.fontSize = CELL_FONT_SIZE;
itemDiv.style.fontWeight = "bolder";
itemDiv.style.fontFamily = "Arial";
itemDiv.style.textAlign = "center";
itemDiv.style.verticalAlign = "middle";
itemDiv.style.padding = "0";
itemDiv.style.backgroundColor = CELL_COLOR;
itemDiv.style.borderBottomWidth = "1px";
itemDiv.style.borderBottomColor = "#000000";
itemDiv.style.borderBottomStyle = "Solid";
itemDiv.style.borderRightWidth = "1px";
itemDiv.style.borderRightColor = "#000000";
itemDiv.style.borderRightStyle = "Solid";
if (showID) {
itemDiv.style.borderLeftWidth = "1px";
itemDiv.style.borderLeftColor = "#000000";
itemDiv.style.borderLeftStyle = "Solid";
//Fix IE/Firefox mismatch
itemDiv.style.width = (document.all ? "100%" : "96%");
if (first) {
itemDiv.style.borderTopWidth = "1px";
itemDiv.style.borderTopColor = "#000000";
itemDiv.style.borderTopStyle = "Solid";
}
}
else {
//Fix IE/Firefox mismatch
itemDiv.style.width = (document.all ? "100%" : "98%");
}
itemSpan.style.width = "80%";
itemSpan.style.height = CELL_HEIGHT;
itemSpan.style.lineHeight = CELL_HEIGHT;
itemSpan.style.position = "relative";
itemSpan.style.left = "0";
itemSpan.style.top = "0";
itemSpan.style.fontSize = CELL_FONT_SIZE;
itemSpan.style.fontWeight = "bolder";
itemSpan.style.fontFamily = "Arial";
itemSpan.style.textAlign = "right";
itemSpan.style.verticalAlign = "middle";
itemSpan.style.display = "block";
itemSpan.style.marginRight = "6px";
itemTextBox.style.width = "100%";
itemTextBox.style.height = (parseInt(CELL_HEIGHT) - (document.all ? 2 : 0)) + "px";
itemTextBox.style.lineHeight = (parseInt(CELL_HEIGHT) - (document.all ? 2 : 0)) + "px";
itemTextBox.style.position = "relative";
itemTextBox.style.left = "0";
itemTextBox.style.top = (document.all ? "-1" : "0");
itemTextBox.style.fontSize = CELL_FONT_SIZE;
itemTextBox.style.fontWeight = "bolder";
itemTextBox.style.fontFamily = "Arial";
itemTextBox.style.textAlign = "right";
itemTextBox.style.verticalAlign = "middle";
itemTextBox.style.display = "none";
itemTextBox.maxLength = 3;
itemTextBox.style.paddingRight = "8px";
itemDiv.dataCell = this;
this.itemDiv = itemDiv;
this.textBox = itemTextBox;
this.span = itemSpan;
//Parent must implement IDataSet
this.parent = null;
this.enabled = true;
var that = this;
/**
* Mouse down event handler
*/
this.itemDiv.onmousedown = function() {
//Only allow editing if higher cells have values
if (that.itemDiv.previousSibling &&
that.itemDiv.previousSibling.dataCell.getValue() == undefined)
return true;
if (that.parent.enabled && that.enabled &&
(that.textBox.style.display == "none")) {
that.textBox.style.display = "block";
that.span.style.display = "none";
if (!(navigator && navigator.userAgent && navigator.userAgent.match('Safari') && !navigator.userAgent.match('Chrome'))) {
that.textBox.focus();
} else {
that.textBox.select();
}
}
// Firefox and chrome expect 'false', safari expects 'true'??
if (navigator && navigator.userAgent && navigator.userAgent.match('Safari') && !(navigator.userAgent.match('Chrome'))) {
return false;
} else {
return false;
}
};
/**
* Sets cursor to text box
* (For IE)
*/
this.textBox.onfocus = function() {
that.textBox.select();
}
/**
* Textbox unfocus event handler
*/
this.textBox.onblur = function() {
/*
if ((navigator && navigator.userAgent && navigator.userAgent.match('Safari') && !navigator.userAgent.match('Chrome'))) {
alert('SAFARI USER - We are working on some bug fixes. Please switch a browser or try again later');
}
*/
//if (that.textBox.style.display == "block") {
if (that.textBox.value === "") {
that.parent.clearCell(that);
}
else
that.setValue(that.textBox.value, false, true);
that.textBox.style.display = "none";
that.span.style.display = "block";
//}
if (that.ondatachange) { that.ondatachange(); }
return true;
}
/**
* Textbox keypress event handler
*/
this.textBox.onkeypress = function(e) {
var keyCode, keynum;
if (window.event) { //IE
keyCode = event.keyCode;
keynum = keyCode;
}
else {
keyCode = e.keyCode;
keynum = e.which;
}
if (keyCode == 13) {
//Enter
if (that.textBox.value === "") {
that.parent.clearCell(that);
}
else
that.setValue(that.textBox.value, false, true);
if (that.parent.isCPU && that.parent.computer) {
that.parent.computer.highlightPCAddress();
}
if (that.ondatachange && !that.onenterpressed) that.ondatachange();
if (that.onenterpressed) {
that.onenterpressed(serialID);
if (innerValue != 0) return;
}
}
//Retrieve Esc keyCode
var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
if (keyCode == Esc || keyCode == 13) {
//Enter or Esc
that.textBox.style.display = "none";
that.span.style.display = "block";
return true;
}
else if (keyCode == 8) {
//Backspace
return true;
}
else if (keynum == 0) {
//Other control keys (Arrows, etc.)
return true;
}
else {
//Other - allow only digits and minus, no more than three
keychar = String.fromCharCode(keynum);
var numcheck;
if (that.textBox.value=="")
numcheck = /[\d\-]/;
else
numcheck = /\d/;
var negcheck = /\-\d*/;
if (negcheck.test(that.textBox.value))
that.textBox.maxLength = 4;
else
that.textBox.maxLength = 3;
return numcheck.test(keychar);
}
}
var innerValue = 0;
var innerEmpty = true;
/**
* Sets the value of the Data Cell
* Returns an object with property 'overflow'
*/
this.setValue = function(stringValue, empty, forceDisplay) {
if (empty) {
that.span.innerHTML = "";
that.textBox.value = "";
innerValue = 0;
innerEmpty = true;
return true;
}
numcheck = /\-{0,1}\d/;
if (!numcheck.test(stringValue)) return false;
var numValue;
try {
numValue = parseInt(stringValue, 10);
} catch (e) {
return false;
}
var overflow = numValue > 999 || numValue < -999;
if (overflow) return { overflow: true };
var absVal = Math.abs(numValue);
stringValue = absVal;
var sign = (numValue < 0) ? '-' : '';
stringValue = sign + stringValue;//.substr(stringValue.length - 3, 3);
//if (that.parent.isInputList) that.parent.ioChip.saveInput();
if (forceDisplay || !document.computer || document.computer.getAnimation() != ANIMATE_NONE) {
that.span.innerHTML = stringValue;
that.textBox.value = stringValue;
}
innerValue = stringValue;
innerEmpty = false;
return { overflow: false };
}
this.showValue = function() {
that.span.innerHTML = innerEmpty ? "" : innerValue;
that.textBox.value = innerEmpty ? "" : innerValue;
}
/**
* Returns DataCell's value
*/
this.getValue = function() {
numcheck = /\-{0,1}\d/;
if (innerEmpty || !numcheck.test(innerValue)) return (that.parent && that.parent.dataSetType == "DataArray" ? 0 : undefined);
return parseInt(innerValue, 10);
}
/**
* Returns the string value of this cell (whatever it is)
*/
this.getStringValue = function() {
return that.span.innerHTML;
}
/**
* Clones this DataCell
*/
this.clone = function() {
var newCell = new DataCell();
newCell.setValue(that.getValue());
return newCell;
}
/**
* Highlights (or de-highlights) this DataCell
*/
this.highlight = function(highlightOn) {
if (document.computer && document.computer.getAnimation() == ANIMATE_NONE) return;
var color = highlightOn ? CELL_HIGHLIGHT : CELL_COLOR;
that.itemDiv.style.backgroundColor = color;
var flag = false;
if (that.parent.dataSetType == "DataList") {
flag = (that.parent.frameDiv.scrollTop + that.parent.frameDiv.offsetHeight)
< (that.itemDiv.offsetTop + that.itemDiv.offsetHeight) ||
(that.parent.frameDiv.scrollTop) > (that.itemDiv.offsetTop);
}
else if (that.parent.dataSetType == "DataArray") {
flag = (that.parent.frameDiv.scrollTop + that.parent.frameDiv.offsetHeight)
< (that.itemDiv.parentNode.offsetTop + that.itemDiv.parentNode.offsetHeight) ||
(that.parent.frameDiv.scrollTop) > (that.itemDiv.parentNode.offsetTop);
}
if (flag) {
that.itemDiv.scrollIntoView();
var item = null;
if (that.parent.dataSetType == "DataList")
item = that.itemDiv;
else if (that.parent.dataSetType == "DataArray")
item = that.itemDiv.parentNode;
if (Math.abs(that.parent.frameDiv.scrollTop - item.offsetTop) < parseInt(CELL_HEIGHT) + 1)
that.parent.frameDiv.scrollTop -= 4 + (parseInt(CELL_HEIGHT) * 3);
if (tooSmall) document.body.scrollTop = 0;
}
}
/**
* Returns the absolute left position of this cell
*/
this.getAbsoluteLeft = function() {
return getAbsLeft(this.itemDiv);
}
/**
* Returns the absolute top position of this cell
*/
this.getAbsoluteTop = function() {
return getAbsTop(this.itemDiv);
}
}