1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * @fileoverview A simple container object for the brailling of a
7 * navigation from one object to another.
12 goog
.provide('cvox.NavBraille');
14 goog
.require('cvox.Spannable');
17 * A class capturing the braille for navigation from one object to
19 * @param {{text: (undefined|string|!cvox.Spannable),
20 * startIndex: (undefined|number),
21 * endIndex: (undefined|number)}} kwargs The arguments for braille.
22 * text The text of the object itself, including text from
23 * titles, labels, etc.
24 * startIndex The beginning of a selection within text.
25 * endIndex The end of a selection within text.
28 cvox
.NavBraille = function(kwargs
) {
30 * Text, annotated with DOM nodes.
31 * @type {!cvox.Spannable}
33 this.text
= (kwargs
.text
instanceof cvox
.Spannable
) ?
34 kwargs
.text
: new cvox
.Spannable(kwargs
.text
);
37 * Selection start index.
40 this.startIndex
= goog
.isDef(kwargs
.startIndex
) ? kwargs
.startIndex
: -1;
43 * Selection end index.
46 this.endIndex
= goog
.isDef(kwargs
.endIndex
) ?
47 kwargs
.endIndex
: this.startIndex
;
51 * Convenience for creating simple braille output.
52 * @param {string|!cvox.Spannable} text Text to represent in braille.
53 * @return {!cvox.NavBraille} Braille output without a cursor.
55 cvox
.NavBraille
.fromText = function(text
) {
56 return new cvox
.NavBraille({'text': text
});
61 * Creates a NavBraille from its serialized json form as created
63 * @param {!Object} json the serialized json object.
64 * @return {!cvox.NavBraille}
66 cvox
.NavBraille
.fromJson = function(json
) {
67 if (typeof json
.startIndex
!== 'number' ||
68 typeof json
.endIndex
!== 'number') {
69 throw 'Invalid start or end index in serialized NavBraille: ' + json
;
71 return new cvox
.NavBraille({
72 text
: cvox
.Spannable
.fromJson(json
.spannable
),
73 startIndex
: json
.startIndex
,
74 endIndex
: json
.endIndex
80 * @return {boolean} true if this braille description is empty.
82 cvox
.NavBraille
.prototype.isEmpty = function() {
83 return this.text
.getLength() == 0;
88 * @return {string} A string representation of this object.
90 cvox
.NavBraille
.prototype.toString = function() {
91 return 'NavBraille(text="' + this.text
.toString() + '" ' +
92 ' startIndex="' + this.startIndex
+ '" ' +
93 ' endIndex="' + this.endIndex
+ '")';
98 * Returns a plain old data object with the same data.
99 * Suitable for JSON encoding.
101 * @return {{spannable: Object,
102 * startIndex: number,
103 * endIndex: number}} JSON equivalent.
105 cvox
.NavBraille
.prototype.toJson = function() {
107 spannable
: this.text
.toJson(),
108 startIndex
: this.startIndex
,
109 endIndex
: this.endIndex