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