Skip to content

Commit c885e16

Browse files
committed
HFP-2650 Add language switcher widget
1 parent 9c44285 commit c885e16

24 files changed

+352
-20
lines changed

language/ar.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/bs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/de.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/el.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/en.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,7 @@ H5PEditor.language.core = {
203203
errorNotSupported: 'Parameters contain %used which is not supported.',
204204
errorParamsBroken: 'Parameters are broken.',
205205
libraryMissing: 'Missing required library %lib.',
206-
scriptMissing: 'Could not load upgrades script for %lib.'
206+
scriptMissing: 'Could not load upgrades script for %lib.',
207+
language: 'Language',
208+
noLanguagesSupported: 'No languages supported'
207209
};

language/es.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/eu.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,7 @@ H5PEditor.language.core = {
203203
errorNotSupported: 'Parameters contain %used which is not supported.',
204204
errorParamsBroken: 'Parameters are broken.',
205205
libraryMissing: 'Missing required library %lib.',
206-
scriptMissing: 'Could not load upgrades script for %lib.'
206+
scriptMissing: 'Could not load upgrades script for %lib.',
207+
language: 'Language',
208+
noLanguagesSupported: 'No languages supported'
207209
};

language/fi.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/fr.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/it.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/nb.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameterene inneholder %used som ikke er støttet.',
202202
errorParamsBroken: 'Parameterene er ødelagt.',
203203
libraryMissing: 'Mangler påkrevd bibliotek %lib.',
204-
scriptMissing: 'Kunne ikke laste oppgraderingsskript for %lib.'
204+
scriptMissing: 'Kunne ikke laste oppgraderingsskript for %lib.',
205+
language: 'Språk',
206+
noLanguagesSupported: 'Ingen språk støttet'
205207
};

language/nl.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/nn.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/pl.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/pt-br.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/pt.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

language/tr.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ H5PEditor.language.core = {
201201
errorNotSupported: 'Parameters contain %used which is not supported.',
202202
errorParamsBroken: 'Parameters are broken.',
203203
libraryMissing: 'Missing required library %lib.',
204-
scriptMissing: 'Could not load upgrades script for %lib.'
204+
scriptMissing: 'Could not load upgrades script for %lib.',
205+
language: 'Language',
206+
noLanguagesSupported: 'No languages supported'
205207
};

scripts/h5peditor-form.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Construct a form from library semantics.
44
*/
5-
ns.Form = function () {
5+
ns.Form = function (library, startLanguages) {
66
var self = this;
77

88
this.params = {};
@@ -17,6 +17,12 @@ ns.Form = function () {
1717
'<p class="desc">' +
1818
ns.t('core', 'commonFieldsDescription') +
1919
'</p>' +
20+
'<div class="h5peditor-language-switcher">' +
21+
'<span class="language-label">' + ns.t('core', 'language') + ':</span>' +
22+
'<select>' +
23+
'<option value="-">' + ns.t('core', 'noLanguagesSupported') + '</option>' +
24+
'</select>' +
25+
'</div>' +
2026
'</div>' +
2127
'</div>' +
2228
'</div>'
@@ -46,6 +52,79 @@ ns.Form = function () {
4652

4753
// Alternate background colors
4854
this.zebra = "odd";
55+
56+
// Locate the language switcher DOM element
57+
const $switcher = this.$form.find('.h5peditor-language-switcher select');
58+
const languages = {};
59+
60+
/**
61+
* Create options DOM elements
62+
*
63+
* @private
64+
* @return {string}
65+
*/
66+
const createOptions = function (selected) {
67+
let options = '';
68+
for (let code in languages) {
69+
let label = ns.supportedLanguages[code] ? ns.supportedLanguages[code] : code.toLocaleUpperCase();
70+
options += '<option value="' + code + '"' + (code === selected ? ' selected' : '') + '>' + label + '</option>';
71+
}
72+
return options;
73+
};
74+
75+
/**
76+
* Add new languages for content type.
77+
*
78+
* @param {string} lib uberName
79+
* @param {Array} langs
80+
*/
81+
self.addLanguages = function (lib, langs) {
82+
// Update language counters
83+
for (let i = 0; i < langs.length; i++) {
84+
const code = langs[i];
85+
if (languages[code] === undefined) {
86+
languages[code] = [lib];
87+
}
88+
else {
89+
languages[code].push(lib);
90+
}
91+
}
92+
93+
// Update
94+
$switcher.html(createOptions($switcher.val()));
95+
};
96+
97+
/**
98+
* Remove languages for content type.
99+
*
100+
* @param {string} lib uberName
101+
* @param {Array} langs
102+
*/
103+
self.removeLanguages = function (lib, langs) {
104+
// Update language counters
105+
for (let i = 0; i < langs.length; i++) {
106+
const code = langs[i];
107+
if (languages[code] !== undefined) {
108+
if (languages[code].length === 1) {
109+
delete languages[code];
110+
}
111+
else {
112+
languages[code].splice(languages[code].indexOf(lib), 1);
113+
}
114+
}
115+
}
116+
117+
// Update
118+
$switcher.html(createOptions($switcher.val()));
119+
};
120+
121+
// Handle switching language and loading new translations
122+
$switcher.change(function () {
123+
console.log('Loading new translations', this.value);
124+
});
125+
126+
// Add initial langauges for content type
127+
self.addLanguages(library, startLanguages);
49128
};
50129

51130
/**

scripts/h5peditor-library-selector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ ns.LibrarySelector.prototype.loadSemantics = function (library, params, metadata
324324
overrideParams = that.defaultParams;
325325
}
326326

327-
that.form = new ns.Form();
327+
that.form = new ns.Form(library, ['en', 'de']); // TODO: Missing library.languages
328328
that.form.replace($loading);
329329
that.form.currentLibrary = library;
330330
that.form.processSemantics(semantics, overrideParams, metadata);

scripts/h5peditor-library.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,12 @@ ns.Library.prototype.loadLibrary = function (libraryName, preserveParams) {
404404
// Locate selected library object
405405
const library = that.findLibrary(libraryName);
406406

407+
// Locate form
408+
const ancestor = ns.findAncestor(that.parent);
409+
410+
// Update the main language switcher
411+
ancestor.addLanguages(library.uberName, ['en', 'nb']); // TODO: Missing library.languages
412+
407413
// Store selected Content Type title in metadata for Copyright usage
408414
that.params.metadata.contentType = library.title;
409415

@@ -596,6 +602,12 @@ ns.Library.prototype.removeChildren = function () {
596602
}
597603
}
598604

605+
// Locate selected library object
606+
const lib = this.findLibrary(this.currentLibrary);
607+
608+
// Update the main language switcher
609+
ancestor.removeLanguages(lib.uberName, ['en', 'nb']); // TODO: Missing library.languages
610+
599611
ns.removeChildren(this.children);
600612
};
601613

0 commit comments

Comments
 (0)