basically the whole thing is rewritten :)

This commit is contained in:
Manuel Friedli 2014-07-03 20:10:55 +02:00
parent 7b0e77f16f
commit 5a9ec0e0df

619
js/uhr.js
View file

@ -18,34 +18,32 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"id": 0, "id": 0,
"languages": [], "languages": [],
"themes": [], "themes": [],
register: function register(code, language) { registerLanguage: function registerLanguage(code, language) {
this.languages.reduce(function(element) { var alreadyExists = this.languages.reduce(function (exists, element) {
if (exists) {
return true;
}
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 false; return true;
} }
return true; return false;
}, true); }, false);
if (!alreadyExists) {
language.code = code;
this.languages.push(language);
}
} }
}; };
if (window.uhr !== undefined) { if (window.uhr !== undefined) {
return; return;
} }
window.uhr = { window.uhr = {
id: 0, id: 0,
languages: [], languages: [],
themes: [], themes: [],
register: function (code, language) { register: uhrGlobals.registerLanguage
for (var i = 0; i < this.languages.length; i++) {
if (code === this.languages[i].code) {
console.error('Error: Language code ' + code + ' cannot be registered for language "' + language.language + '" because it is already registered for language "' + this.languages[i].language + '"!');
return false;
}
}
language.code = code;
this.languages.push(language);
}
}; };
// auto-detect themes // auto-detect themes
var styleSheets = $('link[rel=stylesheet]'); var styleSheets = $('link[rel=stylesheet]');
@ -64,293 +62,316 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
if (window.uhr.themes.length === 0) { if (window.uhr.themes.length === 0) {
window.uhr.themes.push({}); window.uhr.themes.push({});
} }
$.widget("fritteli.uhr", {
options: {
width: '100%',
status: 'on',
language: 'de_CH',
theme: window.uhr.themes[0].styleClass,
force: false,
controls: true
},
start: function () {
if (!this._isOn()) {
var uhr = this;
this._timer = window.setInterval(function() {
uhr.options.time = new Date();
uhr._update();
}, 1000);
this._update();
this._setCookie('uhr-status', 'on');
} else {
}
},
stop: function () {
if (this._isOn()) {
window.clearInterval(this._timer);
this._timer = null;
this._update();
this._setCookie('uhr-status', 'off');
}
},
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();
});
this._setCookie('uhr-language', languageKey);
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;
this._setCookie('uhr-theme', theme);
}
},
time: function (time) {
this._currentMinute = -1;
if (time === null) {
this.options.time = new Date();
} else {
if (this._timer !== null) {
window.clearInterval(this._timer);
}
this.options.time = time;
}
this._update();
},
// private variables
_id: -1,
_timer: null,
_currentMinute: -1,
// private methods
_isOn: function () {
return this._timer !== null;
},
_update: function () {
if (this._isOn()) {
var time = this.options.time;
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);
this._highlight('hour' + hour);
},
_language: function () {
for (var i = 0; i < window.uhr.languages.length; i++) {
var language = window.uhr.languages[i];
if (language.code == this.options.language) {
return language;
}
}
// fallback: return empty object
return {};
},
_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) % 24;
}
return hour;
},
_getCoarseMinute: function (date) {
if (typeof this._language().getCoarseMinute === 'function') {
return this._language().getCoarseMinute(date);
}
return date.getMinutes();
},
_getDotMinute: function (date) {
if (typeof this._language().getDotMinute === 'function') {
return this._language().getDotMinute(date);
}
var minutes = date.getMinutes();
return minutes % 5;
},
_create: function () {
this._id = window.uhr.id++;
var userTime = this.options.time;
if (this.options.time === undefined) {
this.options.time = new Date();
}
this._setupHTML();
this._wireFunctionality();
if (userTime !== undefined) {
this.time(userTime);
}
},
_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');
if (this.options.controls) { // public interface methods (exported later)
// on/off switch var start = function start() {
var toggleSwitch = $('<div class="onoffswitch" id="uhr-onoffswitch' + this._id + '"></div>'); if (!isOn(this)) {
toggleSwitch.append('<input type="checkbox" class="onoffswitch-checkbox" id="uhr-onoffswitch-checkbox' + this._id + '" checked="checked" />'); var uhr = this;
toggleSwitch.append('<label class="onoffswitch-label" for="uhr-onoffswitch-checkbox' + this._id + '">' this._timer = window.setInterval(function uhrTimer() {
+ '<div class="onoffswitch-inner"></div>' uhr.options.time = new Date();
+ '<div class="onoffswitch-switch"></div>' update(uhr);
+ '</label>'); }, 1000);
e.after(toggleSwitch); update(this);
setCookie(this, 'uhr-status', 'on');
}
};
var stop = function stop() {
if (isOn(this)) {
window.clearInterval(this._timer);
this._timer = null;
update(this);
setCookie(this, 'uhr-status', 'off');
}
};
var toggle = function toggle() {
if (isOn(this)) {
this.stop();
} else {
this.start();
}
};
var setLanguage = function setLanugage(languageKey) {
if (languageKey !== this.options.language) {
this.options.language = languageKey;
var renderer = new UhrRenderer(language(this), this.element.find('.letterarea'));
var uhr = this;
renderer.render(this, function () {
uhr._currentMinute = -1;
update(uhr);
});
setCookie(this, 'uhr-language', languageKey);
update(this);
}
};
var setTheme = function setTheme(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;
setCookie(this, 'uhr-theme', theme);
}
};
var setTime = function setTime(time) {
this._currentMinute = -1;
if (time === null) {
this.options.time = new Date();
} else {
if (this._timer !== null) {
window.clearInterval(this._timer);
}
this.options.time = time;
}
update(this);
};
// language chooser // private interface methods
if (window.uhr.languages.length > 1) { var create = function create() {
var languageChooser = $('<select id="uhr-languagechooser' + this._id + '"></select>'); this._id = window.uhr.id++;
for (var i = 0; i < window.uhr.languages.length; i++) { var userTime = this.options.time;
var language = window.uhr.languages[i]; if (this.options.time === undefined) {
languageChooser.append('<option value="' + language.code + '">' + language.language + '</option>'); this.options.time = new Date();
} }
e.after(languageChooser); setupHTML(this);
} wireFunctionality(this);
if (userTime !== undefined) {
this.time(userTime);
}
};
// private helper methods (not exported)
// set up
var setupHTML = function setupHTML(uhr) {
var e = uhr.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', uhr.options.width);
var realWidth = e.width();
e.width(realWidth);
e.height(realWidth);
e.css('font-size', (realWidth / 40) + 'px');
// theme chooser if (uhr.options.controls) {
if (window.uhr.themes.length > 1) { // on/off switch
var themeChooser = $('<select id="uhr-themechooser' + this._id + '"></select>'); var toggleSwitch = $('<div class="onoffswitch" id="uhr-onoffswitch' + uhr._id + '"></div>');
for (var i = 0; i < window.uhr.themes.length; i++) { toggleSwitch.append('<input type="checkbox" class="onoffswitch-checkbox" id="uhr-onoffswitch-checkbox' + uhr._id + '" checked="checked" />');
var theme = window.uhr.themes[i]; toggleSwitch.append('<label class="onoffswitch-label" for="uhr-onoffswitch-checkbox' + uhr._id + '">'
themeChooser.append('<option value="' + theme.styleClass + '">' + theme.name + '</option>'); + '<div class="onoffswitch-inner"></div>'
} + '<div class="onoffswitch-switch"></div>'
e.after(themeChooser); + '</label>');
} e.after(toggleSwitch);
}
},
_wireFunctionality: function () {
var uhr = this;
// on/off switch // language chooser
var toggleSwitch = $('#uhr-onoffswitch-checkbox' + this._id); if (window.uhr.languages.length > 1) {
toggleSwitch.on('click', function () { var languageChooser = $('<select id="uhr-languagechooser' + uhr._id + '"></select>');
uhr.toggle(); for (var i = 0; i < window.uhr.languages.length; i++) {
}); var language = window.uhr.languages[i];
var status = $.cookie('uhr-status' + this._id); languageChooser.append('<option value="' + language.code + '">' + language.language + '</option>');
if (status === undefined || this.options.force) { }
status = this.options.status; e.after(languageChooser);
} }
toggleSwitch.prop('checked', status === 'on');
if (status === 'on') {
this.start();
} else {
this.stop();
}
// language chooser // theme chooser
var languageChooser = $('#uhr-languagechooser' + this._id); if (window.uhr.themes.length > 1) {
languageChooser.on('change', function () { var themeChooser = $('<select id="uhr-themechooser' + uhr._id + '"></select>');
uhr.language(this.value); for (var i = 0; i < window.uhr.themes.length; i++) {
}); var theme = window.uhr.themes[i];
var selectedLanguage = $.cookie('uhr-language' + this._id); themeChooser.append('<option value="' + theme.styleClass + '">' + theme.name + '</option>');
if (selectedLanguage === undefined || this.options.force) { }
selectedLanguage = this.options.language; e.after(themeChooser);
} }
var found = false; }
for (var i = 0; i < window.uhr.languages.length; i++) { };
var code = window.uhr.languages[i].code; var wireFunctionality = function wireFunctionality(uhr) {
if (selectedLanguage === code) {
found = true;
break;
}
}
if (!found) {
var fallback;
if (window.uhr.languages.length > 0) {
fallback = window.uhr.languages[0].code;
} else {
fallback = '';
}
console.warn("Language " + selectedLanguage + " not found! Using fallback: " + fallback);
selectedLanguage = fallback;
}
languageChooser.val(selectedLanguage);
this.options.language = "";
this.language(selectedLanguage);
// theme chooser // on/off switch
var themeChooser = $('#uhr-themechooser' + this._id); var toggleSwitch = $('#uhr-onoffswitch-checkbox' + uhr._id);
themeChooser.on('change', function () { toggleSwitch.on('click', function () {
uhr.theme(this.value); uhr.toggle();
}); });
var selectedTheme = $.cookie('uhr-theme' + this._id); var status = $.cookie('uhr-status' + uhr._id);
if (selectedTheme === undefined || this.options.force) { if (status === undefined || uhr.options.force) {
selectedTheme = this.options.theme; status = uhr.options.status;
} }
found = false; toggleSwitch.prop('checked', status === 'on');
for (var i = 0; i < window.uhr.themes.length; i++) { if (status === 'on') {
var styleClass = window.uhr.themes[i].styleClass; uhr.start();
if (selectedTheme === styleClass) { } else {
found = true; uhr.stop();
break; }
}
} // language chooser
if (!found) { var languageChooser = $('#uhr-languagechooser' + uhr._id);
var fallback = window.uhr.themes[0].styleClass; languageChooser.on('change', function () {
console.warn("Theme " + selectedTheme + " not found! Using fallback: " + fallback); uhr.language(this.value);
selectedTheme = fallback; });
} var selectedLanguage = $.cookie('uhr-language' + uhr._id);
themeChooser.val(selectedTheme); if (selectedLanguage === undefined || uhr.options.force) {
this.options.theme = ""; selectedLanguage = uhr.options.language;
this.theme(selectedTheme); }
}, var found = false;
_setCookie: function (cookieName, cookieValue) { for (var i = 0; i < window.uhr.languages.length; i++) {
var options = {}; var code = window.uhr.languages[i].code;
if (this.options.cookiePath !== undefined) { if (selectedLanguage === code) {
options = {expires: 365, path: this.options.cookiePath}; found = true;
} else { break;
options = {expires: 365}; }
} }
$.cookie(cookieName + this._id, cookieValue, options); if (!found) {
} var fallback;
}); if (window.uhr.languages.length > 0) {
fallback = window.uhr.languages[0].code;
} else {
fallback = '';
}
console.warn("Language " + selectedLanguage + " not found! Using fallback: " + fallback);
selectedLanguage = fallback;
}
languageChooser.val(selectedLanguage);
uhr.options.language = "";
uhr.language(selectedLanguage);
// theme chooser
var themeChooser = $('#uhr-themechooser' + uhr._id);
themeChooser.on('change', function () {
uhr.theme(this.value);
});
var selectedTheme = $.cookie('uhr-theme' + uhr._id);
if (selectedTheme === undefined || uhr.options.force) {
selectedTheme = uhr.options.theme;
}
found = false;
for (var i = 0; i < window.uhr.themes.length; i++) {
var styleClass = window.uhr.themes[i].styleClass;
if (selectedTheme === styleClass) {
found = true;
break;
}
}
if (!found) {
var fallback = window.uhr.themes[0].styleClass;
console.warn("Theme " + selectedTheme + " not found! Using fallback: " + fallback);
selectedTheme = fallback;
}
themeChooser.val(selectedTheme);
uhr.options.theme = "";
uhr.theme(selectedTheme);
};
var setCookie = function setCookie(uhr, cookieName, cookieValue) {
var options = {};
if (uhr.options.cookiePath !== undefined) {
options = {expires: 365, path: uhr.options.cookiePath};
} else {
options = {expires: 365};
}
$.cookie(cookieName + uhr._id, cookieValue, options);
};
// business logic
var isOn = function isOn(uhr) {
return uhr._timer !== null;
};
var update = function update(uhr) {
if (isOn(uhr)) {
var time = uhr.options.time;
if (time.getMinutes() === uhr._currentMinute) {
return;
}
uhr._currentMinute = time.getMinutes();
show(uhr, time);
} else {
clear(uhr);
uhr._currentMinute = -1;
}
};
var show = function show(uhr, time) {
var dotMinute = getDotMinute(uhr, time);
var hour = getHour(uhr, time);
var coarseMinute = getCoarseMinute(uhr, time);
clear(uhr);
highlight(uhr, 'on');
for (var i = 1; i <= dotMinute; i++) {
highlight(uhr, 'dot' + i);
}
highlight(uhr, 'minute' + coarseMinute);
highlight(uhr, 'hour' + hour);
};
var highlight = function highlight(uhr, itemClass) {
uhr.element.find('.item.' + itemClass).addClass('active');
};
var clear = function clear(uhr) {
uhr.element.find('.item').removeClass('active');
};
var getDotMinute = function getDotMinute(uhr, date) {
if (typeof language(uhr).getDotMinute === 'function') {
return language(uhr).getDotMinute(date);
}
var minutes = date.getMinutes();
return minutes % 5;
};
var getCoarseMinute = function getCoarseMinute(uhr, date) {
if (typeof language(uhr).getCoarseMinute === 'function') {
return language(uhr).getCoarseMinute(date);
}
return date.getMinutes();
};
var getHour = function getHour(uhr, date) {
if (typeof language(uhr).getHour === 'function') {
return language(uhr).getHour(date);
}
var hour = date.getHours();
if (date.getMinutes() >= 25) {
return (hour + 1) % 24;
}
return hour;
};
var language = function language(uhr) {
var matchingLanguages = window.uhr.languages.filter(function (element) {
return (element.code === this.options.language)
}, uhr);
if (matchingLanguages.length > 0) {
return matchingLanguages[0];
}
// fallback: return empty object
return {};
/* for (var i = 0; i < window.uhr.languages.length; i++) {
var language = window.uhr.languages[i];
if (language.code == uhr.options.language) {
return language;
}
}
// fallback: return empty object
return {};*/
};
$.widget("fritteli.uhr", {
"options": {
width: '100%',
status: 'on',
language: 'de_CH',
theme: window.uhr.themes[0].styleClass,
force: false,
controls: true
},
"start": start,
"stop": stop,
"toggle": toggle,
"language": setLanguage,
"theme": setTheme,
"time": setTime,
// constructor method
"_create": create,
// private variables
"_id": -1,
"_timer": null,
"_currentMinute": -1
});
/** /**
* 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.