| 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 Keeps track of the current braille translators. | 6 * @fileoverview Keeps track of the current braille translators. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 goog.provide('cvox.BrailleTranslatorManager'); | 9 goog.provide('cvox.BrailleTranslatorManager'); |
| 10 | 10 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 * @private | 55 * @private |
| 56 */ | 56 */ |
| 57 this.uncontractedTranslator_ = null; | 57 this.uncontractedTranslator_ = null; |
| 58 /** | 58 /** |
| 59 * @type {string?} | 59 * @type {string?} |
| 60 * @private | 60 * @private |
| 61 */ | 61 */ |
| 62 this.uncontractedTableId_ = null; | 62 this.uncontractedTableId_ = null; |
| 63 | 63 |
| 64 this.fetchTables_(); | 64 this.fetchTables_(); |
| 65 this.loadLiblouis_(0); | 65 document.addEventListener('DOMContentLoaded', this.loadLiblouis_.bind(this), |
| 66 false); |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 cvox.BrailleTranslatorManager.prototype = { | 69 cvox.BrailleTranslatorManager.prototype = { |
| 69 /** | 70 /** |
| 70 * Adds a listener to be called whenever there is a change in the | 71 * Adds a listener to be called whenever there is a change in the |
| 71 * translator(s) returned by other methods of this instance. | 72 * translator(s) returned by other methods of this instance. |
| 72 * @param {function()} listener The listener. | 73 * @param {function()} listener The listener. |
| 73 */ | 74 */ |
| 74 addChangeListener: function(listener) { | 75 addChangeListener: function(listener) { |
| 75 this.changeListeners_.push(listener); | 76 this.changeListeners_.push(listener); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 */ | 197 */ |
| 197 fetchTables_: function() { | 198 fetchTables_: function() { |
| 198 cvox.BrailleTable.getAll(function(tables) { | 199 cvox.BrailleTable.getAll(function(tables) { |
| 199 this.tables_ = tables; | 200 this.tables_ = tables; |
| 200 this.refresh(); | 201 this.refresh(); |
| 201 }.bind(this)); | 202 }.bind(this)); |
| 202 }, | 203 }, |
| 203 | 204 |
| 204 /** | 205 /** |
| 205 * Loads the liblouis instance by attaching it to the document. | 206 * Loads the liblouis instance by attaching it to the document. |
| 206 * Retries a few times if the document doesn't yet have a body. | |
| 207 * @param {number} tries Number of attempts made so far. | |
| 208 * @private | 207 * @private |
| 209 */ | 208 */ |
| 210 loadLiblouis_: function(tries) { | 209 loadLiblouis_: function() { |
| 211 if (!document.body) { | 210 // Cast away nullability. When the document is loaded, it will always |
| 212 if (tries <= 5) { | 211 // have a body. |
| 213 window.setTimeout(this.loadLiblouis_.bind(this, tries + 1), 500); | 212 this.liblouis_.attachToElement( |
| 214 } else { | 213 /** @type {!HTMLBodyElement} */ (document.body)); |
| 215 console.error( | |
| 216 'Timeout waiting for document.body; not initializing braille.'); | |
| 217 } | |
| 218 return; | |
| 219 } | |
| 220 | |
| 221 this.liblouis_.attachToElement(document.body); | |
| 222 }, | 214 }, |
| 223 | 215 |
| 224 /** | 216 /** |
| 225 * @return {!cvox.LibLouis} The liblouis instance used by this object. | 217 * @return {!cvox.LibLouis} The liblouis instance used by this object. |
| 226 */ | 218 */ |
| 227 getLibLouisForTest: function() { | 219 getLibLouisForTest: function() { |
| 228 return this.liblouis_; | 220 return this.liblouis_; |
| 229 }, | 221 }, |
| 230 | 222 |
| 231 /** | 223 /** |
| 232 * @return {!Array.<cvox.BrailleTable.Table>} The currently loaded braille | 224 * @return {!Array.<cvox.BrailleTable.Table>} The currently loaded braille |
| 233 * tables, or an empty array if they are not yet loaded. | 225 * tables, or an empty array if they are not yet loaded. |
| 234 */ | 226 */ |
| 235 getTablesForTest: function() { | 227 getTablesForTest: function() { |
| 236 return this.tables_; | 228 return this.tables_; |
| 237 } | 229 } |
| 238 }; | 230 }; |
| OLD | NEW |