using System; using Bridge; using Bridge.Bootstrap3; using Bridge.Html5; using Bridge.jQuery2; namespace WebMrbc { public class EObjectModalView { jQuery el; JsonObjectInfo model; JsonObjectInfo target; JsonClassGroupInfo currentClassGroup; public event Action Closed; public EObjectModalView() { el = jQuery.Select("#eobject-modal"); } internal void SetEObject(JsonObjectInfo eobject, string imgUrl) { target = eobject; model = eobject.Clone(); var cg = model.type.classGroup; if ((model.attribute == "device") && (model.type == App.NodeProfileClass)) { cg = App.ClassGroups[0]; } var img = (new jQuery("")); img.Attr("src", imgUrl); img.Attr("alt", model.identifier); jQuery.Select("#eobject-modal-eobject img").ReplaceWith(img); jQuery.Select("#eobject_identifier").Val(model.identifier); jQuery.Select("#eobject_instanceCode").Val(model.instanceCode.ToString()); SetAttribute(model.attribute); SetClassGroup(cg, () => { var c = model.type; if ((model.attribute == "device") && (model.type == App.NodeProfileClass)) { c = App.ClassGroups[0].classes[0]; } SetClass(c, UpdateProperties); }); } internal void Render() { el.Modal(new ModalOptions() { Backdrop = "static", Show = true }); } private void SetAttribute(string attribute) { var dropdown = jQuery.Select("#eobject_attribute").Parent(); var button = jQuery.Select("button[type='button']", dropdown); button.Attr("disabled", "disabled"); var menuitem = jQuery.Select("a[role='menuitem']", dropdown); string text; switch (attribute) { case "local": text = menuitem.Get(0).TextContent; break; case "sync": text = menuitem.Get(1).TextContent; break; case "async": text = menuitem.Get(2).TextContent; break; case "device": text = menuitem.Get(3).TextContent; break; default: text = attribute; break; } SetAttribute(attribute, text); } private void SetAttribute(string attribute, string text) { model.attribute = attribute; var button = jQuery.Select("#eobject_attribute"); button.Text(text); var caret = jQuery.Select("span.caret", button); if (caret.Length == 0) { caret = new jQuery(ElementType.Span); caret.Attr("class", "caret"); button.Append(caret); } } private void SetClassGroup(JsonClassGroupInfo cg, Action success = null) { currentClassGroup = cg; var button = jQuery.Select("#eobject_classGroupCode"); button.Text(cg.description); if (model.attribute == "device") { button.RemoveAttr("disabled"); } else { button.Attr("disabled", "disabled"); } var caret = jQuery.Select("span.caret", button); if (caret.Length == 0) { caret = new jQuery(ElementType.Span); caret.Attr("class", "caret"); button.Append(caret); } InitClasss(cg); if (success != null) success(); } private void SetClass(JsonClassInfo c, Action success = null) { model.type = c; var button = jQuery.Select("#eobject_classCode"); button.Text(c.description); if (model.attribute == "device") { button.RemoveAttr("disabled"); } else { button.Attr("disabled", "disabled"); } var caret = jQuery.Select("span.caret", button); if (caret.Length == 0) { caret = new jQuery(ElementType.Span); caret.Attr("class", "caret"); button.Append(caret); } string identifier = jQuery.Select("#eobject_identifier").Val(); if (model.identifier == identifier) { model.identifier = CodeGenerator.GetClassIdentifier(c); jQuery.Select("#eobject_identifier").Val(model.identifier); } InitProperties(c); if (success != null) success(); } internal void InitClassGroups() { var ul = jQuery.Select("#eobject_classGroups"); ul.Html(""); foreach (var cg in App.ClassGroups) { var li = new jQuery(ElementType.LI); li.Attr("role", "presentation"); var a = new jQuery(ElementType.Anchor); a.Attr("role", "menuitem"); a.Append(cg.description); a.Click(cg, new Action(OnSelectClassGroupCode)); li.Append(a); ul.Append(li); } } internal void InitClasss(JsonClassGroupInfo cg) { var ul = jQuery.Select("#eobject_classs"); ul.Html(""); foreach (var c in cg.classes) { var li = new jQuery(ElementType.LI); li.Attr("role", "presentation"); var a = new jQuery(ElementType.Anchor); a.Attr("role", "menuitem"); a.Append(c.description); a.Click(c, new Action(OnSelectClassCode)); li.Append(a); ul.Append(li); } } internal void InitProperties(JsonClassInfo c) { var div = jQuery.Select("#eobject_properties"); div.Html(""); if (c.classGroup.classGroupCode != 0x0E || c.classCode != 0xF0) { foreach (var p in App.BaseObjectPropertyList) { InitProperty(div, p); } } foreach (var p in c.properties) { InitProperty(div, p); } } private void InitProperty(jQuery div, JsonPropertyInfo p) { var label = new jQuery(ElementType.Label); label.Attr("class", "btn btn-default"); var input = new jQuery(ElementType.Input); input.Attr("type", "checkbox"); input.Attr("autocomplete", "off"); label.Append(input); label.Append(p.description); label.Attr("data-ecnl-epc", p.propertyCode.ToString()); div.Append(label); if ((p.required.Length > 0) && (p.required[0] != "NONE")) { label.ButtonToggle(); } } private void UpdateProperties() { var labels = jQuery.Select("#eobject_properties > .btn.btn-default"); foreach (Element l in labels.Get()) { var label = new jQuery(l); if (label.Is(".active")) label.ButtonToggle(); var sepc = label.Attr("data-ecnl-epc"); if (sepc == null) continue; var epc = Script.ParseInt(sepc); JsonPropertyInfo prop = null; foreach (var p in model.properties) { if (p.propertyCode != epc) continue; prop = p; break; } if (prop != null) { label.ButtonToggle(); } } } public void OnChangeIdentifier(Element ele) { var identifier = jQuery.Select("#eobject_identifier").Val(); model.identifier = identifier; } public void OnSelectAttribute(Element sender, string attribute) { var text = sender.TextContent; SetAttribute(attribute, text); } public void OnSelectClassGroupCode(jQueryMouseEvent e) { var cg = (JsonClassGroupInfo)e.Data; SetClassGroup(cg, () => { SetClass(cg.classes[0], UpdateProperties); }); } public void OnSelectClassCode(jQueryMouseEvent e) { SetClass((JsonClassInfo)e.Data, UpdateProperties); } public void OnChangeInstanceCode(Element sender) { model.instanceCode = (byte)Script.ParseInt((new jQuery(sender)).Val()); } public void OnOk(Element ele) { if ((model.attribute == "device") && (model.type == App.NodeProfileClass)) { return; } el.Modal(ModalOperation.Hide); target.type = model.type; target.identifier = model.identifier; target.parent = model.parent; target.instanceCode = model.instanceCode; target.attribute = model.attribute; target.properties = new JsonPropertyInfo[0]; var labels = jQuery.Select("#eobject_properties > .btn.btn-default"); foreach (Element l in labels.Get()) { var label = new jQuery(l); if (!label.Is(".active")) continue; var sepc = label.Attr("data-ecnl-epc"); if (sepc == null) continue; var epc = Script.ParseInt(sepc); JsonPropertyInfo prop = null; if (model.type.classGroup.classGroupCode != 0x0E || model.type.classCode != 0xF0) { foreach (var p in App.BaseObjectPropertyList) { if (p.propertyCode != epc) continue; prop = p; break; } } if (prop != null) { target.properties.Push(prop); } else { foreach (var p in model.type.properties) { if (p.propertyCode != epc) continue; prop = p; break; } if (prop != null) { target.properties.Push(prop); } } } if (Closed != null) Closed(this, true); } public void OnCancel(Element ele) { el.Modal(ModalOperation.Hide); if (Closed != null) Closed(this, false); } } }