forked from noneck/timer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
toastmaster.js
163 lines (140 loc) · 5.38 KB
/
toastmaster.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var SpeechType = (function () {
function SpeechType(name, greenTime, yellowTime, redTime, id) {
this.name = name;
this.greenTime = greenTime;
this.yellowTime = yellowTime;
this.redTime = redTime;
this.id = id;
}
return SpeechType;
})();
var TSTimer = (function () {
function TSTimer(speeches) {
var _this = this;
this.started = false;
this.speeches = speeches;
$.each(this.speeches, function (indexInArray, valueOfElement) {
var newButton = $('<span>').attr('id', valueOfElement.id).addClass('speech-type').html(valueOfElement.name);
newButton.click(function (event) {
_this.activateSpeech($(event.target).attr('id'));
});
newButton.appendTo('#buttons');
});
$(window).resize(function () {
_this.resizeTime();
});
this.resizeTime();
$('#btnReset').click(function () {
_this.resetButton();
});
$('#btnStart').click(function () {
_this.startButton();
});
}
TSTimer.prototype.resetButton = function () {
this.stop();
if($('#showDigits').is(':checked')){
$('#trafficlight').text('0:00');
} else {
$('#trafficlight').text('');
}
$('#body').css('background-color', '#EFEEEF');
this.startTime = null;
};
TSTimer.prototype.startButton = function () {
if (this.started) {
this.stop();
} else {
this.start();
}
};
TSTimer.prototype.resizeTime = function () {
var width = $(window).width();
var x = Math.floor((width < 900) ? (width / 900) * 28 : 28);
$('#trafficlight').css('font-size', x + 'em');
};
TSTimer.prototype.setElementText = function (elapsedSeconds) {
if($('#showDigits').is(':checked')){
$('#trafficlight').text(this.formatTime(elapsedSeconds));
} else {
$('#trafficlight').text(''); // probably redundant
}
if (elapsedSeconds >= this.red) {
$('#body').css('background-color', '#FF4040');
} else if (elapsedSeconds >= this.yellow) {
$('#body').css('background-color', '#FCDC3B');
} else if (elapsedSeconds >= this.green) {
$('#body').css('background-color', '#A7DA7E');
}
};
TSTimer.prototype.timerEvent = function () {
if (!this.startTime) {
this.startTime = new Date();
}
var timeNow = new Date();
var elapsedSeconds = this.timeDiffInSeconds(this.startTime, timeNow);
this.setElementText(elapsedSeconds);
};
TSTimer.prototype.timeDiffInSeconds = function (earlyTime, lateTime) {
var diff = lateTime.getTime() - earlyTime.getTime();
return Math.floor(diff / 1000);
};
TSTimer.prototype.formatTime = function (elapsedSeconds) {
var minutes = Math.floor(elapsedSeconds / 60);
var seconds = elapsedSeconds % 60;
return minutes + ":" + ((seconds < 10) ? "0" + seconds.toString() : seconds.toString());
};
TSTimer.prototype.start = function () {
var _this = this;
$('#btnStart').html('Stop');
this.started = true;
if (this.startTime) {
var newStartTime = new Date().getTime() - (this.stopTime.getTime() - this.startTime.getTime());
this.startTime.setTime(newStartTime);
}
this.green = this.getSecondsFromTextBox('#green-light');
this.yellow = this.getSecondsFromTextBox('#yellow-light');
this.red = this.getSecondsFromTextBox('#red-light');
this.timerToken = setInterval(function () {
return _this.timerEvent();
}, 100);
};
TSTimer.prototype.stop = function () {
$('#btnStart').html('Start');
this.started = false;
this.stopTime = new Date();
clearTimeout(this.timerToken);
};
TSTimer.prototype.getSecondsFromTextBox = function (id) {
var greenLight = $(id).val();
return parseInt(greenLight.split(':')[0]) * 60 + parseInt(greenLight.split(':')[1]);
};
TSTimer.prototype.setDefault = function () {
this.activateSpeech('st-standard');
};
TSTimer.prototype.activateSpeech = function (speechId) {
$.each(this.speeches, function (indexInArray, valueOfElement) {
if (valueOfElement.id === speechId) {
$('#green-light').val(valueOfElement.greenTime);
$('#yellow-light').val(valueOfElement.yellowTime);
$('#red-light').val(valueOfElement.redTime);
}
});
$('.active-speech').removeClass('active-speech');
$('#' + speechId).addClass('active-speech');
};
return TSTimer;
})();
$(document).ready(function () {
var speeches = [];
speeches.push(new SpeechType("Public comment", "0:01", "2:00", "3:00", "st-table-topics"));
speeches.push(new SpeechType("Evaluation", "0:01", "2:30", "3:00", "st-evaluation"));
speeches.push(new SpeechType("Test", "0:00", "0:04", "0:06", "st-test"));
var timer = new TSTimer(speeches);
timer.setDefault();
});
<!-- Other Speech Types
speeches.push(new SpeechType("Icebreaker", "4:00", "5:00", "6:00", "st-icebreaker"));
speeches.push(new SpeechType("Standard", "5:00", "6:00", "7:00", "st-standard"));
speeches.push(new SpeechType("Advanced", "8:00", "9:00", "10:00", "st-advanced"));
-->