summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/braille/braille_table.js
blob: 8957d1c241f6f486e18196e1db28cd03fac27ae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Holds information about a braille table.
 */

goog.provide('cvox.BrailleTable');


/**
 * @typedef {{
 *   locale:string,
 *   dots:string,
 *   id:string,
 *   grade:(string|undefined),
 *   variant:(string|undefined),
 *   fileNames:string
 * }}
 */
cvox.BrailleTable.Table;


/**
 * @const {string}
 */
cvox.BrailleTable.TABLE_PATH = 'braille/tables.json';


/**
 * @const {string}
 * @private
 */
cvox.BrailleTable.COMMON_DEFS_FILENAME_ = 'cvox-common.cti';


/**
 * Retrieves a list of all available braille tables.
 * @param {function(!Array<cvox.BrailleTable.Table>)} callback
 *     Called asynchronously with an array of tables.
 */
cvox.BrailleTable.getAll = function(callback) {
  function appendCommonFilename(tables) {
    // Append the common definitions to all table filenames.
    tables.forEach(function(table) {
      table.fileNames += (',' + cvox.BrailleTable.COMMON_DEFS_FILENAME_);
    });
    return tables;
  }
  var url = chrome.extension.getURL(cvox.BrailleTable.TABLE_PATH);
  if (!url) {
    throw 'Invalid path: ' + cvox.BrailleTable.TABLE_PATH;
  }

  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      if (xhr.status == 200) {
        callback(
            appendCommonFilename(
                /** @type {!Array<cvox.BrailleTable.Table>} */ (
                    JSON.parse(xhr.responseText))));
      }
    }
  };
  xhr.send();
};


/**
 * Finds a table in a list of tables by id.
 * @param {!Array<cvox.BrailleTable.Table>} tables tables to search in.
 * @param {string} id id of table to find.
 * @return {cvox.BrailleTable.Table} The found table, or null if not found.
 */
cvox.BrailleTable.forId = function(tables, id) {
  return tables.filter(function(table) { return table.id === id })[0] || null;
};


/**
 * Returns an uncontracted braille table corresponding to another, possibly
 * contracted, table.  If {@code table} is the lowest-grade table for its
 * locale and dot count, {@code table} itself is returned.
 * @param {!Array<cvox.BrailleTable.Table>} tables tables to search in.
 * @param {!cvox.BrailleTable.Table} table Table to match.
 * @return {!cvox.BrailleTable.Table} Corresponding uncontracted table,
 *     or {@code table} if it is uncontracted.
 */
cvox.BrailleTable.getUncontracted = function(tables, table) {
  function mostUncontractedOf(current, candidate) {
    // An 8 dot table for the same language is prefered over a 6 dot table
    // even if the locales differ by region.
    if (current.dots === '6' &&
        candidate.dots === '8' &&
        current.locale.lastIndexOf(candidate.locale, 0) == 0) {
      return candidate;
    }
    if (current.locale === candidate.locale &&
        current.dots === candidate.dots &&
        goog.isDef(current.grade) &&
        goog.isDef(candidate.grade) &&
        candidate.grade < current.grade) {
      return candidate;
    }
    return current;
  }
  return tables.reduce(mostUncontractedOf, table);
};


/**
 * @param {!cvox.BrailleTable.Table} table Table to get name for.
 * @return {string} Localized display name.
 */
cvox.BrailleTable.getDisplayName = function(table) {
  var localeName = Msgs.getLocaleDisplayName(table.locale);
  if (!table.grade && !table.variant) {
    return localeName;
  } else if (table.grade && !table.variant) {
    return Msgs.getMsg('braille_table_name_with_grade',
                       [localeName, table.grade]);
  } else if (!table.grade && table.variant) {
    return Msgs.getMsg('braille_table_name_with_variant',
                       [localeName, table.variant]);
  } else {
    return Msgs.getMsg('braille_table_name_with_variant_and_grade',
                       [localeName, table.variant, table.grade]);
  }
};