uhr/uhr.js

339 lines
11 KiB
JavaScript
Raw Normal View History

/*
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-11-26 13:25:50 +01:00
/**
* Die Uhr.
2013-11-27 15:40:56 +01:00
* @param clockarea Das jQuery-gewrappte HTML-Element, auf dem die Uhr angezeigt werden soll.
2013-11-26 13:25:50 +01:00
* @param themeElement Das HTML-Stylesheet-Tag, das das Theme-CSS referenziert.
*/
2013-11-28 13:31:47 +01:00
function Uhr(clockarea, settings) {
2013-11-27 18:16:42 +01:00
this.id = Uhr.id++;
2013-11-26 13:25:50 +01:00
this.timer = null;
this.currentTheme = null;
2013-11-27 17:48:41 +01:00
this.currentLayout = Uhr.layouts['undefined'];
2013-11-26 13:25:50 +01:00
this.currentMinute = -1;
2013-11-28 13:31:47 +01:00
this.initHTMLElements(clockarea, settings || {});
}
2013-11-27 18:16:42 +01:00
Uhr.id = 0;
Uhr.layouts = new Array();
Uhr.register = function (locale, layout) {
Uhr.layouts[locale] = layout;
2013-11-26 13:25:50 +01:00
}
Uhr.prototype.toggle = function() {
if (this.isOn()) {
this.stop();
2013-11-19 16:55:01 +01:00
} else {
2013-11-26 13:25:50 +01:00
this.start();
}
}
Uhr.prototype.start = function() {
if (!this.isOn()) {
var uhr = this;
this.timer = window.setInterval(function() {uhr.update();}, 1000);
this.update();
$.cookie('status' + this.id, 'on', {expires: 365, path: '/'});
2013-11-19 16:55:01 +01:00
}
}
2013-11-26 13:25:50 +01:00
Uhr.prototype.stop = function() {
if (this.isOn()) {
window.clearInterval(this.timer);
this.timer = null;
this.update();
$.cookie('status' + this.id, 'off', {expires: 365, path: '/'});
2013-11-26 13:25:50 +01:00
}
}
Uhr.prototype.isOn = function() {
return this.timer != null;
}
Uhr.prototype.setLayout = function(locale) {
var newLayout = Uhr.layouts[locale];
2013-11-26 13:25:50 +01:00
if (newLayout !== undefined && newLayout != this.currentLayout) {
this.currentLayout = newLayout;
2013-11-27 15:40:56 +01:00
var renderer = new UhrRenderer(this.currentLayout, this.letterarea);
2013-11-26 13:25:50 +01:00
renderer.render(this);
$.cookie('layout' + this.id, locale, {expires: 365, path: '/'});
2013-11-26 13:25:50 +01:00
}
}
Uhr.prototype.setTheme = function(theme) {
if (theme != this.currentTheme) {
this.clockarea.removeClass(this.currentTheme);
this.toggleSwitch.removeClass(this.currentTheme);
this.clockarea.addClass(theme);
this.toggleSwitch.addClass(theme);
2013-11-26 13:25:50 +01:00
this.currentTheme = theme;
$.cookie('theme' + this.id, theme, {expires: 365, path: '/'});
2013-11-26 13:25:50 +01:00
}
}
Uhr.prototype.update = function() {
if (this.isOn()) {
var now = new Date();
var dotMinute = this.getDotMinute(now);
if (dotMinute == this.currentMinute) {
return;
}
this.currentMinute = dotMinute;
var hour = this.getHour(now);
var coarseMinute = this.getCoarseMinute(now);
this.clear();
this.highlight('on');
for (var i = 1; i <= dotMinute; i++) {
this.highlight('dot' + i);
}
this.highlight('minute' + coarseMinute);
hour = this.normalizeHour(hour);
this.highlight('hour' + hour);
if (coarseMinute == 0) {
this.highlight('sharphour');
}
} else {
this.clear();
this.currentMinute = -1;
}
}
Uhr.prototype.clear = function() {
2013-11-27 15:40:56 +01:00
this.clockarea.find('.item').removeClass('active');
2013-11-26 13:25:50 +01:00
}
Uhr.prototype.highlight = function(itemClass) {
2013-11-27 15:40:56 +01:00
this.clockarea.find('.item.' + itemClass).addClass('active');
2013-11-25 13:25:02 +01:00
}
2013-11-26 13:25:50 +01:00
Uhr.prototype.getHour = function(date) {
if (typeof this.currentLayout.getHour === 'function') {
return this.currentLayout.getHour(date);
}
var hour = date.getHours();
if (date.getMinutes() >= 25) {
return hour + 1;
}
return hour;
}
Uhr.prototype.getCoarseMinute = function(date) {
if (typeof this.currentLayout.getCoarseMinute === 'function') {
return this.currentLayout.getCoarseMinute(date);
}
var minutes = date.getMinutes();
return minutes - this.getDotMinute(date);
}
Uhr.prototype.getDotMinute = function(date) {
if (typeof this.currentLayout.getDotMinute === 'function') {
return this.currentLayout.getDotMinute(date);
}
var minutes = date.getMinutes();
return minutes % 5;
}
Uhr.prototype.normalizeHour = function(hour) {
if (hour > 12) {
hour %= 12;
}
if (hour == 0) {
return 12;
}
return hour;
}
2013-11-28 13:31:47 +01:00
Uhr.prototype.initHTMLElements = function(clockarea, presets) {
this.createHTMLElements(clockarea);
2013-11-28 13:31:47 +01:00
var force = presets.force || false;
this.initToggleSwitch(presets.status, force);
this.initLayoutSwitch(presets.layout, force);
this.initThemeSwitch(presets.theme, force);
}
Uhr.prototype.createHTMLElements = function(clockarea) {
this.createClockarea(clockarea)
this.letterarea = this.clockarea.find('.letterarea');
this.createToggleSwitch();
this.createLayoutSwitch();
this.createThemeSwitch();
}
Uhr.prototype.createClockarea = function(clockarea) {
clockarea.addClass('uhr');
2013-11-27 18:13:37 +01:00
clockarea.empty();
clockarea.append('<span class="item dot dot1"></span>');
clockarea.append('<span class="item dot dot2"></span>');
clockarea.append('<span class="item dot dot3"></span>');
clockarea.append('<span class="item dot dot4"></span>');
clockarea.append('<div class="letterarea"></div>');
clockarea.append('<div class="reflection"></div>');
this.clockarea = clockarea
2013-11-27 18:13:37 +01:00
}
Uhr.prototype.createToggleSwitch = function() {
this.toggleSwitch = $('<div class="onoffswitch"></div>');
2013-11-27 18:16:42 +01:00
var input = $('<input type="checkbox" name="onoffswitch' + this.id + '" class="onoffswitch-checkbox" id="onoffswitch' + this.id + '" checked="checked" />');
this.toggleSwitch.append(input);
this.toggleSwitch.append('<label class="onoffswitch-label" for="onoffswitch' + this.id + '">'
+ '<div class="onoffswitch-inner"></div>'
+ '<div class="onoffswitch-switch"></div>'
+ '</label>');
this.clockarea.after(this.toggleSwitch);
}
Uhr.prototype.createLayoutSwitch = function () {
this.layoutSwitch = $('<select></select>')
for (var code in Uhr.layouts) {
if (Uhr.layouts.hasOwnProperty(code)) {
var layout = Uhr.layouts[code];
var option = $('<option value="' + code + '">' + layout.language + '</option>')
this.layoutSwitch.append(option);
}
}
this.clockarea.after(this.layoutSwitch);
}
Uhr.prototype.createThemeSwitch = function () {
this.themeSwitch = $('<select></select>');
this.themeSwitch.append('<option value="black">Schwarz</option>');
this.themeSwitch.append('<option value="red">Rot</option>');
this.themeSwitch.append('<option value="blue">Blau</option>');
this.themeSwitch.append('<option value="green">Grün</option>');
this.themeSwitch.append('<option value="white">Weiss</option>');
this.clockarea.after(this.themeSwitch);
}
2013-11-28 13:31:47 +01:00
Uhr.prototype.initToggleSwitch = function(defaultValue, overrideCookie) {
var input = $('#onoffswitch' + this.id);
2013-11-27 18:13:37 +01:00
var uhr = this;
input.on('click', function() {
uhr.toggle();
});
var status = $.cookie('status' + this.id);
2013-11-28 13:36:56 +01:00
if (status == undefined || (overrideCookie && (defaultValue != undefined))) {
2013-11-28 13:31:47 +01:00
status = defaultValue;
}
if (status == undefined || status == 'undefined') {
status = 'on';
}
if (status == 'on') {
this.start();
2013-11-28 09:40:04 +01:00
input.prop('checked', true);
} else {
this.stop();
2013-11-28 09:40:04 +01:00
input.prop('checked', false);
}
2013-11-27 18:13:37 +01:00
}
2013-11-28 13:31:47 +01:00
Uhr.prototype.initLayoutSwitch = function(defaultValue, overrideCookie) {
var uhr = this;
this.layoutSwitch.on('change', function() {
uhr.setLayout(this.value);
});
var selectedLayout = $.cookie('layout' + this.id);
2013-11-28 13:36:56 +01:00
if (selectedLayout == undefined || (overrideCookie && (defaultValue != undefined))) {
2013-11-28 13:31:47 +01:00
selectedLayout = defaultValue;
}
2013-11-28 13:31:47 +01:00
if (selectedLayout == undefined || selectedLayout == 'undefined') {
selectedLayout = 'de_CH';
}
this.layoutSwitch.val(selectedLayout);
this.setLayout(selectedLayout);
}
2013-11-28 13:31:47 +01:00
Uhr.prototype.initThemeSwitch = function(defaultValue, overrideCookie) {
var uhr = this;
this.themeSwitch.on('change', function() {
uhr.setTheme(this.value);
});
var selectedTheme = $.cookie('theme' + this.id);
2013-11-28 13:36:56 +01:00
if (selectedTheme == undefined || (overrideCookie && (defaultValue != undefined))) {
2013-11-28 13:31:47 +01:00
selectedTheme = defaultValue;
}
if (selectedTheme == undefined || selectedTheme == 'undefined') {
selectedTheme = 'black';
}
this.themeSwitch.val(selectedTheme);
this.setTheme(selectedTheme);
}
2013-11-26 13:25:50 +01:00
Uhr.register('undefined', {
language: 'Please choose your language',
values: []
});
2013-11-26 13:25:50 +01:00
/**
* Hilfsklasse zum Rendern der Uhr.
* @param layout Layout-Objekt, das gerendert werden soll.
* @param renderarea Das jQuery-gewrappte HTML-Element, auf dem gerendert werden soll.
*/
function UhrRenderer(layout, renderarea) {
this.layout = layout;
this.renderarea = renderarea;
}
UhrRenderer.prototype.render = function(uhr) {
var renderer = this;
this.renderarea.fadeOut('fast', function() {
renderer.renderarea.empty();
for (var y = 0; y < renderer.layout.values.length; y++) {
for (var x = 0; x < renderer.layout.values[y].length; x++) {
var letter = renderer.layout.values[y][x];
renderer.renderarea.append(letter.toString());
}
if (y < renderer.layout.values.length - 1) {
renderer.renderarea.append('<br/>');
}
}
uhr.currentMinute = -1;
uhr.update();
renderer.renderarea.fadeIn('fast');
});
}
/**
* Ein Buchstabe. Hilfsklasse für den Renderer und Inhalt der Layout-Arrays.
* @param value Der Buchstabe, der Dargestellt werden soll.
* @param style Die CSS-Styleklassen des Buchstabens.
*/
2013-11-25 16:58:21 +01:00
function Letter(value, style) {
this.value = value;
2013-11-25 16:58:21 +01:00
this.style = style || '';
this.getStyle = function() {
2013-11-26 13:25:50 +01:00
return 'item letter ' + style;
}
this.getValue = function() {
return value;
}
}
Letter.prototype.toString = function letterToString() {
2013-11-26 13:25:50 +01:00
return '<span class="' + this.getStyle() + '">' + this.getValue() + '</span>';
}
2013-11-25 11:05:25 +01:00
/**
* Hilfsfunktion, um einen Buchstaben zu erzeugen.
*
* @param letter string: Der Buchstabe, der angezeigt werden soll
* @example l('I', 'is') erzeugt den Buchstaben 'I' mit der CSS-Styleklasse 'is'
*/
function l(letter, style) {
return new Letter(letter, style);
}
2013-11-25 11:05:25 +01:00
/**
* Hilfsfunktion, um einen Buchstaben zu erzeugen, der zu einem Stunden-Wort gehört.
*
2013-11-25 12:53:54 +01:00
* @param letter string: Der Buchstabe, der angezeigt werden soll
* @param hours... integer: Eine Aufzählung von Stundenwerten, zu welchen der Buchstabe als aktiv angezeigt werden soll
* @example h('Z', 2), 11 erzeugt den Buchstaben 'Z', der um 2:xx, 11:xx, 14:xx und 23:xx aktiv angezeigt wird
2013-11-25 11:05:25 +01:00
*/
2013-11-25 12:53:54 +01:00
function h(letter) {
var style = '';
for (var i = 1; i < arguments.length; i++) {
style += ' hour' + arguments[i];
}
return l(letter, style);
}
2013-11-25 11:05:25 +01:00
/**
* Hilfsfunktion, um einen Buchstaben zu erzeugen, der zu einem Minuten-Wort gehört.
*
* @param letter string: Der Buchstabe, der angezeigt werden soll
2013-11-25 12:53:54 +01:00
* @param minutes... integer: Eine Aufzählung von Minutenwerten, zu welchen der Buchstabe als aktiv angezeigt werden soll
2013-11-25 11:05:25 +01:00
* @example m('A', 5, 10, 15, 20, 35) erzeugt den Buchstaben 'A' der um :05, :10, :15, :20 und :35 aktiv angezeigt wird
*/
function m(letter) {
var style = '';
for (var i = 1; i < arguments.length; i++) {
style += ' minute' + arguments[i];
}
return l(letter, style);
}