Mercurial > templog
annotate web/views/set.tpl @ 191:8318d50d766d
gets current params
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 11 Feb 2014 22:10:32 +0800 |
parents | 101c66da848d |
children | 4fa8cbf31065 |
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 | |
79 </style> | |
80 <title>Set templog</title> | |
81 </head> | |
82 | |
83 | |
84 <script type="html/num_input"> | |
85 <div id="{id}"> | |
184 | 86 <span class="existing">{title} <span id="oldvalue">{oldvaluetext}{unit}</span></span> |
182 | 87 <br/> |
188 | 88 <input type="number" class="input" name="input_{name}" /> |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
89 <input type="button" class="button_down" value="-"/> |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
90 <input type="button" class="button_up" value="+"/> |
182 | 91 </div> |
92 </script> | |
93 | |
94 <script type="html/yesno_button"> | |
95 <div id="{id}"> | |
184 | 96 <span class="existing">{title} <span id="oldvalue">{oldvaluetext}</span></span> |
182 | 97 <br/> |
189 | 98 <input type="button" class="button_no yesno" value="No"/> |
99 <input type="button" class="button_yes yesno" value="Yes"/> | |
182 | 100 </div> |
101 </script> | |
102 | |
103 <script> | |
104 | |
185 | 105 function Setter(params, csrf_blob) { |
182 | 106 var self = $.observable(this); |
107 | |
108 self.params = params; | |
185 | 109 self.csrf_blob = csrf_blob |
182 | 110 |
111 $.each(self.params, function(idx, param) { | |
112 param.id = "param_id_" + idx; | |
184 | 113 param.oldvalue = param.value |
114 if (typeof(param.oldvalue) == "boolean") | |
115 { | |
116 param.oldvaluetext = param.oldvalue ? "Yes" : "No"; | |
117 } | |
118 else | |
119 { | |
120 param.oldvaluetext = param.oldvalue; | |
121 } | |
182 | 122 }); |
123 | |
184 | 124 self.edit = function(param, newvalue) { |
125 param.value = newvalue; | |
182 | 126 params[param.name] = param; |
127 self.trigger("edit", param); | |
128 } | |
129 | |
130 self.adjust = function(param, updown) { | |
131 // XXX increment | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
132 param.value += (param.amount*updown); |
182 | 133 self.trigger("edit", param); |
134 } | |
135 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
136 self.save = function() { |
185 | 137 self.trigger("status", "Saving...") |
138 | |
139 var post_json = {}; | |
140 post_json.csrf_blob = self.csrf_blob; | |
141 post_json.params = | |
142 self.params.map(function(v, idx, array) { | |
143 return { | |
144 name: v.name, | |
145 value: v.value | |
146 }; | |
147 }); | |
148 | |
149 var post_data = {data: JSON.stringify(post_json)}; | |
150 | |
151 var req = $.ajax({type: "POST", | |
188 | 152 url: "set/update", |
185 | 153 data: post_data}); |
154 | |
155 req.done(function(data, status, hdr) { | |
156 self.trigger("status", "Saved") | |
157 }); | |
158 | |
159 req.fail(function(data, status, hdr) { | |
160 self.trigger("status", | |
161 "Failed: " + status + "\n" + hdr.responseText) | |
162 }); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
163 } |
182 | 164 } |
165 | |
166 (function() { 'use strict'; | |
167 | |
168 var params = {{!inline_data}}; | |
185 | 169 var csrf_blob = "{{!csrf_blob}}"; |
189 | 170 var allowed = {{allowed}}; |
185 | 171 window.setter = new Setter(params, csrf_blob); |
182 | 172 |
173 var number_template = $("[type='html/num_input']").html(); | |
174 var button_template = $("[type='html/yesno_button']").html(); | |
175 | |
176 setter.on("add", add); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
177 |
182 | 178 setter.on("edit", function(param) |
179 { | |
180 var el = $("#" + param.id); | |
181 if (param.kind === "number") | |
182 { | |
184 | 183 set_text_state(el, param); |
182 | 184 } |
185 else if (param.kind === "yesno") | |
186 { | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
187 set_button_state(el, param.value); |
182 | 188 } |
184 | 189 var same; |
190 switch (typeof(param.oldvalue)) | |
191 { | |
192 case "boolean": | |
193 same = ((!param.value) == (!param.oldvalue)); | |
194 break; | |
195 case "number": | |
196 same = Math.abs(param.value - param.oldvalue) < 1e-3 * param.amount; | |
197 break; | |
198 default: | |
199 same = (param.value === param.oldvalue); | |
200 } | |
201 | |
202 $("#oldvalue", el).toggleClass("modified", !same); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
203 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
204 |
185 | 205 setter.on("status", function(status) { |
206 $('#status').text(status) | |
207 }) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
208 |
185 | 209 var root; |
182 | 210 |
185 | 211 window.onload = function() { |
212 // clear list and add new ones | |
213 root = $("#paramlist"); | |
182 | 214 |
185 | 215 root.empty() && $.each(setter.params, function (idx, p) { |
216 add(p); | |
217 }) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
218 |
189 | 219 if (!allowed) { |
220 $("#savebutton").attr("disabled", true); | |
221 $('#status').text("No cert") | |
222 } | |
223 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
224 $("#savebutton").click(function() { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
225 setter.save(); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
226 }) |
185 | 227 } |
182 | 228 |
184 | 229 function set_text_state(el, param) |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
230 { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
231 var input = $(".input", el); |
184 | 232 var s = Number(param.value).toFixed(param.digits) |
233 input.text(s).val(s) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
234 } |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
235 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
236 function set_button_state(el, value) |
182 | 237 { |
184 | 238 $(".button_yes", el).toggleClass("onbutton", value); |
239 $(".button_no", el).toggleClass("onbutton", !value); | |
182 | 240 } |
241 | |
242 function add(param) | |
243 { | |
244 if (param.kind === "number") | |
245 { | |
246 var el = $($.render(number_template, param)).appendTo(root); | |
247 var input = $(".input", el); | |
183
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 input.keyup(function(e) { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
250 if (e.which == 13) |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
251 { |
184 | 252 setter.edit(param, Number(this.value)); |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
253 } |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
254 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
255 |
184 | 256 input.blur(function(e) { |
257 setter.edit(param, Number(this.value)); | |
258 }); | |
259 | |
191 | 260 $(".button_up", el).on("vmousedown", function(e) { |
261 e.preventDefault(); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
262 setter.adjust(param, 1); |
184 | 263 this.blur() |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
264 }); |
191 | 265 $(".button_down", el).on("vmousedown", function(e) { |
266 e.preventDefault(); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
267 setter.adjust(param, -1); |
184 | 268 this.blur() |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
269 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
270 |
184 | 271 set_text_state(el, param); |
182 | 272 } |
273 else if (param.kind === "yesno") | |
274 { | |
275 var el = $($.render(button_template, param)).appendTo(root); | |
276 var button_yes = $(".button_yes", el); | |
277 var button_no = $(".button_no", el); | |
278 | |
191 | 279 button_yes.on("vmousedown", function(e) { |
184 | 280 setter.edit(param, true); |
281 this.blur() | |
182 | 282 }) |
283 | |
191 | 284 button_no.on("vmousedown", function(e) { |
184 | 285 setter.edit(param, false); |
286 this.blur() | |
182 | 287 }) |
288 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
289 set_button_state(el, param.value); |
182 | 290 } |
291 } | |
292 | |
293 })() | |
294 | |
188 | 295 |
296 | |
182 | 297 </script> |
298 | |
185 | 299 <body> |
300 | |
301 <section id="paramlist"> | |
302 </section> | |
303 | |
189 | 304 <span id="savebox"> |
185 | 305 <input type="button" id="savebutton" value="Save"/> |
189 | 306 <span id="status"></span> |
307 </span> | |
185 | 308 |
309 | |
310 </body> | |
311 | |
182 | 312 </html> |