Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 ChromeVox panel. | |
| 7 * | |
| 8 */ | |
| 9 | |
| 10 goog.provide('Panel'); | |
| 11 | |
| 12 goog.require('Msgs'); | |
| 13 goog.require('PanelCommand'); | |
| 14 | |
| 15 function $(id) { | |
| 16 return document.getElementById(id); | |
| 17 } | |
| 18 | |
| 19 /** | |
| 20 * Class to manage the panel. | |
| 21 * @constructor | |
| 22 */ | |
| 23 Panel = function() { | |
| 24 }; | |
| 25 | |
| 26 /** | |
| 27 * Initialize the panel. | |
| 28 */ | |
| 29 Panel.init = function() { | |
| 30 /** @type {Element} @private */ | |
| 31 this.speechContainer_ = $('speech-container'); | |
| 32 | |
| 33 /** @type {Element} @private */ | |
| 34 this.speechElement_ = $('speech'); | |
| 35 | |
| 36 /** @type {Element} @private */ | |
| 37 this.brailleContainer_ = $('braille-container'); | |
| 38 | |
| 39 /** @type {Element} @private */ | |
| 40 this.brailleTextElement_ = $('braille-text'); | |
| 41 | |
| 42 /** @type {Element} @private */ | |
| 43 this.brailleCellsElement_ = $('braille-cells'); | |
| 44 | |
| 45 Panel.updateFromPrefs(); | |
| 46 window.addEventListener('storage', function(event) { | |
| 47 if (event.key == 'brailleCaptions') { | |
| 48 Panel.updateFromPrefs(); | |
| 49 } | |
| 50 }, false); | |
| 51 | |
| 52 window.addEventListener('message', function(message) { | |
| 53 var command = JSON.parse(message.data); | |
| 54 Panel.exec(/** @type {PanelCommand} */(command)); | |
| 55 }, false); | |
| 56 | |
| 57 $('menu').addEventListener('click', Panel.onMenu, false); | |
| 58 $('options').addEventListener('click', Panel.onOptions, false); | |
| 59 $('close').addEventListener('click', Panel.onClose, false); | |
| 60 | |
| 61 Msgs.addTranslatedMessagesToDom(document); | |
| 62 }; | |
| 63 | |
| 64 /** | |
| 65 * Update the display based on prefs. | |
| 66 */ | |
| 67 Panel.updateFromPrefs = function() { | |
| 68 if (localStorage['brailleCaptions'] === String(true)) { | |
| 69 this.speechContainer_.style.visibility = 'hidden'; | |
| 70 this.brailleContainer_.style.visibility = 'visible'; | |
| 71 } else { | |
| 72 this.speechContainer_.style.visibility = 'visible'; | |
| 73 this.brailleContainer_.style.visibility = 'hidden'; | |
| 74 } | |
| 75 }; | |
| 76 | |
| 77 /** | |
| 78 * Execute a command to update the panel. | |
| 79 * | |
| 80 * @param {PanelCommand} command The command to execute. | |
| 81 */ | |
| 82 Panel.exec = function(command) { | |
| 83 /** | |
| 84 * Escape text so it can be safely added to HTML. | |
| 85 * @param {*} str Text to be added to HTML, will be cast to string. | |
| 86 * @return {string} The escaped string. | |
| 87 */ | |
| 88 function escapeForHtml(str) { | |
| 89 return String(str) | |
| 90 .replace(/&/g, '&') | |
| 91 .replace(/</g, '<') | |
| 92 .replace(/\>/g, '>') | |
| 93 .replace(/"/g, '"') | |
| 94 .replace(/'/g, ''') | |
| 95 .replace(/\//g, '/'); | |
| 96 } | |
| 97 | |
| 98 switch (command.type) { | |
| 99 case PanelCommandType.CLEAR_SPEECH: | |
| 100 this.speechElement_.innerHTML = ''; | |
| 101 break; | |
| 102 case PanelCommandType.ADD_NORMAL_SPEECH: | |
| 103 if (this.speechElement_.innerHTML != '') { | |
| 104 this.speechElement_.innerHTML += ' '; | |
| 105 } | |
| 106 this.speechElement_.innerHTML += '<span class="usertext">' + | |
| 107 escapeForHtml(command.data) + | |
| 108 '</span>'; | |
| 109 break; | |
| 110 case PanelCommandType.ADD_ANNOTATION_SPEECH: | |
| 111 if (this.speechElement_.innerHTML != '') { | |
| 112 this.speechElement_.innerHTML += ' '; | |
| 113 } | |
| 114 this.speechElement_.innerHTML += escapeForHtml(command.data); | |
| 115 break; | |
| 116 case PanelCommandType.UPDATE_BRAILLE: | |
| 117 this.brailleTextElement_.textContent = | |
| 118 escapeForHtml(command.data.text); | |
|
Peter Lundblad
2015/11/10 10:46:36
Shouldn't escape when setting textContent, in this
dmazzoni
2015/11/10 17:33:17
Done.
| |
| 119 this.brailleCellsElement_.textContent = | |
| 120 escapeForHtml(command.data.braille); | |
|
Peter Lundblad
2015/11/10 10:46:36
Same here.
dmazzoni
2015/11/10 17:33:17
Done.
| |
| 121 break; | |
| 122 } | |
| 123 }; | |
| 124 | |
| 125 /** | |
| 126 * Open the ChromeVox Menu. | |
| 127 */ | |
| 128 Panel.onMenu = function() { | |
| 129 window.location = '#fullscreen'; | |
| 130 // TODO(dmazzoni): implement the menu UI here. | |
| 131 }; | |
| 132 | |
| 133 /** | |
| 134 * Open the ChromeVox Options. | |
| 135 */ | |
| 136 Panel.onOptions = function() { | |
| 137 var bkgnd = | |
| 138 chrome.extension.getBackgroundPage()['global']['backgroundObj']; | |
| 139 bkgnd['showOptionsPage'](); | |
| 140 window.location = '#'; | |
| 141 }; | |
| 142 | |
| 143 /** | |
| 144 * Exit ChromeVox. | |
| 145 */ | |
| 146 Panel.onClose = function() { | |
| 147 window.location = '#close'; | |
| 148 }; | |
| 149 | |
| 150 window.addEventListener('load', function() { | |
| 151 Panel.init(); | |
| 152 }, false); | |
| OLD | NEW |