accessible/tests/mochitest/grid.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/accessible/tests/mochitest/grid.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +const nsIDOMKeyEvent = Components.interfaces.nsIDOMKeyEvent;
     1.5 +
     1.6 +/**
     1.7 + * Create grid object based on HTML table.
     1.8 + */
     1.9 +function grid(aTableIdentifier)
    1.10 +{
    1.11 +  this.getRowCount = function getRowCount()
    1.12 +  {
    1.13 +    return this.table.rows.length - (this.table.tHead ? 1 : 0);
    1.14 +  }
    1.15 +  this.getColsCount = function getColsCount()
    1.16 +  {
    1.17 +    return this.table.rows[0].cells.length;
    1.18 +  }
    1.19 +
    1.20 +  this.getRowAtIndex = function getRowAtIndex(aIndex)
    1.21 +  {
    1.22 +    return this.table.rows[this.table.tHead ? aIndex + 1 : aIndex];
    1.23 +  }
    1.24 +
    1.25 +  this.getMaxIndex = function getMaxIndex()
    1.26 +  {
    1.27 +    return this.getRowCount() * this.getColsCount() - 1;
    1.28 +  }
    1.29 +
    1.30 +  this.getCellAtIndex = function getCellAtIndex(aIndex)
    1.31 +  {
    1.32 +    var rowCount = this.getRowCount();
    1.33 +    var colsCount = this.getColsCount();
    1.34 +
    1.35 +    var rowIdx = Math.floor(aIndex / colsCount);
    1.36 +    var colIdx = aIndex % colsCount;
    1.37 +
    1.38 +    var row = this.getRowAtIndex(rowIdx);
    1.39 +    return row.cells[colIdx];
    1.40 +  }
    1.41 +
    1.42 +  this.getIndexByCell = function getIndexByCell(aCell)
    1.43 +  {
    1.44 +    var colIdx = aCell.cellIndex;
    1.45 +
    1.46 +    var rowIdx = aCell.parentNode.rowIndex;
    1.47 +    if (this.table.tHead)
    1.48 +      rowIdx -= 1;
    1.49 +
    1.50 +    var colsCount = this.getColsCount();
    1.51 +    return rowIdx * colsCount + colIdx;
    1.52 +  }
    1.53 +
    1.54 +  this.getCurrentCell = function getCurrentCell()
    1.55 +  {
    1.56 +    var rowCount = this.table.rows.length;
    1.57 +    var colsCount = this.getColsCount();
    1.58 +    for (var rowIdx = 0; rowIdx < rowCount; rowIdx++) {
    1.59 +      for (var colIdx = 0; colIdx < colsCount; colIdx++) {
    1.60 +        var cell = this.table.rows[rowIdx].cells[colIdx];
    1.61 +        if (cell.hasAttribute("tabindex"))
    1.62 +          return cell;
    1.63 +      }
    1.64 +    }
    1.65 +    return null;
    1.66 +  }
    1.67 +
    1.68 +  this.initGrid = function initGrid()
    1.69 +  {
    1.70 +    this.table.addEventListener("keypress", this, false);
    1.71 +    this.table.addEventListener("click", this, false);
    1.72 +  }
    1.73 +
    1.74 +  this.handleEvent = function handleEvent(aEvent)
    1.75 +  {
    1.76 +    if (aEvent instanceof nsIDOMKeyEvent)
    1.77 +      this.handleKeyEvent(aEvent);
    1.78 +    else
    1.79 +      this.handleClickEvent(aEvent);
    1.80 +  }
    1.81 +
    1.82 +  this.handleKeyEvent = function handleKeyEvent(aEvent)
    1.83 +  {
    1.84 +    if (aEvent.target.localName != "td")
    1.85 +      return;
    1.86 +
    1.87 +    var cell = aEvent.target;
    1.88 +    switch(aEvent.keyCode) {
    1.89 +      case nsIDOMKeyEvent.DOM_VK_UP:
    1.90 +        var colsCount = this.getColsCount();
    1.91 +        var idx = this.getIndexByCell(cell);
    1.92 +        var upidx = idx - colsCount;
    1.93 +        if (upidx >= 0) {
    1.94 +          cell.removeAttribute("tabindex");
    1.95 +          var upcell = this.getCellAtIndex(upidx);
    1.96 +          upcell.setAttribute("tabindex", "0");
    1.97 +          upcell.focus();
    1.98 +        }
    1.99 +        break;
   1.100 +
   1.101 +      case nsIDOMKeyEvent.DOM_VK_DOWN:
   1.102 +        var colsCount = this.getColsCount();
   1.103 +        var idx = this.getIndexByCell(cell);
   1.104 +        var downidx = idx + colsCount;
   1.105 +        if (downidx <= this.getMaxIndex()) {
   1.106 +          cell.removeAttribute("tabindex");
   1.107 +          var downcell = this.getCellAtIndex(downidx);
   1.108 +          downcell.setAttribute("tabindex", "0");
   1.109 +          downcell.focus();
   1.110 +        }
   1.111 +        break;
   1.112 +
   1.113 +      case nsIDOMKeyEvent.DOM_VK_LEFT:
   1.114 +        var idx = this.getIndexByCell(cell);
   1.115 +        if (idx > 0) {
   1.116 +          cell.removeAttribute("tabindex");
   1.117 +          var prevcell = this.getCellAtIndex(idx - 1);
   1.118 +          prevcell.setAttribute("tabindex", "0");
   1.119 +          prevcell.focus();
   1.120 +        }
   1.121 +        break;
   1.122 +
   1.123 +      case nsIDOMKeyEvent.DOM_VK_RIGHT:
   1.124 +        var idx = this.getIndexByCell(cell);
   1.125 +        if (idx < this.getMaxIndex()) {
   1.126 +          cell.removeAttribute("tabindex");
   1.127 +          var nextcell = this.getCellAtIndex(idx + 1);
   1.128 +          nextcell.setAttribute("tabindex", "0");
   1.129 +          nextcell.focus();
   1.130 +        }
   1.131 +        break;
   1.132 +    }
   1.133 +  }
   1.134 +
   1.135 +  this.handleClickEvent = function handleClickEvent(aEvent)
   1.136 +  {
   1.137 +    if (aEvent.target.localName != "td")
   1.138 +      return;
   1.139 +    
   1.140 +    var curCell = this.getCurrentCell();
   1.141 +    var cell = aEvent.target;
   1.142 +
   1.143 +    if (cell != curCell) {
   1.144 +      curCell.removeAttribute("tabindex");
   1.145 +      cell.setAttribute("tabindex", "0");
   1.146 +      cell.focus();
   1.147 +    }
   1.148 +  }
   1.149 +
   1.150 +  this.table = getNode(aTableIdentifier);
   1.151 +  this.initGrid();
   1.152 +}

mercurial