uhr-objekt erstellt und funktionalität gezügelt

This commit is contained in:
Manuel Friedli 2013-11-26 12:31:50 +01:00
parent 48886ff958
commit d69c6ecfab
3 changed files with 238 additions and 112 deletions

95
index-test.html Normal file
View file

@ -0,0 +1,95 @@
<!DOCTYPE html>
<html manifest="manifest.appcache">
<head>
<title>Die Zeit als Wort - in HTML, CSS und JS</title>
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript" src="uhr-object.js"></script>
<script type="text/javascript" src="uhr.js"></script>
<script type="text/javascript" src="uhr-de_CH.js"></script>
<script type="text/javascript" src="uhr-de.js"></script>
<script type="text/javascript" src="uhr-en.js"></script>
<link rel="stylesheet" type="text/css" href="uhr.css" />
<link rel="stylesheet" type="text/css" href="uhr-black.css" id="theme" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="uhr">
<span class="item dot dot1" id="dot1"></span>
<span class="item dot dot2" id="dot2"></span>
<span class="item dot dot3" id="dot3"></span>
<span class="item dot dot4" id="dot4"></span>
<div id="renderarea"></div>
<!-- glossy reflection -->
<span id="reflection"></span>
</div>
<select id="themeswitcher">
<option value="black">Schwarz</option>
<option value="red">Rot</option>
<option value="blue">Blau</option>
<option value="green">Grün</option>
<option value="white">Weiss</option>
</select>
<select id="layoutswitcher">
<option value="de_CH">Bärndütsch</option>
<option value="de">Hochdeutsch</option>
<option value="en">English</option>
</select>
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="onoffswitch" checked="checked" onclick="uhr.toggle();" />
<label class="onoffswitch-label" for="onoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<p id="disclaimer">Created by fritteli, inspired by <a href="http://www.qlocktwo.com/">QLOCKTWO</a>.
<script type="text/javascript">
var uhr = new Uhr($('#renderarea'), $('#theme'));
uhr.register('de_CH', layout['de_CH']);
uhr.register('de', layout['de']);
uhr.register('en', layout['en']);
$(document).ready(function() {
$('#layoutswitcher').on('change', function() {
uhr.setLayout(this.value);
});
$('#themeswitcher').on('change', function() {
uhr.setTheme(this.value);
});
$.cookie.defaults.expires = 365;
$.cookie.defaults.path = '/';
var theme = $.cookie('theme');
var layout = $.cookie('layout');
var status = $.cookie('status');
if (theme == undefined) {
theme = 'black';
}
if(layout == undefined) {
layout = 'de_CH';
}
if (status == undefined) {
status = 'on';
}
$('#themeswitcher').val(theme);
uhr.setTheme(theme);
$('#layoutswitcher').val(layout);
uhr.setLayout(layout);
if (status == 'on') {
uhr.start();
if(!isOn()) {
uhr.stop();
$('#onoffswitch').click();
}
} else {
uhr.stop();
if(isOn()) {
uhr.start();
$('#onoffswitch').click();
}
}
});
</script>
</body>
</html>

143
uhr-object.js Normal file
View file

@ -0,0 +1,143 @@
function Uhr(renderarea, themeElement) {
this.renderarea = renderarea;
this.themeElement = themeElement;
this.timer = null;
this.currentTheme = null;
this.currentLayout = null;
this.layouts = new Array();
this.currentMinute = -1;
}
Uhr.prototype.toggle = function() {
if (this.isOn()) {
this.stop();
} else {
this.start();
}
}
Uhr.prototype.start = function() {
if (!this.isOn()) {
var uhr = this;
this.timer = window.setInterval(function() {uhr.update();}, 1000);
this.update();
$.cookie('status', 'on');
}
}
Uhr.prototype.stop = function() {
if (this.isOn()) {
window.clearInterval(this.timer);
this.timer = null;
this.update();
$.cookie('status', 'off');
}
}
Uhr.prototype.isOn = function() {
return this.timer != null;
}
Uhr.prototype.register = function(key, layout) {
this.layouts[key] = layout;
}
Uhr.prototype.setLayout = function(locale) {
var newLayout = this.layouts[locale];
if (newLayout !== undefined && newLayout != this.currentLayout) {
this.currentLayout = newLayout;
var renderer = new UhrRenderer(this.currentLayout, this.renderarea);
renderer.render(this);
$.cookie('layout', locale);
}
}
Uhr.prototype.setTheme = function(theme) {
if (theme != this.currentTheme) {
this.currentTheme = theme;
this.themeElement.attr('href', 'uhr-' + theme + '.css');
$.cookie('theme', theme);
}
}
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() {
this.renderarea.find('.item').removeClass('active');
}
Uhr.prototype.highlight = function(itemClass) {
this.renderarea.find('.item.' + itemClass).addClass('active');
}
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;
}
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.update();
renderer.renderarea.fadeIn('fast');
});
}

