-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbreathing.js
147 lines (125 loc) · 3.91 KB
/
breathing.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
var circle = document.getElementById("circle")
var text = document.getElementById("text")
var main = document.getElementById("main")
var mainView = document.getElementById("main-view")
var sidebarToggle = document.getElementById("sidebar-toggle")
var time = [4, 4, 4, 4]
var words = ["inhale", "hold", "exhale", "pause"]
var sidebarOpen = false
var textDisplay = true
function breathe() {
text.textContent = words[0]
circle.style.transition = `all ${time[0]}s ease-in-out`
circle.style.webkitTransition = `all ${time[0]}s ease-in-out`
circle.style.transform = "scale(1)"
window.setTimeout(function() {
if (time[1] > 0)
text.textContent = words[1]
window.setTimeout(function() {
text.textContent = words[2]
circle.style.transition = `all ${time[2]}s ease-in-out`
circle.style.webkitTransition = `all ${time[2]}s ease-in-out`
circle.style.transform = "scale(0.25)"
window.setTimeout(function() {
if (time[3] > 0)
text.textContent = words[3]
window.setTimeout(breathe, time[3] * 1000)
}, time[2] * 1000)
}, time[1] * 1000)
}, time[0] * 1000)
}
function toggleText(event) {
if (textDisplay) {
text.style.color = "transparent"
textDisplay = false
event.cancelBubble = true
document.onclick = toggleText
}
else {
text.style.color = "white"
textDisplay = true
document.onclick = null
}
}
function openSidebar() {
main.style.transform = "translateX(-15em)"
sidebarOpen = true
sidebarToggle.className = "open"
}
function closeSidebar() {
window.scrollTo(0, 0)
main.style.transform = "translateX(0)"
sidebarOpen = false
sidebarToggle.className = ""
}
function toggleSidebar() {
(sidebarOpen) ? closeSidebar(): openSidebar()
}
function handleKeypress(e) {
var key = e.keyCode || e.which
if (key == 13) {
if (["time", "word", "save"].indexOf(e.target.name) > -1)
saveSettings()
else if (e.target.name == "reset")
resetSettings()
else if (e.target.id == "noSleepToggle")
toggleNoSleep()
else
toggleSidebar()
sidebarToggle.focus()
}
}
function saveSettings() {
let settings = []
let textNodes = document.querySelectorAll("input[type='text']")
let numberNodes = document.querySelectorAll("input[type='number']")
for (let i = 0; i < 4; i++) {
if (numberNodes[i].value <= 0 && i % 2 == 1) {
numberNodes[i].value = 0
textNodes[i].value = ""
}
if (numberNodes[i].value < 2 && i % 2 == 0)
numberNodes[i].value = 2
if (numberNodes[i].value > 10)
numberNodes[i].value = 10
}
for (let i = 0; i < 4; i++) {
words[i] = textNodes[i].value
time[i] = numberNodes[i].value
settings[i] = [words[i], time[i]]
}
window.localStorage.setItem("settings", JSON.stringify(settings))
toggleSidebar()
}
function resetSettings() {
time = [4, 4, 4, 4]
words = ["inhale", "hold", "exhale", "pause"]
let settings = []
let textNodes = document.querySelectorAll("input[type='text']")
let numberNodes = document.querySelectorAll("input[type='number']")
for (let i = 0; i < 4; i++) {
textNodes[i].value = words[i]
numberNodes[i].value = time[i]
settings[i] = [words[i], time[i]]
}
window.localStorage.setItem("settings", JSON.stringify(settings))
toggleSidebar()
}
function loadSettings() {
let settings = window.localStorage.getItem("settings")
if (settings !== null) {
settings = JSON.parse(settings)
let textNodes = document.querySelectorAll("input[type='text']")
let numberNodes = document.querySelectorAll("input[type='number']")
for (let i = 0; i < 4; i++) {
textNodes[i].value = words[i] = settings[i][0]
numberNodes[i].value = time[i] = settings[i][1]
}
}
}
document.onkeypress = handleKeypress
text.onclick = toggleText
sidebarToggle.onclick = toggleSidebar
mainView.ontouchmove = function(e) { e.preventDefault() } //prevent mobile scroll
loadSettings()
window.setTimeout(breathe, 4000)