JS/UI for sending volume

This commit is contained in:
Brandon Thomas committed 2016-03-06 04:28:31 -05:00
1 parent 91a61537bb
commit 8d2637ea72
6 files changed
+42 -15

No files matched your search

+10 -3
View File
@@ -7,12 +7,19 @@ h1 {
}
input {
width: 70%;
border: 1px solid #ccc;
padding: 8px;
margin-bottom: 1em;
}
input#text {
width: 70%;
}
input#vol {
width: 30px;
}
button {
background-color: #eee;
border: 1px solid #ccc;
@@ -42,13 +49,13 @@ select {
}
.error input {
.error input#text {
border-color: #f55;
background-color: #fee;
color: #500;
}
.ok input {
.ok input#text {
border-color: #5a5;
background-color: #efe;
}
+5 -4
View File
@@ -6,7 +6,7 @@
window.Events = {
/** Handle user input inside the input box. */
handleTyping: function(ev) {
var sentence = $('input').val();
var sentence = $('input#text').val();
if (ev.keyCode === 27) {
$(this).val(''); // ESC key.
@@ -20,7 +20,7 @@
handleBodyTyping: function(ev) {
if (ev.keyCode === 27) {
// Handle ESC key.
$('input').select();
$('input#text').select();
Ui.clearInput();
Ui.clearSuggestedWords();
Ui.setFromInput('');
@@ -38,12 +38,13 @@
var $audio = $('#sound'),
sentence = $('#text').val(),
speaker = $('select').val(),
volume = $('input#vol').val(),
query = encodeURIComponent(sentence),
url = '/speak?v=' + speaker + '&s=' + query;
url = '/speak?v=' + speaker + '&s=' + query + '&vol=' + volume;
ev.preventDefault();
Url.setState(speaker, sentence);
Url.setState(speaker, sentence, volume);
Sound.play(url);
return false;
+5 -2
View File
@@ -6,7 +6,7 @@
/** Install event handlers. */
var installEventHandlers = function() {
$('form').submit(function(ev) { return Events.handleFormSubmit(ev); });
$('input').on('keyup', function(ev) { return Events.handleTyping(ev); });
$('input#text').on('keyup', function(ev) { return Events.handleTyping(ev); });
$('body').on('keyup', function(ev) { return Events.handleBodyTyping(ev); });
$('select').on('change', function(ev) { return Events.handleVoiceSelect(ev); });
}
@@ -15,6 +15,7 @@
var uiInitialize = function() {
var sentence = Url.getSentence(),
speaker = Url.getSpeaker() || Library.getDefaultSpeaker(),
volume = Url.getVolume() || 1.0, // TODO: Better place for default volume
urlPreviousState = false;
if (speaker) {
@@ -22,10 +23,12 @@
}
if (sentence.length > 0) {
$('input').val(sentence);
$('input#text').val(sentence);
urlPreviousState = true;
}
$('input#vol').val(volume);
Library.load(function(library) { Ui.libraryLoadCallback(library); });
Sound.install();
+2 -2
View File
@@ -40,7 +40,7 @@
/** Clear the input box. */
clearInput: function() {
$('input').val('');
$('input#text').val('');
},
// TODO: Rename, redesign.
@@ -58,7 +58,7 @@
/** Set state with respect to input. */
setStateFromInput: function() {
var rawSentence = $('input').val();
var rawSentence = $('input#text').val();
if (this.currentVocabulary.checkSentence(rawSentence)) {
this.setState('ok');
} else {
+19 -4
View File
@@ -10,6 +10,9 @@
/** JSON key for the `sentence` param. */
_SENTENCE_KEY: 's',
/** JSON key for the `volume` param. */
_VOLUME_KEY: 'vol',
/** Get the sentence from the `window.location`. */
getSentence: function() {
var state = this.parseState(window.location),
@@ -32,24 +35,36 @@
}
},
/** Get the volume from the `window.location`. */
getVolume: function() {
var state = this.parseState(window.location);
if (!state || !(this._VOLUME_KEY in state)) {
return null;
} else {
return state[this._VOLUME_KEY];
}
},
// TODO: Independent functions to set both speaker and sentence separately.
/** Set the `window.history` speaker and sentence. */
setState: function(speaker, rawSentence) {
var urlHash = this.fromSpeakerAndSentence(speaker, rawSentence);
/** Set the `window.history` speaker, volume, and sentence. */
setState: function(speaker, rawSentence, volume) {
var urlHash = this.fromParams(speaker, rawSentence, volume);
console.log('set state, urlhash = ', urlHash);
window.history.replaceState(null, null, urlHash);
},
/** Encode a speaker and sentence in a URL hash. */
fromSpeakerAndSentence: function(speaker, rawSentence) {
fromParams: function(speaker, rawSentence, volume) {
var cleanedSentence = SentenceHelper.cleanSentence(rawSentence),
cleanedSpeaker = speaker, // TODO: Check against speakers.
cleanedVolume = volume, // TODO: Filter invalid values.
state = {},
json = null,
uriEncoded = null;
state[this._SPEAKER_KEY] = cleanedSpeaker;
state[this._SENTENCE_KEY] = cleanedSentence;
state[this._VOLUME_KEY] = cleanedVolume;
json = JSON.stringify(state);
+1
View File
@@ -19,6 +19,7 @@
<option value="trump">trump</option>
<option value="hillary">hillary</option>
</select>
<input id="vol" value="">
<button id="submit">Submit</button>
</div>
</form>