-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclock.coffee
executable file
·85 lines (72 loc) · 3.13 KB
/
clock.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(($) ->
throw 'No Stud.IP environment' unless typeof STUDIP is 'object' and typeof $ is 'function'
throw 'Invalid jQuery version, required version is 1.4.3' unless $.fn? and $.fn.jquery? and $.fn.jquery >= '1.4.3'
# enable periodic synchronisation with server timestamp, requires Stud.IP 2.2
# STUDIP.jsupdate_enable = (STUDIP.VERSION >= '2.2')
# simple pad function
pad = (what, length = 2) -> ('0000' + what).slice -length
# Quick port of PHP's date() function to JS, far from complete
date = (format = 'H:i:s', timestamp = null) ->
timestamp = new Date(timestamp) unless typeof timestamp is 'object'
days = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag']
mapping =
d: 'getDate'
H: 'getHours'
i: 'getMinutes'
s: 'getSeconds'
format.replace /[dHilmsY]/g, (token) ->
method = mapping[token]
switch token
when 'l' then days[timestamp.getDay()].toLocaleString()
when 'm' then pad(timestamp.getMonth() + 1)
when 'Y' then (timestamp.getYear() % 100) + 2000
else
if method? then pad timestamp[method]() else token
class Clock
constructor: (element = '.clockip', @offset = 0, @format = 'H:i:s') ->
@element = $(element)
@interval = null
metadata = @element.data() ? {}
@format = metadata.format if metadata.format?
if 'matchMedia' in window
if metadata.mediumformat?
tokens = metadata.mediumformat.split(')')
@format = tokens[1] if window.matchMedia(tokens[0] + ')').matches
if metadata.smallformat?
tokens = metadata.smallformat.split(')')
@format = tokens[1] if window.matchMedia(tokens[0] + ')').matches
@adjust(metadata.timestamp) if metadata.timestamp?
adjust: (timestamp) =>
@offset = if timestamp then timestamp - (new Date()).getTime() else 0
display: =>
now = (new Date()).getTime()
time = date(@format, now + @offset)
if time isnt @last
@element.text time
@last = time
@element.show()
start: =>
@stop()
@interval = setInterval(@display, 100)
stop: =>
@element.hide()
clearInterval(@interval) if @interval?
@interval = null
@last = null
# initialize upon domready
$ ->
clock = new Clock
if clock.element.hasClass('uni-oldenburg') && $('header').length is 0
clock.element.hide()
return
if clock.element.hasClass('uni-augsburg')
infobox_clock = $ """
<b>Aktuelle Serverzeit:</b><br>
"""
clock.element
.addClass('sidebar')
.appendTo(infobox_clock)
infobox_clock.appendTo('.infoboxrahmen')
_(clock.start).defer()
STUDIP.CLOCK = clock
)(jQuery)