nav_braille.js revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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. 4 5/** 6 * @fileoverview A simple container object for the brailling of a 7 * navigation from one object to another. 8 * 9 */ 10 11 12goog.provide('cvox.NavBraille'); 13 14goog.require('cvox.ChromeVox'); 15goog.require('cvox.CursorSelection'); 16goog.require('cvox.Spannable'); 17 18/** 19 * A class capturing the braille for navigation from one object to 20 * another. 21 * @param {{text: (undefined|string|!cvox.Spannable), 22 * startIndex: (undefined|number), 23 * endIndex: (undefined|number)}} kwargs The arguments for braille. 24 * text The text of the object itself, including text from 25 * titles, labels, etc. 26 * startIndex The beginning of a selection within text. 27 * endIndex The end of a selection within text. 28 * @constructor 29 */ 30cvox.NavBraille = function(kwargs) { 31 /** 32 * Text, annotated with DOM nodes. 33 * @type {!cvox.Spannable} 34 */ 35 this.text = (kwargs.text instanceof cvox.Spannable) ? 36 kwargs.text : new cvox.Spannable(kwargs.text); 37 38 /** 39 * Selection start index. 40 * @type {number} 41 */ 42 this.startIndex = goog.isDef(kwargs.startIndex) ? kwargs.startIndex : -1; 43 44 /** 45 * Selection end index. 46 * @type {number} 47 */ 48 this.endIndex = goog.isDef(kwargs.endIndex) ? 49 kwargs.endIndex : this.startIndex; 50}; 51 52/** 53 * Convenience for creating simple braille output. 54 * @param {string|!cvox.Spannable} text Text to represent in braille. 55 * @return {!cvox.NavBraille} Braille output without a cursor. 56 */ 57cvox.NavBraille.fromText = function(text) { 58 return new cvox.NavBraille({'text': text}); 59}; 60 61 62/** 63 * Creates a NavBraille from its serialized json form as created 64 * by toJson(). 65 * @param {!Object} json the serialized json object. 66 * @return {!cvox.NavBraille} 67 */ 68cvox.NavBraille.fromJson = function(json) { 69 if (typeof json.startIndex !== 'number' || 70 typeof json.endIndex !== 'number') { 71 throw 'Invalid start or end index in serialized NavBraille: ' + json; 72 } 73 return new cvox.NavBraille({ 74 text: cvox.Spannable.fromJson(json.spannable), 75 startIndex: json.startIndex, 76 endIndex: json.endIndex 77 }); 78}; 79 80 81/** 82 * @return {boolean} true if this braille description is empty. 83 */ 84cvox.NavBraille.prototype.isEmpty = function() { 85 return this.text.getLength() == 0; 86}; 87 88 89/** 90 * @return {string} A string representation of this object. 91 */ 92cvox.NavBraille.prototype.toString = function() { 93 return 'NavBraille(text="' + this.text.toString() + '" ' + 94 ' startIndex="' + this.startIndex + '" ' + 95 ' endIndex="' + this.endIndex + '")'; 96}; 97 98 99/** 100 * Returns a plain old data object with the same data. 101 * Suitable for JSON encoding. 102 * 103 * @return {{spannable: Object, 104 * startIndex: number, 105 * endIndex: number}} JSON equivalent. 106 */ 107cvox.NavBraille.prototype.toJson = function() { 108 return { 109 spannable: this.text.toJson(), 110 startIndex: this.startIndex, 111 endIndex: this.endIndex 112 }; 113}; 114 115 116/** 117 * Sends braille to the background page. 118 */ 119cvox.NavBraille.prototype.write = function() { 120 cvox.ChromeVox.braille.write(this); 121}; 122