// $Id: editgrid.js 32 2007-08-11 21:24:09Z unicinfo $
// Copyright (c) 2004 Kurt M. Brown, x2ii.info.
// This file is subject to the terms and conditions of the GNU General Public
// License. See the file COPYING for more details.

xi.editgrid = {};

xi.editgrid.text   = 1;
xi.editgrid.number = 2;
xi.editgrid.money  = 3;

xi.editgrid.Cell = function(type, value, readOnly)
{
    this.type = type;
    this.readOnly = readOnly == true;
    this.alignRight = (type == xi.editgrid.number || type == xi.editgrid.money);
    this.isMoney = (type == xi.editgrid.money);
    this.isNumber = (type == xi.editgrid.number);
    if (this.isMoney)
        value = parseInt(value, 10);
    else if (this.isNumber)
        value = parseFloat(value);
    this.value = value;
    this.originalValue = value;
}
xi.editgrid.Cell.prototype.setValue = function(value)
{
    if (this.isMoney || this.isNumber) {
        value = parseFloat(value);
        if (isNaN(value))
            value = this.originalValue;
        else if (this.isMoney)
            value = parseInt(xi.round(100*value), 10);
    }
    this.value = value;
}
xi.editgrid.Cell.prototype.didChange = function()
{
    return this.value != this.originalValue;
}

// Cell instantiators

xi.editgrid.textCell = function(value, readOnly)
{
    return new xi.editgrid.Cell(xi.editgrid.text, value, readOnly);
}
xi.editgrid.numberCell = function(value, readOnly)
{
    return new xi.editgrid.Cell(xi.editgrid.number, value, readOnly);
}
xi.editgrid.moneyCell = function(value, readOnly)
{
    return new xi.editgrid.Cell(xi.editgrid.money, value, readOnly);
}

xi.editgrid.Data = function()
{
    this.rowNum = 0;
    this.colNum = 0;
    this.rows = [];
}
xi.editgrid.Data.prototype.pushRow = function(cells)
{
    this.rows.push(cells);
}
xi.editgrid.Data.prototype.getCell = function(rowNum, colNum)
{
    return rowNum === undefined ?
        this.rows[this.rowNum][this.colNum] : this.rows[rowNum][colNum]
}
xi.editgrid.Data.prototype.getAnonId = function()
{
    return this.rowNum + '-' + this.colNum;
}
xi.editgrid.Data.prototype.anyChanged = function()
{
    var changed = false;

    for each (var row in this.rows) {
        // select cells from row as array to allow for user added keys
        // to a row.
        for (var i = 0; i < row.length; ++i) {
            if (row[i].didChange()) {
                changed = true;
                break;
                break;
            }
        }
    }
    return changed;
}

xi.editgrid.createElementHtml = function(tagName)
{
    return document.createElementNS('http://www.w3.org/1999/xhtml', tagName);
}
xi.editgrid.writeLabel = function(td, label)
{
    td.removeAllChildren();
    return td.appendChild(document.createTextNode(label));
}
xi.editgrid.writeTableCell = function(td, cell)
{
    td.removeAllChildren();
    return td.appendChild(
        document.createTextNode(
            cell.isMoney ? xi.money(cell.value) : cell.value
        )
    );
}
xi.editgrid.editCell = function(edit, td, charEvent, cell)
{
    edit.removeAllChildren();
    var textbox = edit.appendChild(document.createElement('textbox'));
    textbox.size = 8; // from cell.type?
    textbox.cell = cell;
    textbox.top = td.offsetTop;
    textbox.left = td.offsetLeft;
    textbox.td = td;
    if (charEvent)
        textbox.value = String.fromCharCode(charEvent.charCode);
    else if (cell.isMoney)
        textbox.value = xi.decimalMoney(cell.value);
    else
        textbox.value = cell.value;
    edit.collapsed = false;
    textbox.focus();
}
xi.editgrid.acceptEdit = function(edit)
{
    const textbox = edit.firstChild;
    var cell = textbox.cell;
    cell.setValue(textbox.value);
    textbox.td.setAttribute('changed', cell.didChange());
    xi.editgrid.writeTableCell(textbox.td, cell);
    edit.collapsed = true;
}

