tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

grid.js (3947B)


      1 /* import-globals-from common.js */
      2 
      3 /**
      4 * Create grid object based on HTML table.
      5 */
      6 function grid(aTableIdentifier) {
      7  this.getRowCount = function getRowCount() {
      8    return this.table.rows.length - (this.table.tHead ? 1 : 0);
      9  };
     10  this.getColsCount = function getColsCount() {
     11    return this.table.rows[0].cells.length;
     12  };
     13 
     14  this.getRowAtIndex = function getRowAtIndex(aIndex) {
     15    return this.table.rows[this.table.tHead ? aIndex + 1 : aIndex];
     16  };
     17 
     18  this.getMaxIndex = function getMaxIndex() {
     19    return this.getRowCount() * this.getColsCount() - 1;
     20  };
     21 
     22  this.getCellAtIndex = function getCellAtIndex(aIndex) {
     23    var colsCount = this.getColsCount();
     24 
     25    var rowIdx = Math.floor(aIndex / colsCount);
     26    var colIdx = aIndex % colsCount;
     27 
     28    var row = this.getRowAtIndex(rowIdx);
     29    return row.cells[colIdx];
     30  };
     31 
     32  this.getIndexByCell = function getIndexByCell(aCell) {
     33    var colIdx = aCell.cellIndex;
     34 
     35    var rowIdx = aCell.parentNode.rowIndex;
     36    if (this.table.tHead) {
     37      rowIdx -= 1;
     38    }
     39 
     40    var colsCount = this.getColsCount();
     41    return rowIdx * colsCount + colIdx;
     42  };
     43 
     44  this.getCurrentCell = function getCurrentCell() {
     45    var rowCount = this.table.rows.length;
     46    var colsCount = this.getColsCount();
     47    for (var rowIdx = 0; rowIdx < rowCount; rowIdx++) {
     48      for (var colIdx = 0; colIdx < colsCount; colIdx++) {
     49        var cell = this.table.rows[rowIdx].cells[colIdx];
     50        if (cell.hasAttribute("tabindex")) {
     51          return cell;
     52        }
     53      }
     54    }
     55    return null;
     56  };
     57 
     58  this.initGrid = function initGrid() {
     59    this.table.addEventListener("keypress", this);
     60    this.table.addEventListener("click", this);
     61  };
     62 
     63  this.handleEvent = function handleEvent(aEvent) {
     64    if (aEvent instanceof KeyboardEvent) {
     65      this.handleKeyEvent(aEvent);
     66    } else {
     67      this.handleClickEvent(aEvent);
     68    }
     69  };
     70 
     71  this.handleKeyEvent = function handleKeyEvent(aEvent) {
     72    if (aEvent.target.localName != "td") {
     73      return;
     74    }
     75 
     76    var cell = aEvent.target;
     77    switch (aEvent.keyCode) {
     78      case KeyboardEvent.DOM_VK_UP: {
     79        let colsCount = this.getColsCount();
     80        let idx = this.getIndexByCell(cell);
     81        var upidx = idx - colsCount;
     82        if (upidx >= 0) {
     83          cell.removeAttribute("tabindex");
     84          var upcell = this.getCellAtIndex(upidx);
     85          upcell.setAttribute("tabindex", "0");
     86          upcell.focus();
     87        }
     88        break;
     89      }
     90      case KeyboardEvent.DOM_VK_DOWN: {
     91        let colsCount = this.getColsCount();
     92        let idx = this.getIndexByCell(cell);
     93        var downidx = idx + colsCount;
     94        if (downidx <= this.getMaxIndex()) {
     95          cell.removeAttribute("tabindex");
     96          var downcell = this.getCellAtIndex(downidx);
     97          downcell.setAttribute("tabindex", "0");
     98          downcell.focus();
     99        }
    100        break;
    101      }
    102      case KeyboardEvent.DOM_VK_LEFT: {
    103        let idx = this.getIndexByCell(cell);
    104        if (idx > 0) {
    105          cell.removeAttribute("tabindex");
    106          var prevcell = this.getCellAtIndex(idx - 1);
    107          prevcell.setAttribute("tabindex", "0");
    108          prevcell.focus();
    109        }
    110        break;
    111      }
    112      case KeyboardEvent.DOM_VK_RIGHT: {
    113        let idx = this.getIndexByCell(cell);
    114        if (idx < this.getMaxIndex()) {
    115          cell.removeAttribute("tabindex");
    116          var nextcell = this.getCellAtIndex(idx + 1);
    117          nextcell.setAttribute("tabindex", "0");
    118          nextcell.focus();
    119        }
    120        break;
    121      }
    122    }
    123  };
    124 
    125  this.handleClickEvent = function handleClickEvent(aEvent) {
    126    if (aEvent.target.localName != "td") {
    127      return;
    128    }
    129 
    130    var curCell = this.getCurrentCell();
    131    var cell = aEvent.target;
    132 
    133    if (cell != curCell) {
    134      curCell.removeAttribute("tabindex");
    135      cell.setAttribute("tabindex", "0");
    136      cell.focus();
    137    }
    138  };
    139 
    140  this.table = getNode(aTableIdentifier);
    141  this.initGrid();
    142 }