source: EcnlProtoTool/trunk/webapp/webmrbc/EObjectModalView.cs@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csharp
File size: 8.1 KB
Line 
1using System;
2using Bridge;
3using Bridge.Bootstrap3;
4using Bridge.Html5;
5using Bridge.jQuery2;
6
7namespace WebMrbc
8{
9 public class EObjectModalView
10 {
11 jQuery el;
12 JsonObjectInfo model;
13 JsonObjectInfo target;
14 JsonClassGroupInfo currentClassGroup;
15
16 public event Action<EObjectModalView, bool> Closed;
17
18 public EObjectModalView()
19 {
20 el = jQuery.Select("#eobject-modal");
21 }
22
23 internal void SetEObject(JsonObjectInfo eobject, string imgUrl)
24 {
25 target = eobject;
26 model = eobject.Clone();
27
28 var cg = model.type.classGroup;
29 if ((model.attribute == "device") && (model.type == App.NodeProfileClass)) {
30 cg = App.ClassGroups[0];
31 }
32
33 var img = (new jQuery("<img>"));
34 img.Attr("src", imgUrl);
35 img.Attr("alt", model.identifier);
36 jQuery.Select("#eobject-modal-eobject img").ReplaceWith(img);
37
38 jQuery.Select("#eobject_identifier").Val(model.identifier);
39 jQuery.Select("#eobject_instanceCode").Val(model.instanceCode.ToString());
40 SetAttribute(model.attribute);
41
42 SetClassGroup(cg, () => {
43 var c = model.type;
44 if ((model.attribute == "device") && (model.type == App.NodeProfileClass)) {
45 c = App.ClassGroups[0].classes[0];
46 }
47 SetClass(c, UpdateProperties);
48 });
49 }
50
51 internal void Render()
52 {
53 el.Modal(new ModalOptions() {
54 Backdrop = "static",
55 Show = true
56 });
57 }
58
59 private void SetAttribute(string attribute)
60 {
61 var dropdown = jQuery.Select("#eobject_attribute").Parent();
62 var button = jQuery.Select("button[type='button']", dropdown);
63 button.Attr("disabled", "disabled");
64
65 var menuitem = jQuery.Select("a[role='menuitem']", dropdown);
66
67 string text;
68 switch (attribute) {
69 case "local":
70 text = menuitem.Get(0).TextContent;
71 break;
72 case "sync":
73 text = menuitem.Get(1).TextContent;
74 break;
75 case "async":
76 text = menuitem.Get(2).TextContent;
77 break;
78 case "device":
79 text = menuitem.Get(3).TextContent;
80 break;
81 default:
82 text = attribute;
83 break;
84 }
85
86 SetAttribute(attribute, text);
87 }
88
89 private void SetAttribute(string attribute, string text)
90 {
91 model.attribute = attribute;
92
93 var button = jQuery.Select("#eobject_attribute");
94 button.Text(text);
95
96 var caret = jQuery.Select("span.caret", button);
97 if (caret.Length == 0) {
98 caret = new jQuery(ElementType.Span);
99 caret.Attr("class", "caret");
100 button.Append(caret);
101 }
102 }
103
104 private void SetClassGroup(JsonClassGroupInfo cg, Action success = null)
105 {
106 currentClassGroup = cg;
107
108 var button = jQuery.Select("#eobject_classGroupCode");
109 button.Text(cg.description);
110
111 if (model.attribute == "device") {
112 button.RemoveAttr("disabled");
113 }
114 else {
115 button.Attr("disabled", "disabled");
116 }
117
118 var caret = jQuery.Select("span.caret", button);
119 if (caret.Length == 0) {
120 caret = new jQuery(ElementType.Span);
121 caret.Attr("class", "caret");
122 button.Append(caret);
123 }
124
125 InitClasss(cg);
126 if (success != null) success();
127 }
128
129 private void SetClass(JsonClassInfo c, Action success = null)
130 {
131 model.type = c;
132
133 var button = jQuery.Select("#eobject_classCode");
134 button.Text(c.description);
135
136 if (model.attribute == "device") {
137 button.RemoveAttr("disabled");
138 }
139 else {
140 button.Attr("disabled", "disabled");
141 }
142
143 var caret = jQuery.Select("span.caret", button);
144 if (caret.Length == 0) {
145 caret = new jQuery(ElementType.Span);
146 caret.Attr("class", "caret");
147 button.Append(caret);
148 }
149
150 string identifier = jQuery.Select("#eobject_identifier").Val();
151 if (model.identifier == identifier) {
152 model.identifier = CodeGenerator.GetClassIdentifier(c);
153 jQuery.Select("#eobject_identifier").Val(model.identifier);
154 }
155
156 InitProperties(c);
157 if (success != null) success();
158 }
159
160 internal void InitClassGroups()
161 {
162 var ul = jQuery.Select("#eobject_classGroups");
163
164 ul.Html("");
165
166 foreach (var cg in App.ClassGroups) {
167 var li = new jQuery(ElementType.LI);
168 li.Attr("role", "presentation");
169
170 var a = new jQuery(ElementType.Anchor);
171 a.Attr("role", "menuitem");
172 a.Append(cg.description);
173 a.Click(cg, new Action<jQueryMouseEvent>(OnSelectClassGroupCode));
174
175 li.Append(a);
176 ul.Append(li);
177 }
178 }
179
180 internal void InitClasss(JsonClassGroupInfo cg)
181 {
182 var ul = jQuery.Select("#eobject_classs");
183
184 ul.Html("");
185
186 foreach (var c in cg.classes) {
187 var li = new jQuery(ElementType.LI);
188 li.Attr("role", "presentation");
189
190 var a = new jQuery(ElementType.Anchor);
191 a.Attr("role", "menuitem");
192 a.Append(c.description);
193 a.Click(c, new Action<jQueryMouseEvent>(OnSelectClassCode));
194
195 li.Append(a);
196 ul.Append(li);
197 }
198 }
199
200 internal void InitProperties(JsonClassInfo c)
201 {
202 var div = jQuery.Select("#eobject_properties");
203
204 div.Html("");
205
206 if (c.classGroup.classGroupCode != 0x0E || c.classCode != 0xF0) {
207 foreach (var p in App.BaseObjectPropertyList) {
208 InitProperty(div, p);
209 }
210 }
211
212 foreach (var p in c.properties) {
213 InitProperty(div, p);
214 }
215 }
216
217 private void InitProperty(jQuery div, JsonPropertyInfo p)
218 {
219 var label = new jQuery(ElementType.Label);
220 label.Attr("class", "btn btn-default");
221
222 var input = new jQuery(ElementType.Input);
223 input.Attr("type", "checkbox");
224 input.Attr("autocomplete", "off");
225
226 label.Append(input);
227 label.Append(p.description);
228 label.Attr("data-ecnl-epc", p.propertyCode.ToString());
229
230 div.Append(label);
231
232 if ((p.required.Length > 0) && (p.required[0] != "NONE")) {
233 label.ButtonToggle();
234 }
235 }
236
237 private void UpdateProperties()
238 {
239 var labels = jQuery.Select("#eobject_properties > .btn.btn-default");
240
241 foreach (Element l in labels.Get()) {
242 var label = new jQuery(l);
243 if (label.Is(".active"))
244 label.ButtonToggle();
245
246 var sepc = label.Attr("data-ecnl-epc");
247 if (sepc == null)
248 continue;
249
250 var epc = Script.ParseInt(sepc);
251 JsonPropertyInfo prop = null;
252 foreach (var p in model.properties) {
253 if (p.propertyCode != epc)
254 continue;
255 prop = p;
256 break;
257 }
258 if (prop != null) {
259 label.ButtonToggle();
260 }
261 }
262 }
263
264 public void OnChangeIdentifier(Element ele)
265 {
266 var identifier = jQuery.Select("#eobject_identifier").Val();
267 model.identifier = identifier;
268 }
269
270 public void OnSelectAttribute(Element sender, string attribute)
271 {
272 var text = sender.TextContent;
273 SetAttribute(attribute, text);
274 }
275
276 public void OnSelectClassGroupCode(jQueryMouseEvent e)
277 {
278 var cg = (JsonClassGroupInfo)e.Data;
279 SetClassGroup(cg, () => {
280 SetClass(cg.classes[0], UpdateProperties);
281 });
282 }
283
284 public void OnSelectClassCode(jQueryMouseEvent e)
285 {
286 SetClass((JsonClassInfo)e.Data, UpdateProperties);
287 }
288
289 public void OnChangeInstanceCode(Element sender)
290 {
291 model.instanceCode = (byte)Script.ParseInt((new jQuery(sender)).Val());
292 }
293
294 public void OnOk(Element ele)
295 {
296 if ((model.attribute == "device") && (model.type == App.NodeProfileClass)) {
297 return;
298 }
299
300 el.Modal(ModalOperation.Hide);
301
302 target.type = model.type;
303 target.identifier = model.identifier;
304 target.parent = model.parent;
305 target.instanceCode = model.instanceCode;
306 target.attribute = model.attribute;
307 target.properties = new JsonPropertyInfo[0];
308
309 var labels = jQuery.Select("#eobject_properties > .btn.btn-default");
310
311 foreach (Element l in labels.Get()) {
312 var label = new jQuery(l);
313 if (!label.Is(".active"))
314 continue;
315
316 var sepc = label.Attr("data-ecnl-epc");
317 if (sepc == null)
318 continue;
319
320 var epc = Script.ParseInt(sepc);
321 JsonPropertyInfo prop = null;
322 if (model.type.classGroup.classGroupCode != 0x0E || model.type.classCode != 0xF0) {
323 foreach (var p in App.BaseObjectPropertyList) {
324 if (p.propertyCode != epc)
325 continue;
326 prop = p;
327 break;
328 }
329 }
330 if (prop != null) {
331 target.properties.Push(prop);
332 }
333 else {
334 foreach (var p in model.type.properties) {
335 if (p.propertyCode != epc)
336 continue;
337 prop = p;
338 break;
339 }
340 if (prop != null) {
341 target.properties.Push(prop);
342 }
343 }
344 }
345
346 if (Closed != null)
347 Closed(this, true);
348 }
349
350 public void OnCancel(Element ele)
351 {
352 el.Modal(ModalOperation.Hide);
353
354 if (Closed != null)
355 Closed(this, false);
356 }
357 }
358}
Note: See TracBrowser for help on using the repository browser.