2013-11-27 11:12:50 +01:00
/ *
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/>.
* /
2014-01-11 03:26:53 +01:00
( function ( $ ) {
2014-06-28 16:40:24 +02:00
'use strict' ;
2014-01-11 03:26:53 +01:00
2014-01-11 20:56:21 +01:00
if ( window . _uhr !== undefined ) {
2014-01-11 03:26:53 +01:00
return ;
}
2014-01-11 20:56:21 +01:00
window . _uhr = {
2014-01-11 03:26:53 +01:00
id : 0 ,
2014-06-28 01:04:53 +02:00
languages : [ ] ,
2014-06-28 16:40:24 +02:00
themes : [ ] ,
register : function ( code , language ) {
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 ) ;
}
2014-01-11 20:56:21 +01:00
} ;
2014-06-28 02:00:34 +02:00
// auto-detect themes
2014-06-28 23:12:20 +02:00
var styleSheets = $ ( 'link[rel=stylesheet]' ) ;
2014-06-28 10:54:28 +02:00
for ( var i = 0 ; i < styleSheets . length ; i ++ ) {
var styleSheet = $ ( styleSheets [ i ] ) ;
var styleClass = styleSheet . attr ( 'data-class' ) ;
if ( styleClass !== undefined ) {
var name = styleSheet . attr ( 'data-name' ) ;
if ( name === undefined ) {
name = styleClass ;
}
2014-06-28 15:53:08 +02:00
window . _uhr . themes . push ( { 'styleClass' : styleClass , 'name' : name } ) ;
2014-06-28 10:54:28 +02:00
}
2014-06-28 02:00:34 +02:00
}
2014-06-28 13:56:20 +02:00
// fall-back if no theme was included
if ( window . _uhr . themes . length == 0 ) {
window . _uhr . themes . push ( { } ) ;
}
2014-01-11 20:56:21 +01:00
$ . widget ( "fritteli.uhr" , {
options : {
width : '100%' ,
status : 'on' ,
language : 'de_CH' ,
2014-06-28 15:53:08 +02:00
theme : window . _uhr . themes [ 0 ] . styleClass ,
2014-01-20 15:47:13 +01:00
force : false ,
2014-06-26 14:41:56 +02:00
controls : true
2014-01-11 20:56:21 +01:00
} ,
start : function ( ) {
if ( ! this . _isOn ( ) ) {
var uhr = this ;
this . _timer = window . setInterval ( function ( ) {
2014-06-26 14:13:21 +02:00
uhr . options . time = new Date ( ) ;
2014-01-11 20:56:21 +01:00
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 ( ) ;
2014-01-11 03:26:53 +01:00
}
} ,
2014-01-11 20:56:21 +01:00
language : function ( languageKey ) {
if ( languageKey !== this . options . language ) {
this . options . language = languageKey ;
var renderer = new UhrRenderer ( this . _language ( ) , this . element . find ( '.letterarea' ) ) ;
2014-01-11 22:54:46 +01:00
var uhr = this ;
renderer . render ( this , function ( ) {
uhr . _currentMinute = - 1 ;
uhr . _update ( ) ;
} ) ;
2014-01-11 20:56:21 +01:00
$ . cookie ( 'uhr-language' + this . _id , languageKey , { expires : 365 , path : '/' } ) ;
this . _update ( ) ;
2014-01-11 03:26:53 +01:00
}
} ,
2014-01-11 20:56:21 +01:00
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 : '/' } ) ;
}
} ,
2014-01-11 22:54:46 +01:00
time : function ( time ) {
2014-06-27 12:27:43 +02:00
this . _currentMinute = - 1 ;
2014-01-11 22:54:46 +01:00
if ( time == null ) {
2014-06-26 14:13:21 +02:00
this . options . time = new Date ( ) ;
2014-01-11 22:54:46 +01:00
} else {
if ( this . _timer != null ) {
window . clearInterval ( this . _timer ) ;
}
2014-06-26 14:13:21 +02:00
this . options . time = time ;
2014-01-11 22:54:46 +01:00
}
2014-06-26 14:13:21 +02:00
this . _update ( ) ;
2014-01-11 22:54:46 +01:00
} ,
2014-01-11 20:56:21 +01:00
// private variables
_id : - 1 ,
_timer : null ,
_currentMinute : - 1 ,
// private methods
_isOn : function ( ) {
return this . _timer !== null ;
} ,
_update : function ( ) {
if ( this . _isOn ( ) ) {
2014-06-26 14:13:21 +02:00
var time = this . options . time ;
2014-01-11 22:54:46 +01:00
if ( time . getMinutes ( ) == this . _currentMinute ) {
2014-01-11 03:26:53 +01:00
return ;
}
2014-01-11 22:54:46 +01:00
this . _currentMinute = time . getMinutes ( ) ;
this . _show ( time ) ;
2014-01-11 03:26:53 +01:00
} else {
2014-01-11 20:56:21 +01:00
this . _clear ( ) ;
this . _currentMinute = - 1 ;
2014-01-11 03:26:53 +01:00
}
} ,
2014-01-11 22:54:46 +01:00
_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 ) ;
} ,
2014-01-11 20:56:21 +01:00
_language : function ( ) {
2014-06-28 16:40:24 +02:00
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 { } ;
2014-01-11 03:26:53 +01:00
} ,
2014-01-11 20:56:21 +01:00
_highlight : function ( itemClass ) {
this . element . find ( '.item.' + itemClass ) . addClass ( 'active' ) ;
2014-01-11 03:26:53 +01:00
} ,
2014-01-11 20:56:21 +01:00
_clear : function ( ) {
this . element . find ( '.item' ) . removeClass ( 'active' ) ;
2014-01-11 03:26:53 +01:00
} ,
2014-01-11 20:56:21 +01:00
_getHour : function ( date ) {
if ( typeof this . _language ( ) . getHour === 'function' ) {
return this . _language ( ) . getHour ( date ) ;
2014-01-11 03:26:53 +01:00
}
var hour = date . getHours ( ) ;
if ( date . getMinutes ( ) >= 25 ) {
return hour + 1 ;
}
return hour ;
} ,
2014-01-11 20:56:21 +01:00
_getCoarseMinute : function ( date ) {
if ( typeof this . _language ( ) . getCoarseMinute === 'function' ) {
return this . _language ( ) . getCoarseMinute ( date ) ;
2014-01-11 03:26:53 +01:00
}
2014-06-26 14:41:56 +02:00
return date . getMinutes ( ) ;
2014-01-11 03:26:53 +01:00
} ,
2014-01-11 20:56:21 +01:00
_getDotMinute : function ( date ) {
if ( typeof this . _language ( ) . getDotMinute === 'function' ) {
return this . _language ( ) . getDotMinute ( date ) ;
2014-01-11 03:26:53 +01:00
}
var minutes = date . getMinutes ( ) ;
return minutes % 5 ;
} ,
2014-01-11 20:56:21 +01:00
_create : function ( ) {
this . _id = window . _uhr . id ++ ;
2014-06-26 14:41:56 +02:00
var userTime = this . options . time ;
if ( this . options . time === undefined ) {
this . options . time = new Date ( ) ;
}
2014-01-11 20:56:21 +01:00
this . _setupHTML ( ) ;
this . _wireFunctionality ( ) ;
2014-06-26 14:41:56 +02:00
if ( userTime !== undefined ) {
this . time ( userTime ) ;
2014-01-11 22:54:46 +01:00
}
2014-01-11 12:18:11 +01:00
} ,
2014-01-11 20:56:21 +01:00
_setupHTML : function ( ) {
var e = this . element ;
// Base clock area
2014-01-11 03:26:53 +01:00
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>' ) ;
2014-01-11 20:56:21 +01:00
e . css ( 'width' , this . options . width ) ;
2014-06-19 23:09:58 +02:00
var realWidth = e . width ( ) ;
2014-01-11 03:26:53 +01:00
e . width ( realWidth ) ;
e . height ( realWidth ) ;
e . css ( 'font-size' , ( realWidth / 40 ) + 'px' ) ;
2014-01-20 15:47:13 +01:00
if ( this . options . controls ) {
// 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 ) ;
2014-01-11 03:26:53 +01:00
2014-01-20 15:47:13 +01:00
// language chooser
2014-06-28 16:40:24 +02:00
if ( window . _uhr . languages . length > 1 ) {
2014-06-28 12:10:24 +02:00
var languageChooser = $ ( '<select id="uhr-languagechooser' + this . _id + '"></select>' ) ;
2014-06-28 16:40:24 +02:00
for ( var i = 0 ; i < window . _uhr . languages . length ; i ++ ) {
var language = window . _uhr . languages [ i ] ;
languageChooser . append ( '<option value="' + language . code + '">' + language . language + '</option>' ) ;
2014-01-20 15:47:13 +01:00
}
2014-06-28 12:10:24 +02:00
e . after ( languageChooser ) ;
2014-01-11 03:26:53 +01:00
}
2014-01-20 15:47:13 +01:00
// theme chooser
2014-06-28 12:10:24 +02:00
if ( window . _uhr . themes . length > 1 ) {
var themeChooser = $ ( '<select id="uhr-themechooser' + this . _id + '"></select>' ) ;
for ( var i = 0 ; i < window . _uhr . themes . length ; i ++ ) {
var theme = window . _uhr . themes [ i ] ;
2014-06-28 15:53:08 +02:00
themeChooser . append ( '<option value="' + theme . styleClass + '">' + theme . name + '</option>' ) ;
2014-06-28 12:10:24 +02:00
}
e . after ( themeChooser ) ;
2014-06-28 01:04:53 +02:00
}
2014-01-20 15:47:13 +01:00
}
2014-01-11 20:56:21 +01:00
} ,
_wireFunctionality : function ( ) {
var uhr = this ;
2014-01-11 03:26:53 +01:00
2014-01-11 20:56:21 +01:00
// on/off switch
var toggleSwitch = $ ( '#uhr-onoffswitch-checkbox' + this . _id ) ;
toggleSwitch . on ( 'click' , function ( ) {
uhr . toggle ( ) ;
2014-01-11 03:26:53 +01:00
} ) ;
2014-01-11 20:56:21 +01:00
var status = $ . cookie ( 'uhr-status' + this . _id ) ;
if ( status == undefined || this . options . force ) {
status = this . options . status ;
2014-01-11 03:26:53 +01:00
}
2014-01-11 20:56:21 +01:00
toggleSwitch . prop ( 'checked' , status == 'on' ) ;
2014-01-11 03:26:53 +01:00
if ( status == 'on' ) {
2014-01-11 20:56:21 +01:00
this . start ( ) ;
2014-01-11 03:26:53 +01:00
} else {
2014-01-11 20:56:21 +01:00
this . stop ( ) ;
2014-01-11 03:26:53 +01:00
}
2014-01-11 20:56:21 +01:00
// language chooser
var languageChooser = $ ( '#uhr-languagechooser' + this . _id ) ;
languageChooser . on ( 'change' , function ( ) {
uhr . language ( this . value ) ;
2014-01-11 03:26:53 +01:00
} ) ;
2014-01-11 20:56:21 +01:00
var selectedLanguage = $ . cookie ( 'uhr-language' + this . _id ) ;
if ( selectedLanguage == undefined || this . options . force ) {
selectedLanguage = this . options . language ;
2014-01-11 03:26:53 +01:00
}
2014-06-28 16:40:24 +02:00
var found = false ;
for ( var i = 0 ; i < window . _uhr . languages . length ; i ++ ) {
var code = window . _uhr . languages [ i ] . code ;
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 ;
}
2014-01-11 20:56:21 +01:00
languageChooser . val ( selectedLanguage ) ;
this . options . language = "" ;
this . language ( selectedLanguage ) ;
2014-01-11 03:26:53 +01:00
2014-01-11 20:56:21 +01:00
// theme chooser
var themeChooser = $ ( '#uhr-themechooser' + this . _id ) ;
themeChooser . on ( 'change' , function ( ) {
uhr . theme ( this . value ) ;
2014-01-11 03:26:53 +01:00
} ) ;
2014-01-11 20:56:21 +01:00
var selectedTheme = $ . cookie ( 'uhr-theme' + this . _id ) ;
if ( selectedTheme == undefined || this . options . force ) {
selectedTheme = this . options . theme ;
2014-01-11 03:26:53 +01:00
}
2014-06-28 16:40:24 +02:00
found = false ;
2014-06-28 13:56:20 +02:00
for ( var i = 0 ; i < window . _uhr . themes . length ; i ++ ) {
2014-06-28 15:53:08 +02:00
var styleClass = window . _uhr . themes [ i ] . styleClass ;
2014-06-28 13:56:20 +02:00
if ( selectedTheme == styleClass ) {
found = true ;
break ;
}
}
if ( ! found ) {
2014-06-28 15:53:08 +02:00
var fallback = window . _uhr . themes [ 0 ] . styleClass ;
2014-06-28 13:56:20 +02:00
console . warn ( "Theme " + selectedTheme + " not found! Using fallback: " + fallback ) ;
selectedTheme = fallback ;
}
2014-01-11 20:56:21 +01:00
themeChooser . val ( selectedTheme ) ;
this . options . theme = "" ;
this . theme ( selectedTheme ) ;
2013-11-26 13:25:50 +01:00
}
2013-11-28 08:51:37 +01:00
} ) ;
2014-06-28 15:53:08 +02: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 ;
2014-06-25 12:52:32 +02:00
}
2014-06-28 15:53:08 +02:00
UhrRenderer . prototype . render = function ( uhr , beforeshow ) {
var renderer = this ;
if ( this . layout . _parsed === undefined ) {
switch ( this . layout . version ) {
case 2 :
var delegate = new _UhrRendererV2Delegate ( this . layout ) ;
this . layout . _parsed = delegate . parse ( ) ;
break ;
default :
console . warn ( "Unknown layout version: '" + this . layout . version + "'" ) ;
return ;
2013-11-26 13:25:50 +01:00
}
}
2014-06-28 15:53:08 +02:00
var letters = this . layout . _parsed ;
this . renderarea . fadeOut ( 'fast' , function ( ) {
renderer . renderarea . empty ( ) ;
for ( var y = 0 ; y < letters . length ; y ++ ) {
for ( var x = 0 ; x < letters [ y ] . length ; x ++ ) {
var letter = letters [ y ] [ x ] ;
renderer . renderarea . append ( letter . toString ( ) ) ;
}
if ( y < letters . length - 1 ) {
renderer . renderarea . append ( '<br/>' ) ;
}
}
if ( typeof beforeshow === 'function' ) {
beforeshow ( ) ;
}
renderer . renderarea . fadeIn ( 'fast' ) ;
} ) ;
} ;
function _UhrRendererV2Delegate ( layout ) {
this . layout = layout ;
this . _parseArrayOrObject = function ( letters , styleClass , input ) {
if ( Array . isArray ( input ) ) {
for ( var i = 0 ; i < input . length ; i ++ ) {
this . _parseObject ( letters , styleClass , input [ i ] ) ;
}
} else {
this . _parseObject ( letters , styleClass , input ) ;
}
2014-06-26 15:52:08 +02:00
}
2014-06-28 15:53:08 +02:00
this . _parseObject = function ( letters , styleClass , object ) {
for ( var line in object ) {
if ( object . hasOwnProperty ( line ) ) {
var highlightLetters = object [ line ] ;
for ( var i = 0 ; i < highlightLetters . length ; i ++ ) {
var x = highlightLetters [ i ] - 1 ;
letters [ line - 1 ] [ x ] . addStyle ( styleClass ) ;
}
2014-06-26 15:52:08 +02:00
}
}
}
2014-06-28 15:53:08 +02:00
this . _parseTimeDefinition = function ( letters , styleClass , definition ) {
for ( var listString in definition ) {
if ( definition . hasOwnProperty ( listString ) ) {
var array = listString . split ( ',' ) ;
var highlightLetters = definition [ listString ] ;
for ( var index = 0 ; index < array . length ; index ++ ) {
this . _parseArrayOrObject ( letters , styleClass + array [ index ] , highlightLetters ) ;
}
2014-06-26 15:52:08 +02:00
}
}
}
}
2014-06-28 15:53:08 +02:00
_UhrRendererV2Delegate . prototype . parse = function ( ) {
var letters = [ ] ;
for ( var i = 0 ; i < this . layout . letters . length ; i ++ ) {
var line = [ ] ;
var string = this . layout . letters [ i ] ;
for ( var c = 0 ; c < string . length ; c ++ ) {
var character = new Letter ( string [ c ] ) ;
line . push ( character ) ;
}
letters . push ( line ) ;
2014-06-26 15:52:08 +02:00
}
2014-06-28 15:53:08 +02:00
this . _parseArrayOrObject ( letters , 'on' , this . layout . permanent ) ;
this . _parseTimeDefinition ( letters , 'minute' , this . layout . minutes ) ;
this . _parseTimeDefinition ( letters , 'hour' , this . layout . hours ) ;
return letters ;
2014-06-19 23:09:58 +02:00
} ;
2014-06-28 15:53:08 +02:00
/ * *
* 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 ' + this . style ;
} ;
this . getValue = function ( ) {
return value ;
}
this . addStyle = function ( style ) {
if ( this . style == '' ) {
this . style = style ;
} else {
this . style += ' ' + style ;
}
2014-06-26 13:46:10 +02:00
}
}
2014-06-28 15:53:08 +02:00
Letter . prototype . toString = function letterToString ( ) {
return '<span class="' + this . getStyle ( ) + '">' + this . getValue ( ) + '</span>' ;
} ;
} ) ( jQuery ) ;