define(['dialogHelper', 'voiceReceiver', 'voiceProcessor', 'globalize', 'emby-button', 'css!./voice.css', 'material-icons', 'css!./../formdialog'], function (dialogHelper, voicereceiver, voiceprocessor, globalize) { 'use strict'; var lang = 'en-US'; /// Shuffle array. /// The array. /// array function shuffleArray(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } /// Gets sample commands. /// The sample commands. function getSampleCommands(groupid) { return voiceprocessor.getCommandGroups().then(function (commandGroups) { groupid = typeof (groupid) !== 'undefined' ? groupid : ''; var commands = []; commandGroups.map(function (group) { if ((group.items && group.items.length > 0) && (groupid !== group.groupid || groupid === '')) { group.items.map(function (item) { if (item.commandtemplates && item.commandtemplates.length > 0) { item.commandtemplates.map(function (templates) { commands.push(templates); }); } }); } }); return shuffleArray(commands); }); } /// Gets command group. /// The groupid. /// The command group. function getCommandGroup(groupid) { return voicereceiver.getCommandGroups() .then(function (commandgroups) { if (commandgroups) { var idx = -1; idx = commandgroups.map(function (e) { return e.groupid; }).indexOf(groupid); if (idx > -1) { return commandgroups[idx]; } else { return null; } } else { return null; } }); } /// Renders the sample commands. /// The element. /// The commands. /// . function renderSampleCommands(elem, commands) { commands.length = Math.min(commands.length, 4); commands = commands.map(function (c) { return '
"' + c + '"
'; }).join(''); elem.querySelector('.exampleCommands').innerHTML = commands; } var currentDialog; /// Shows the voice help. /// . function showVoiceHelp(groupid, title) { console.log("Showing Voice Help", groupid, title); var isNewDialog = false; var dlg; function onCancelClick() { dialogHelper.close(dlg); } if (!currentDialog) { isNewDialog = true; dlg = dialogHelper.createDialog({ size: 'medium', removeOnClose: true }); dlg.classList.add('formDialog'); var html = ''; html += '
'; html += ''; html += '

'; html += globalize.translate('sharedcomponents#VoiceInput'); html += '

'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '

' + globalize.translate('sharedcomponents#HeaderSaySomethingLike') + '

'; html += '
'; html += '
'; // defaultVoiceHelp html += '
'; html += '
'; html += '

' + globalize.translate('sharedcomponents#HeaderYouSaid') + '

'; html += '

'; html += '

' + globalize.translate('sharedcomponents#MessageWeDidntRecognizeCommand') + '

'; html += '
'; html += ''; html += '

' + globalize.translate('sharedcomponents#MessageIfYouBlockedVoice') + '

'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; dlg.innerHTML = html; dialogHelper.open(dlg); currentDialog = dlg; dlg.addEventListener('close', function () { voicereceiver.cancel(); currentDialog = null; }); var closeButtons = dlg.querySelectorAll('.btnCancelVoiceInput'); for (var i = 0, length = closeButtons.length; i < length; i++) { closeButtons[i].addEventListener('click', onCancelClick); } dlg.querySelector('.btnRetry').addEventListener('click', function () { dlg.querySelector('.unrecognizedCommand').classList.add('hide'); dlg.querySelector('.defaultVoiceHelp').classList.remove('hide'); listen(); }); } dlg = currentDialog; if (groupid) { getCommandGroup(groupid) .then( function (grp) { dlg.querySelector('#voiceDialogGroupName').innerText = ' ' + grp.name; }); getSampleCommands(groupid) .then(function (commands) { renderSampleCommands(currentDialog, commands); listen(); }) .catch(function (e) { console.log("Error", e); }); } else if (isNewDialog) { getSampleCommands() .then(function (commands) { renderSampleCommands(currentDialog, commands); }); } } function processInput(input) { return voiceprocessor.processTranscript(input); } /// Shows the unrecognized command help. /// . function showUnrecognizedCommandHelp(command) { //speak("I don't understend this command"); if (command) { currentDialog.querySelector('.voiceInputText').innerText = command; } currentDialog.querySelector('.unrecognizedCommand').classList.remove('hide'); currentDialog.querySelector('.defaultVoiceHelp').classList.add('hide'); } /// Shows the commands. /// The create user interface. /// . function showCommands(result) { //speak('Hello, what can I do for you?'); if (result) { showVoiceHelp(result.groupid, result.name); } else { showVoiceHelp(); } } function resetDialog() { if (currentDialog) { currentDialog.querySelector('.unrecognizedCommand').classList.add('hide'); currentDialog.querySelector('.defaultVoiceHelp').classList.remove('hide'); } } function showDialog() { resetDialog(); showCommands(); listen(); } function listen() { voicereceiver.listen({ lang: lang || "en-US" }).then(processInput).then(function (result) { closeDialog(); // Put a delay here in case navigation/popstate is involved. Allow that to flush out setTimeout(function () { result.fn(); }, 1); }, function (result) { if (result.error === 'group') { showVoiceHelp(result.item.groupid, result.groupName); return; } showUnrecognizedCommandHelp(result.text || ''); }); } function closeDialog() { dialogHelper.close(currentDialog); voicereceiver.cancel(); } /// An enum constant representing the window. voice input manager option. return { showDialog: showDialog }; });