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