OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Holds information about a braille table. | 6 * @fileoverview Holds information about a braille table. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('cvox.BrailleTable'); | 9 goog.provide('cvox.BrailleTable'); |
10 | 10 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 var url = chrome.extension.getURL(cvox.BrailleTable.TABLE_PATH); | 51 var url = chrome.extension.getURL(cvox.BrailleTable.TABLE_PATH); |
52 if (!url) { | 52 if (!url) { |
53 throw 'Invalid path: ' + cvox.BrailleTable.TABLE_PATH; | 53 throw 'Invalid path: ' + cvox.BrailleTable.TABLE_PATH; |
54 } | 54 } |
55 | 55 |
56 var xhr = new XMLHttpRequest(); | 56 var xhr = new XMLHttpRequest(); |
57 xhr.open('GET', url, true); | 57 xhr.open('GET', url, true); |
58 xhr.onreadystatechange = function() { | 58 xhr.onreadystatechange = function() { |
59 if (xhr.readyState == 4) { | 59 if (xhr.readyState == 4) { |
60 if (xhr.status == 200) { | 60 if (xhr.status == 200) { |
61 callback( | 61 callback(appendCommonFilename( |
62 appendCommonFilename( | 62 /** @type {!Array<cvox.BrailleTable.Table>} */ ( |
63 /** @type {!Array<cvox.BrailleTable.Table>} */ ( | 63 JSON.parse(xhr.responseText)))); |
64 JSON.parse(xhr.responseText)))); | |
65 } | 64 } |
66 } | 65 } |
67 }; | 66 }; |
68 xhr.send(); | 67 xhr.send(); |
69 }; | 68 }; |
70 | 69 |
71 | 70 |
72 /** | 71 /** |
73 * Finds a table in a list of tables by id. | 72 * Finds a table in a list of tables by id. |
74 * @param {!Array<cvox.BrailleTable.Table>} tables tables to search in. | 73 * @param {!Array<cvox.BrailleTable.Table>} tables tables to search in. |
75 * @param {string} id id of table to find. | 74 * @param {string} id id of table to find. |
76 * @return {cvox.BrailleTable.Table} The found table, or null if not found. | 75 * @return {cvox.BrailleTable.Table} The found table, or null if not found. |
77 */ | 76 */ |
78 cvox.BrailleTable.forId = function(tables, id) { | 77 cvox.BrailleTable.forId = function(tables, id) { |
79 return tables.filter(function(table) { return table.id === id; })[0] || null; | 78 return tables.filter(function(table) { |
| 79 return table.id === id; |
| 80 })[0] || |
| 81 null; |
80 }; | 82 }; |
81 | 83 |
82 | 84 |
83 /** | 85 /** |
84 * Returns an uncontracted braille table corresponding to another, possibly | 86 * Returns an uncontracted braille table corresponding to another, possibly |
85 * contracted, table. If {@code table} is the lowest-grade table for its | 87 * contracted, table. If {@code table} is the lowest-grade table for its |
86 * locale and dot count, {@code table} itself is returned. | 88 * locale and dot count, {@code table} itself is returned. |
87 * @param {!Array<cvox.BrailleTable.Table>} tables tables to search in. | 89 * @param {!Array<cvox.BrailleTable.Table>} tables tables to search in. |
88 * @param {!cvox.BrailleTable.Table} table Table to match. | 90 * @param {!cvox.BrailleTable.Table} table Table to match. |
89 * @return {!cvox.BrailleTable.Table} Corresponding uncontracted table, | 91 * @return {!cvox.BrailleTable.Table} Corresponding uncontracted table, |
90 * or {@code table} if it is uncontracted. | 92 * or {@code table} if it is uncontracted. |
91 */ | 93 */ |
92 cvox.BrailleTable.getUncontracted = function(tables, table) { | 94 cvox.BrailleTable.getUncontracted = function(tables, table) { |
93 function mostUncontractedOf(current, candidate) { | 95 function mostUncontractedOf(current, candidate) { |
94 // An 8 dot table for the same language is prefered over a 6 dot table | 96 // An 8 dot table for the same language is prefered over a 6 dot table |
95 // even if the locales differ by region. | 97 // even if the locales differ by region. |
96 if (current.dots === '6' && | 98 if (current.dots === '6' && candidate.dots === '8' && |
97 candidate.dots === '8' && | |
98 current.locale.lastIndexOf(candidate.locale, 0) == 0) { | 99 current.locale.lastIndexOf(candidate.locale, 0) == 0) { |
99 return candidate; | 100 return candidate; |
100 } | 101 } |
101 if (current.locale === candidate.locale && | 102 if (current.locale === candidate.locale && |
102 current.dots === candidate.dots && | 103 current.dots === candidate.dots && goog.isDef(current.grade) && |
103 goog.isDef(current.grade) && | 104 goog.isDef(candidate.grade) && candidate.grade < current.grade) { |
104 goog.isDef(candidate.grade) && | |
105 candidate.grade < current.grade) { | |
106 return candidate; | 105 return candidate; |
107 } | 106 } |
108 return current; | 107 return current; |
109 } | 108 } |
110 return tables.reduce(mostUncontractedOf, table); | 109 return tables.reduce(mostUncontractedOf, table); |
111 }; | 110 }; |
112 | 111 |
113 | 112 |
114 /** | 113 /** |
115 * @param {!cvox.BrailleTable.Table} table Table to get name for. | 114 * @param {!cvox.BrailleTable.Table} table Table to get name for. |
116 * @return {string} Localized display name. | 115 * @return {string} Localized display name. |
117 */ | 116 */ |
118 cvox.BrailleTable.getDisplayName = function(table) { | 117 cvox.BrailleTable.getDisplayName = function(table) { |
119 var localeName = Msgs.getLocaleDisplayName(table.locale); | 118 var localeName = Msgs.getLocaleDisplayName(table.locale); |
120 if (!table.grade && !table.variant) { | 119 if (!table.grade && !table.variant) { |
121 return localeName; | 120 return localeName; |
122 } else if (table.grade && !table.variant) { | 121 } else if (table.grade && !table.variant) { |
123 return Msgs.getMsg('braille_table_name_with_grade', | 122 return Msgs.getMsg( |
124 [localeName, table.grade]); | 123 'braille_table_name_with_grade', [localeName, table.grade]); |
125 } else if (!table.grade && table.variant) { | 124 } else if (!table.grade && table.variant) { |
126 return Msgs.getMsg('braille_table_name_with_variant', | 125 return Msgs.getMsg( |
127 [localeName, table.variant]); | 126 'braille_table_name_with_variant', [localeName, table.variant]); |
128 } else { | 127 } else { |
129 return Msgs.getMsg('braille_table_name_with_variant_and_grade', | 128 return Msgs.getMsg( |
130 [localeName, table.variant, table.grade]); | 129 'braille_table_name_with_variant_and_grade', |
| 130 [localeName, table.variant, table.grade]); |
131 } | 131 } |
132 }; | 132 }; |
OLD | NEW |