session.js
1.72 KB
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
function Session(idSelector, infoSelector, user)
{
this.user = user;
this.idElement = $(idSelector);
this.canvas = $(infoSelector + " canvas").get(0);
this.context = this.canvas.getContext("2d");
this._interval = null;
this.update();
}
Session.prototype.update = function() {
$.getJSON("/sessinfo/", $.proxy(this._update, this));
}
Session.prototype.clear = function() {
this.id = "none";
this.timeout = 0;
this.timeleft = 0;
this._fraction = 0.0;
this._leftWidth = 0.0;
this.idElement.empty().append(this.id);
this.user.update();
this._stop();
}
/*
* not real private but as a convention I use _ prefix to indicate
* internal use only.
*/
Session.prototype._update = function(data)
{
this.id = ("0" == data.id)? "none" : data.id;
this.timeout = data.timeout;
this.timeleft = data.timeleft;
this._fraction = this.canvas.width / this.timeout;
this._leftWidth = this._fraction * this.timeleft;
this.idElement.empty().append(this.id);
this._start();
}
Session.prototype._start = function()
{
this._draw();
if (0 < this.timeleft && null === this._interval) {
this._interval = setInterval($.proxy(this._process, this), 5000);
}
}
Session.prototype._process = function()
{
if (0 >= this.timeleft) {
this.clear();
}
else {
this.timeleft-=5;
this._leftWidth -= 5*this._fraction;
this._draw();
}
}
Session.prototype._draw = function() {
this.context.fillStyle = "rgb(255, 0, 0)";
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.context.fillStyle = "rgb(0, 255, 0)";
this.context.fillRect(0, 0, this._leftWidth, this.canvas.height);
}
Session.prototype._stop = function()
{
clearInterval(this._interval);
this._interval = null;
}
// vim: set ts=4 sw=4: