summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/braille/nav_braille.js
blob: 4aaa9487ec810bb24e116b69d83cd1e9e3dfd16b (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
// 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 A simple container object for the brailling of a
 * navigation from one object to another.
 *
 */


goog.provide('cvox.NavBraille');

goog.require('cvox.Spannable');

/**
 * A class capturing the braille for navigation from one object to
 * another.
 * @param {{text: (undefined|string|!cvox.Spannable),
 *          startIndex: (undefined|number),
 *          endIndex: (undefined|number)}} kwargs The arguments for braille.
 *  text The text of the object itself, including text from
 *     titles, labels, etc.
 *  startIndex The beginning of a selection within text.
 *  endIndex The end of a selection within text.
 * @constructor
 */
cvox.NavBraille = function(kwargs) {
  /**
   * Text, annotated with DOM nodes.
   * @type {!cvox.Spannable}
   */
  this.text = (kwargs.text instanceof cvox.Spannable) ?
      kwargs.text : new cvox.Spannable(kwargs.text);

  /**
   * Selection start index.
   * @type {number}
   */
  this.startIndex = goog.isDef(kwargs.startIndex) ? kwargs.startIndex : -1;

  /**
   * Selection end index.
   * @type {number}
   */
  this.endIndex = goog.isDef(kwargs.endIndex) ?
      kwargs.endIndex : this.startIndex;
};

/**
 * Convenience for creating simple braille output.
 * @param {string|!cvox.Spannable} text Text to represent in braille.
 * @return {!cvox.NavBraille} Braille output without a cursor.
 */
cvox.NavBraille.fromText = function(text) {
  return new cvox.NavBraille({'text': text});
};


/**
 * Creates a NavBraille from its serialized json form as created
 * by toJson().
 * @param {!Object} json the serialized json object.
 * @return {!cvox.NavBraille}
 */
cvox.NavBraille.fromJson = function(json) {
  if (typeof json.startIndex !== 'number' ||
      typeof json.endIndex !== 'number') {
    throw 'Invalid start or end index in serialized NavBraille: ' + json;
  }
  return new cvox.NavBraille({
    text: cvox.Spannable.fromJson(json.spannable),
    startIndex: json.startIndex,
    endIndex: json.endIndex
  });
};


/**
 * @return {boolean} true if this braille description is empty.
 */
cvox.NavBraille.prototype.isEmpty = function() {
  return this.text.getLength() == 0;
};


/**
 * @return {string} A string representation of this object.
 */
cvox.NavBraille.prototype.toString = function() {
  return 'NavBraille(text="' + this.text.toString() + '" ' +
         ' startIndex="' + this.startIndex + '" ' +
         ' endIndex="' + this.endIndex + '")';
};


/**
 * Returns a plain old data object with the same data.
 * Suitable for JSON encoding.
 *
 * @return {{spannable: Object,
 *           startIndex: number,
 *           endIndex: number}} JSON equivalent.
 */
cvox.NavBraille.prototype.toJson = function() {
  return {
    spannable: this.text.toJson(),
    startIndex: this.startIndex,
    endIndex: this.endIndex
  };
};