private void BindCollection(PropertyInfo prop, object model) { var value = prop.GetValue(model); var collectionGeneric = prop.PropertyType.GetGenericArguments().FirstOrDefault(); var listProperties = FWReflectionHelper.GetProperties(collectionGeneric, BindingFlags.Public | BindingFlags.Instance).Where(f => f.IsDefined(typeof(FWDataBindAttribute))); // Creates the class represent var collectionModel = Serialize(Activator.CreateInstance(collectionGeneric), collectionGeneric, _context, true); var propertiesNames = listProperties.Select(f => f.Name); var customString = GetCustomBindings(_context, collectionGeneric.Name); _innerProperties.Append($"fw.{collectionGeneric.Name} = function(parent,{string.Join(",", propertiesNames)}) {{ var self=this; {collectionModel._modelData} {collectionModel._datasourceData} {customString} }};"); // Adds the existing values var array = new List <string>(); if (value is IEnumerable list) { foreach (var item in list) { var itemValues = listProperties.Select(f => PropertyValue(f.GetValue(item), f.PropertyType)); array.Add($"new fw.{collectionGeneric.Name}(this,{string.Join(",", itemValues)})"); } } _modelData.Append($"self.{prop.Name}=ko.observableArray([{string.Join(",", array)}]);"); }
/// <summary> /// Creates a new control. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> public FWInputControlFactory(FWRequestContext requestContext, object model, ModelMetadata metadata) { _requestContext = requestContext; _modelType = FWReflectionHelper.GetUnderlyingType(metadata.ModelType); _model = model; _metadata = metadata; }
private void SetValue(object source, object target, MemberInfo sourceMember, MemberInfo targetMember) { var targetType = targetMember.GetMemberType(); if (!FWReflectionHelper.IsPrimitive(targetType)) { var complexObject = sourceMember.GetValue(source); if (complexObject != null) { // Gets the target object property value var targetMemberValue = targetMember.GetValue(target); // Gets the property type or, if it is an interface, from the source object type var type = (!targetType.IsInterface) ? targetType : complexObject.GetType(); if (targetMemberValue == null) { targetMemberValue = Activator.CreateInstance(type); } targetMemberValue = InjectFrom(type, complexObject); targetMember.SetValue(target, targetMemberValue); } } else { var val = sourceMember.GetValue(source); // Checks to see if there are any maps for the primitive. val = Omu.ValueInjecter.Mapper.Instance.MapPrimitive(targetType, val); targetMember.SetValue(target, val); } }
/// <summary> /// Creates the control main element. /// </summary> /// <returns>The control IFWHtmlElement interface.</returns> protected override IFWHtmlElement CreateControl() { var input = new FWInputElement(Name, FWInputType.Hidden); input.MergeAttributes(Attributes); if (!string.IsNullOrWhiteSpace(CustomCss)) { input.AddCssClass(CustomCss); } input.Id = Id; input.DataType = "fw-hidden"; if (DataBind) { DataBind.AddMainBind(FWBindConfiguration.VALUE); input.DataBind = DataBind.CreateBind(); } else if (Model != null) { input.Value = Model.ToString(); } else if (FWReflectionHelper.IsNumeric(ModelType)) { // If the property is a number, but the model is null, the value defaults to 0. input.Value = "0"; } return(input); }
/// <summary> /// Creates a new Checkbox control. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> /// <param name="datasource">The checkbox datasource.</param> public FWCheckboxControl(FWRequestContext requestContext, object model, ModelMetadata metadata, IEnumerable <FWDatasourceItem> datasource) : base(requestContext, model, metadata) { _values = new Dictionary <string, string>(); foreach (var item in datasource) { _values.Add(item.Id, item.Value); } if (FWReflectionHelper.IsCollection(metadata.ModelType)) { var list = model as IEnumerable; foreach (var item in list) { _selected.Add(item.ToString()); } } else { if (model != null) { _selected.Add(model.ToString()); } } }
/// <summary> /// Creates a new Radio control. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> public FWRadioControl(FWRequestContext requestContext, object model, ModelMetadata metadata) : base(requestContext, model, metadata) { if (FWReflectionHelper.CheckType <bool>(metadata.ModelType)) { _isBoolean = true; _values = new Dictionary <string, string> { { "true", GetModelResource("TRUE") }, { "false", GetModelResource("FALSE") } }; if (model != null) { _selected = model.ToString(); } } else if (FWReflectionHelper.IsEnum(metadata.ModelType)) { var enumDictionary = FWEnumHelper.GetEnumAsDictonary(metadata.ModelType); _values = new Dictionary <string, string>(); // Gets the resource values for the enum items. foreach (var item in enumDictionary) { _values.Add(item.Key, GetModelResource(item.Value)); } _selected = FWEnumHelper.GetValues(model as Enum).FirstOrDefault(); } else { throw new FWMissingDatasourceException(Id); } }
private string GetPropertyValue() { switch (_modelType.Name) { case nameof(String): return(_model.ToString()); case nameof(Boolean): return(GetCheckboxText()); case nameof(DateTime): return(((DateTime)_model).ToString("d")); } if (FWReflectionHelper.IsEnum(_modelType)) { if (_model == null) { return(string.Empty); } return(FWStringLocalizer.GetModelResource($"{OriginalId}_{_model.ToString()}", _metadata.ContainerType?.Name)); } if (_metadata.AdditionalValues.ContainsKey(nameof(FWSelectAttribute))) { return(GetSelectText()); } return(_model.ToString()); }
/// <summary> /// Creates the framework control. /// </summary> /// <param name="context">Contains information associated with the current HTML tag.</param> /// <param name="output">A stateful HTML element used to generate an HTML tag.</param> /// <returns>The control instance.</returns> protected override IFWHtmlElement RenderControl(TagHelperContext context, TagHelperOutput output) { if (!FWReflectionHelper.IsCollection(For.Metadata.ModelType)) { throw new FWInvalidModelException("Invalid model for detail. Must use a collection."); } FWControl control = null; switch (Type) { case FWDetailType.Table: control = new FWDetailTableControl(RequestContext, For.Model as IEnumerable, For.Metadata); control.Attributes.Add("data-detailtype", "table"); break; case FWDetailType.Grid: control = new FWDetailGridControl(RequestContext, For.Model as IEnumerable, For.Metadata); control.Attributes.Add("data-detailtype", "grid"); break; } control.Attributes.Add("data-control", "detail"); return(control); }
private void InjectDictionary(IDictionary dictionary, IDictionary target, Type targetType) { foreach (DictionaryEntry item in dictionary) { // Gets the value type. var itemValueType = targetType.GetGenericArguments().ElementAt(1); // If the list is for primitive values, just add the value to the list if (FWReflectionHelper.IsPrimitive(itemValueType)) { target.Add(item.Key, item.Value); } else { if (item.Value != null) { // If the list is for complex types, try to map each item. var newItem = InjectFrom(itemValueType, item.Value); target.Add(item.Key, newItem); } else { target.Add(item.Key, null); } } } }
private string PropertyValue(object propertyValue, Type propertyType) { var type = FWReflectionHelper.GetUnderlyingType(propertyType); if (propertyValue == null) { var defaultValue = FWReflectionHelper.GetDefault(propertyType); return(defaultValue == null ? "''" : defaultValue.ToString()); } if (type == FWKnownTypes.String) { return($"'{propertyValue.ToString().Replace("'", "\\'")}'"); } else if (type == FWKnownTypes.Bool) { return((bool)propertyValue ? "true" : "false"); } else if (type == FWKnownTypes.Decimal) { return(((decimal)propertyValue).ToString(new CultureInfo("en"))); } else if (type == FWKnownTypes.DateTime) { return($"moment.utc('{((DateTime)propertyValue).ToString("yyyy-MM-ddTHH:mm:ss")}')"); } return(propertyValue.ToString()); }
/// <summary> /// Maps a property. /// </summary> protected virtual void InjectMember(object source, object target, Type targetType) { var properties = FWReflectionHelper.GetPublicProperties(targetType, false); InjectMemberProperties(source, target, properties); InjectPrivateMembers(source, target, targetType); }
private void InjectPrivateMembers(object source, object target, Type targetType) { var privateMembers = FWReflectionHelper.GetMembers(targetType, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(f => f.IsDefined(typeof(FWMapPrivateAttribute))); if (privateMembers.Any()) { InjectMemberProperties(source, target, privateMembers); } }
/// <summary> /// Creates a new Display control. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> public FWDisplayControl(FWRequestContext requestContext, object model, ModelMetadata metadata) : base(metadata) { Name = metadata.PropertyName; _requestContext = requestContext; _modelType = FWReflectionHelper.GetUnderlyingType(metadata.ModelType); _model = model; _metadata = metadata; }
/// <summary> /// Adds a property to the model. /// </summary> /// <param name="prop">The property PropertyInfo.</param> /// <param name="model">The model object reference.</param> private void Databind(PropertyInfo prop, object model) { if (FWReflectionHelper.IsPrimitive(prop.PropertyType)) { BindPrimitive(prop, model); } else if (FWReflectionHelper.IsCollection(prop.PropertyType)) { BindCollection(prop, model); } }
private void Loop(object model, Type modelType) { var properties = FWReflectionHelper.GetProperties(modelType, BindingFlags.Public | BindingFlags.Instance); foreach (var prop in properties) { // Checks if the property has the FWDataBind attribute. if (prop.IsDefined(typeof(FWDataBindAttribute))) { Databind(prop, model); } } }
/// <summary> /// Initializes a new instance of the <see cref="FWInputControl" />. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> protected FWInputControl(FWRequestContext requestContext, object model, ModelMetadata metadata) : base(metadata) { RequestContext = requestContext; Name = metadata.PropertyName; Model = model; DisplayName = metadata.DisplayName; ModelType = FWReflectionHelper.GetUnderlyingType(metadata.ModelType); ContainerType = metadata.ContainerType; IsRequired = metadata.IsRequired(); IsReadOnly = metadata.IsReadOnly; }
/// <summary> /// Creates a new DetailGrid control. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> public FWDetailGridControl(FWRequestContext requestContext, IEnumerable model, ModelMetadata metadata) : base(requestContext, model, metadata) { _listType = metadata.ModelType.GenericTypeArguments.FirstOrDefault(); if (_listType == null) { throw new FWInvalidModelException("Detail Grid model must be a generic list."); } _properties = FWReflectionHelper.GetPublicProperties(_listType).Where(f => !f.IsDefined(typeof(FWDatasourceAttribute))); _modelPrefix = metadata.PropertyName; _parentType = metadata.ContainerType ?? metadata.ModelType; }
/// <summary> /// Creates a new Textbox control. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> public FWTextboxControl(FWRequestContext requestContext, object model, ModelMetadata metadata) : base(requestContext, model, metadata) { if (metadata.AdditionalValues.ContainsKey(nameof(FWMaskAttribute))) { var mask = (metadata.AdditionalValues[nameof(FWMaskAttribute)] as FWMaskAttribute); _mask = mask.Mask; _reversemask = mask.Reverse; } if (_regexPattern == null) { if (FWReflectionHelper.IsNumeric(ModelType)) { _regexPattern = (FWReflectionHelper.IsIntegral(ModelType)) ? @"^\d+$" : $@"^[0-9]\d*(\{CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator}\d*)?$"; } } }
/// <summary> /// Maps a source object to a target. /// </summary> /// <param name="source">The source object.</param> /// <param name="target">The target object.</param> protected sealed override void Inject(object source, object target) { if (source == null) { return; } var targetType = target.GetType(); // Checks to see if its mapping a list if (FWReflectionHelper.IsCollection(targetType)) { InjectCollection(source, target, targetType); } else { InjectMember(source, target, targetType); } }
private FWInputControl AttemptCreateControlByModelType() { if (_modelType == FWKnownTypes.Bool) { return(CreateCheckbox()); } if (_modelType == FWKnownTypes.DateTime) { return(CreateDatepicker()); } // Checks if the model is an enum. if (FWReflectionHelper.IsEnum(_modelType)) { return(CreateSelect()); } return(CreateTextbox()); }
/// <summary> /// Generates the columns from the model metadata. /// </summary> protected virtual void GenerateColumnsFromMetadata() { // Gets the properties IEnumerable <PropertyInfo> properties = FWReflectionHelper.GetPublicProperties(ListType); // Finds the property metadata ModelMetadata listMetadata = RequestContext.MetadataProvider.GetMetadataForType(ListType); // Creates all data columns foreach (var prop in properties) { var itemMetadata = listMetadata.Properties.Where(f => f.PropertyName == prop.Name).First(); if (!itemMetadata.AdditionalValues.ContainsKey(nameof(FWDatasourceAttribute))) { FWTableColumn column = CreateColumn(itemMetadata, prop); column.Property = prop; Columns.Add(column); } } }
/// <summary> /// Creates a new Checkbox control. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> public FWCheckboxControl(FWRequestContext requestContext, object model, ModelMetadata metadata) : base(requestContext, model, metadata) { if (FWReflectionHelper.CheckType <bool>(metadata.ModelType)) { _isBoolean = true; _values = new Dictionary <string, string> { { "true", DisplayName } }; if (model != null) { _selected.Add(model.ToString()); } //If the property is boolean, there is no sense in being required. if (metadata.ModelType == FWKnownTypes.Bool) { IsRequired = false; } HideLabel(); } else if (FWReflectionHelper.IsEnum(metadata.ModelType)) { var enumDictionary = FWEnumHelper.GetEnumAsDictonary(metadata.ModelType); _values = new Dictionary <string, string>(); // Gets the resource values for the enum items. foreach (var item in enumDictionary) { _values.Add(item.Key, GetModelResource(item.Value)); } _selected = FWEnumHelper.GetValues(model as Enum).ToList(); } else { throw new FWMissingDatasourceException(Id); } }
/// <summary> /// Creates a new Select control. /// </summary> /// <param name="requestContext">The request helper.</param> /// <param name="model">The current model.</param> /// <param name="metadata">The model metadata.</param> /// <param name="container">The model container object.</param> public FWSelectControl(FWRequestContext requestContext, object model, ModelMetadata metadata, object container = null) : base(requestContext, model, metadata) { if (metadata.UnderlyingOrModelType == FWKnownTypes.Bool) { _datasource = new List <IFWDatasourceItem> { new FWDatasourceItem() { Id = "true", Value = GetModelResource("TRUE") }, new FWDatasourceItem() { Id = "false", Value = GetModelResource("FALSE") } }; if (model != null) { _selected = model.ToString(); } } else if (FWReflectionHelper.IsEnum(metadata.ModelType)) { var enumDictionary = FWEnumHelper.GetEnumAsDictonary(metadata.ModelType); var datasource = new List <IFWDatasourceItem>(); // Gets the resource values for the enum items. foreach (var item in enumDictionary) { datasource.Add(new FWDatasourceItem() { Id = item.Key, Value = GetModelResource(item.Value) }); } _datasource = datasource; _selected = FWEnumHelper.GetValues(model as Enum).FirstOrDefault(); } else { // Looks for the datasource inside the view model. _datasource = FWDatasourceHelper.GetDatasourceFromModel(_datasourceName, requestContext.Model); // If the datasource is not found, try to look for it inside the container object. if (_datasource == null) { _datasource = FWDatasourceHelper.GetDatasourceFromModel(_datasourceName, container); } if (model != null) { _selected = model.ToString(); } } if (DataBind) { requestContext.HttpContext.AddDatasource(metadata.PropertyName, metadata.ContainerMetadata.ModelType, _datasource); _datasourceName = metadata.PropertyName; } _allowClear = !IsRequired; }
/// <summary> /// Checks if the model is required or is a non nullable value. /// </summary> /// <param name="metadata">The model metadata.</param> /// <returns>true if the model is required, false otherwise.</returns> public static bool IsRequired(this ModelMetadata metadata) { return(metadata.IsRequired | !FWReflectionHelper.AllowsNullValue(metadata.ModelType)); }