Ejemplo n.º 1
0
        private void TryBuildPrimitiveInput(System.Reflection.ParameterInfo parameter, object defaultValue, ref bool addValue, ref bool handled, out TagBuilder toAdd, out Type paramType)
        {
            toAdd = null;

            paramType = parameter.ParameterType;
            // if the parameter type is a primitive then use the appropriate html input
            // string - text
            if (paramType == typeof(int) || paramType == typeof(long))
            {
                toAdd   = NumberBuilder(parameter.Name);
                handled = true;
            }
            else if (paramType == typeof(string))
            {
                toAdd   = StringBuilder(parameter.Name);
                handled = true;
            }
            // DateTime - text with jQuery datepicker
            else if (paramType == typeof(DateTime))
            {
                addValue = defaultValue != null;
                toAdd    = DateTimeBuilder(parameter.Name)
                           .ValueIf(addValue, ((DateTime)defaultValue).ToShortDateString());

                handled = true;
            }// bool - checkbox
            else if (paramType == typeof(bool))
            {
                toAdd = BooleanBuilder(parameter.Name)
                        .CheckedIf(defaultValue != null && (bool)defaultValue);

                handled = true;
            }// enum - radio list
            else if (paramType.IsEnum)
            {
                toAdd    = RadioList.FromEnum(paramType, defaultValue);
                addValue = false;

                handled = true;
            }
        }
Ejemplo n.º 2
0
        internal protected void AppendInputsFor(Type paramType, object defaultValues, TagBuilder container, int recursionThusFar)
        {
            List <PropertyInfo> properties = new List <PropertyInfo>(paramType.GetProperties());

            properties.Sort((l, r) => l.MetadataToken.CompareTo(r.MetadataToken));
            string paramTypeName = paramType.Name;

            foreach (PropertyInfo property in properties)
            {
                if (!PropertyInclusionPredicate(property))
                {
                    continue;
                }

                LabelAttribute labelAttr = property.GetCustomAttributeOfType <LabelAttribute>();
                string         labelText = labelAttr ?? string.Format(this.LabelFormat, property.Name.PascalSplit(" "));
                string         id        = "{0}_{1}"._Format(paramTypeName, property.Name);

                TagBuilder label = new TagBuilder("label")
                                   .Html(labelText)
                                   .Attr("for", id)
                                   .Css(LabelCssClass);

                bool   addLabel        = this.AddLabels;
                bool   breakAfterLabel = (this.Layout == ParameterLayouts.BreakAfterLabels) || (labelAttr != null && labelAttr.PostBreak);
                object defaultValue    = null;

                if (defaultValues != null)
                {
                    defaultValue = GetDefaultValue(defaultValues, property, defaultValue);
                }

                bool addValue = true;

                Type propType = property.PropertyType;

                bool isArray = propType.IsArray;
                if (isArray)
                {
                    propType = propType.GetElementType();
                }
                else if (propType.IsGenericType &&
                         propType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propType = propType.GetGenericArguments()[0];
                }

                StringInput attr;
                bool        isHidden  = false;
                bool        wasObject = false;
                bool        typeAdded = false;

                TagBuilder toAdd = null;

                if (property.HasCustomAttributeOfType <StringInput>(true, out attr))
                {
                    attr.Default      = attr.Default == null ? defaultValue : attr.Default;
                    attr.PropertyName = property.Name;
                    toAdd             = attr.CreateInput().DataSetIf(propType.IsEnum, "type", "string");
                    typeAdded         = propType.IsEnum;
                    addLabel          = attr.AddLabel.HasValue ? attr.AddLabel.Value : addLabel;
                    isHidden          = attr.IsHidden.HasValue ? attr.IsHidden.Value : isHidden;
                    breakAfterLabel   = attr.BreakAfterLabel.HasValue ? attr.BreakAfterLabel.Value : breakAfterLabel;
                    addValue          = attr.AddValue.HasValue ? attr.AddValue.Value : addValue;
                }
                else if (propType == typeof(int) || propType == typeof(long))
                {
                    toAdd = NumberBuilder(property.Name);
                }
                else if (propType == typeof(string))
                {
                    toAdd = StringBuilder(property.Name);
                }
                else if (propType == typeof(DateTime))
                {
                    DateTime defaultDate = defaultValue == null ? DateTime.MinValue : (DateTime)defaultValue;
                    addValue = defaultDate > DateTime.MinValue;
                    toAdd    = DateTimeBuilder(property.Name)
                               .ValueIf(addValue, defaultDate.ToShortDateString());

                    addValue = false;
                }
                else if (propType == typeof(bool))
                {
                    toAdd = BooleanBuilder(property.Name)
                            .CheckedIf(defaultValue != null && (bool)defaultValue);
                }
                else if (propType.IsEnum)
                {
                    toAdd = RadioList.FromEnum(propType, defaultValue)
                            .DataSet("type", "enum");
                    typeAdded = true;
                    addValue  = false;
                    wasObject = true; // prevent new br
                }
                else if (recursionThusFar < RecursionLimit)
                {
                    toAdd     = FieldsetFor(propType, defaultValue, property.Name, ++recursionThusFar);
                    addLabel  = false;
                    wasObject = true;
                    typeAdded = true;
                }

                if (toAdd != null)
                {
                    toAdd.Id(id)
                    .Attr("name", property.Name)
                    .Attr("itemprop", property.Name)     // schema.org
                    .DataSetIf(!typeAdded, "type", GetDataSetType(propType))
                    .ValueIf(addValue && defaultValue != null, (defaultValue as string))
                    .DataSetIf(isArray, "array", "true");

                    container
                    .BrIf(labelAttr != null && labelAttr.PreBreak && addLabel)
                    .ChildIf(addLabel, label)
                    .BrIf(addLabel && breakAfterLabel)
                    .Child(toAdd, toAdd.TagName.ToLowerInvariant().Equals("input") ? TagRenderMode.SelfClosing: TagRenderMode.Normal)
                    .BrIf(!isHidden && !wasObject &&
                          (
                              this.Layout == ParameterLayouts.Default ||
                              this.Layout == ParameterLayouts.BreakAfterLabels
                          )
                          );
                }
            }
        }
