361 lines
11 KiB
JavaScript
361 lines
11 KiB
JavaScript
/*
|
|
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/>.
|
|
*/
|
|
(function($) {
|
|
"use strict";
|
|
|
|
if (window._uhr !== undefined) {
|
|
return;
|
|
}
|
|
|
|
window._uhr = {
|
|
id: 0,
|
|
languages: new Array()
|
|
};
|
|
$.widget("fritteli.uhr", {
|
|
options: {
|
|
width: '100%',
|
|
status: 'on',
|
|
language: 'de_CH',
|
|
theme: 'black',
|
|
force: false
|
|
},
|
|
start: function() {
|
|
if (!this._isOn()) {
|
|
var uhr = this;
|
|
this._timer = window.setInterval(function() {
|
|
uhr._update();
|
|
}, 1000);
|
|
this._update();
|
|
$.cookie('uhr-status' + this._id, 'on', {expires: 365, path: '/'});
|
|
} else {
|
|
}
|
|
},
|
|
stop: function() {
|
|
if(this._isOn()) {
|
|
window.clearInterval(this._timer);
|
|
this._timer = null;
|
|
this._update();
|
|
$.cookie('uhr-status' + this._id, 'off', {expires: 365, path: '/'});
|
|
}
|
|
},
|
|
toggle: function() {
|
|
if(this._isOn()) {
|
|
this.stop();
|
|
} else {
|
|
this.start();
|
|
}
|
|
},
|
|
language: function(languageKey) {
|
|
if (languageKey !== this.options.language) {
|
|
this.options.language = languageKey;
|
|
var renderer = new UhrRenderer(this._language(), this.element.find('.letterarea'));
|
|
var uhr = this;
|
|
renderer.render(this, function() {
|
|
uhr._currentMinute = -1;
|
|
uhr._update();
|
|
});
|
|
$.cookie('uhr-language' + this._id, languageKey, {expires: 365, path: '/'});
|
|
this._update();
|
|
}
|
|
},
|
|
theme: function(theme) {
|
|
if (theme != this.options.theme) {
|
|
this.element.removeClass(this.options.theme).addClass(theme);
|
|
$('#uhr-onoffswitch' + this._id).removeClass(this.options.theme).addClass(theme);
|
|
this.options.theme = theme;
|
|
$.cookie('uhr-theme' + this._id, theme, {expires: 365, path: '/'});
|
|
}
|
|
},
|
|
time: function(time) {
|
|
this.options.time = time;
|
|
if (time == null) {
|
|
this._currentMinute = -1;
|
|
this._update();
|
|
} else {
|
|
if (this._timer != null) {
|
|
window.clearInterval(this._timer);
|
|
}
|
|
var renderer = new UhrRenderer(this._language(), this.element.find('.letterarea'));
|
|
var uhr = this;
|
|
renderer.render(this, function() {
|
|
uhr._show(time);
|
|
});
|
|
|
|
}
|
|
},
|
|
// private variables
|
|
_id: -1,
|
|
_timer: null,
|
|
_currentMinute: -1,
|
|
// private methods
|
|
_isOn: function() {
|
|
return this._timer !== null;
|
|
},
|
|
_update: function() {
|
|
if (this._isOn()) {
|
|
var time = new Date();
|
|
if (time.getMinutes() == this._currentMinute) {
|
|
return;
|
|
}
|
|
this._currentMinute = time.getMinutes();
|
|
this._show(time);
|
|
} else {
|
|
this._clear();
|
|
this._currentMinute = -1;
|
|
}
|
|
},
|
|
_show: function(time) {
|
|
var dotMinute = this._getDotMinute(time);
|
|
var hour = this._getHour(time);
|
|
var coarseMinute = this._getCoarseMinute(time);
|
|
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');
|
|
}
|
|
},
|
|
_language: function() {
|
|
return window._uhr.languages[this.options.language];
|
|
},
|
|
_highlight: function(itemClass) {
|
|
this.element.find('.item.' + itemClass).addClass('active');
|
|
},
|
|
_clear: function() {
|
|
this.element.find('.item').removeClass('active');
|
|
},
|
|
_getHour: function(date) {
|
|
if (typeof this._language().getHour === 'function') {
|
|
return this._language().getHour(date);
|
|
}
|
|
var hour = date.getHours();
|
|
if (date.getMinutes() >= 25) {
|
|
return hour + 1;
|
|
}
|
|
return hour;
|
|
},
|
|
_getCoarseMinute: function(date) {
|
|
if (typeof this._language().getCoarseMinute === 'function') {
|
|
return this._language().getCoarseMinute(date);
|
|
}
|
|
var minutes = date.getMinutes();
|
|
return minutes - this._getDotMinute(date);
|
|
},
|
|
_getDotMinute: function(date) {
|
|
if (typeof this._language().getDotMinute === 'function') {
|
|
return this._language().getDotMinute(date);
|
|
}
|
|
var minutes = date.getMinutes();
|
|
return minutes % 5;
|
|
},
|
|
_normalizeHour: function(hour) {
|
|
if (hour > 12) {
|
|
hour %= 12;
|
|
}
|
|
if (hour == 0) {
|
|
return 12;
|
|
}
|
|
return hour;
|
|
},
|
|
_create: function() {
|
|
this._id = window._uhr.id++;
|
|
this._setupHTML();
|
|
this._wireFunctionality();
|
|
if (this.options.time !== undefined) {
|
|
this.time(this.options.time);
|
|
}
|
|
},
|
|
_setupHTML: function() {
|
|
var e = this.element;
|
|
// Base clock area
|
|
e.addClass('uhr');
|
|
e.empty();
|
|
e.append('<span class="item dot dot1"></span>');
|
|
e.append('<span class="item dot dot2"></span>');
|
|
e.append('<span class="item dot dot3"></span>');
|
|
e.append('<span class="item dot dot4"></span>');
|
|
e.append('<div class="letterarea"></div>');
|
|
e.append('<div class="reflection"></div>');
|
|
e.css('width', this.options.width);
|
|
var realWidth = e.width()
|
|
e.width(realWidth);
|
|
e.height(realWidth);
|
|
e.css('font-size', (realWidth / 40) + 'px');
|
|
|
|
// on/off switch
|
|
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('<label class="onoffswitch-label" for="uhr-onoffswitch-checkbox' + this._id + '">'
|
|
+ '<div class="onoffswitch-inner"></div>'
|
|
+ '<div class="onoffswitch-switch"></div>'
|
|
+ '</label>');
|
|
e.after(toggleSwitch);
|
|
|
|
// language chooser
|
|
var languageChooser = $('<select id="uhr-languagechooser' + this._id + '"></select>')
|
|
for (var code in window._uhr.languages) {
|
|
if (window._uhr.languages.hasOwnProperty(code)) {
|
|
var language = window._uhr.languages[code];
|
|
languageChooser.append('<option value="' + code + '">' + language.language + '</option>');
|
|
}
|
|
}
|
|
e.after(languageChooser);
|
|
|
|
// theme chooser
|
|
var themeChooser = $('<select id="uhr-themechooser' + this._id + '"></select>');
|
|
themeChooser.append('<option value="black">Schwarz</option>');
|
|
themeChooser.append('<option value="red">Rot</option>');
|
|
themeChooser.append('<option value="blue">Blau</option>');
|
|
themeChooser.append('<option value="green">Grün</option>');
|
|
themeChooser.append('<option value="white">Weiss</option>');
|
|
e.after(themeChooser);
|
|
},
|
|
_wireFunctionality: function() {
|
|
var e = this.element;
|
|
var uhr = this;
|
|
|
|
// on/off switch
|
|
var toggleSwitch = $('#uhr-onoffswitch-checkbox' + this._id);
|
|
toggleSwitch.on('click', function() {
|
|
uhr.toggle();
|
|
});
|
|
var status = $.cookie('uhr-status' + this._id);
|
|
if (status == undefined || this.options.force) {
|
|
status = this.options.status;
|
|
}
|
|
toggleSwitch.prop('checked', status == 'on');
|
|
if (status == 'on') {
|
|
this.start();
|
|
} else {
|
|
this.stop();
|
|
}
|
|
|
|
// language chooser
|
|
var languageChooser = $('#uhr-languagechooser' + this._id);
|
|
languageChooser.on('change', function() {
|
|
uhr.language(this.value);
|
|
});
|
|
var selectedLanguage = $.cookie('uhr-language' + this._id);
|
|
if (selectedLanguage == undefined || this.options.force) {
|
|
selectedLanguage = this.options.language;
|
|
}
|
|
languageChooser.val(selectedLanguage);
|
|
this.options.language = "";
|
|
this.language(selectedLanguage);
|
|
|
|
// theme chooser
|
|
var themeChooser = $('#uhr-themechooser' + this._id);
|
|
themeChooser.on('change', function() {
|
|
uhr.theme(this.value);
|
|
});
|
|
var selectedTheme = $.cookie('uhr-theme' + this._id);
|
|
if (selectedTheme == undefined || this.options.force) {
|
|
selectedTheme = this.options.theme;
|
|
}
|
|
themeChooser.val(selectedTheme);
|
|
this.options.theme = "";
|
|
this.theme(selectedTheme);
|
|
}
|
|
});
|
|
})(jQuery);
|
|
|
|
/**
|
|
* 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, beforeshow) {
|
|
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/>');
|
|
}
|
|
}
|
|
if (typeof beforeshow === 'function') {
|
|
beforeshow();
|
|
}
|
|
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.
|
|
*/
|
|
function Letter(value, style) {
|
|
this.value = value;
|
|
this.style = style || '';
|
|
this.getStyle = function() {
|
|
return 'item letter ' + style;
|
|
}
|
|
this.getValue = function() {
|
|
return value;
|
|
}
|
|
}
|
|
Letter.prototype.toString = function letterToString() {
|
|
return '<span class="' + this.getStyle() + '">' + this.getValue() + '</span>';
|
|
}
|
|
/**
|
|
* 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);
|
|
}
|
|
/**
|
|
* Hilfsfunktion, um einen Buchstaben zu erzeugen, der zu einem Stunden-Wort gehört.
|
|
*
|
|
* @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
|
|
*/
|
|
function h(letter) {
|
|
var style = '';
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
style += ' hour' + arguments[i];
|
|
}
|
|
return l(letter, style);
|
|
}
|
|
/**
|
|
* Hilfsfunktion, um einen Buchstaben zu erzeugen, der zu einem Minuten-Wort gehört.
|
|
*
|
|
* @param letter string: Der Buchstabe, der angezeigt werden soll
|
|
* @param minutes... integer: Eine Aufzählung von Minutenwerten, zu welchen der Buchstabe als aktiv angezeigt werden soll
|
|
* @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);
|
|
}
|