Skip to content

Commit a21f756

Browse files
committed
Fixed name space polution in STT node
1 parent ed41c70 commit a21f756

File tree

3 files changed

+170
-169
lines changed

3 files changed

+170
-169
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Node-RED Watson Nodes for IBM Bluemix
88
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>
99

1010
### New in version 0.4.15
11+
- Name space fixes to Speech to Text Node
1112

1213
### New in version 0.4.14
1314
- The dialog for the Text to Speech service now loads available voices dynamically. This allows

services/speech_to_text/v1.html

+101-99
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
</div>
2222
</div>
2323

24-
<div>
25-
<label id="node-label-message"><i class="fa fa-exclamation-triangle"></i></label>
24+
<div>
25+
<label id="node-label-message"><i class="fa fa-exclamation-triangle"></i></label>
2626
</div>
2727

28-
<div class="form-row">
28+
<div class="form-row">
2929
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
3030
<input type="text" id="node-input-name" placeholder="Name"></input>
3131
</div>
@@ -61,7 +61,7 @@
6161
<label for="node-input-continuous"><i class="fa fa-audio-o"></i> Continuous</label>
6262
<input type="checkbox" id="node-input-continuous"></input>
6363
</select>
64-
</div>
64+
</div>
6565

6666
</script>
6767

@@ -83,7 +83,7 @@
8383
automatically but the is not true in reverse.</p>
8484
<p>Use the continuous property to choose whether the decoding should stop at the first pause or wait until the end of the file.</p>
8585
<p>The returned audio transcription will be returned on <code>msg.transcription</code>.</p>
86-
<p>The full response, including alternative transcriptions can be found on
86+
<p>The full response, including alternative transcriptions can be found on
8787
<code>msg.fullresult</code>.</p>
8888
<p>For more information about the Speech To Text service, read the <a href="https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/speech-to-text.html">documentation</a>.</p>
8989
</script>
@@ -92,81 +92,82 @@
9292

9393
// Need to simulate a namespace, as some of the variables had started to leak across nodes
9494
function STT () {
95-
var models = null;
96-
var LANGUAGES = null;
97-
var languages = null;
98-
var bands = null;
99-
var language_selected = '';
100-
var band_selected = '';
101-
}
102-
103-
// This is the namespace for stt. Currently only contains models, but more vars and functions may need to be
95+
}
96+
97+
98+
// This is the namespace for stt. Currently only contains models, but more vars and functions may need to be
10499
// moved in if there is a clash with other nodes.
105100
var stt = new STT();
106-
107-
stt.LANGUAGES = { 'en-US' : 'US English',
108-
'pt-BR': 'Portuguese Braziilian',
109-
'en-UK': 'UK English' ,
110-
'zh-CN': 'Mandarin',
111-
'es-ES': 'Spanish',
112-
'ar-AR': 'Arablic',
101+
stt.models = null;
102+
stt.languages = null;
103+
stt.bands = null;
104+
stt.language_selected = '';
105+
stt.band_selected = '';
106+
107+
stt.LANGUAGES = { 'en-US': 'US English',
108+
'pt-BR': 'Portuguese Braziilian',
109+
'en-UK': 'UK English' ,
110+
'fr-FR': 'French',
111+
'zh-CN': 'Mandarin',
112+
'es-ES': 'Spanish',
113+
'ar-AR': 'Arablic',
113114
'ja-JP': 'Japanese'
114115
};
115116

116117
// sorting functions
117-
function onlyUnique(value, index, self) {
118+
stt.onlyUnique = function (value, index, self) {
118119
return self.indexOf(value) === index;
119120
}
120121

121-
// Function to be used at the start, as don't want to expose any fields, unless the models are
122-
// available. The models can only be fetched if the credentials are available.
123-
function hideEverything() {
122+
// Function to be used at the start, as don't want to expose any fields, unless the models are
123+
// available. The models can only be fetched if the credentials are available.
124+
stt.hideEverything = function () {
124125
if (!stt.models) {
125126
$('#credentials-not-found').show();
126-
$('label#node-label-message').parent().hide();
127-
$('input#node-input-continuous').parent().hide();
127+
$('label#node-label-message').parent().hide();
128+
$('input#node-input-continuous').parent().hide();
128129
$('select#node-input-lang').parent().hide();
129-
$('select#node-input-band').parent().hide();
130-
}
130+
$('select#node-input-band').parent().hide();
131+
}
131132
}
132133

133-
// Check if there is a model then can show the fields.
134-
// available. The models can only be fetched if the credentials are available.
135-
function visibilityCheck() {
134+
// Check if there is a model then can show the fields.
135+
// available. The models can only be fetched if the credentials are available.
136+
stt.VisibilityCheck = function () {
136137
if (stt.models) {
137-
$('label#node-label-message').parent().hide();
138-
$('input#node-input-continuous').parent().show();
139-
$('select#node-input-lang').parent().show();
140-
$('select#node-input-band').parent().show();
138+
$('label#node-label-message').parent().hide();
139+
$('input#node-input-continuous').parent().show();
140+
$('select#node-input-lang').parent().show();
141+
$('select#node-input-band').parent().show();
141142
} else {
142-
$('label#node-label-message').parent().hide();
143-
$('input#node-input-continuous').parent().hide();
144-
$('select#node-input-lang').parent().hide();
145-
$('select#node-input-band').parent().hide();
146-
}
143+
$('label#node-label-message').parent().hide();
144+
$('input#node-input-continuous').parent().hide();
145+
$('select#node-input-lang').parent().hide();
146+
$('select#node-input-band').parent().hide();
147+
}
147148
}
148149

149150

150151
// Simple check that is only invoked if the service is not bound into bluemix. In this case the
151-
// user has to provide credentials. Once there are credentials, then the stt.models are retrieved.
152-
function checkCredentials() {
152+
// user has to provide credentials. Once there are credentials, then the stt.models are retrieved.
153+
stt.checkCredentials = function () {
153154
var u = $('#node-input-username').val();
154155
var p = $('#node-input-password').val();
155156

156-
if (u && u.length && p) {
157+
if (u && u.length && p) {
157158
if (!stt.models) {
158-
getModels();
159-
}
160-
}
159+
stt.getModels();
160+
}
161+
}
161162
}
162163

163164
// Populate the quality select field
164-
function populateBands() {
165+
stt.populateBands = function () {
165166
if (!stt.bands && stt.models) {
166167
stt.bands = stt.models.map(function(m) {
167168
return m.name.split('_')[1];
168169
});
169-
var unique_bands = stt.bands.filter(onlyUnique);
170+
var unique_bands = stt.bands.filter(stt.onlyUnique);
170171
stt.bands = unique_bands;
171172
}
172173

@@ -183,52 +184,54 @@
183184
+ '"' + b + '"'
184185
+ selectedText
185186
+ '>'
186-
+ b
187-
+ '</option>');
187+
+ b
188+
+ '</option>');
188189
});
189190
}
190191
}
191192

