2009-05-06 22:59:49 -07:00
|
|
|
const nsIDOMKeyEvent = Components.interfaces.nsIDOMKeyEvent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create grid object based on HTML table.
|
|
|
|
*/
|
|
|
|
function grid(aTableIdentifier)
|
|
|
|
{
|
2009-09-14 20:55:26 -07:00
|
|
|
this.getRowCount = function getRowCount()
|
2009-05-06 22:59:49 -07:00
|
|
|
{
|
|
|
|
return this.table.rows.length - (this.table.tHead ? 1 : 0);
|
|
|
|
}
|
|
|
|
this.getColsCount = function getColsCount()
|
|
|
|
{
|
|
|
|
return this.table.rows[0].cells.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getRowAtIndex = function getRowAtIndex(aIndex)
|
|
|
|
{
|
|
|
|
return this.table.rows[this.table.tHead ? aIndex + 1 : aIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getMaxIndex = function getMaxIndex()
|
|
|
|
{
|
2009-09-14 20:55:26 -07:00
|
|
|
return this.getRowCount() * this.getColsCount() - 1;
|
2009-05-06 22:59:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.getCellAtIndex = function getCellAtIndex(aIndex)
|
|
|
|
{
|
2009-09-14 20:55:26 -07:00
|
|
|
var rowCount = this.getRowCount();
|
2009-05-06 22:59:49 -07:00
|
|
|
var colsCount = this.getColsCount();
|
|
|
|
|
|
|
|
var rowIdx = Math.floor(aIndex / colsCount);
|
|
|
|
var colIdx = aIndex % colsCount;
|
|
|
|
|
|
|
|
var row = this.getRowAtIndex(rowIdx);
|
|
|
|
return row.cells[colIdx];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getIndexByCell = function getIndexByCell(aCell)
|
|
|
|
{
|
|
|
|
var colIdx = aCell.cellIndex;
|
|
|
|
|
|
|
|
var rowIdx = aCell.parentNode.rowIndex;
|
|
|
|
if (this.table.tHead)
|
|
|
|
rowIdx -= 1;
|
|
|
|
|
|
|
|
var colsCount = this.getColsCount();
|
|
|
|
return rowIdx * colsCount + colIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getCurrentCell = function getCurrentCell()
|
|
|
|
{
|
2009-09-10 18:07:56 -07:00
|
|
|
var rowCount = this.table.rows.length;
|
2009-05-06 22:59:49 -07:00
|
|
|
var colsCount = this.getColsCount();
|
2009-09-10 18:07:56 -07:00
|
|
|
for (var rowIdx = 0; rowIdx < rowCount; rowIdx++) {
|
2009-05-06 22:59:49 -07:00
|
|
|
for (var colIdx = 0; colIdx < colsCount; colIdx++) {
|
|
|
|
var cell = this.table.rows[rowIdx].cells[colIdx];
|
|
|
|
if (cell.hasAttribute("tabindex"))
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.initGrid = function initGrid()
|
|
|
|
{
|
|
|
|
this.table.addEventListener("keypress", this, false);
|
|
|
|
this.table.addEventListener("click", this, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handleEvent = function handleEvent(aEvent)
|
|
|
|
{
|
|
|
|
if (aEvent instanceof nsIDOMKeyEvent)
|
|
|
|
this.handleKeyEvent(aEvent);
|
|
|
|
else
|
|
|
|
this.handleClickEvent(aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handleKeyEvent = function handleKeyEvent(aEvent)
|
|
|
|
{
|
2009-06-09 00:41:19 -07:00
|
|
|
if (aEvent.target.localName != "td")
|
2009-05-06 22:59:49 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
var cell = aEvent.target;
|
|
|
|
switch(aEvent.keyCode) {
|
|
|
|
case nsIDOMKeyEvent.DOM_VK_UP:
|
|
|
|
var colsCount = this.getColsCount();
|
|
|
|
var idx = this.getIndexByCell(cell);
|
|
|
|
var upidx = idx - colsCount;
|
|
|
|
if (upidx >= 0) {
|
|
|
|
cell.removeAttribute("tabindex");
|
|
|
|
var upcell = this.getCellAtIndex(upidx);
|
|
|
|
upcell.setAttribute("tabindex", "0");
|
|
|
|
upcell.focus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIDOMKeyEvent.DOM_VK_DOWN:
|
|
|
|
var colsCount = this.getColsCount();
|
|
|
|
var idx = this.getIndexByCell(cell);
|
|
|
|
var downidx = idx + colsCount;
|
|
|
|
if (downidx <= this.getMaxIndex()) {
|
|
|
|
cell.removeAttribute("tabindex");
|
|
|
|
var downcell = this.getCellAtIndex(downidx);
|
|
|
|
downcell.setAttribute("tabindex", "0");
|
|
|
|
downcell.focus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIDOMKeyEvent.DOM_VK_LEFT:
|
|
|
|
var idx = this.getIndexByCell(cell);
|
|
|
|
if (idx > 0) {
|
|
|
|
cell.removeAttribute("tabindex");
|
|
|
|
var prevcell = this.getCellAtIndex(idx - 1);
|
|
|
|
prevcell.setAttribute("tabindex", "0");
|
|
|
|
prevcell.focus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIDOMKeyEvent.DOM_VK_RIGHT:
|
|
|
|
var idx = this.getIndexByCell(cell);
|
|
|
|
if (idx < this.getMaxIndex()) {
|
|
|
|
cell.removeAttribute("tabindex");
|
|
|
|
var nextcell = this.getCellAtIndex(idx + 1);
|
|
|
|
nextcell.setAttribute("tabindex", "0");
|
|
|
|
nextcell.focus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handleClickEvent = function handleClickEvent(aEvent)
|
|
|
|
{
|
2009-06-09 00:41:19 -07:00
|
|
|
if (aEvent.target.localName != "td")
|
2009-05-06 22:59:49 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
var curCell = this.getCurrentCell();
|
|
|
|
var cell = aEvent.target;
|
|
|
|
|
|
|
|
if (cell != curCell) {
|
|
|
|
curCell.removeAttribute("tabindex");
|
|
|
|
cell.setAttribute("tabindex", "0");
|
|
|
|
cell.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.table = getNode(aTableIdentifier);
|
|
|
|
this.initGrid();
|
|
|
|
}
|