// $Id: viewgrid.js 25 2007-07-24 03:33:10Z 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.viewgrid = {};

xi.viewgrid.text   = 1;
xi.viewgrid.number = 2;
xi.viewgrid.money  = 3;
xi.viewgrid.epoch  = 4;

xi.viewgrid.Cell = function(type, value, onChoice, control)
{
    this.type = type;
    this.alignRight = (type == xi.viewgrid.number || type == xi.viewgrid.money);
    if (type == xi.viewgrid.money)
        value = parseInt(value, 10);
    else if (type == xi.viewgrid.number)
        value = parseFloat(value);
    this.value = value;
    this.onChoice = onChoice;
    this.control = control;
}
xi.viewgrid.Cell.prototype.toString = function()
{
    var val = this.value;
    if (val !== '') {
        if (this.type == xi.viewgrid.money)
            val = xi.money(val);
        else if (this.type == xi.viewgrid.epoch)
            val = xi.formatTime(val);
    }
    return val;
}

// Cell instantiators

xi.viewgrid.textCell = function(value, onChoice, control)
{
    return new xi.viewgrid.Cell(xi.viewgrid.text, value, onChoice, control);
}
xi.viewgrid.numberCell = function(value, onChoice, control)
{
    return new xi.viewgrid.Cell(xi.viewgrid.number, value, onChoice, control);
}
xi.viewgrid.moneyCell = function(value, onChoice, control)
{
    return new xi.viewgrid.Cell(xi.viewgrid.money, value, onChoice, control);
}
xi.viewgrid.epochCell = function(value, onChoice, control)
{
    return new xi.viewgrid.Cell(xi.viewgrid.epoch, value, onChoice, control);
}

xi.viewgrid.writeLabel = function(row, label)
{
    var description = row.appendChild(document.createElement('description'));
    description.appendChild(document.createTextNode(label));
    return description;
}
xi.viewgrid.writeHeaderButton = function(row, label, colNum, control)
{
    var button = row.appendChild(document.createElement('button'));
    button.control = control;
    button.colNum = colNum;
    button.sortDir = 0;
    button.setAttribute('onclick', 'this.onSort()');
    button.onSort = function() {
        if (this.sortDir > 0)
            this.sortDir = -1;
        else
            ++this.sortDir;

        if (this.sortDir == 0)
            this.setAttribute('imageName', 'none');
        else if (this.sortDir > 0)
            this.setAttribute('imageName', 'sort-asc');
        else
            this.setAttribute('imageName', 'sort-dsc');

        this.control.sort(this.colNum, this.sortDir);
    };
    var description = button.appendChild(document.createElement('description'));
    description.appendChild(document.createTextNode(label));
    return description;
}
xi.viewgrid.writeCell = function(row, cell, data)
{
    const val = cell.toString();
    var eCell;
    if (cell.onChoice) {
        eCell = row.appendChild(document.createElement('button'));
        eCell.setAttribute('label', val);
        eCell.onChoice = cell.onChoice;
        eCell.data = data;
        eCell.control = cell.control;
        eCell.setAttribute('onclick', 'this.onChoice()');
    }
    else
        eCell = xi.viewgrid.writeLabel(row, val);
    if (cell.alignRight)
        eCell.setAttribute('class', 'right');
}

