converted many old-fashioned for-loops to modern ones

This commit is contained in:
Manuel Friedli 2014-07-03 22:03:54 +02:00
parent dd752c8db0
commit cd58ff4ba3

View file

@ -19,16 +19,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"languages": [], "languages": [],
"themes": [], "themes": [],
registerLanguage: function registerLanguage(code, language) { registerLanguage: function registerLanguage(code, language) {
var alreadyExists = uhrGlobals.languages.reduce(function (exists, element) { var alreadyExists = uhrGlobals.languages.some(function (element) {
if (exists) {
return true;
}
if (code === element.code) { if (code === element.code) {
console.error("Error: Language code '" + code + "' cannot be registered for language '" + language.language + "' because it is already registered for language '" + element.language + "'!"); console.error("Error: Language code '" + code + "' cannot be registered for language '" + language.language + "' because it is already registered for language '" + element.language + "'!");
return true; return true;
} }
return false; return false;
}, false); });
if (!alreadyExists) { if (!alreadyExists) {
language.code = code; language.code = code;
uhrGlobals.languages.push(language); uhrGlobals.languages.push(language);
@ -37,9 +34,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
}; };
// auto-detect themes // auto-detect themes
var styleSheets = $('link[rel=stylesheet]'); $('link[rel=stylesheet]').each(function (index, item) {
for (var i = 0; i < styleSheets.length; i++) { var styleSheet = $(item);
var styleSheet = $(styleSheets[i]);
var styleClass = styleSheet.attr('data-class'); var styleClass = styleSheet.attr('data-class');
if (styleClass !== undefined) { if (styleClass !== undefined) {
var name = styleSheet.attr('data-name'); var name = styleSheet.attr('data-name');
@ -48,7 +44,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
} }
uhrGlobals.themes.push({'styleClass': styleClass, 'name': name}); uhrGlobals.themes.push({'styleClass': styleClass, 'name': name});
} }
} });
// fall-back if no theme was included // fall-back if no theme was included
if (uhrGlobals.themes.length === 0) { if (uhrGlobals.themes.length === 0) {
uhrGlobals.themes.push({}); uhrGlobals.themes.push({});
@ -205,14 +201,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
if (selectedLanguage === undefined || uhr.options.force) { if (selectedLanguage === undefined || uhr.options.force) {
selectedLanguage = uhr.options.language; selectedLanguage = uhr.options.language;
} }
var found = false; var found = uhrGlobals.languages.some(function (item) {
for (var i = 0; i < uhrGlobals.languages.length; i++) { return selectedLanguage === item.code;
var code = uhrGlobals.languages[i].code; });
if (selectedLanguage === code) {
found = true;
break;
}
}
if (!found) { if (!found) {
var fallback; var fallback;
if (uhrGlobals.languages.length > 0) { if (uhrGlobals.languages.length > 0) {
@ -236,14 +227,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
if (selectedTheme === undefined || uhr.options.force) { if (selectedTheme === undefined || uhr.options.force) {
selectedTheme = uhr.options.theme; selectedTheme = uhr.options.theme;
} }
found = false; found = uhrGlobals.themes.some(function (item) {
for (var i = 0; i < uhrGlobals.themes.length; i++) { return selectedTheme === item.styleClass;
var styleClass = uhrGlobals.themes[i].styleClass; });
if (selectedTheme === styleClass) {
found = true;
break;
}
}
if (!found) { if (!found) {
var fallback = uhrGlobals.themes[0].styleClass; var fallback = uhrGlobals.themes[0].styleClass;
console.warn("Theme " + selectedTheme + " not found! Using fallback: " + fallback); console.warn("Theme " + selectedTheme + " not found! Using fallback: " + fallback);
@ -377,15 +363,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
var letters = this.layout._parsed; var letters = this.layout._parsed;
this.renderarea.fadeOut('fast', function () { this.renderarea.fadeOut('fast', function () {
renderer.renderarea.empty(); renderer.renderarea.empty();
for (var y = 0; y < letters.length; y++) { letters.forEach(function(line, index, array) {
for (var x = 0; x < letters[y].length; x++) { line.forEach(function (letter) {
var letter = letters[y][x];
renderer.renderarea.append(letter.toString()); renderer.renderarea.append(letter.toString());
} });
if (y < letters.length - 1) { if (index < array.length - 1) {
renderer.renderarea.append('<br/>'); renderer.renderarea.append('<br/>');
} }
} });
if (typeof beforeshow === 'function') { if (typeof beforeshow === 'function') {
beforeshow(); beforeshow();
} }
@ -396,47 +381,42 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
this.layout = layout; this.layout = layout;
this._parseArrayOrObject = function (letters, styleClass, input) { this._parseArrayOrObject = function (letters, styleClass, input) {
if (Array.isArray(input)) { if (Array.isArray(input)) {
for (var i = 0; i < input.length; i++) { input.forEach(function (item) {
this._parseObject(letters, styleClass, input[i]); this._parseObject(letters, styleClass, item);
} }, this);
} else { } else {
this._parseObject(letters, styleClass, input); this._parseObject(letters, styleClass, input);
} }
} };
this._parseObject = function (letters, styleClass, object) { this._parseObject = function (letters, styleClass, object) {
for (var line in object) { Object.keys(object).forEach(function (y) {
if (object.hasOwnProperty(line)) { var highlightLetters = object[y];
var highlightLetters = object[line]; highlightLetters.forEach(function (x) {
for (var i = 0; i < highlightLetters.length; i++) { letters[y - 1][x - 1].addStyle(styleClass);
var x = highlightLetters[i] - 1; });
letters[line - 1][x].addStyle(styleClass); });
} };
}
}
}
this._parseTimeDefinition = function(letters, styleClass, definition) { this._parseTimeDefinition = function(letters, styleClass, definition) {
for (var listString in definition) { var renderer = this;
if (definition.hasOwnProperty(listString)) { Object.keys(definition).forEach(function (listString) {
var array = listString.split(','); var array = listString.split(',');
var highlightLetters = definition[listString]; var highlightLetters = definition[listString];
for (var index = 0; index < array.length; index++) { array.forEach(function (item) {
this._parseArrayOrObject(letters, styleClass + array[index], highlightLetters); renderer._parseArrayOrObject(letters, styleClass + item, highlightLetters);
} });
} });
} };
}
} }
_UhrRendererV2Delegate.prototype.parse = function() { _UhrRendererV2Delegate.prototype.parse = function() {
var letters = []; var letters = [];
for (var i = 0; i < this.layout.letters.length; i++) { this.layout.letters.forEach(function(string) {
var line = []; var line = [];
var string = this.layout.letters[i];
for (var c = 0; c < string.length; c++) { for (var c = 0; c < string.length; c++) {
var character = new Letter(string[c]); var character = new Letter(string[c]);
line.push(character); line.push(character);
} }
letters.push(line); letters.push(line);
} });
this._parseArrayOrObject(letters, 'on', this.layout.permanent); this._parseArrayOrObject(letters, 'on', this.layout.permanent);
this._parseTimeDefinition(letters, 'minute', this.layout.minutes); this._parseTimeDefinition(letters, 'minute', this.layout.minutes);
this._parseTimeDefinition(letters, 'hour', this.layout.hours); this._parseTimeDefinition(letters, 'hour', this.layout.hours);