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

132
js/uhr.js
View file

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