table_walker.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import * as DomUtil from '../common/dom_util.js';
  2. import { KeyCode } from '../common/event_util.js';
  3. import { SemanticRole, SemanticType } from '../semantic_tree/semantic_meaning.js';
  4. import { SyntaxWalker } from './syntax_walker.js';
  5. import { WalkerMoves } from './walker.js';
  6. export class TableWalker extends SyntaxWalker {
  7. constructor(node, generator, highlighter, xml) {
  8. super(node, generator, highlighter, xml);
  9. this.node = node;
  10. this.generator = generator;
  11. this.highlighter = highlighter;
  12. this.firstJump = null;
  13. this.key_ = null;
  14. this.row_ = 0;
  15. this.currentTable_ = null;
  16. this.keyMapping.set(KeyCode.ZERO, this.jumpCell.bind(this));
  17. this.keyMapping.set(KeyCode.ONE, this.jumpCell.bind(this));
  18. this.keyMapping.set(KeyCode.TWO, this.jumpCell.bind(this));
  19. this.keyMapping.set(KeyCode.THREE, this.jumpCell.bind(this));
  20. this.keyMapping.set(KeyCode.FOUR, this.jumpCell.bind(this));
  21. this.keyMapping.set(KeyCode.FIVE, this.jumpCell.bind(this));
  22. this.keyMapping.set(KeyCode.SIX, this.jumpCell.bind(this));
  23. this.keyMapping.set(KeyCode.SEVEN, this.jumpCell.bind(this));
  24. this.keyMapping.set(KeyCode.EIGHT, this.jumpCell.bind(this));
  25. this.keyMapping.set(KeyCode.NINE, this.jumpCell.bind(this));
  26. }
  27. move(key) {
  28. this.key_ = key;
  29. const result = super.move(key);
  30. this.modifier = false;
  31. return result;
  32. }
  33. up() {
  34. this.moved = WalkerMoves.UP;
  35. return this.eligibleCell_() ? this.verticalMove_(false) : super.up();
  36. }
  37. down() {
  38. this.moved = WalkerMoves.DOWN;
  39. return this.eligibleCell_() ? this.verticalMove_(true) : super.down();
  40. }
  41. jumpCell() {
  42. if (!this.isInTable_() || this.key_ === null) {
  43. return this.getFocus();
  44. }
  45. if (this.moved === WalkerMoves.ROW) {
  46. this.moved = WalkerMoves.CELL;
  47. const column = this.key_ - KeyCode.ZERO;
  48. if (!this.isLegalJump_(this.row_, column)) {
  49. return this.getFocus();
  50. }
  51. return this.jumpCell_(this.row_, column);
  52. }
  53. const row = this.key_ - KeyCode.ZERO;
  54. if (row > this.currentTable_.childNodes.length) {
  55. return this.getFocus();
  56. }
  57. this.row_ = row;
  58. this.moved = WalkerMoves.ROW;
  59. return this.getFocus().clone();
  60. }
  61. undo() {
  62. const focus = super.undo();
  63. if (focus === this.firstJump) {
  64. this.firstJump = null;
  65. }
  66. return focus;
  67. }
  68. eligibleCell_() {
  69. const primary = this.getFocus().getSemanticPrimary();
  70. return (this.modifier &&
  71. primary.type === SemanticType.CELL &&
  72. TableWalker.ELIGIBLE_CELL_ROLES.indexOf(primary.role) !== -1);
  73. }
  74. verticalMove_(direction) {
  75. const parent = this.previousLevel();
  76. if (!parent) {
  77. return null;
  78. }
  79. const origFocus = this.getFocus();
  80. const origIndex = this.levels.indexOf(this.primaryId());
  81. const origLevel = this.levels.pop();
  82. const parentIndex = this.levels.indexOf(parent);
  83. const row = this.levels.get(direction ? parentIndex + 1 : parentIndex - 1);
  84. if (!row) {
  85. this.levels.push(origLevel);
  86. return null;
  87. }
  88. this.setFocus(this.singletonFocus(row));
  89. const children = this.nextLevel();
  90. const newNode = children[origIndex];
  91. if (!newNode) {
  92. this.setFocus(origFocus);
  93. this.levels.push(origLevel);
  94. return null;
  95. }
  96. this.levels.push(children);
  97. return this.singletonFocus(children[origIndex]);
  98. }
  99. jumpCell_(row, column) {
  100. if (!this.firstJump) {
  101. this.firstJump = this.getFocus();
  102. this.virtualize(true);
  103. }
  104. else {
  105. this.virtualize(false);
  106. }
  107. const id = this.currentTable_.id.toString();
  108. let level;
  109. do {
  110. level = this.levels.pop();
  111. } while (level.indexOf(id) === -1);
  112. this.levels.push(level);
  113. this.setFocus(this.singletonFocus(id));
  114. this.levels.push(this.nextLevel());
  115. const semRow = this.currentTable_.childNodes[row - 1];
  116. this.setFocus(this.singletonFocus(semRow.id.toString()));
  117. this.levels.push(this.nextLevel());
  118. return this.singletonFocus(semRow.childNodes[column - 1].id.toString());
  119. }
  120. isLegalJump_(row, column) {
  121. const xmlTable = DomUtil.querySelectorAllByAttrValue(this.getRebuilt().xml, 'id', this.currentTable_.id.toString())[0];
  122. if (!xmlTable || xmlTable.hasAttribute('alternative')) {
  123. return false;
  124. }
  125. const rowNode = this.currentTable_.childNodes[row - 1];
  126. if (!rowNode) {
  127. return false;
  128. }
  129. const xmlRow = DomUtil.querySelectorAllByAttrValue(xmlTable, 'id', rowNode.id.toString())[0];
  130. if (!xmlRow || xmlRow.hasAttribute('alternative')) {
  131. return false;
  132. }
  133. return !!(rowNode && rowNode.childNodes[column - 1]);
  134. }
  135. isInTable_() {
  136. let snode = this.getFocus().getSemanticPrimary();
  137. while (snode) {
  138. if (TableWalker.ELIGIBLE_TABLE_TYPES.indexOf(snode.type) !== -1) {
  139. this.currentTable_ = snode;
  140. return true;
  141. }
  142. snode = snode.parent;
  143. }
  144. return false;
  145. }
  146. }
  147. TableWalker.ELIGIBLE_CELL_ROLES = [
  148. SemanticRole.DETERMINANT,
  149. SemanticRole.ROWVECTOR,
  150. SemanticRole.BINOMIAL,
  151. SemanticRole.SQUAREMATRIX,
  152. SemanticRole.MULTILINE,
  153. SemanticRole.MATRIX,
  154. SemanticRole.VECTOR,
  155. SemanticRole.CASES,
  156. SemanticRole.TABLE
  157. ];
  158. TableWalker.ELIGIBLE_TABLE_TYPES = [
  159. SemanticType.MULTILINE,
  160. SemanticType.MATRIX,
  161. SemanticType.VECTOR,
  162. SemanticType.CASES,
  163. SemanticType.TABLE
  164. ];