fade controls in and out

This commit is contained in:
Manuel Friedli 2014-08-12 11:55:34 +02:00
parent a3dce2322b
commit fcfdaaa2cd

970
js/uhr.js
View file

@ -1,485 +1,485 @@
/* /*
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License 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';
var uhrGlobals = { var uhrGlobals = {
"id": 0, "id": 0,
"languages": [], "languages": [],
"themes": [], "themes": [],
registerLanguage: function registerLanguage(code, language) { registerLanguage: function registerLanguage(code, language) {
var alreadyExists = uhrGlobals.languages.some(function (element) { var alreadyExists = uhrGlobals.languages.some(function (element) {
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;
}); });
if (!alreadyExists) { if (!alreadyExists) {
language.code = code; language.code = code;
uhrGlobals.languages.push(language); uhrGlobals.languages.push(language);
} }
} }
}; };
// auto-detect themes // auto-detect themes
$('link[rel=stylesheet]').each(function (index, item) { $('link[rel=stylesheet]').each(function (index, item) {
var styleSheet = $(item); var styleSheet = $(item);
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');
if (name === undefined) { if (name === undefined) {
name = styleClass; name = styleClass;
} }
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({});
} }
// public interface methods (exported later) // public interface methods (exported later)
var start = function start() { var start = function start() {
if (!isOn.bind(this)()) { if (!isOn.bind(this)()) {
this.timer = window.setInterval(function () { this.timer = window.setInterval(function () {
this.options.time = new Date(); this.options.time = new Date();
update.bind(this)(); update.bind(this)();
}.bind(this), 1000); }.bind(this), 1000);
update.bind(this)(); update.bind(this)();
setCookie.bind(this)('uhr-status', 'on'); setCookie.bind(this)('uhr-status', 'on');
} }
}; };
var stop = function stop() { var stop = function stop() {
if (isOn.bind(this)()) { if (isOn.bind(this)()) {
window.clearInterval(this.timer); window.clearInterval(this.timer);
this.timer = null; this.timer = null;
update.bind(this)(); update.bind(this)();
setCookie.bind(this)('uhr-status', 'off'); setCookie.bind(this)('uhr-status', 'off');
} }
}; };
var toggle = function toggle() { var toggle = function toggle() {
if (isOn.bind(this)()) { if (isOn.bind(this)()) {
this.stop(); this.stop();
} else { } else {
this.start(); this.start();
} }
}; };
var setLanguage = function setLanugage(languageKey) { var setLanguage = function setLanugage(languageKey) {
if (languageKey !== this.options.language) { if (languageKey !== this.options.language) {
this.options.language = languageKey; this.options.language = languageKey;
var renderer = new UhrRenderer(language.bind(this)(), this.element.find('.letterarea')); var renderer = new UhrRenderer(language.bind(this)(), this.element.find('.letterarea'));
renderer.render.bind(this)(function () { renderer.render.bind(this)(function () {
this.currentMinute = -1; this.currentMinute = -1;
update.bind(this)(); update.bind(this)();
}.bind(this)); }.bind(this));
setCookie.bind(this)('uhr-language', languageKey); setCookie.bind(this)('uhr-language', languageKey);
update.bind(this)(); update.bind(this)();
} }
}; };
var setTheme = function setTheme(theme) { var setTheme = function setTheme(theme) {
if (theme !== this.options.theme) { if (theme !== this.options.theme) {
this.element.removeClass(this.options.theme).addClass(theme); this.element.removeClass(this.options.theme).addClass(theme);
$('#uhr-onoffswitch' + this.id).removeClass(this.options.theme).addClass(theme); $('#uhr-onoffswitch' + this.id).removeClass(this.options.theme).addClass(theme);
this.options.theme = theme; this.options.theme = theme;
setCookie.bind(this)('uhr-theme', theme); setCookie.bind(this)('uhr-theme', theme);
} }
}; };
var setTime = function setTime(time) { var setTime = function setTime(time) {
this.currentMinute = -1; this.currentMinute = -1;
if (time === null) { if (time === null) {
this.options.time = new Date(); this.options.time = new Date();
} else { } else {
if (this.timer !== null) { if (this.timer !== null) {
window.clearInterval(this.timer); window.clearInterval(this.timer);
} }
this.options.time = time; this.options.time = time;
} }
update.bind(this)(); update.bind(this)();
}; };
var setWidth = function setWidth(width) { var setWidth = function setWidth(width) {
var e = this.element; var e = this.element;
e.css('width', width); e.css('width', width);
var realWidth = e.width(); var realWidth = e.width();
e.width(realWidth); e.width(realWidth);
e.height(realWidth); e.height(realWidth);
e.css('font-size', (realWidth / 40) + 'px'); e.css('font-size', (realWidth / 40) + 'px');
}; };
// private interface methods // private interface methods
var create = function create() { var create = function create() {
this.id = uhrGlobals.id++; this.id = uhrGlobals.id++;
this.timer = null; this.timer = null;
this.currentMinute = -1; this.currentMinute = -1;
var userTime = this.options.time; var userTime = this.options.time;
if (this.options.time === undefined) { if (this.options.time === undefined) {
this.options.time = new Date(); this.options.time = new Date();
} }
setupHTML.bind(this)(); setupHTML.bind(this)();
wireFunctionality.bind(this)(); wireFunctionality.bind(this)();
if (userTime !== undefined) { if (userTime !== undefined) {
this.time(userTime); this.time(userTime);
} }
}; };
// private helper methods (not exported) // private helper methods (not exported)
var showConfigScreen = function showConfigScreen() { var showConfigScreen = function showConfigScreen() {
$('#uhr-controlpanel' + this.id).show(); $('#uhr-controlpanel' + this.id).fadeIn('fast');
}; };
// set up // set up
var setupHTML = function setupHTML() { var setupHTML = function setupHTML() {
var e = this.element; var e = this.element;
// Base clock area // Base clock area
e.addClass('uhr'); e.addClass('uhr');
e.empty(); e.empty();
e.append('<span class="item dot dot1"></span>'); e.append('<span class="item dot dot1"></span>');
e.append('<span class="item dot dot2"></span>'); e.append('<span class="item dot dot2"></span>');
e.append('<span class="item dot dot3"></span>'); e.append('<span class="item dot dot3"></span>');
e.append('<span class="item dot dot4"></span>'); e.append('<span class="item dot dot4"></span>');
e.append('<div class="letterarea"></div>'); e.append('<div class="letterarea"></div>');
e.append('<div class="reflection"></div>'); e.append('<div class="reflection"></div>');
setWidth.bind(this)(this.options.width); setWidth.bind(this)(this.options.width);
if (this.options.controls) { if (this.options.controls) {
var configlink = $('<a class="uhr-configlink" id="uhr-configlink' + this.id + '"></a>'); var configlink = $('<a class="uhr-configlink" id="uhr-configlink' + this.id + '"></a>');
configlink.on('click', function () { configlink.on('click', function () {
showConfigScreen.bind(this)(); showConfigScreen.bind(this)();
}.bind(this)); }.bind(this));
e.after(configlink); e.after(configlink);
var controlpanel = $('<div class="uhr-controlpanel" id="uhr-controlpanel' + this.id + '"></div>'); var controlpanel = $('<div class="uhr-controlpanel" id="uhr-controlpanel' + this.id + '"></div>');
var content = $('<div class="content"></div>'); var content = $('<div class="content"></div>');
controlpanel.append(content); controlpanel.append(content);
// on/off switch // on/off switch
var toggleSwitch = $('<div class="onoffswitch" id="uhr-onoffswitch' + this.id + '"></div>'); var toggleSwitch = $('<div class="onoffswitch" id="uhr-onoffswitch' + this.id + '"></div>');
toggleSwitch.append('<input type="checkbox" class="onoffswitch-checkbox" id="uhr-onoffswitch-checkbox' + this.id + '" checked="checked" />'); toggleSwitch.append('<input type="checkbox" class="onoffswitch-checkbox" id="uhr-onoffswitch-checkbox' + this.id + '" checked="checked" />');
toggleSwitch.append('<label class="onoffswitch-label" for="uhr-onoffswitch-checkbox' + this.id + '">' toggleSwitch.append('<label class="onoffswitch-label" for="uhr-onoffswitch-checkbox' + this.id + '">'
+ '<div class="onoffswitch-inner"></div>' + '<div class="onoffswitch-inner"></div>'
+ '<div class="onoffswitch-switch"></div>' + '<div class="onoffswitch-switch"></div>'
+ '</label>'); + '</label>');
content.append(toggleSwitch); content.append(toggleSwitch);
// language chooser // language chooser
if (uhrGlobals.languages.length > 1) { if (uhrGlobals.languages.length > 1) {
var languageChooser = $('<select id="uhr-languagechooser' + this.id + '"></select>'); var languageChooser = $('<select id="uhr-languagechooser' + this.id + '"></select>');
uhrGlobals.languages.forEach(function (item) { uhrGlobals.languages.forEach(function (item) {
languageChooser.append('<option value="' + item.code + '">' + item.language + '</option>'); languageChooser.append('<option value="' + item.code + '">' + item.language + '</option>');
}); });
content.append(languageChooser); content.append(languageChooser);
} }
// theme chooser // theme chooser
if (uhrGlobals.themes.length > 1) { if (uhrGlobals.themes.length > 1) {
var themeChooser = $('<select id="uhr-themechooser' + this.id + '"></select>'); var themeChooser = $('<select id="uhr-themechooser' + this.id + '"></select>');
uhrGlobals.themes.forEach(function (item) { uhrGlobals.themes.forEach(function (item) {
themeChooser.append('<option value="' + item.styleClass + '">' + item.name + '</option>'); themeChooser.append('<option value="' + item.styleClass + '">' + item.name + '</option>');
}); });
content.append(themeChooser); content.append(themeChooser);
} }
var closebutton = $('<a class="uhr-closecontrolpanel" id="uhr-closecontrolpanel' + this.id + '"></a>'); var closebutton = $('<a class="uhr-closecontrolpanel" id="uhr-closecontrolpanel' + this.id + '"></a>');
closebutton.on('click', function () { closebutton.on('click', function () {
$('#uhr-controlpanel' + this.id).hide(); $('#uhr-controlpanel' + this.id).fadeOut('fast');
}.bind(this)); }.bind(this));
content.append(closebutton); content.append(closebutton);
e.after(controlpanel); e.after(controlpanel);
controlpanel.hide(); controlpanel.hide();
} }
}; };
var wireFunctionality = function wireFunctionality() { var wireFunctionality = function wireFunctionality() {
// on/off switch // on/off switch
var toggleSwitch = $('#uhr-onoffswitch-checkbox' + this.id); var toggleSwitch = $('#uhr-onoffswitch-checkbox' + this.id);
toggleSwitch.on('click', function () { toggleSwitch.on('click', function () {
this.toggle(); this.toggle();
}.bind(this)); }.bind(this));
var status = $.cookie('uhr-status' + this.id); var status = $.cookie('uhr-status' + this.id);
if (status === undefined || this.options.force) { if (status === undefined || this.options.force) {
status = this.options.status; status = this.options.status;
} }
toggleSwitch.prop('checked', status === 'on'); toggleSwitch.prop('checked', status === 'on');
if (status === 'on') { if (status === 'on') {
this.start(); this.start();
} else { } else {
this.stop(); this.stop();
} }
// language chooser // language chooser
var languageChooser = $('#uhr-languagechooser' + this.id); var languageChooser = $('#uhr-languagechooser' + this.id);
languageChooser.on('change', function () { languageChooser.on('change', function () {
var languageKey = $('#uhr-languagechooser' + this.id).val(); var languageKey = $('#uhr-languagechooser' + this.id).val();
this.language(languageKey); this.language(languageKey);
}.bind(this)); }.bind(this));
var selectedLanguage = $.cookie('uhr-language' + this.id); var selectedLanguage = $.cookie('uhr-language' + this.id);
if (selectedLanguage === undefined || this.options.force) { if (selectedLanguage === undefined || this.options.force) {
selectedLanguage = this.options.language; selectedLanguage = this.options.language;
} }
var found = uhrGlobals.languages.some(function (item) { var found = uhrGlobals.languages.some(function (item) {
return selectedLanguage === item.code; return selectedLanguage === item.code;
}); });
if (!found) { if (!found) {
var fallbackLanguage; var fallbackLanguage;
if (uhrGlobals.languages.length > 0) { if (uhrGlobals.languages.length > 0) {
fallbackLanguage = uhrGlobals.languages[0].code; fallbackLanguage = uhrGlobals.languages[0].code;
} else { } else {
fallbackLanguage = ''; fallbackLanguage = '';
} }
console.warn("Language '" + selectedLanguage + "' not found! Using fallback '" + fallbackLanguage + "'"); console.warn("Language '" + selectedLanguage + "' not found! Using fallback '" + fallbackLanguage + "'");
selectedLanguage = fallbackLanguage; selectedLanguage = fallbackLanguage;
} }
languageChooser.val(selectedLanguage); languageChooser.val(selectedLanguage);
this.options.language = ""; this.options.language = "";
this.language(selectedLanguage); this.language(selectedLanguage);
// theme chooser // theme chooser
var themeChooser = $('#uhr-themechooser' + this.id); var themeChooser = $('#uhr-themechooser' + this.id);
themeChooser.on('change', function () { themeChooser.on('change', function () {
var themeKey = $('#uhr-themechooser' + this.id).val(); var themeKey = $('#uhr-themechooser' + this.id).val();
this.theme(themeKey); this.theme(themeKey);
}.bind(this)); }.bind(this));
var selectedTheme = $.cookie('uhr-theme' + this.id); var selectedTheme = $.cookie('uhr-theme' + this.id);
if (selectedTheme === undefined || this.options.force) { if (selectedTheme === undefined || this.options.force) {
selectedTheme = this.options.theme; selectedTheme = this.options.theme;
} }
found = uhrGlobals.themes.some(function (item) { found = uhrGlobals.themes.some(function (item) {
return selectedTheme === item.styleClass; return selectedTheme === item.styleClass;
}); });
if (!found) { if (!found) {
var fallbackTheme = uhrGlobals.themes[0].styleClass; var fallbackTheme = uhrGlobals.themes[0].styleClass;
console.warn("Theme '" + selectedTheme + "' not found! Using fallback '" + fallbackTheme + "'"); console.warn("Theme '" + selectedTheme + "' not found! Using fallback '" + fallbackTheme + "'");
selectedTheme = fallbackTheme; selectedTheme = fallbackTheme;
} }
themeChooser.val(selectedTheme); themeChooser.val(selectedTheme);
this.options.theme = ""; this.options.theme = "";
this.theme(selectedTheme); this.theme(selectedTheme);
}; };
var setCookie = function setCookie(cookieName, cookieValue) { var setCookie = function setCookie(cookieName, cookieValue) {
var options = {}; var options = {};
if (this.options.cookiePath !== undefined) { if (this.options.cookiePath !== undefined) {
options = {expires: 365, path: this.options.cookiePath}; options = {expires: 365, path: this.options.cookiePath};
} else { } else {
options = {expires: 365}; options = {expires: 365};
} }
$.cookie(cookieName + this.id, cookieValue, options); $.cookie(cookieName + this.id, cookieValue, options);
}; };
// business logic // business logic
var isOn = function isOn() { var isOn = function isOn() {
return this.timer !== null; return this.timer !== null;
}; };
var update = function update() { var update = function update() {
if (isOn.bind(this)()) { if (isOn.bind(this)()) {
var time = this.options.time; var time = this.options.time;
if (!language.bind(this)().hasOwnProperty('seconds')) { if (!language.bind(this)().hasOwnProperty('seconds')) {
if (time.getMinutes() === this.currentMinute) { if (time.getMinutes() === this.currentMinute) {
return; return;
} }
this.currentMinute = time.getMinutes(); this.currentMinute = time.getMinutes();
} }
show.bind(this)(time); show.bind(this)(time);
} else { } else {
clear.bind(this)(); clear.bind(this)();
this.currentMinute = -1; this.currentMinute = -1;
} }
}; };
var show = function show(time) { var show = function show(time) {
var second = getSecond.bind(this)(time); var second = getSecond.bind(this)(time);
var dotMinute = getDotMinute.bind(this)(time); var dotMinute = getDotMinute.bind(this)(time);
var hour = getHour.bind(this)(time); var hour = getHour.bind(this)(time);
var coarseMinute = getCoarseMinute.bind(this)(time); var coarseMinute = getCoarseMinute.bind(this)(time);
clear.bind(this)(); clear.bind(this)();
highlight.bind(this)('on'); highlight.bind(this)('on');
for (var i = 1; i <= dotMinute; i++) { for (var i = 1; i <= dotMinute; i++) {
highlight.bind(this)('dot' + i); highlight.bind(this)('dot' + i);
} }
highlight.bind(this)('second' + second); highlight.bind(this)('second' + second);
highlight.bind(this)('minute' + coarseMinute); highlight.bind(this)('minute' + coarseMinute);
highlight.bind(this)('hour' + hour); highlight.bind(this)('hour' + hour);
}; };
var highlight = function highlight(itemClass) { var highlight = function highlight(itemClass) {
this.element.find('.item.' + itemClass).addClass('active'); this.element.find('.item.' + itemClass).addClass('active');
}; };
var clear = function clear() { var clear = function clear() {
this.element.find('.item').removeClass('active'); this.element.find('.item').removeClass('active');
}; };
var getSecond = function getSecond(date) { var getSecond = function getSecond(date) {
if (typeof language.bind(this)().getSeconds === 'function') { if (typeof language.bind(this)().getSeconds === 'function') {
return language.bind(this)().getSeconds(date); return language.bind(this)().getSeconds(date);
} }
return date.getSeconds(); return date.getSeconds();
}; };
var getDotMinute = function getDotMinute(date) { var getDotMinute = function getDotMinute(date) {
if (typeof language.bind(this)().getDotMinute === 'function') { if (typeof language.bind(this)().getDotMinute === 'function') {
return language.bind(this)().getDotMinute(date); return language.bind(this)().getDotMinute(date);
} }
var minutes = date.getMinutes(); var minutes = date.getMinutes();
return minutes % 5; return minutes % 5;
}; };
var getCoarseMinute = function getCoarseMinute(date) { var getCoarseMinute = function getCoarseMinute(date) {
if (typeof language.bind(this)().getCoarseMinute === 'function') { if (typeof language.bind(this)().getCoarseMinute === 'function') {
return language.bind(this)().getCoarseMinute(date); return language.bind(this)().getCoarseMinute(date);
} }
return date.getMinutes(); return date.getMinutes();
}; };
var getHour = function getHour(date) { var getHour = function getHour(date) {
if (typeof language.bind(this)().getHour === 'function') { if (typeof language.bind(this)().getHour === 'function') {
return language.bind(this)().getHour(date); return language.bind(this)().getHour(date);
} }
var hour = date.getHours(); var hour = date.getHours();
if (date.getMinutes() >= 25) { if (date.getMinutes() >= 25) {
return (hour + 1) % 24; return (hour + 1) % 24;
} }
return hour; return hour;
}; };
var language = function language() { var language = function language() {
var matchingLanguages = uhrGlobals.languages.filter(function (element) { var matchingLanguages = uhrGlobals.languages.filter(function (element) {
return (element.code === this.options.language); return (element.code === this.options.language);
}, this); }, this);
if (matchingLanguages.length > 0) { if (matchingLanguages.length > 0) {
return matchingLanguages[0]; return matchingLanguages[0];
} }
// fallback: return empty object // fallback: return empty object
return {}; return {};
}; };
$.widget("fritteli.uhr", { $.widget("fritteli.uhr", {
"options": { "options": {
width: '100%', width: '100%',
status: 'on', status: 'on',
language: 'de_CH', language: 'de_CH',
theme: uhrGlobals.themes[0].styleClass, theme: uhrGlobals.themes[0].styleClass,
force: false, force: false,
controls: true, controls: true,
cookiePath: undefined cookiePath: undefined
}, },
"start": start, "start": start,
"stop": stop, "stop": stop,
"toggle": toggle, "toggle": toggle,
"language": setLanguage, "language": setLanguage,
"theme": setTheme, "theme": setTheme,
"time": setTime, "time": setTime,
"width": setWidth, "width": setWidth,
// constructor method // constructor method
"_create": create "_create": create
}); });
$.fritteli.uhr.register = uhrGlobals.registerLanguage; $.fritteli.uhr.register = uhrGlobals.registerLanguage;
/** /**
* Hilfsklasse zum Rendern der Uhr. * Hilfsklasse zum Rendern der Uhr.
* @param layout Layout-Objekt, das gerendert werden soll. * @param layout Layout-Objekt, das gerendert werden soll.
* @param renderarea Das jQuery-gewrappte HTML-Element, auf dem gerendert werden soll. * @param renderarea Das jQuery-gewrappte HTML-Element, auf dem gerendert werden soll.
*/ */
function UhrRenderer(layout, renderarea) { function UhrRenderer(layout, renderarea) {
this.render = function render(beforeshow) { this.render = function render(beforeshow) {
if (layout.parsed === undefined) { if (layout.parsed === undefined) {
switch (layout.version) { switch (layout.version) {
case 2: case 2:
var delegate = new UhrRendererV2Delegate(layout); var delegate = new UhrRendererV2Delegate(layout);
var parsedLayout = delegate.parse(); var parsedLayout = delegate.parse();
Object.defineProperty(layout, "parsed", {"value": parsedLayout, "writable": false, "configurable": false}); Object.defineProperty(layout, "parsed", {"value": parsedLayout, "writable": false, "configurable": false});
break; break;
default: default:
console.warn("Unknown layout version: '" + layout.version + "'"); console.warn("Unknown layout version: '" + layout.version + "'");
return; return;
} }
} }
var letters = layout.parsed; var letters = layout.parsed;
renderarea.fadeOut('fast', function () { renderarea.fadeOut('fast', function () {
renderarea.empty(); renderarea.empty();
letters.forEach(function (line, index, array) { letters.forEach(function (line, index, array) {
line.forEach(function (letter) { line.forEach(function (letter) {
renderarea.append(letter.toString()); renderarea.append(letter.toString());
}); });
if (index < array.length - 1) { if (index < array.length - 1) {
renderarea.append('<br/>'); renderarea.append('<br/>');
} }
}); });
if (typeof beforeshow === 'function') { if (typeof beforeshow === 'function') {
beforeshow(); beforeshow();
} }
renderarea.fadeIn('fast'); renderarea.fadeIn('fast');
}); });
}; };
} }
function UhrRendererV2Delegate(layout) { function UhrRendererV2Delegate(layout) {
function parseArrayOrObject(letters, styleClass, input) { function parseArrayOrObject(letters, styleClass, input) {
if (typeof input !== 'undefined' && input !== null) { if (typeof input !== 'undefined' && input !== null) {
if (Array.isArray(input)) { if (Array.isArray(input)) {
input.forEach(function (item) { input.forEach(function (item) {
parseObject(letters, styleClass, item); parseObject(letters, styleClass, item);
}); });
} else { } else {
parseObject(letters, styleClass, input); parseObject(letters, styleClass, input);
} }
} }
} }
function parseObject(letters, styleClass, object) { function parseObject(letters, styleClass, object) {
if (typeof object !== 'undefined' && object !== null) { if (typeof object !== 'undefined' && object !== null) {
Object.keys(object).forEach(function (y) { Object.keys(object).forEach(function (y) {
var highlightLetters = object[y]; var highlightLetters = object[y];
highlightLetters.forEach(function (x) { highlightLetters.forEach(function (x) {
letters[y - 1][x - 1].addStyle(styleClass); letters[y - 1][x - 1].addStyle(styleClass);
}); });
}); });
} }
} }
function parseTimeDefinition(letters, styleClass, definition) { function parseTimeDefinition(letters, styleClass, definition) {
if (typeof definition !== 'undefined' && definition !== null) { if (typeof definition !== 'undefined' && definition !== null) {
Object.keys(definition).forEach(function (listString) { Object.keys(definition).forEach(function (listString) {
var array = listString.split(','); var array = listString.split(',');
var highlightLetters = definition[listString]; var highlightLetters = definition[listString];
array.forEach(function (item) { array.forEach(function (item) {
parseArrayOrObject(letters, styleClass + item, highlightLetters); parseArrayOrObject(letters, styleClass + item, highlightLetters);
}); });
}); });
} }
} }
this.parse = function parse() { this.parse = function parse() {
var letters = []; var letters = [];
layout.letters.forEach(function (string) { layout.letters.forEach(function (string) {
var line = []; var line = [];
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);
}); });
parseArrayOrObject(letters, 'on', layout.permanent); parseArrayOrObject(letters, 'on', layout.permanent);
parseTimeDefinition(letters, 'second', layout.seconds); parseTimeDefinition(letters, 'second', layout.seconds);
parseTimeDefinition(letters, 'minute', layout.minutes); parseTimeDefinition(letters, 'minute', layout.minutes);
parseTimeDefinition(letters, 'hour', layout.hours); parseTimeDefinition(letters, 'hour', layout.hours);
return letters; return letters;
}; };
} }
/** /**
* Ein Buchstabe. Hilfsklasse für den Renderer und Inhalt der Layout-Arrays. * Ein Buchstabe. Hilfsklasse für den Renderer und Inhalt der Layout-Arrays.
* @param value Der Buchstabe, der Dargestellt werden soll. * @param value Der Buchstabe, der Dargestellt werden soll.
* @param style Die CSS-Styleklassen des Buchstabens. * @param style Die CSS-Styleklassen des Buchstabens.
*/ */
function Letter(value, style) { function Letter(value, style) {
var myValue = value; var myValue = value;
var myStyle = style || ''; var myStyle = style || '';
this.addStyle = function (style) { this.addStyle = function (style) {
if (myStyle === '') { if (myStyle === '') {
myStyle = style; myStyle = style;
} else { } else {
myStyle += ' ' + style; myStyle += ' ' + style;
} }
}; };
this.toString = function () { this.toString = function () {
return '<span class="item letter ' + myStyle + '">' + myValue + '</span>'; return '<span class="item letter ' + myStyle + '">' + myValue + '</span>';
}; };
} }
})(jQuery); })(jQuery);