braille_table.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 Holds information about a braille table. 7 */ 8 9goog.provide('cvox.BrailleTable'); 10 11 12/** 13 * @typedef {{ 14 * locale:string, 15 * dots:string, 16 * id:string, 17 * grade:(string|undefined), 18 * fileName:string 19 * }} 20 */ 21cvox.BrailleTable.Table; 22 23 24/** 25 * @const {string} 26 */ 27cvox.BrailleTable.TABLE_PATH = 'chromevox/background/braille/tables.json'; 28 29 30/** 31 * Retrieves a list of all available braille tables. 32 * @param {function(!Array.<cvox.BrailleTable.Table>)} callback 33 */ 34cvox.BrailleTable.getAll = function(callback) { 35 var url = chrome.extension.getURL(cvox.BrailleTable.TABLE_PATH); 36 if (!url) { 37 throw 'Invalid path: ' + cvox.BrailleTable.TABLE_PATH; 38 } 39 40 var xhr = new XMLHttpRequest(); 41 xhr.open('GET', url, true); 42 xhr.onreadystatechange = function() { 43 if (xhr.readyState == 4) { 44 if (xhr.status == 200) { 45 callback(/** @type {!Array.<cvox.BrailleTable.Table>} */ ( 46 JSON.parse(xhr.responseText))); 47 } 48 } 49 }; 50 xhr.send(); 51}; 52 53 54/** 55 * Finds a table in a list of tables by id. 56 * @param {!Array.<cvox.BrailleTable.Table>} tables tables to search in. 57 * @param {string} id id of table to find. 58 * @return {cvox.BrailleTable.Table} 59 */ 60cvox.BrailleTable.forId = function(tables, id) { 61 return tables.filter(function(table) { return table.id === id })[0] || null; 62}; 63 64 65/** 66 * Returns an uncontracted braille table corresponding to another, possibly 67 * contracted, table. If {@code table} is the lowest-grade table for its 68 * locale and dot count, {@code table} itself is returned. 69 * @param {!Array.<cvox.BrailleTable.Table>} tables tables to search in. 70 * @param {!cvox.BrailleTable.Table} table Table to match. 71 * @return {!cvox.BrailleTable.Table} Corresponding uncontracted table, 72 * or {@code table} if it is uncontracted. 73 */ 74cvox.BrailleTable.getUncontracted = function(tables, table) { 75 function mostUncontractedOf(current, candidate) { 76 // An 8 dot table for the same language is prefered over a 6 dot table 77 // even if the locales differ by region. 78 if (current.dots === '6' && 79 candidate.dots === '8' && 80 current.locale.lastIndexOf(candidate.locale, 0) == 0) { 81 return candidate; 82 } 83 if (current.locale === candidate.locale && 84 current.dots === candidate.dots && 85 goog.isDef(current.grade) && 86 goog.isDef(candidate.grade) && 87 candidate.grade < current.grade) { 88 return candidate; 89 } 90 return current; 91 } 92 return tables.reduce(mostUncontractedOf, table); 93}; 94