forked from tleilax/studip-clockip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.js
executable file
·103 lines (95 loc) · 3.19 KB
/
clock.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
(function($, STUDIP) {
var Clock, date, pad;
if (!(typeof STUDIP === 'object' && typeof $ === 'function')) {
throw 'No Stud.IP environment';
}
if (!(($.fn != null) && ($.fn.jquery != null) && $.fn.jquery >= '1.4.3')) {
throw 'Invalid jQuery version, required version is 1.4.3';
}
pad = function(what, length) {
if (length == null) length = 2;
return ('0000' + what).slice(-length);
};
date = function(format, timestamp) {
var days, mapping;
if (format == null) format = 'H:i:s';
if (timestamp == null) timestamp = null;
if (typeof timestamp !== 'object') timestamp = new Date(timestamp);
days = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'];
mapping = {
d: 'getDate',
H: 'getHours',
i: 'getMinutes',
s: 'getSeconds'
};
return format.replace(/[dHilmsY]/g, function(token) {
var method;
method = mapping[token];
switch (token) {
case 'l':
return days[timestamp.getDay()].toLocaleString();
case 'm':
return pad(timestamp.getMonth() + 1);
case 'Y':
return (timestamp.getYear() % 100) + 2000;
default:
if (method != null) {
return pad(timestamp[method]());
} else {
return token;
}
}
});
};
Clock = (function() {
function Clock(element, offset, format) {
var metadata, _ref;
if (element == null) element = '.clockip';
this.offset = offset != null ? offset : 0;
this.format = format != null ? format : 'H:i:s';
this.stop = __bind(this.stop, this);
this.start = __bind(this.start, this);
this.display = __bind(this.display, this);
this.adjust = __bind(this.adjust, this);
this.element = $(element);
this.interval = null;
metadata = (_ref = this.element.data()) != null ? _ref : {};
if (metadata.format != null) this.format = metadata.format;
if (metadata.timestamp != null) this.adjust(metadata.timestamp);
}
Clock.prototype.adjust = function(timestamp) {
return this.offset = timestamp ? timestamp - (new Date()).getTime() : 0;
};
Clock.prototype.display = function() {
var now, time;
now = (new Date()).getTime();
time = date(this.format, now + this.offset);
if (time !== this.last) {
this.element.text(time);
return this.last = time;
}
};
Clock.prototype.start = function() {
this.stop();
return this.interval = setInterval(this.display, 100);
};
Clock.prototype.stop = function() {
if (this.interval != null) clearInterval(interval);
this.interval = null;
return this.last = null;
};
return Clock;
})();
return $(function() {
var clock, infobox_clock;
clock = new Clock;
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();
return STUDIP.CLOCK = clock;
});
})(jQuery, STUDIP);