Ejemplo n.º 3
0
        internal protected void AppendInputsForDynamic(dynamic target, TagBuilder container, bool setValues, int recursionThusFar)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(target);//new List<PropertyDescriptor>(target.GetProperties());
            string paramTypeName = target.Name;

            foreach (PropertyDescriptor property in properties)
            {
                //LabelAttribute labelAttr = property.GetCustomAttributeOfType<LabelAttribute>();
                string labelText = string.Format(this.LabelFormat, property.Name.PascalSplit(" "));
                string id        = "{0}_{1}"._Format(paramTypeName, property.Name);

                TagBuilder label = new TagBuilder("label")
                                   .Html(labelText)
                                   .Attr("for", id)
                                   .Css(LabelCssClass);

                bool   addLabel        = this.AddLabels;
                bool   breakAfterLabel = this.Layout == ParameterLayouts.BreakAfterLabels;
                object defaultValue    = property.GetValue(target);

                bool addValue = true;

                Type propType = property.PropertyType;

                bool isArray = propType.IsArray;
                if (isArray)
                {
                    propType = propType.GetElementType();
                }
                else if (propType.IsGenericType &&
                         propType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propType = propType.GetGenericArguments()[0];
                }

                bool isHidden  = false;
                bool wasObject = false;
                bool typeAdded = false;

                TagBuilder toAdd = null;

                if (propType == typeof(int) || propType == typeof(long))
                {
                    toAdd = NumberBuilder(property.Name);
                }
                else if (propType == typeof(string))
                {
                    toAdd = StringBuilder(property.Name);
                }
                else if (propType == typeof(DateTime))
                {
                    DateTime defaultDate = defaultValue == null ? DateTime.MinValue : (DateTime)defaultValue;
                    addValue = defaultDate > DateTime.MinValue;
                    toAdd    = DateTimeBuilder(property.Name)
                               .ValueIf(addValue, defaultDate.ToShortDateString());

                    addValue = false;
                }
                else if (propType == typeof(bool))
                {
                    toAdd = BooleanBuilder(property.Name);
                    if (defaultValue != null)
                    {
                        bool bVal;
                        Boolean.TryParse(defaultValue.ToString(), out bVal);
                        toAdd.CheckedIf(defaultValue != null && bVal);
                    }
                }
                else if (propType.IsEnum)
                {
                    toAdd = RadioList.FromEnum(propType, defaultValue)
                            .DataSet("type", "enum");
                    typeAdded = true;
                    addValue  = false;
                    wasObject = true; // prevent new br
                }
                else if (recursionThusFar < RecursionLimit)
                {
                    toAdd     = FieldsetFor(propType, defaultValue, property.Name, ++recursionThusFar);
                    addLabel  = false;
                    wasObject = true;
                    typeAdded = true;
                }

                if (toAdd != null)
                {
                    toAdd.Id(id)
                    .Attr("name", property.Name)
                    .Attr("itemprop", property.Name)     // schema.org
                    .DataSetIf(!typeAdded, "type", GetDataSetType(propType))
                    .ValueIf(addValue && defaultValue != null, (defaultValue as string))
                    .DataSetIf(isArray, "array", "true");

                    container
                    .ChildIf(addLabel, label)
                    .BrIf(addLabel && breakAfterLabel)
                    .Child(toAdd, toAdd.TagName.ToLowerInvariant().Equals("input") ? TagRenderMode.SelfClosing : TagRenderMode.Normal)
                    .BrIf(!isHidden && !wasObject &&
                          (
                              this.Layout == ParameterLayouts.Default ||
                              this.Layout == ParameterLayouts.BreakAfterLabels
                          )
                          );
                }
            }
        }