public BsEditorHtmlBuilder(TModel model, ViewContext viewContext) : base(viewContext) { this.viewContext = viewContext; this.renderer = new BsEditorRenderer <TModel>(this); this.tabConfigurator = new BsEditorTabConfigurator <TModel>(viewContext); this.groupConfigurator = new BsEditorGroupConfigurator <TModel>(viewContext); var type = typeof(TModel); var props = type.GetProperties(); var editableTabIds = new List <object>(); // find out what tabs are editable to send to group configurator TODO refactor somehow (3 foreach ..) foreach (var prop in props) { BsEditorTabAttribute tabAttr = null; if (ReflectionHelpers.TryGetAttribute(prop, out tabAttr)) { if (tabAttr.Editable) { editableTabIds.Add(tabAttr.Id); } } } //if (!this.IsAjaxRequest()) // we don't care about groups // { foreach (var prop in props) { BsEditorGroupAttribute groupAttr = null; if (ReflectionHelpers.TryGetAttribute(prop, out groupAttr)) { var value = prop.GetValue(model); InvokeAddGroupConfig(value, prop, groupAttr, editableTabIds); // TODO send editableTabIds } } // } foreach (var prop in props) { BsEditorTabAttribute tabAttr = null; if (ReflectionHelpers.TryGetAttribute(prop, out tabAttr)) { tabConfigurator.AddNavTab(tabAttr); var value = prop.GetValue(model); InvokeAddTabConfig(value, prop, tabAttr); // this has to happen after group configuration } } }
private void InvokeAddGroupConfig(object value, PropertyInfo prop, BsEditorGroupAttribute attr, List <object> editableTabIds) { var propertyType = prop.PropertyType; var propertyName = prop.Name; if (!propertyType.GetInterfaces().Contains(typeof(IBsEditorGroupModel))) { throw new Exception("The model with BsEditorGroupAttribute must inherit BsEditorGroupModel"); } var genericArgs = propertyType.GetGenericArguments(); var count = genericArgs.Count(); if (count == 0) { var baseType = propertyType.BaseType; genericArgs = baseType.GetGenericArguments(); count = genericArgs.Count(); } if (count > 0) { MethodInfo method = null, generic = null; Type rowType = genericArgs[0]; method = typeof(BsEditorGroupConfigurator <TModel>).GetMethod("Add", this.Bindings()); generic = method.MakeGenericMethod(propertyType, rowType); generic.Invoke(this.groupConfigurator, new object[] { attr, value, editableTabIds.ToArray(), propertyName }); } }
private BsEditorGroupBuilder GetGroup <TValue>(Expression <Func <TModel, TValue> > expression) { var prop = expression.GetPropertyInfo <TModel, TValue>(); BsEditorGroupAttribute attr = null; if (ReflectionHelpers.TryGetAttribute(prop, out attr)) { var id = attr.Id; return(this.Groups[id]); } throw new Exception("Property " + prop.Name + " has no BsGroupEditorAttribute"); }
private void Add <TEditor, TRow>(BsEditorGroupAttribute attr, IBsEditorGroupModel model, object[] editableTabIds, string propertyName) where TEditor : IBsEditorGroupModel where TRow : BsEditorGroupItemModel, new() { var group = new BsEditorGroupBuilder <TEditor>(model, this.viewContext, editableTabIds) .Id(attr.Id); group.SetPropertyName(propertyName); var rowType = typeof(TRow); var genericArgs = rowType.BaseType.GetGenericArguments(); if (genericArgs.Count() == 1) // register renderer via reflection because we don't know the type of TForm { var method = typeof(BsEditorGroupBuilder <TEditor>).GetMethod("RegisterRenderer", BindingFlags.Default | BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic); var generic = method.MakeGenericMethod(typeof(TRow), genericArgs[0]); // genericArgs[0] => TForm generic.Invoke(group, null); } else { group.renderer = new BsEditorGroupRenderer <TEditor, TRow>(group); } if (model != null) { var connection = model.GetTabGroupConnection(); if (connection != null) { connection.GroupId = attr.Id; this.Connections.Add(connection); } } InsertGroup <TEditor, TRow>(attr.Id, group); }