112
uhr.js
View file

@ -1,91 +1,8 @@
var clock = null;
var currentMinute = -1;
var layout = new Array();
layout['default'] = {
language: 'Undefined',
values: []
};
var currentLayout = layout['default'];
function highlightCurrentTime() {
var now = new Date();
var dotMinute = getDotMinute(now);
if (dotMinute == currentMinute) {
return;
}
currentMinute = dotMinute;
var hour = getHour(now);
var coarseMinute = getCoarseMinute(now);
resetItems();
for (var i = 1; i <= dotMinute; i++) {
highlight('dot' + i);
}
highlight('minute' + coarseMinute);
hour = normalizeHour(hour);
highlight('hour' + hour);
if (coarseMinute == 0) {
highlight('sharphour');
}
}
function getHour(date) {
if (typeof currentLayout.getHour === 'function') {
return currentLayout.getHour(date);
}
var hour = date.getHours();
if (date.getMinutes() >= 25) {
return hour + 1;
}
return hour;
}
function getCoarseMinute(date) {
if (typeof currentLayout.getCoarseMinute === 'function') {
return currentLayout.getCoarseMinute(date);
}
var minutes = date.getMinutes();
return minutes - getDotMinute(date);
}
function getDotMinute(date) {
if (typeof currentLayout.getDotMinute === 'function') {
return currentLayout.getDotMinute(date);
}
var minutes = date.getMinutes();
return minutes % 5;
}
function clearDisplay() {
$('.item').removeClass('active');
}
function resetItems() {
clearDisplay()
highlight('on');
}
function highlight(itemClass) {
$('.item.' + itemClass).addClass('active');
}
function normalizeHour(hour) {
if (hour > 12) {
hour %= 12;
}
if (hour == 0) {
return 12;
}
return hour;
}
function startClock() {
if (clock == null) {
highlightCurrentTime();
clock = window.setInterval(highlightCurrentTime, 1000);
}
$.cookie('status', 'on');
}
function stopClock() {
if (clock != null) {
window.clearInterval(clock);
clock = null;
currentMinute = -1;
clearDisplay();
}
$.cookie('status', 'off');
}
function updateClockState() {
if (isOn()) {
startClock();
@ -96,35 +13,6 @@ function updateClockState() {
function isOn() {
return $('#onoffswitch').is(':checked');
}
function switchTheme(theme) {
$('#theme').attr('href', 'uhr-' + theme + '.css');
$.cookie('theme', theme);
}
function switchLayout(locale) {
stopClock();
currentLayout = layout[locale];
if (currentLayout == undefined) {
currentLayout = layout['default'];
}
renderLayout();
if (isOn()) {
startClock();
}
$.cookie('layout', locale);
}
function renderLayout() {
var container = $('#renderarea');
container.empty();
for (var y = 0; y < currentLayout.values.length; y++) {
for (var x = 0; x < currentLayout.values[y].length; x++) {
var letter = currentLayout.values[y][x];
container.append(letter.toString());
}
if (y < currentLayout.values.length - 1) {
container.append('<br/>');
}
}
}
function Letter(value, style) {
this.value = value;
this.style = style || '';