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