Mercurial > templog
annotate web/views/set.tpl @ 184:cbe14244a372
a few fixes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 06 Feb 2014 20:35:43 +0800 |
parents | 177f616893e6 |
children | adbf70d1449f |
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"> |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
5 <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=8,minimum-scale=0.1"> |
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> |
182 | 7 <script> |
8 %include riot.min.js | |
9 </script> | |
10 | |
11 <style type="text/css"> | |
12 span.no_selection { | |
13 -webkit-user-select: none; // webkit (safari, chrome) browsers | |
14 -moz-user-select: none; // mozilla browsers | |
15 -khtml-user-select: none; // webkit (konqueror) browsers | |
16 } | |
17 | |
18 body { | |
19 font-family: "sans-serif"; | |
20 } | |
21 | |
22 input { | |
23 border: 2px solid transparent; | |
24 border-radius: 4px; | |
25 background-color: white; | |
26 border-color: black; | |
27 padding: 0; | |
28 font-size: 80%; | |
29 } | |
30 | |
31 input[type="button"] { | |
32 width: 4em; | |
33 height: 4em; | |
34 margin-left: 0.5em; | |
35 } | |
36 | |
37 input[type="submit"] { | |
38 width: 10em; | |
39 height: 4em; | |
40 margin-top: 1em; | |
41 align: center; | |
42 } | |
43 | |
44 input[type="text"] { | |
45 height: 4em; | |
46 text-align: center; | |
47 } | |
48 | |
49 .onbutton { | |
50 background-color: #cdf; | |
51 } | |
52 | |
184 | 53 .modified { |
54 color: #d00; | |
55 font-weight: bold; | |
56 } | |
57 | |
182 | 58 .existing { |
59 margin-top: 1em; | |
60 font-size: 70%; | |
61 } | |
62 | |
63 </style> | |
64 <title>Set templog</title> | |
65 </head> | |
66 | |
67 <body> | |
68 | |
69 <section id="paramlist"> | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
70 </section> |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
71 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
72 <div id="jsontest"> |
182 | 73 </div> |
74 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
75 <input type="button" id="savebutton" value="Save"/> |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
76 |
182 | 77 </body> |
78 | |
79 <script type="html/num_input"> | |
80 <div id="{id}"> | |
184 | 81 <span class="existing">{title} <span id="oldvalue">{oldvaluetext}{unit}</span></span> |
182 | 82 <br/> |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
83 <input type="text" class="input" name="input_{name}" /> |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
84 <input type="button" class="button_down" value="-"/> |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
85 <input type="button" class="button_up" value="+"/> |
182 | 86 </div> |
87 </script> | |
88 | |
89 <script type="html/yesno_button"> | |
90 <div id="{id}"> | |
184 | 91 <span class="existing">{title} <span id="oldvalue">{oldvaluetext}</span></span> |
182 | 92 <br/> |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
93 <input type="button" class="button_no" value="No"/> |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
94 <input type="button" class="button_yes" value="Yes"/> |
182 | 95 </div> |
96 </script> | |
97 | |
98 <script> | |
99 | |
100 function Setter(params) { | |
101 var self = $.observable(this); | |
102 | |
103 self.params = params; | |
104 | |
105 $.each(self.params, function(idx, param) { | |
106 param.id = "param_id_" + idx; | |
184 | 107 param.oldvalue = param.value |
108 if (typeof(param.oldvalue) == "boolean") | |
109 { | |
110 param.oldvaluetext = param.oldvalue ? "Yes" : "No"; | |
111 } | |
112 else | |
113 { | |
114 param.oldvaluetext = param.oldvalue; | |
115 } | |
182 | 116 }); |
117 | |
184 | 118 self.edit = function(param, newvalue) { |
119 param.value = newvalue; | |
182 | 120 params[param.name] = param; |
121 self.trigger("edit", param); | |
122 } | |
123 | |
124 self.adjust = function(param, updown) { | |
125 // XXX increment | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
126 param.value += (param.amount*updown); |
182 | 127 self.trigger("edit", param); |
128 } | |
129 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
130 self.save = function() { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
131 var j = JSON.stringify(self.params); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
132 self.trigger("saved", j) |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
133 } |
182 | 134 } |
135 | |
136 (function() { 'use strict'; | |
137 | |
138 var params = {{!inline_data}}; | |
139 window.setter = new Setter(params); | |
140 | |
141 var root = $("#paramlist"); | |
142 | |
143 var number_template = $("[type='html/num_input']").html(); | |
144 var button_template = $("[type='html/yesno_button']").html(); | |
145 | |
146 setter.on("add", add); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
147 |
182 | 148 setter.on("edit", function(param) |
149 { | |
150 var el = $("#" + param.id); | |
151 if (param.kind === "number") | |
152 { | |
184 | 153 set_text_state(el, param); |
182 | 154 } |
155 else if (param.kind === "yesno") | |
156 { | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
157 set_button_state(el, param.value); |
182 | 158 } |
184 | 159 var same; |
160 switch (typeof(param.oldvalue)) | |
161 { | |
162 case "boolean": | |
163 same = ((!param.value) == (!param.oldvalue)); | |
164 break; | |
165 case "number": | |
166 same = Math.abs(param.value - param.oldvalue) < 1e-3 * param.amount; | |
167 break; | |
168 default: | |
169 same = (param.value === param.oldvalue); | |
170 } | |
171 | |
172 $("#oldvalue", el).toggleClass("modified", !same); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
173 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
174 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
175 setter.on("saved", function(j) { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
176 $("#jsontest").text(j); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
177 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
178 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
179 |
182 | 180 |
181 $.route(function(hash) { | |
182 | |
183 // clear list and add new ones | |
184 root.empty() && $.each(setter.params, function (idx, p) { | |
185 add(p); | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
186 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
187 $("#savebutton").click(function() { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
188 setter.save(); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
189 }) |
182 | 190 }) |
191 }) | |
192 | |
184 | 193 function set_text_state(el, param) |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
194 { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
195 var input = $(".input", el); |
184 | 196 var s = Number(param.value).toFixed(param.digits) |
197 input.text(s).val(s) | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
198 } |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
199 |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
200 function set_button_state(el, value) |
182 | 201 { |
184 | 202 $(".button_yes", el).toggleClass("onbutton", value); |
203 $(".button_no", el).toggleClass("onbutton", !value); | |
182 | 204 } |
205 | |
206 function add(param) | |
207 { | |
208 if (param.kind === "number") | |
209 { | |
210 var el = $($.render(number_template, param)).appendTo(root); | |
211 var input = $(".input", el); | |
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 input.keyup(function(e) { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
214 if (e.which == 13) |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
215 { |
184 | 216 setter.edit(param, Number(this.value)); |
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 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
219 |
184 | 220 input.blur(function(e) { |
221 setter.edit(param, Number(this.value)); | |
222 }); | |
223 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
224 $(".button_up", el).click(function() { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
225 setter.adjust(param, 1); |
184 | 226 this.blur() |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
227 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
228 $(".button_down", el).click(function() { |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
229 setter.adjust(param, -1); |
184 | 230 this.blur() |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
231 }); |
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
232 |
184 | 233 set_text_state(el, param); |
182 | 234 } |
235 else if (param.kind === "yesno") | |
236 { | |
237 var el = $($.render(button_template, param)).appendTo(root); | |
238 var button_yes = $(".button_yes", el); | |
239 var button_no = $(".button_no", el); | |
240 | |
241 button_yes.click(function() { | |
184 | 242 setter.edit(param, true); |
243 this.blur() | |
182 | 244 }) |
245 | |
246 button_no.click(function() { | |
184 | 247 setter.edit(param, false); |
248 this.blur() | |
182 | 249 }) |
250 | |
183
177f616893e6
param editor roughly working
Matt Johnston <matt@ucc.asn.au>
parents:
182
diff
changeset
|
251 set_button_state(el, param.value); |
182 | 252 } |
253 } | |
254 | |
255 })() | |
256 | |
257 </script> | |
258 | |
259 </html> |