Mercurial > templog
annotate web/views/set.tpl @ 212:fbdd16b45b21
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 26 Jun 2014 23:03:32 +0800 |
parents | 4fa8cbf31065 |
children | f2838211f6ec |
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 } | |
126 else | |
127 { | |
128 param.oldvaluetext = param.oldvalue; | |
129 } | |
182 | 130 }); |
131 | |
184 | 132 self.edit = function(param, newvalue) { |
133 param.value = newvalue; | |
182 | 134 params[param.name] = param; |
135 self.trigger("edit", param); | |
136 } | |
137 | |
138 self.adjust = function(param, updown) { | |
139 // XXX increment | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
140 param.value += (param.amount*updown); |
182 | 141 self.trigger("edit", param); |
142 } | |
143 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
144 self.save = function() { |
185 | 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", | |
188 | 160 url: "set/update", |
185 | 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", | |
194 | 169 "Failed: " + data.status + ' ' |
170 + data.statusText + ' ' + data.responseText) | |
185 | 171 }); |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
172 } |
182 | 173 } |
174 | |
175 (function() { 'use strict'; | |
176 | |
177 var params = {{!inline_data}}; | |
185 | 178 var csrf_blob = "{{!csrf_blob}}"; |
189 | 179 var allowed = {{allowed}}; |
185 | 180 window.setter = new Setter(params, csrf_blob); |
182 | 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); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
186 |
182 | 187 setter.on("edit", function(param) |
188 { | |
189 var el = $("#" + param.id); | |
190 if (param.kind === "number") | |
191 { | |
184 | 192 set_text_state(el, param); |
182 | 193 } |
194 else if (param.kind === "yesno") | |
195 { | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
196 set_button_state(el, param.value); |
182 | 197 } |
184 | 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); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
212 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
213 |
185 | 214 setter.on("status", function(status) { |
215 $('#status').text(status) | |
216 }) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
217 |
185 | 218 var root; |
182 | 219 |
185 | 220 window.onload = function() { |
221 // clear list and add new ones | |
222 root = $("#paramlist"); | |
182 | 223 |
185 | 224 root.empty() && $.each(setter.params, function (idx, p) { |
225 add(p); | |
226 }) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
227 |
189 | 228 if (!allowed) { |
229 $("#savebutton").attr("disabled", true); | |
230 $('#status').text("No cert") | |
231 } | |
232 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
233 $("#savebutton").click(function() { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
234 setter.save(); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
235 }) |
185 | 236 } |
182 | 237 |
184 | 238 function set_text_state(el, param) |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
239 { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
240 var input = $(".input", el); |
184 | 241 var s = Number(param.value).toFixed(param.digits) |
242 input.text(s).val(s) | |
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 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
245 function set_button_state(el, value) |
182 | 246 { |
184 | 247 $(".button_yes", el).toggleClass("onbutton", value); |
248 $(".button_no", el).toggleClass("onbutton", !value); | |
182 | 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); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
257 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
258 input.keyup(function(e) { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
259 if (e.which == 13) |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
260 { |
184 | 261 setter.edit(param, Number(this.value)); |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
262 } |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
263 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
264 |
184 | 265 input.blur(function(e) { |
266 setter.edit(param, Number(this.value)); | |
267 }); | |
268 | |
191 | 269 $(".button_up", el).on("vmousedown", function(e) { |
270 e.preventDefault(); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
271 setter.adjust(param, 1); |
184 | 272 this.blur() |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
273 }); |
191 | 274 $(".button_down", el).on("vmousedown", function(e) { |
275 e.preventDefault(); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
276 setter.adjust(param, -1); |
184 | 277 this.blur() |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
278 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
279 |
184 | 280 set_text_state(el, param); |
182 | 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 | |
191 | 288 button_yes.on("vmousedown", function(e) { |
184 | 289 setter.edit(param, true); |
290 this.blur() | |
182 | 291 }) |
292 | |
191 | 293 button_no.on("vmousedown", function(e) { |
184 | 294 setter.edit(param, false); |
295 this.blur() | |
182 | 296 }) |
297 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
298 set_button_state(el, param.value); |
182 | 299 } |
300 } | |
301 | |
302 })() | |
303 | |
188 | 304 |
305 | |
182 | 306 </script> |
307 | |
185 | 308 <body> |
309 | |
310 <section id="paramlist"> | |
311 </section> | |
312 | |
189 | 313 <span id="savebox"> |
185 | 314 <input type="button" id="savebutton" value="Save"/> |
189 | 315 <span id="status"></span> |
316 </span> | |
185 | 317 |
318 | |
319 </body> | |
320 | |
182 | 321 </html> |