-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLanguageSelector.js
More file actions
439 lines (383 loc) · 14 KB
/
LanguageSelector.js
File metadata and controls
439 lines (383 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* # LanguageSelector
* Copyright(c) 2017 Stefano Balietti <ste@nodegame.org>
* MIT Licensed
*
* Manages and displays information about languages available and selected
*
* @TODO: bubble event in case of buttons (now there are many listeners).
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('LanguageSelector', LanguageSelector);
// ## Meta-data
LanguageSelector.version = '0.6.2';
LanguageSelector.description = 'Display information about the current ' +
'language and allows to change language.';
LanguageSelector.title = 'Language';
LanguageSelector.className = 'languageselector';
LanguageSelector.texts.loading = 'Loading language information...';
// ## Dependencies
LanguageSelector.dependencies = {
JSUS: {}
};
/**
* ## LanguageSelector constructor
*
* Manages the setting and display of the language used
*
* @param {object} options Optional. Configuration options
*
* @see Player.lang
*/
function LanguageSelector(options) {
var that = this;
this.options = options;
/**
* ### LanguageSelector.availableLanguages
*
* Object containing an object per availble language.
*
* The language object contains at least the following properties:
*
* - `name`: Name of the language in English.
* - `nativeName`: Native name of the language
* - `shortName`: An abbreviation for the language, also determines the
* path to the context files for this language.
*
* The key for each language object is its `shortName`.
*
* @see Player.lang
*/
this.availableLanguages = {
en: {
name: 'English',
nativeName: 'English',
shortName: 'en'
}
};
/**
* ### LanguageSelector.currentLanguageIndex
*
* A reference to the currently used language
*
* @see LanguageSelector.availableLanguages
*/
this.currentLanguage = null;
/**
* ### LanguageSelector.buttonListLength
*
* Specifies maximum number of radio buttons used in selection tool
*/
this.buttonListLength = null;
/**
* ### LanguageSelector.displayForm
*
* The form in which the widget displays the language information
*/
this.displayForm = null;
/**
* ### LanguageSelector.optionsLabel
*
* Array containing the labels for the language selection optionsDisplay
*/
this.optionsLabel = {};
/**
* ### LanguageSelector.optionsDisplay
*
* Array containing the optionsDisplay for the language selection
*/
this.optionsDisplay = {};
/**
* ### LanguageSelector.loadingDiv
*
* Div displaying information on whether the languages have been loaded
*/
this.loadingDiv = null;
/**
* ### LanguageSelector.languagesLoaded
*
* Flag indicating whether languages have been loaded from server
*/
this.languagesLoaded = false;
/**
* ## LanguageSelector.usingButtons
*
* Flag indicating if the interface should have buttons
*
* Default: TRUE.
*/
this.usingButtons = true;
/**
* ## LanguageSelector.updatePlayer
*
* Specifies when updating the player
*
* Available options:
*
* - false: alias for 'never',
* - 'never': never notifies,
* - 'onselect': each time a selection is made,
* - 'ondone': when current step is done.
*
* Default: 'ondone'
*/
this.updatePlayer = 'ondone';
/**
* ## LanguageSelector.setUriPrefix
*
* If TRUE, the Window URI prefix is updated when the player is updated
*
* Default: TRUE.
*
* @see GameWindow.setUriPrefix
*/
this.setUriPrefix = true;
/**
* ## LanguageSelector.notifyServer
*
* If TRUE, a message is sent to the server when the player is updated
*
* Default: TRUE.
*/
this.notifyServer = true;
/**
* ### LanguageSelector.onLangCallback
*
* Function to be called when languages have been loaded
*
* Initializes form displaying the information as well
* as the optionsDisplay and their labels.
* Initializes language to English.
* Forwards to `LanguageSelector.onLangCallbackExtension` at the very
* end.
*
* @param {object} msg GameMsg
*
* @see LanguageSelector.setLanguage
*/
this.onLangCallback = function(msg) {
var language;
// Clear display.
while (that.displayForm.firstChild) {
that.displayForm.removeChild(that.displayForm.firstChild);
}
// Initialize widget.
that.availableLanguages = msg.data;
if (that.usingButtons) {
// Creates labeled buttons.
for (language in msg.data) {
if (msg.data.hasOwnProperty(language)) {
that.optionsLabel[language] = W.get('label', {
id: language + 'Label',
'for': language + 'RadioButton'
});
that.optionsDisplay[language] = W.get('input', {
id: language + 'RadioButton',
type: 'radio',
name: 'languageButton',
value: msg.data[language].name
});
that.optionsDisplay[language].onclick =
makeSetLanguageOnClick(language);
that.optionsLabel[language].appendChild(
that.optionsDisplay[language]);
that.optionsLabel[language].appendChild(
document.createTextNode(
msg.data[language].nativeName));
W.add('br', that.displayForm);
that.optionsLabel[language].className =
'unselectedButtonLabel';
that.displayForm.appendChild(
that.optionsLabel[language]);
}
}
}
else {
that.displaySelection = W.get('select', 'selectLanguage');
for (language in msg.data) {
that.optionsLabel[language] =
document.createTextNode(msg.data[language].nativeName);
that.optionsDisplay[language] = W.get('option', {
id: language + 'Option',
value: language
});
that.optionsDisplay[language].appendChild(
that.optionsLabel[language]);
that.displaySelection.appendChild(
that.optionsDisplay[language]);
}
that.displayForm.appendChild(that.displaySelection);
that.displayForm.onchange = function() {
that.setLanguage(that.displaySelection.value,
that.updatePlayer === 'onselect');
};
}
that.loadingDiv.style.display = 'none';
that.languagesLoaded = true;
// Initialize with current value inside player object,
// or default to English. Does not update the player object yet.
that.setLanguage(node.player.lang.shortName || 'en', false);
// Extension point.
if (that.onLangCallbackExtension) {
that.onLangCallbackExtension(msg);
that.onLangCallbackExtension = null;
}
function makeSetLanguageOnClick(langStr) {
return function() {
that.setLanguage(langStr, that.updatePlayer === 'onselect');
};
}
};
/**
* ### LanguageSelector.onLangCallbackExtension
*
* Extension point to `LanguageSelector.onLangCallback`
*
* @see LanguageSelector.onLangCallback
*/
this.onLangCallbackExtension = null;
}
// ## LanguageSelector methods
/**
* ### LanguageSelector.init
*
* Initializes the widget
*
* @param {object} options Optional. Configuration options
*
* @see LanguageSelector.onLangCallback
*/
LanguageSelector.prototype.init = function(options) {
J.mixout(options, this.options);
this.options = options;
if ('undefined' !== typeof this.options.usingButtons) {
this.usingButtons = !!this.options.usingButtons;
}
if ('undefined' !== typeof this.options.notifyServer) {
if (false === this.options.notifyServer) {
this.options.notifyServer = 'never';
}
else if ('string' === typeof this.options.notifyServer) {
if ('never' === this.options.notifyServer ||
'onselect' === this.options.notifyServer ||
'ondone' === this.options.notifyServer) {
this.notifyServer = this.options.notifyServer;
}
else {
throw new Error('LanguageSelector.init: invalid value ' +
'for notifyServer: "' +
this.options.notifyServer + '". Valid ' +
'values: "never","onselect", "ondone".');
}
}
else {
throw new Error('LanguageSelector.init: options.notifyServer ' +
'must be ' +
this.options.notifyServer);
}
}
if ('undefined' !== typeof this.options.setUriPrefix) {
this.setUriPrefix = !!this.options.setUriPrefix;
}
// Register listener.
// TODO: should it be moved into the listeners method?
// TODO: calling init twice will add it twice.
node.on.lang(this.onLangCallback);
// Display initialization.
this.displayForm = W.get('form', 'radioButtonForm');
this.loadingDiv = W.add('div', this.displayForm);
this.loadingDiv.innerHTML = this.getText('loading');
this.loadLanguages();
};
LanguageSelector.prototype.append = function() {
this.bodyDiv.appendChild(this.displayForm);
};
/**
* ### LanguageSelector.setLanguage
*
* Sets language within the widget and globally and updates the display
*
* @param {string} langName shortName of language to be set
* @param {boolean} updatePlayer If FALSE, the language is set only
* inside the widget, and no changes are made to the player object.
* Default: TRUE
*
* @see NodeGameClient.setLanguage
*/
LanguageSelector.prototype.setLanguage = function(langName, updatePlayer) {
if (this.usingButtons) {
// Uncheck current language button and change className of label.
if (this.currentLanguage !== null &&
this.currentLanguage !== this.availableLanguages[langName] ) {
this.optionsDisplay[this.currentLanguage].checked =
'unchecked';
this.optionsLabel[this.currentLanguage].className =
'unselectedButtonLabel';
}
}
// Set current language index.
this.currentLanguage = langName;
if (this.usingButtons) {
// Check language button and change className of label.
this.optionsDisplay[this.currentLanguage].checked = 'checked';
this.optionsLabel[this.currentLanguage].className =
'selectedButtonLabel';
}
else {
this.displaySelection.value = this.currentLanguage;
}
// Update node.player.
if (updatePlayer !== false) {
node.setLanguage(this.availableLanguages[this.currentLanguage],
this.setUriPrefix, this.notifyServer);
}
};
/**
* ### LanguageSelector.updateAvailableLanguages
*
* Updates available languages asynchronously
*
* @param {object} options Optional. Configuration options
*/
LanguageSelector.prototype.updateAvalaibleLanguages = function(options) {
if (options && options.callback) {
this.onLangCallbackExtension = options.callback;
}
node.socket.send(node.msg.create({
target: "LANG",
to: "SERVER",
action: "get"
}));
};
/**
* ### LanguageSelector.loadLanguages
*
* Loads languages once from server
*
* @param {object} options Optional. Configuration options
*
* @see LanguageSelector.updateAvalaibleLanguages
*/
LanguageSelector.prototype.loadLanguages = function(options) {
if (!this.languagesLoaded) this.updateAvalaibleLanguages(options);
else if (options && options.callback) options.callback();
};
/**
* ### LanguageSelector.listeners
*
* Implements Widget.listeners
*/
LanguageSelector.prototype.listeners = function() {
var that;
that = this;
node.events.step.on('REALLY_DONE', function() {
if (that.updatePlayer === 'ondone') {
node.setLanguage(that.availableLanguages[that.currentLanguage],
that.setUriPrefix, that.notifyServer);
}
});
};
})(node);