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 Holds information about a braille table.
9 goog
.provide('cvox.BrailleTable');
17 * grade:(string|undefined),
18 * variant:(string|undefined),
22 cvox
.BrailleTable
.Table
;
28 cvox
.BrailleTable
.TABLE_PATH
= 'braille/tables.json';
35 cvox
.BrailleTable
.COMMON_DEFS_FILENAME_
= 'cvox-common.cti';
39 * Retrieves a list of all available braille tables.
40 * @param {function(!Array<cvox.BrailleTable.Table>)} callback
41 * Called asynchronously with an array of tables.
43 cvox
.BrailleTable
.getAll = function(callback
) {
44 function appendCommonFilename(tables
) {
45 // Append the common definitions to all table filenames.
46 tables
.forEach(function(table
) {
47 table
.fileNames
+= (',' + cvox
.BrailleTable
.COMMON_DEFS_FILENAME_
);
51 var url
= chrome
.extension
.getURL(cvox
.BrailleTable
.TABLE_PATH
);
53 throw 'Invalid path: ' + cvox
.BrailleTable
.TABLE_PATH
;
56 var xhr
= new XMLHttpRequest();
57 xhr
.open('GET', url
, true);
58 xhr
.onreadystatechange = function() {
59 if (xhr
.readyState
== 4) {
60 if (xhr
.status
== 200) {
63 /** @type {!Array<cvox.BrailleTable.Table>} */ (
64 JSON
.parse(xhr
.responseText
))));
73 * Finds a table in a list of tables by id.
74 * @param {!Array<cvox.BrailleTable.Table>} tables tables to search in.
75 * @param {string} id id of table to find.
76 * @return {cvox.BrailleTable.Table} The found table, or null if not found.
78 cvox
.BrailleTable
.forId = function(tables
, id
) {
79 return tables
.filter(function(table
) { return table
.id
=== id
})[0] || null;
84 * Returns an uncontracted braille table corresponding to another, possibly
85 * contracted, table. If {@code table} is the lowest-grade table for its
86 * locale and dot count, {@code table} itself is returned.
87 * @param {!Array<cvox.BrailleTable.Table>} tables tables to search in.
88 * @param {!cvox.BrailleTable.Table} table Table to match.
89 * @return {!cvox.BrailleTable.Table} Corresponding uncontracted table,
90 * or {@code table} if it is uncontracted.
92 cvox
.BrailleTable
.getUncontracted = function(tables
, table
) {
93 function mostUncontractedOf(current
, candidate
) {
94 // An 8 dot table for the same language is prefered over a 6 dot table
95 // even if the locales differ by region.
96 if (current
.dots
=== '6' &&
97 candidate
.dots
=== '8' &&
98 current
.locale
.lastIndexOf(candidate
.locale
, 0) == 0) {
101 if (current
.locale
=== candidate
.locale
&&
102 current
.dots
=== candidate
.dots
&&
103 goog
.isDef(current
.grade
) &&
104 goog
.isDef(candidate
.grade
) &&
105 candidate
.grade
< current
.grade
) {
110 return tables
.reduce(mostUncontractedOf
, table
);
115 * @param {!cvox.BrailleTable.Table} table Table to get name for.
116 * @return {string} Localized display name.
118 cvox
.BrailleTable
.getDisplayName = function(table
) {
119 var msgs
= cvox
.ChromeVox
.msgs
;
120 var localeName
= msgs
.getLocaleDisplayName(table
.locale
);
121 if (!table
.grade
&& !table
.variant
) {
123 } else if (table
.grade
&& !table
.variant
) {
124 return msgs
.getMsg('braille_table_name_with_grade',
125 [localeName
, table
.grade
]);
126 } else if (!table
.grade
&& table
.variant
) {
127 return msgs
.getMsg('braille_table_name_with_variant',
128 [localeName
, table
.variant
]);
130 return msgs
.getMsg('braille_table_name_with_variant_and_grade',
131 [localeName
, table
.variant
, table
.grade
]);