-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
184 lines (151 loc) · 5.68 KB
/
scripts.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Created by not-much-io on 3.10.15.
*/
/*This function is run when the page is loaded.
* Variables are being set here, that will house the state of the app.
* */
(function() {
//Constants
//These constant are just used to refer to the pomodoro state.
window.Constants = {
STOPPED : "The pomodoro is stopped",
RUNNING : "The pomodoro is running",
INTERVAL_WORK: "The pomodoro is in work mode",
INTERVAL_REST: "The pomodoro is in rest mode"
};
//State
//What state is the pomodoro currently in.
window.pomState = window.Constants.STOPPED;
//Which interval is the pomodoro currently in.
window.pomInterval = undefined;
//Next pomodoro interval
window.pomNextInterval = undefined;
//The time currently being counted down from, -1 if pomodoro stopped
window.currPomTime = -1;
//DOM and atate sycing
//This ID will be used to reference the interval running the timer
window.intervalID = undefined;
//Let syncing the state and DOM happen in the background. NOTE: 8-16ms is not detectable by the human eye
window.setInterval(syncStateAndDom, 10);
//Settings
//Seconds that the Work phase of the pomodoro lasts
window.pomWorkTime = 25*60;
//Seconds that the Rest phase of the pomodoro lasts.
window.pomRestTime = 5*60;
})();
/*Function to handle setting settings from interface*/
function setSetting(setting, val) {
if (setting == "workT") {
window.pomWorkTime = val*60;
} else if (setting == "restT") {
window.pomRestTime = val*60;
} else {
console.log("Undefined setting");
}
}
/*Raises notification, currently only sound. Browser notification could be added.*/
function notify() {
var audio = new Audio("resources/notif.mp3");
audio.play();
}
// This function is called inside an interval call and will repeatedly run and sync the state values and the DOM.
function syncStateAndDom() {
var workTimeLbl = document.getElementById("workTimeLbl");
var restTimeLbl = document.getElementById("restTimeLbl");
var display = document.getElementById("display");
workTimeLbl.textContent = "Work Time (" + window.pomWorkTime/60 + " min)";
restTimeLbl.textContent = "Rest Time (" + window.pomRestTime/60 + " min)";
display.textContent = pomTimeToLabel(window.currPomTime);
}
// This function will handle converting the current pomodoro running time to the label displayed in the HTML.
function pomTimeToLabel(pomRunTime) {
if (pomRunTime == -1) {
return "00:00:00" + " ina.";
}
//Pad zeros
function pad(x) {
return x < 10 ? "0" + x : x;
}
var hours, minutes, seconds;
hours = minutes = 0;
seconds = pomRunTime;
while (seconds >= 60) {
seconds -= 60;
minutes++;
}
while (minutes >= 60) {
minutes -= 60;
hours++;
}
return pad(hours.toString()) + ":" +
pad(minutes.toString()) + ":" +
pad(seconds.toString()) +
(window.pomInterval == window.Constants.INTERVAL_WORK ? " wrk." : " rst.");
}
// This function will handle everything associated with switching state.
function toggleState() {
//Select the DOM element (HTML tag) with the id display.
var actionButtonIcon = document.getElementById("actionButtonIcon");
//Change state variables
if (window.pomState == window.Constants.STOPPED) {
window.pomState = window.Constants.RUNNING;
window.currPomTime = window.pomWorkTime;
actionButtonIcon.textContent = "stop";
} else if (window.pomState == window.Constants.RUNNING) {
window.pomState = window.Constants.STOPPED;
window.currPomTime = -1;
actionButtonIcon.textContent = "play_arrow"
} else {
console.log("Undefined pomState");
}
}
// This function will take the pomodoro running time and check if it is time for a state switch.
function isOver(pomodoroRunningTime) {
return pomodoroRunningTime <= 0;
}
//The timer mechanism itself will just be calling this function every 1000ms. This function will handle updating the timer values.
function updateTimer() {
if (!isOver(window.currPomTime)) {
window.currPomTime--;
} else {
notify();
var tmpNextInterval = window.pomNextInterval;
window.pomNextInterval = window.pomInterval;
window.pomInterval = tmpNextInterval;
if (window.pomInterval == window.Constants.INTERVAL_WORK) {
window.currPomTime = window.pomWorkTime;
} else if (window.pomInterval == window.Constants.INTERVAL_REST) {
window.currPomTime = window.pomRestTime;
} else {
console.log("Undefined interval");
}
}
}
//This function will handle everything associated with starting the pomodoro.
function startPomodoro() {
//Set up initial Interval states
window.pomInterval = window.Constants.INTERVAL_WORK;
window.pomNextInterval = window.Constants.INTERVAL_REST;
//Switch state
toggleState();
//Activate countdown. Every 1000ms, update timer will be called. Also the interval ID is returned.
window.intervalID = window.setInterval(updateTimer, 1000)
}
//This function will handle everything associated with stopping the pomodoro.
function stopPomodoro() {
//Switch state
toggleState();
//Deactivate countdown
clearInterval(window.intervalID);
}
//This function will handle what happens if the action button is clicked
function onActionButtonClicked() {
var state = window.pomState;
if (state == window.Constants.STOPPED) {
startPomodoro();
} else if (state == window.Constants.RUNNING) {
stopPomodoro();
} else {
console.log("Undefined behaviour in onActionButtonClicked");
}
}