192193
// Register the handlers for the fields
193-
function handlersUI() {
194+
stt.handlersUI = function () {
194195
$('#node-input-username').change(function(val){
195-
checkCredentials();
196-
});
196+
stt.checkCredentials();
197+
});
197198
$('#node-input-password').change(function(val){
198-
checkCredentials();
199-
});
199+
stt.checkCredentials();
200+
});
200201

201202
$('#node-input-lang').change(function (val) {
202203
stt.language_selected = $('#node-input-lang').val();
203-
populateBands();
204-
});
204+
stt.populateBands();
205+
});
205206

206207
$('#node-input-band').change(function (val) {
207208
stt.band_selected = $('#node-input-band').val();
208-
});
209+
});
209210
}
210211

211212
// Function called when either when the models have been retrieved, or
212213
// on dialog load, if the model has already been retrieved
213-
function postModelCheck() {
214-
processModels();
215-
visibilityCheck();
214+
stt.postModelCheck = function () {
215+
stt.processModels();
216+
stt.VisibilityCheck();
216217
}
217218

218-
// Retrieve the available models from the server, if data is returned, then
219-
// can enable the dynamic selection fields.
220-
function getModels(haveCredentials) {
219+
// Retrieve the available models from the server, if data is returned, then
220+
// can enable the dynamic selection fields.
221+
stt.getModels = function (haveCredentials) {
221222
var u = $('#node-input-username').val();
222223
var p = $('#node-input-password').val();
223224

224225
$.getJSON('watson-speech-to-text/models/', {un: u, pwd: p}).done(function (data) {
225226
if (data.error) {
226227
$('label#node-label-message').parent().show();
227-
$('label#node-label-message').text(data.error);
228+
$('label#node-label-message').text(data.error);
228229
} else if (data.models) {
230+
console.log('models found');
231+
console.log(data.models);
229232
stt.models = data.models;
230-
//have_credentials = true;
231-
postModelCheck();
233+
//have_credentials = true;
234+
stt.postModelCheck();
232235
}
233236
}).fail(function (err) {
234237
$('label#node-label-message').parent().show();
@@ -238,15 +241,15 @@
238241
}
239242

240243
// Called to complete the languages selection table
241-
function processLanguages() {
244+
stt.processSTTLanguages = function () {
242245
if (!stt.languages && stt.models) {
243246
stt.languages = stt.models.map(function(m) {
244247
return m.language;
245248
});
246249
}
247250
if (stt.languages) {
248251
$('select#node-input-lang').empty();
249-
var unique_langs = stt.languages.filter(onlyUnique);
252+
var unique_langs = stt.languages.filter(stt.onlyUnique);
250253

251254
unique_langs.forEach(function(l) {
252255
var selectedText = '';
@@ -258,49 +261,48 @@
258261
+ '"' + l + '"'
259262
+ selectedText
260263
+ '>'
261-
+ (stt.LANGUAGES[l] ? stt.LANGUAGES[l] : l)
262-
+ '</option>');
264+
+ (stt.LANGUAGES[l] ? stt.LANGUAGES[l] : l)
265+
+ '</option>');
263266
});
264-
265267
}
266268
}
267269

268270
// Called to work through the models, completing the dyanmic selection fields.
269-
function processModels() {
271+
stt.processModels = function () {
270272
if (stt.models) {
271-
processLanguages();
272-
populateBands();
273+
stt.processSTTLanguages();
274+
stt.populateBands();
273275
}
274276
}
275277

276278
// The dynamic nature of the selection fields in this node has caused problems.
277-
// Whenever there is a fetch for the models, on a page refresh or applicaiton
278-
// restart, the settings for the dynamic fields are lost.
279+
// Whenever there is a fetch for the models, on a page refresh or applicaiton
280+
// restart, the settings for the dynamic fields are lost.
279281
// So hidden (text) fields are being used to squirrel away the values, so that
280282
// they can be restored.
281-
function restoreFromHidden() {
282-
stt.language_selected = $('#node-input-langhidden').val();
283+
stt.restoreFromHidden = function () {
284+
stt.language_selected = $('#node-input-langhidden').val();
283285
$('select#node-input-lang').val(stt.language_selected);
284286

285-
stt.band_selected = $('#node-input-bandhidden').val();
287+
stt.band_selected = $('#node-input-bandhidden').val();
286288
$('select#node-input-band').val(stt.band_selected);
287-
}
289+
}
288290

289291
// This is the on edit prepare function, which will be invoked everytime the dialog
290-
// is shown.
291-
function oneditprepare() {
292-
hideEverything();
293-
restoreFromHidden();
294-
handlersUI();
292+
// is shown.
293+
function sttoneditprepare() {
294+
stt.hideEverything();
295+
stt.restoreFromHidden();
296+
stt.handlersUI();
295297

296298
$.getJSON('watson-speech-to-text/vcap/')
297-
// for some reason the getJSON resets the vars so need to restoreFromHidden again
299+
// for some reason the getJSON resets the vars so need to restoreFromHidden again
298300
// so again.
299301
.done(function (service) {
300-
restoreFromHidden();
302+
stt.restoreFromHidden();
301303
$('.credentials').toggle(!service);
302-
if (!stt.models) {getModels(service);}
303-
else {postModelCheck();}
304+
if (!stt.models) {stt.getModels(service);}
305+
else {stt.postModelCheck();}
304306
})
305307
.fail(function () {
306308
$('.credentials').show();
@@ -310,9 +312,9 @@
310312
}
311313

312314
// Save the values in the dyanmic lists to the hidden fields.
313-
function oneditsave(){
314-
$('#node-input-langhidden').val(stt.language_selected);
315-
$('#node-input-bandhidden').val(stt.band_selected);
315+
function sttoneditsave(){
316+
$('#node-input-langhidden').val(stt.language_selected);
317+
$('#node-input-bandhidden').val(stt.band_selected);
316318
}
317319

318320
(function() {
@@ -324,8 +326,8 @@
324326
lang: {value: ''},
325327
langhidden: {value: ''},
326328
band: {value: ''},
327-
bandhidden: {value: ''},
328-
password: {value: ''}
329+
bandhidden: {value: ''},
330+
password: {value: ''}
329331
},
330332
credentials: {
331333
username: {type:'text'} //,
@@ -342,9 +344,9 @@
342344
labelStyle: function() {
343345
return this.name ? 'node_label_italic' : '';
344346
},
345-
oneditprepare: oneditprepare,
346-
oneditsave: oneditsave
347+
oneditprepare: sttoneditprepare,
348+
oneditsave: sttoneditsave
347349
});
348350
})();
349351

350-
</script>
352+
</script>

0 commit comments

Comments
 (0)