summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/chromevox/injected/navigation_shifter.js
blob: 01187fb663d0b5fd0f75ccc8d539724c8e344ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview The purpose of this class is to delegate to the correct walker
 * based on the navigation state that it is in. The navigation state is a
 * simplified view of the external environment; the smallest amount of knowledge
 * needed to correctly delegate. One example is whether the user
 * is subnavigating. Note that while this class does
 * decide which walker to delegate to, it does NOT decide when its state
 * should be changed. This is done by the layer above. The reason for this
 * separation is that trying to make the decision here would require a lot
 * of knowledge about the environment, making this class harder to
 * test and maintain.
 *
 * This class knows about the public interfaces of all the walkers (rather
 * than just of the abstract class) since there are currently walker operations
 * which apply only to specific walkers.
 *
 * The navigation model is organized around having a chain of walkers with
 * increasing "granularity". This means (with a few exceptions), that if
 * walker A is more granular than walker B, then every valid selection in A
 * is a subset of a valid selection in B. For example, characters are
 * more granular than words, because every character is either a word or
 * inside a word.
 *
 * Note that while any callers may assume the granularity chain exists (after
 * all, there is a method makeMoreGranular()), they may not assume anything
 * about the order in which the walkers occur in this chain. This is because
 * the order may depend on the navigation state, and having external interaction
 * would slow down the changes we could make to this class (which is a problem,
 * since this is one of the core classes that impacts user-perceptible
 * navigation).
 *
 * Thinking of adding something here? Ask these questions:
 * Is it exposing functionality in some walker, the execution of which depends
 * on navigation state?
 *  Then it is a good candidate.
 * Does it require knowing more about the environment?
 *  If you are sure that it belongs here, then the minimum amount of knowledge
 *  to make the delegation decision should be added as state to this class.
 *  The decision for when this state changes should not be made in this class.
 *
 */


goog.provide('cvox.NavigationShifter');

goog.require('cvox.AbstractShifter');
goog.require('cvox.CharacterWalker');
goog.require('cvox.GroupWalker');
goog.require('cvox.LayoutLineWalker');
goog.require('cvox.ObjectWalker');
goog.require('cvox.SentenceWalker');
goog.require('cvox.TraverseContent');
goog.require('cvox.WordWalker');


/**
 * @constructor
 * @extends {cvox.AbstractShifter}
 */
cvox.NavigationShifter = function() {
  this.reset_();
  goog.base(this);
};
goog.inherits(cvox.NavigationShifter, cvox.AbstractShifter);

// These "const" literals may be used, but no order may be assumed
// between them by any outside callers.
/**
 * @type {Object.<string, number>}
 */
cvox.NavigationShifter.GRANULARITIES = {
  'CHARACTER': 0,
  'WORD': 1,
  'LINE': 2,
  'SENTENCE': 3,
  'OBJECT': 4,
  'GROUP': 5
};


/**
 * Stores state variables in a provided object.
 *
 * @param {Object} store The object.
 */
cvox.NavigationShifter.prototype.storeOn = function(store) {
  store['granularity'] = this.getGranularity();
};


/**
 * Updates the object with state variables from an earlier storeOn call.
 *
 * @param {Object} store The object.
 */
cvox.NavigationShifter.prototype.readFrom = function(store) {
  this.setGranularity(store['granularity']);
};


/**
 * @override
 */
cvox.NavigationShifter.prototype.next = function(sel) {
  var ret = this.currentWalker_.next(sel);
  if (this.currentWalkerIndex_ <= cvox.NavigationShifter.GRANULARITIES.LINE &&
      ret) {
    cvox.TraverseContent.getInstance().syncToCursorSelection(
        ret.clone().setReversed(false));
    cvox.TraverseContent.getInstance().updateSelection();
  }
  return ret;
};


/**
 * @override
 */
cvox.NavigationShifter.prototype.sync = function(sel) {
  return this.currentWalker_.sync(sel);
};


/**
 * @override
 */
cvox.NavigationShifter.prototype.getName = function() {
  return cvox.ChromeVox.msgs.getMsg('navigation_shifter');
};


