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