git.haldean.org sousvide / 39d7c0c
Move timer JS into its own file Will Haldean Brown 8 years ago
3 changed file(s) with 72 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
77 <script src="jquery.min.js"></script>
88 <script src="d3.v3.min.js" charset="utf-8"></script>
99 <script src="themes.js"></script>
10 <script src="chart.js"></script>
11 <script src="timers.js"></script>
1012 <script src="sousvide.js"></script>
11 <script src="chart.js"></script>
1213 </head>
1314 <body class="bg-primary">
1415 <h1 class="fg-secondary">SV2.1.0</h1>
22 var targetDisplayElem, targetChangeElem, targetInputElem
33 var pInputElem, iInputElem, dInputElem
44 var enabledElem, maxErrElem
5 var timerElem, timerAudio
65
76 var enableButton, disableButton, lastEnabled = undefined
87
8786 dInputElem.setAttribute('value', data.Pid.D)
8887 }
8988
90 function getTimerData() {
91 $.ajax({
92 url: '/timers',
93 type: 'json',
94 success: function(resp) {
95 displayTimers(resp)
96 }
97 })
98 setTimeout(getTimerData, 1000)
99 }
100
101 function durationFormat(nano) {
102 var neg = nano < 0
103 if (neg) nano *= -1
104 var sec = Math.floor(nano / 1e9)
105 var min = Math.floor(sec / 60)
106 sec -= min * 60
107 var hr = Math.floor(min / 60)
108 min -= hr * 60
109 if (min < 10) min = '0' + min
110 if (sec < 10) sec = '0' + sec
111 return (neg ? '-' : '') + hr + 'h' + min + 'm' + sec + 's'
112 }
113
114 function makeTimer(timer) {
115 tr = document.createElement('tr')
116
117 td = document.createElement('td')
118 td.innerHTML = timer.Name + ' (' + durationFormat(timer.SetTime) + ')'
119
120 form = document.createElement('form')
121 form.setAttribute('method', 'POST')
122 form.setAttribute('action', 'delete_timer')
123 del = document.createElement('input')
124 del.setAttribute('type', 'submit')
125 del.setAttribute('value', 'dismiss')
126 form.appendChild(del)
127 hid = document.createElement('input')
128 hid.setAttribute('type', 'hidden')
129 hid.setAttribute('name', 'id')
130 hid.setAttribute('value', timer.Id)
131 form.appendChild(hid)
132 td.appendChild(form)
133
134 tr.appendChild(td)
135
136 td = document.createElement('td')
137 td.innerHTML = durationFormat(timer.TimeRemaining)
138 $(td).addClass('val')
139 if (timer.Expired) {
140 $(td).addClass('expired')
141 }
142 tr.appendChild(td)
143
144 return tr
145 }
146
147 function displayTimers(data) {
148 timerElem.innerHTML = ''
149 for (var i = 0; i < data.length; i++) {
150 timer = data[i]
151 console.log(timer)
152 if (timer.Expired) {
153 timerAudio.play()
154 }
155 timerElem.appendChild(makeTimer(timer));
156 }
157 }
158
15989 function attachRequest(elem, path, blinkUntil) {
16090 $(elem).click(function(e) {
16191 e.preventDefault();
0 var timerElem, timerAudio
1
2 function getTimerData() {
3 $.ajax({
4 url: '/timers',
5 type: 'json',
6 success: function(resp) {
7 displayTimers(resp)
8 }
9 })
10 setTimeout(getTimerData, 1000)
11 }
12
13 function durationFormat(nano) {
14 var neg = nano < 0
15 if (neg) nano *= -1
16 var sec = Math.floor(nano / 1e9)
17 var min = Math.floor(sec / 60)
18 sec -= min * 60
19 var hr = Math.floor(min / 60)
20 min -= hr * 60
21 if (min < 10) min = '0' + min
22 if (sec < 10) sec = '0' + sec
23 return (neg ? '-' : '') + hr + 'h' + min + 'm' + sec + 's'
24 }
25
26 function makeTimer(timer) {
27 tr = document.createElement('tr')
28
29 td = document.createElement('td')
30 td.innerHTML = timer.Name + ' (' + durationFormat(timer.SetTime) + ')'
31
32 form = document.createElement('form')
33 form.setAttribute('method', 'POST')
34 form.setAttribute('action', 'delete_timer')
35 del = document.createElement('input')
36 del.setAttribute('type', 'submit')
37 del.setAttribute('value', 'dismiss')
38 form.appendChild(del)
39 hid = document.createElement('input')
40 hid.setAttribute('type', 'hidden')
41 hid.setAttribute('name', 'id')
42 hid.setAttribute('value', timer.Id)
43 form.appendChild(hid)
44 td.appendChild(form)
45
46 tr.appendChild(td)
47
48 td = document.createElement('td')
49 td.innerHTML = durationFormat(timer.TimeRemaining)
50 $(td).addClass('val')
51 if (timer.Expired) {
52 $(td).addClass('expired')
53 }
54 tr.appendChild(td)
55
56 return tr
57 }
58
59 function displayTimers(data) {
60 timerElem.innerHTML = ''
61 for (var i = 0; i < data.length; i++) {
62 timer = data[i]
63 console.log(timer)
64 if (timer.Expired) {
65 timerAudio.play()
66 }
67 timerElem.appendChild(makeTimer(timer));
68 }
69 }