/**
 * @override
 */
cvox.NavigationShifter.prototype.getDescription = function(prevSel, sel) {
  return this.currentWalker_.getDescription(prevSel, sel);
};


/**
 * Gets the braille representation of a node-based selection.
 * @override
 */
cvox.NavigationShifter.prototype.getBraille = function(prevSel, sel) {
  return this.lineWalker_.getBraille(prevSel, sel);
};


/**
 * Delegates to currentWalker_.
 * @return {string} The message string.
 */
cvox.NavigationShifter.prototype.getGranularityMsg = function() {
  return this.currentWalker_.getGranularityMsg();
};


/**
 * @override
 */
cvox.NavigationShifter.prototype.makeMoreGranular = function() {
  goog.base(this, 'makeMoreGranular');
  this.currentWalkerIndex_ = Math.max(this.currentWalkerIndex_ - 1, 0);
  if (!cvox.NavigationShifter.allowSentence && this.currentWalkerIndex_ ==
      cvox.NavigationShifter.GRANULARITIES.SENTENCE) {
    this.currentWalkerIndex_--;
  }
  this.currentWalker_ = this.walkers_[this.currentWalkerIndex_];
};

/**
 * @override
 */
cvox.NavigationShifter.prototype.makeLessGranular = function() {
  goog.base(this, 'makeLessGranular');
  this.currentWalkerIndex_ =
      Math.min(this.currentWalkerIndex_ + 1, this.walkers_.length - 1);
  if (!cvox.NavigationShifter.allowSentence && this.currentWalkerIndex_ ==
      cvox.NavigationShifter.GRANULARITIES.SENTENCE) {
    this.currentWalkerIndex_++;
  }
  this.currentWalker_ = this.walkers_[this.currentWalkerIndex_];
};

/**
 * Shift to a specified granularity.
 * NOTE: after a shift, we are no longer subnavigating, if we were.
 * @param {number} granularity The granularity to shift to.
 */
cvox.NavigationShifter.prototype.setGranularity = function(granularity) {
  this.ensureNotSubnavigating();
  this.currentWalkerIndex_ = granularity;
  this.currentWalker_ = this.walkers_[this.currentWalkerIndex_];
};

/**
 * Gets the granularity.
 * @return {number} The current granularity.
 *
 */
cvox.NavigationShifter.prototype.getGranularity = function() {
  var wasSubnavigating = this.isSubnavigating();
  this.ensureNotSubnavigating();
  var ret = this.currentWalkerIndex_;
  if (wasSubnavigating) {
    this.ensureSubnavigating();
  }
  return ret;
};


/** Actions. */
/**
 * @override
 */
cvox.NavigationShifter.prototype.hasAction = function(name) {
  if (name == 'toggleLineType') {
    return true;
  }
  return goog.base(this, 'hasAction', name);
};


/**
 * @override
 */
cvox.NavigationShifter.create = function(sel) {
  return new cvox.NavigationShifter();
};


/**
 * Resets navigation shifter to a "new" state. Makes testing easier.
 * @private
 */
cvox.NavigationShifter.prototype.reset_ = function() {
  this.groupWalker_ = new cvox.GroupWalker();
  this.objectWalker_ = new cvox.ObjectWalker();
  this.sentenceWalker_ = new cvox.SentenceWalker();
  this.lineWalker_ = new cvox.LayoutLineWalker();
  this.wordWalker_ = new cvox.WordWalker();
  this.characterWalker_ = new cvox.CharacterWalker();

  this.walkers_ = [
      this.characterWalker_,
      this.wordWalker_,
      this.lineWalker_,
      this.sentenceWalker_,
      this.objectWalker_,
      this.groupWalker_
  ];
  this.currentWalkerIndex_ = this.walkers_.indexOf(this.groupWalker_);

  /**
   * @type {cvox.AbstractWalker}
   * @private
   */
  this.currentWalker_ = this.walkers_[this.currentWalkerIndex_];
};


/**
 * @type {boolean}
 */
cvox.NavigationShifter.allowSentence = false;