register languages with help of a function. that enables advances functionality with fallback-language and makes it generally more robust.

this commit concludes the refactoring of the layout definition.
This commit is contained in:
Manuel Friedli 2014-06-28 16:40:24 +02:00
parent d9ae1f3783
commit bb027261b2
5 changed files with 47 additions and 18 deletions

View file

@ -67,4 +67,4 @@ var layout = {
"12": {9:[7,8,9,10,11]} "12": {9:[7,8,9,10,11]}
} }
}; };
window._uhr.languages['de'] = layout; window,_uhr.register('de', layout);

View file

@ -93,4 +93,4 @@ var layout = {
} }
}; };
// Das Layout bei der Uhr unter dem Code "de_CH" registrieren. // Das Layout bei der Uhr unter dem Code "de_CH" registrieren.
window._uhr.languages['de_CH'] = layout; window,_uhr.register('de_CH', layout);

View file

@ -68,4 +68,4 @@ var layout = {
"12": {10:[1,2,3,4,5,6]} "12": {10:[1,2,3,4,5,6]}
} }
}; };
window._uhr.languages['de_CH_genau'] = layout; window,_uhr.register('de_CH_genau', layout);

View file

@ -88,4 +88,4 @@ var layout = {
return hour; return hour;
} }
}; };
window._uhr.languages['en'] = layout; window,_uhr.register('en', layout);

57
uhr.js
View file

@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
(function($) { (function($) {
"use strict"; 'use strict';
if (window._uhr !== undefined) { if (window._uhr !== undefined) {
return; return;
@ -22,7 +22,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
window._uhr = { window._uhr = {
id: 0, id: 0,
languages: [], languages: [],
themes: [] themes: [],
register: function(code, language) {
for (var i = 0; i < this.languages.length; i++) {
if (code == this.languages[i].code) {
console.error('Error: Language code ' + code + ' cannot be registered for language "' + language.language + '" because it is already registered for language "' + this.languages[i].language + '"!');
return false;
}
}
language.code = code;
this.languages.push(language);
}
}; };
// auto-detect themes // auto-detect themes
var styleSheets = $('head link[rel=stylesheet]'); var styleSheets = $('head link[rel=stylesheet]');
@ -148,7 +158,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
} }
}, },
_language: function() { _language: function() {
return window._uhr.languages[this.options.language]; for (var i = 0; i < window._uhr.languages.length; i++) {
var language = window._uhr.languages[i];
if (language.code == this.options.language) {
return language;
}
}
// fallback: return empty object
return {};
}, },
_highlight: function(itemClass) { _highlight: function(itemClass) {
this.element.find('.item.' + itemClass).addClass('active'); this.element.find('.item.' + itemClass).addClass('active');
@ -228,17 +245,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
e.after(toggleSwitch); e.after(toggleSwitch);
// language chooser // language chooser
var options = []; if (window._uhr.languages.length > 1) {
for (var code in window._uhr.languages) {
if (window._uhr.languages.hasOwnProperty(code)) {
var language = window._uhr.languages[code];
options.push('<option value="' + code + '">' + language.language + '</option>');
}
}
if (options.length > 1) {
var languageChooser = $('<select id="uhr-languagechooser' + this._id + '"></select>'); var languageChooser = $('<select id="uhr-languagechooser' + this._id + '"></select>');
for (var i = 0; i < options.length; i++) { for (var i = 0; i < window._uhr.languages.length; i++) {
languageChooser.append(options[i]); var language = window._uhr.languages[i];
languageChooser.append('<option value="' + language.code + '">' + language.language + '</option>');
} }
e.after(languageChooser); e.after(languageChooser);
} }
@ -282,6 +293,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
if (selectedLanguage == undefined || this.options.force) { if (selectedLanguage == undefined || this.options.force) {
selectedLanguage = this.options.language; selectedLanguage = this.options.language;
} }
var found = false;
for (var i = 0; i < window._uhr.languages.length; i++) {
var code = window._uhr.languages[i].code;
if (selectedLanguage == code) {
found = true;
break;
}
}
if (!found) {
var fallback;
if (window._uhr.languages.length > 0) {
fallback = window._uhr.languages[0].code;
} else {
fallback = '';
}
console.warn("Language " + selectedLanguage + " not found! Using fallback: " + fallback);
selectedLanguage = fallback;
}
languageChooser.val(selectedLanguage); languageChooser.val(selectedLanguage);
this.options.language = ""; this.options.language = "";
this.language(selectedLanguage); this.language(selectedLanguage);
@ -295,7 +324,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
if (selectedTheme == undefined || this.options.force) { if (selectedTheme == undefined || this.options.force) {
selectedTheme = this.options.theme; selectedTheme = this.options.theme;
} }
var found = false; found = false;
for (var i = 0; i < window._uhr.themes.length; i++) { for (var i = 0; i < window._uhr.themes.length; i++) {
var styleClass = window._uhr.themes[i].styleClass; var styleClass = window._uhr.themes[i].styleClass;
if (selectedTheme == styleClass) { if (selectedTheme == styleClass) {