| 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 var appWindow = chrome.app.window.current(); | 5 var appWindow = chrome.app.window.current(); |
| 6 | 6 |
| 7 document.addEventListener('DOMContentLoaded', function() { | 7 document.addEventListener('DOMContentLoaded', function() { |
| 8 var flow = new Flow(); | 8 var flow = new Flow(); |
| 9 flow.startFlow(); | 9 flow.startFlow(); |
| 10 | 10 |
| 11 var closeAppWindow = function(e) { | 11 var closeAppWindow = function(e) { |
| 12 var classes = e.target.classList; | 12 var classes = e.target.classList; |
| 13 if (classes.contains('close') || classes.contains('finish-button')) { | 13 if (classes.contains('close') || classes.contains('finish-button')) { |
| 14 flow.stopTraining(); |
| 14 appWindow.close(); | 15 appWindow.close(); |
| 15 e.preventDefault(); | 16 e.preventDefault(); |
| 16 } | 17 } |
| 17 }; | 18 }; |
| 18 | 19 |
| 19 // TODO(kcarattini): Cancel training in NaCl module for hotword-only and | |
| 20 // speech-training close and cancel buttons. | |
| 21 $('steps').addEventListener('click', closeAppWindow); | 20 $('steps').addEventListener('click', closeAppWindow); |
| 22 | 21 |
| 23 $('hw-agree-button').addEventListener('click', function(e) { | 22 $('hw-agree-button').addEventListener('click', function(e) { |
| 24 flow.advanceStep(); | 23 flow.advanceStep(); |
| 24 flow.startTraining(); |
| 25 e.preventDefault(); | 25 e.preventDefault(); |
| 26 }); | 26 }); |
| 27 | 27 |
| 28 $('settings-link').addEventListener('click', function(e) { | 28 $('settings-link').addEventListener('click', function(e) { |
| 29 chrome.browser.openTab({'url': 'chrome://settings'}, function() {}); | 29 chrome.browser.openTab({'url': 'chrome://settings'}, function() {}); |
| 30 e.preventDefault(); | 30 e.preventDefault(); |
| 31 }); | 31 }); |
| 32 | |
| 33 /** | |
| 34 * Updates steps of the training UI. | |
| 35 * @param {string} pagePrefix Prefix of the element ids for this page. | |
| 36 * @param {Event} e The click event. | |
| 37 */ | |
| 38 function doTraining(pagePrefix, e) { | |
| 39 // TODO(kcarattini): Update this to respond to a hotword trigger once | |
| 40 // speech training is implemented. | |
| 41 var steps = $(pagePrefix + '-training').querySelectorAll('.train'); | |
| 42 var index = Array.prototype.indexOf.call(steps, e.currentTarget); | |
| 43 if (!steps[index]) | |
| 44 return; | |
| 45 | |
| 46 // TODO(kcarattini): Localize this string. | |
| 47 steps[index].querySelector('.text').textContent = 'Recorded'; | |
| 48 steps[index].classList.remove('listening'); | |
| 49 steps[index].classList.add('recorded'); | |
| 50 | |
| 51 if (steps[index + 1]) { | |
| 52 steps[index + 1].classList.remove('not-started'); | |
| 53 steps[index + 1].classList.add('listening'); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 /** | |
| 58 * Adds event listeners to the training UI. | |
| 59 * @param {string} pagePrefix Prefix of the element ids for this page. | |
| 60 */ | |
| 61 function addTrainingEventListeners(pagePrefix) { | |
| 62 var steps = $(pagePrefix + '-training').querySelectorAll('.train'); | |
| 63 for (var i = 0; i < steps.length; ++i) { | |
| 64 steps[i].addEventListener('click', function(e) { | |
| 65 doTraining(pagePrefix, e); | |
| 66 e.preventDefault(); | |
| 67 | |
| 68 if (e.currentTarget != steps[steps.length - 1]) | |
| 69 return; | |
| 70 | |
| 71 // Update the 'Cancel' button. | |
| 72 var buttonElem = $(pagePrefix + '-cancel-button'); | |
| 73 // TODO(kcarattini): Localize this string. | |
| 74 buttonElem.textContent = 'Please wait ...'; | |
| 75 buttonElem.classList.add('grayed-out'); | |
| 76 buttonElem.classList.remove('finish-button'); | |
| 77 | |
| 78 setTimeout(function() { | |
| 79 if (chrome.hotwordPrivate.setAudioLoggingEnabled) | |
| 80 chrome.hotwordPrivate.setAudioLoggingEnabled(true, function() {}); | |
| 81 | |
| 82 if (chrome.hotwordPrivate.setHotwordAlwaysOnSearchEnabled) { | |
| 83 chrome.hotwordPrivate.setHotwordAlwaysOnSearchEnabled(true, | |
| 84 flow.advanceStep.bind(flow)); | |
| 85 } | |
| 86 }, 2000); | |
| 87 }); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 addTrainingEventListeners('speech-training'); | |
| 92 addTrainingEventListeners('hotword-only'); | |
| 93 }); | 32 }); |
| OLD | NEW |