node_state.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 The cvox.NodeState typedef.
7 */
8
9goog.provide('cvox.NodeState');
10goog.provide('cvox.NodeStateUtil');
11
12/**
13 * Holds the state of a node.  It is an Array or Arrays of strings and numbers.
14 * Each sub array is in the format:
15 * [state, opt_arg, opt_arg, ...].  These sub arrays map directly to a
16 * cvox.ChromeVox.getMsg() call. For example [list_position, 3, 5] maps to
17 * getMsg('list_position', [3, 5]);
18 *
19 * @typedef {!Array.<!Array.<string|number>>}
20 */
21cvox.NodeState;
22
23/**
24 * Returns a localized, readable string with the NodeState.
25 *
26 * NOTE(deboer): Once AriaUtil and DomUtil are using NodeState exclusively, this
27 * function can be moved into DescriptionUtil, removing the cvox.ChromeVox
28 * dependency here.
29 *
30 * @param {cvox.NodeState} state The node state.
31 * @return {string} The readable state string.
32 */
33cvox.NodeStateUtil.expand = function(state) {
34  try {
35    return state.map(function(s) {
36      if (s.length < 1) {
37        throw new Error('cvox.NodeState must have at least one entry');
38      }
39      var args = s.slice(1).map(function(a) {
40        if (typeof a == 'number') {
41          return cvox.ChromeVox.msgs.getNumber(a);
42        }
43        return a;
44      });
45      return cvox.ChromeVox.msgs.getMsg(/** @type {string} */ (s[0]), args);
46    }).join(' ');
47  } catch (e) {
48    throw new Error('error: ' + e + ' state: ' + state);
49  }
50};
51