-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
172 lines (139 loc) · 3.42 KB
/
popup.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
var refreshDisplayTimeout;
var bgpage = chrome.extension.getBackgroundPage();
var previousValues = [3, 5, 10, 30];
var editing = false;
var running = false;
document.addEventListener('DOMContentLoaded', function () {
load();
});
function show(section)
{
document.getElementById(section).style.display = "block";
}
function showInline(section)
{
document.getElementById(section).style.display = "inline";
}
function hide(section)
{
document.getElementById(section).style.display = "none";
}
function load()
{
chrome.extension.getBackgroundPage().console.log('***** run');
show("display");
editing = false;
if(!bgpage.alarmDate)
{
for(var i = 0; i < document.choices.radio.length; i++)
if(localStorage[i] != null)
document.getElementById("s"+i).textContent = localStorage[i];
}
else
{
show("display");
refreshDisplay();
}
if (!bgpage.alarmDate)
setTimer();
}
function swap()
{
editing = true;
// swap text with fields
for(var i = 0; i < document.choices.radio.length; i++)
{
var span = document.getElementById("s"+i);
var num = parseInt(span.textContent);
previousValues[i] = num;
var html = "<input class='input-mini' type='text' name='custom' id='c"+i;
html += "' value='"+num;
html += "'>";
// used to select on click and auto save on change
span.innerHTML = html;
}
// swap edit button with done button
var butt = document.getElementById("swapper");
butt.innerHTML = "<a href='#' id='done' class='btn'><i class='icon-ok'></i></a>";
document.querySelector('#done').addEventListener('click', swapBack);
}
function swapBack()
{
// swap fields with text
for(var i = 0; i < document.choices.radio.length; i++)
{
var span = document.getElementById("s"+i);
var num = parseInt(document.getElementById("c"+i).value);
if(isValid(num))
{
localStorage[i] = num;
span.textContent = num;
}
else
span.textContent = previousValues[i];
}
// swap done button with edit button
var butt = document.getElementById("swapper");
butt.innerHTML = "<a href='#' id='wrench' class='btn'><i class='icon-wrench'></i></a>";
document.querySelector('#wrench').addEventListener('click', swap);
editing = false;
}
function setTimer()
{
running = true;
// make sure we're dealing with text not fields
if(editing)
swapBack();
// SET background timer for selected number
// HIDE settings, DISPLAY countdown
var num = 0.5;
// set timer, hide settings, display reset button
if(isValid(num))
{
bgpage.setAlarm(num * 60000);
show("display");
refreshDisplay();
}
else
bgpage.error();
}
// Returns true if 0 <= amt <= 240
function isValid(amt)
{
if(isNaN(amt) || (amt == null))
return false;
else if((amt < 0) || (amt > 240))
return false;
else
return true;
}
function refreshDisplay()
{
percent = bgpage.getTimeLeftPercent();
if(percent < 15)
document.getElementById("bar").style.color = "grey";
document.getElementById("bar").textContent = bgpage.getTimeLeftString();
document.getElementById("bar").style.width = percent + "%";
refreshDisplayTimeout = setTimeout(refreshDisplay, 100);
}
function pauseTimer()
{
bgpage.pause();
clearTimeout(refreshDisplayTimeout);
}
function resumeTimer()
{
refreshDisplay();
bgpage.resume();
}
function restartTimer()
{
refreshDisplay();
bgpage.restart();
}
function reset()
{
clearTimeout(refreshDisplayTimeout);
bgpage.turnOff();
show("display");
}