-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
224 lines (184 loc) · 6.73 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const stopwatchesHTML = document.querySelector(".stopwatches");
const addStopwatches = document.querySelector(".add-stopwatches");
const storedStopwatches = JSON.parse(localStorage.getItem("stopwatches"));
let stopwatches = storedStopwatches || {};
let saveInterval;
function increaseTime(index) {
var stopwatch = stopwatches[index];
stopwatch.seconds++;
if (stopwatch.seconds >= 60) {
stopwatch.minutes++;
stopwatch.seconds = 0;
if (stopwatch.minutes >= 60) {
stopwatch.minutes = 0;
stopwatch.hours++;
}
}
updateTimer(stopwatch, index);
}
function padTimeWithZero(time) {
return time ? (time > 9 ? time : "0" + time) : "00";
}
function getTime(stopwatch) {
return {
hours : padTimeWithZero(stopwatch.hours),
minutes : padTimeWithZero(stopwatch.minutes),
seconds : padTimeWithZero(stopwatch.seconds)
}
}
function updateTimer(stopwatch, index) {
const listItem = document.querySelector(`#stopwatch-${index}`);
const time = getTime(stopwatch);
listItem.textContent = time.hours + ":" + time.minutes + ":" + time.seconds;
}
function toggleStopwatch(e) {
const element = e.target;
if (!element.className.includes("time")) return;
const index = element.dataset.index;
const item = stopwatches[index];
if (item.active) {
clearInterval(item.timer);
element.classList.remove("mdl-button--colored");
element.classList.add("mdl-button--accent");
} else {
element.classList.remove("mdl-button--accent");
element.classList.add("mdl-button--colored");
item.timer = setInterval(increaseTime, 1000, index);
}
item.active = !item.active;
}
function createStopwatchListItem(stopwatch, index) {
let listItem = document.createElement("li");
let labelStopwatchName = createStopwatchName(stopwatch, index);
let buttonStopwatchTime = createStopwatchButton(stopwatch, index);
let buttonRemoveStopwatch = createRemoveButton(index);
let buttonResetStopwatch = createRefreshButton(index);
listItem.id = `item-${index}`;
listItem.setAttribute("class", "mdl-list__item");
listItem.appendChild(labelStopwatchName);
listItem.appendChild(buttonStopwatchTime);
listItem.appendChild(buttonResetStopwatch);
listItem.appendChild(buttonRemoveStopwatch);
return listItem;
}
function createStopwatchName(stopwatch, index) {
let labelStopwatchName = document.createElement("div");
labelStopwatchName.setAttribute("contentEditable", true);
labelStopwatchName.innerHTML = stopwatch.text;
labelStopwatchName.setAttribute("class", "stopwatch-name mdl-cell mdl-cell--7-col");
labelStopwatchName.addEventListener("blur", function(){
if (this.innerHTML !== '') {
stopwatches[index].text = this.innerHTML;
} else {
this.innerHTML = stopwatches[index].text;
}
});
return labelStopwatchName;
}
function createStopwatchButton(stopwatch, index) {
let buttonStopwatchTime = document.createElement("button");
buttonStopwatchTime.dataset.index = index;
buttonStopwatchTime.id = `stopwatch-${index}`;
buttonStopwatchTime.setAttribute("class", "time time-inactive mdl-button mdl-js-button mdl-button--raised mdl-button--accent");
const time = getTime(stopwatch);
buttonStopwatchTime.innerHTML = time.hours + ":" + time.minutes + ":" + time.seconds;
return buttonStopwatchTime;
}
function createIcon(iconName) {
let icon = document.createElement("i");
icon.setAttribute("class", "material-icons");
icon.innerHTML = iconName;
return icon;
}
function createRefreshButton(index) {
let buttonResetStopwatch = document.createElement("button");
let iconRefresh = createIcon("refresh");
buttonResetStopwatch.setAttribute("class", "stopwatch-option mdl-button mdl-js-button mdl-button--fab mdl-button--mini-fab");
buttonResetStopwatch.addEventListener("click", function(){
resetStopwatch(index)
this.blur();
});
buttonResetStopwatch.appendChild(iconRefresh);
return buttonResetStopwatch;
}
function createRemoveButton(index) {
let buttonRemoveStopwatch = document.createElement("button");
let iconRemove = createIcon("remove");
buttonRemoveStopwatch.setAttribute("class", "stopwatch-option mdl-button mdl-js-button mdl-button--fab mdl-button--mini-fab");
buttonRemoveStopwatch.addEventListener("click", function(){deleteStopwatch(index)});
buttonRemoveStopwatch.appendChild(iconRemove);
return buttonRemoveStopwatch;
}
function createStopwatch(text) {
return {
active : false,
hours : 0,
minutes : 0,
seconds : 0,
text : text,
timer : null
};
}
function addStopwatch(e){
e.preventDefault();
const taskInput = this.querySelector("[name=item]");
const text = taskInput.value;
const id = createId();
stopwatches[id] = createStopwatch(text);
const listItem = createStopwatchListItem(stopwatches[id], id);
stopwatchesHTML.appendChild(listItem);
this.reset();
taskInput.parentElement.classList.remove("is-focus", "is-dirty");
setAutoSave();
}
function setAutoSave() {
if (!saveInterval && Object.keys(stopwatches).length > 0) {
saveInterval = setInterval(saveData, 2000);
}
}
function deleteStopwatch(elementId) {
clearInterval(stopwatches[elementId].timer);
delete stopwatches[elementId];
const item = document.getElementById(`item-${elementId}`);
stopwatchesHTML.removeChild(item);
if (Object.keys(stopwatches).length == 0) {
localStorage.setItem("stopwatches", JSON.stringify(stopwatches));
clearInterval(saveInterval);
}
}
function createId() {
var currentdate = new Date();
var idBasedOnDateTime = currentdate.getDay() + "" +
currentdate.getMonth() + "" +
currentdate.getFullYear() + "" +
currentdate.getHours() + "" +
currentdate.getMinutes() + "" +
currentdate.getSeconds() + "" +
currentdate.getMilliseconds();
return idBasedOnDateTime;
}
function resetStopwatch(elementId) {
var stopwatch = stopwatches[elementId];
clearInterval(stopwatch.timer);
stopwatch.seconds = 0;
stopwatch.minutes = 0;
stopwatch.hours = 0;
let stopwatchButton = document.getElementById(`stopwatch-${elementId}`);
const time = getTime(stopwatch);
stopwatchButton.innerHTML = time.hours + ":" + time.minutes + ":" + time.seconds;
stopwatchButton.classList.remove("mdl-button--colored");
stopwatchButton.classList.add("mdl-button--accent");
}
function saveData() {
localStorage.setItem("stopwatches", JSON.stringify(stopwatches));
}
function populateList(){
setAutoSave();
for (var key in stopwatches) {
const listItem = createStopwatchListItem(stopwatches[key], key);
stopwatchesHTML.appendChild(listItem);
}
}
stopwatchesHTML.addEventListener("click", toggleStopwatch);
addStopwatches.addEventListener("submit", addStopwatch);
populateList();