Mercurial > templog
annotate web/views/set.tpl @ 232:a01b7bccccd3
improve exception handling
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 11 Apr 2015 21:09:13 +0800 |
parents | f2838211f6ec |
children | 61269311ed3d |
rev | line source |
---|---|
182 | 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | |
3 <head> | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
189 | 5 <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,minimum-scale=1"> |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
6 <script src="jquery-2.1.0.min.js"></script> |
188 | 7 <script src="jquery.mobile.custom.min.js"></script> |
182 | 8 <script> |
9 %include riot.min.js | |
10 </script> | |
11 | |
12 <style type="text/css"> | |
13 span.no_selection { | |
14 -webkit-user-select: none; // webkit (safari, chrome) browsers | |
15 -moz-user-select: none; // mozilla browsers | |
16 -khtml-user-select: none; // webkit (konqueror) browsers | |
17 } | |
18 | |
19 body { | |
189 | 20 font-family: sans-serif; |
182 | 21 } |
22 | |
23 input { | |
24 border: 2px solid transparent; | |
25 border-radius: 4px; | |
26 background-color: white; | |
27 border-color: black; | |
28 padding: 0; | |
189 | 29 font-size: 30pt; |
30 height: 34pt; | |
182 | 31 } |
32 | |
33 input[type="button"] { | |
189 | 34 width: 34pt; |
35 margin-left: 4pt; | |
36 -webkit-appearance: none; | |
37 -moz-appearance: none; | |
38 background:#fff; | |
39 vertical-align: center; | |
182 | 40 } |
41 | |
42 input[type="submit"] { | |
43 margin-top: 1em; | |
44 align: center; | |
189 | 45 width: 10em; |
182 | 46 } |
47 | |
188 | 48 input[type="text"], input[type="number"] { |
182 | 49 text-align: center; |
189 | 50 width: 4em; |
182 | 51 } |
52 | |
185 | 53 #savebox { |
189 | 54 vertical-align: center; |
185 | 55 width: 100%; |
56 } | |
57 | |
189 | 58 input[type="button"].onbutton { |
59 background: #ccc; | |
60 } | |
61 | |
62 input[type="button"].yesno { | |
63 width: 2.5em; | |
64 } | |
65 | |
66 input[type="button"]#savebutton { | |
67 width: 5em; | |
182 | 68 } |
69 | |
184 | 70 .modified { |
71 color: #d00; | |
72 font-weight: bold; | |
73 } | |
74 | |
182 | 75 .existing { |
189 | 76 margin-top: 10pt; |
182 | 77 } |
78 | |
194 | 79 span.inputrow { |
80 //vertical-align: center; | |
81 } | |
82 | |
182 | 83 </style> |
84 <title>Set templog</title> | |
85 </head> | |
86 | |
87 | |
88 <script type="html/num_input"> | |
89 <div id="{id}"> | |
184 | 90 <span class="existing">{title} <span id="oldvalue">{oldvaluetext}{unit}</span></span> |
182 | 91 <br/> |
194 | 92 <span class="inputrow"> |
188 | 93 <input type="number" class="input" name="input_{name}" /> |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
94 <input type="button" class="button_down" value="-"/> |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
95 <input type="button" class="button_up" value="+"/> |
194 | 96 </span> |
182 | 97 </div> |
98 </script> | |
99 | |
100 <script type="html/yesno_button"> | |
101 <div id="{id}"> | |
184 | 102 <span class="existing">{title} <span id="oldvalue">{oldvaluetext}</span></span> |
182 | 103 <br/> |
194 | 104 <span class="inputrow"> |
189 | 105 <input type="button" class="button_no yesno" value="No"/> |
106 <input type="button" class="button_yes yesno" value="Yes"/> | |
194 | 107 </span> |
182 | 108 </div> |
109 </script> | |
110 | |
111 <script> | |
112 | |
185 | 113 function Setter(params, csrf_blob) { |
182 | 114 var self = $.observable(this); |
115 | |
116 self.params = params; | |
185 | 117 self.csrf_blob = csrf_blob |
182 | 118 |
119 $.each(self.params, function(idx, param) { | |
120 param.id = "param_id_" + idx; | |
184 | 121 param.oldvalue = param.value |
122 if (typeof(param.oldvalue) == "boolean") | |
123 { | |
124 param.oldvaluetext = param.oldvalue ? "Yes" : "No"; | |
125 } | |
225 | 126 else if (param.kind == "number") |
127 { | |
128 param.oldvaluetext = Number(param.oldvalue).toFixed(param.digits) | |
129 } | |
184 | 130 else |
131 { | |
132 param.oldvaluetext = param.oldvalue; | |
133 } | |
182 | 134 }); |
135 | |
184 | 136 self.edit = function(param, newvalue) { |
137 param.value = newvalue; | |
182 | 138 params[param.name] = param; |
139 self.trigger("edit", param); | |
140 } | |
141 | |
142 self.adjust = function(param, updown) { | |
143 // XXX increment | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
144 param.value += (param.amount*updown); |
182 | 145 self.trigger("edit", param); |
146 } | |
147 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
148 self.save = function() { |
185 | 149 self.trigger("status", "Saving...") |
150 | |
151 var post_json = {}; | |
152 post_json.csrf_blob = self.csrf_blob; | |
153 post_json.params = | |
154 self.params.map(function(v, idx, array) { | |
155 return { | |
156 name: v.name, | |
157 value: v.value | |
158 }; | |
159 }); | |
160 | |
161 var post_data = {data: JSON.stringify(post_json)}; | |
162 | |
163 var req = $.ajax({type: "POST", | |
188 | 164 url: "set/update", |
185 | 165 data: post_data}); |
166 | |
167 req.done(function(data, status, hdr) { | |
168 self.trigger("status", "Saved") | |
169 }); | |
170 | |
171 req.fail(function(data, status, hdr) { | |
172 self.trigger("status", | |
194 | 173 "Failed: " + data.status + ' ' |
174 + data.statusText + ' ' + data.responseText) | |
185 | 175 }); |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
176 } |
182 | 177 } |
178 | |
179 (function() { 'use strict'; | |
180 | |
181 var params = {{!inline_data}}; | |
185 | 182 var csrf_blob = "{{!csrf_blob}}"; |
189 | 183 var allowed = {{allowed}}; |
185 | 184 window.setter = new Setter(params, csrf_blob); |
182 | 185 |
186 var number_template = $("[type='html/num_input']").html(); | |
187 var button_template = $("[type='html/yesno_button']").html(); | |
188 | |
189 setter.on("add", add); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
190 |
182 | 191 setter.on("edit", function(param) |
192 { | |
193 var el = $("#" + param.id); | |
194 if (param.kind === "number") | |
195 { | |
184 | 196 set_text_state(el, param); |
182 | 197 } |
198 else if (param.kind === "yesno") | |
199 { | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
200 set_button_state(el, param.value); |
182 | 201 } |
184 | 202 var same; |
203 switch (typeof(param.oldvalue)) | |
204 { | |
205 case "boolean": | |
206 same = ((!param.value) == (!param.oldvalue)); | |
207 break; | |
208 case "number": | |
209 same = Math.abs(param.value - param.oldvalue) < 1e-3 * param.amount; | |
210 break; | |
211 default: | |
212 same = (param.value === param.oldvalue); | |
213 } | |
214 | |
215 $("#oldvalue", el).toggleClass("modified", !same); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
216 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
217 |
185 | 218 setter.on("status", function(status) { |
219 $('#status').text(status) | |
220 }) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
221 |
185 | 222 var root; |
182 | 223 |
185 | 224 window.onload = function() { |
225 // clear list and add new ones | |
226 root = $("#paramlist"); | |
182 | 227 |
185 | 228 root.empty() && $.each(setter.params, function (idx, p) { |
229 add(p); | |
230 }) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
231 |
189 | 232 if (!allowed) { |
233 $("#savebutton").attr("disabled", true); | |
234 $('#status').text("No cert") | |
235 } | |
236 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
237 $("#savebutton").click(function() { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
238 setter.save(); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
239 }) |
185 | 240 } |
182 | 241 |
184 | 242 function set_text_state(el, param) |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
243 { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
244 var input = $(".input", el); |
184 | 245 var s = Number(param.value).toFixed(param.digits) |
246 input.text(s).val(s) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
247 } |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
248 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
249 function set_button_state(el, value) |
182 | 250 { |
184 | 251 $(".button_yes", el).toggleClass("onbutton", value); |
252 $(".button_no", el).toggleClass("onbutton", !value); | |
182 | 253 } |
254 | |
255 function add(param) | |
256 { | |
257 if (param.kind === "number") | |
258 { | |
259 var el = $($.render(number_template, param)).appendTo(root); | |
260 var input = $(".input", el); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
261 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
262 input.keyup(function(e) { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
263 if (e.which == 13) |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
264 { |
184 | 265 setter.edit(param, Number(this.value)); |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
266 } |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
267 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
268 |
184 | 269 input.blur(function(e) { |
270 setter.edit(param, Number(this.value)); | |
271 }); | |
272 | |
191 | 273 $(".button_up", el).on("vmousedown", function(e) { |
274 e.preventDefault(); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
275 setter.adjust(param, 1); |
184 | 276 this.blur() |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
277 }); |
191 | 278 $(".button_down", el).on("vmousedown", function(e) { |
279 e.preventDefault(); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
280 setter.adjust(param, -1); |
184 | 281 this.blur() |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
282 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
283 |
184 | 284 set_text_state(el, param); |
182 | 285 } |
286 else if (param.kind === "yesno") | |
287 { | |
288 var el = $($.render(button_template, param)).appendTo(root); | |
289 var button_yes = $(".button_yes", el); | |
290 var button_no = $(".button_no", el); | |
291 | |
191 | 292 button_yes.on("vmousedown", function(e) { |
184 | 293 setter.edit(param, true); |
294 this.blur() | |
182 | 295 }) |
296 | |
191 | 297 button_no.on("vmousedown", function(e) { |
184 | 298 setter.edit(param, false); |
299 this.blur() | |
182 | 300 }) |
301 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
302 set_button_state(el, param.value); |
182 | 303 } |
304 } | |
305 | |
306 })() | |
307 | |
188 | 308 |
309 | |
182 | 310 </script> |
311 | |
185 | 312 <body> |
313 | |
314 <section id="paramlist"> | |
315 </section> | |
316 | |
189 | 317 <span id="savebox"> |
185 | 318 <input type="button" id="savebutton" value="Save"/> |
189 | 319 <span id="status"></span> |
320 </span> | |
185 | 321 |
322 | |
323 </body> | |
324 | |
182 | 325 </html> |