Ejemplo n.º 1
0
 protected void ApplyCss(ITagBuilder tag)
 {
     if (additionalClasses == null)
     {
         return;
     }
     foreach (var value in additionalClasses)
     {
         if (!string.IsNullOrWhiteSpace(value))
         {
             tag.AddCssClass(value);
         }
     }
 }
Ejemplo n.º 2
0
        protected override void WriteSelfStart(System.IO.TextWriter writer)
        {
            var formContext      = GetNearestParent <IForm>();
            var formGroupContext = GetNearestParent <FormGroup>();
            var controlContext   = GetNearestParent <IControlContext>();

            ITagBuilder div = null;

            if (!Size.IsEmpty())
            {
                // Inline forms does not support sized controls (we need 'some other' sizing rules?)
                if (formContext != null && formContext.Type != FormType.Inline)
                {
                    if (formGroupContext != null && formGroupContext.WithSizedControl)
                    {
                        div = Helper.CreateTagBuilder("div");
                        div.AddCssClass(Size.ToCssClass());
                        div.WriteStartTag(writer);
                    }
                    else
                    {
                        throw new InvalidOperationException("Size not allowed - call WithSizedControls() on FormGroup.");
                    }
                }
            }

            object value = null;

            var tb = Helper.CreateTagBuilder("select");

            tb.AddCssClass("form-control");

            if (controlContext != null)
            {
                tb.MergeAttribute("id", controlContext.FieldName, true);
                tb.MergeAttribute("name", controlContext.FieldName, true);
                if (controlContext.IsRequired)
                {
                    tb.MergeAttribute("required", "required", true);
                }
                value = controlContext.FieldValue;
            }
            if (Disabled)
            {
                tb.MergeAttribute("disabled", "disabled", true);
            }

            ApplyCss(tb);
            ApplyAttributes(tb);

            tb.WriteStartTag(writer);

            if (Items != null)
            {
                foreach (var item in Items)
                {
                    item.Parent = this;
                    item.WriteTo(writer);
                }
            }

            endTag = tb.GetEndTag() + (div == null ? string.Empty : div.GetEndTag());
        }
Ejemplo n.º 3
0
        protected override void WriteSelf(System.IO.TextWriter writer)
        {
            var formContext      = GetNearestParent <IForm>();
            var formGroupContext = GetNearestParent <FormGroup>();
            var controlContext   = GetNearestParent <IControlContext>();

            ITagBuilder div = null;

            if (!Size.IsEmpty())
            {
                // Inline forms does not support sized controls (we need 'some other' sizing rules?)
                if (formContext != null && formContext.Type != FormType.Inline)
                {
                    if (formGroupContext != null && formGroupContext.WithSizedControl)
                    {
                        div = Helper.CreateTagBuilder("div");
                        div.AddCssClass(Size.ToCssClass());
                        div.WriteStartTag(writer);
                    }
                    else
                    {
                        throw new InvalidOperationException("Size not allowed - call WithSizedControls() on FormGroup.");
                    }
                }
            }

            var tb = Helper.CreateTagBuilder("textarea");

            tb.AddCssClass("form-control");
            if (Rows != 0)
            {
                tb.MergeAttribute("rows", Rows.ToString(CultureInfo.InvariantCulture), true);
            }
            if (controlContext != null)
            {
                tb.MergeAttribute("id", controlContext.FieldName, true);
                tb.MergeAttribute("name", controlContext.FieldName, true);
                if (controlContext.IsRequired)
                {
                    tb.MergeAttribute("required", "required", true);
                }
            }
            if (Disabled)
            {
                tb.MergeAttribute("disabled", "disabled", true);
            }

            ApplyCss(tb);
            ApplyAttributes(tb);

            tb.WriteStartTag(writer);

            if (controlContext != null && controlContext.FieldValue != null)
            {
                writer.Write(Helper.HtmlEncode(controlContext.FieldValue.ToString()));
            }

            tb.WriteEndTag(writer);

            if (div != null)
            {
                div.WriteEndTag(writer);
            }
        }
Ejemplo n.º 4
0
        protected override void WriteSelf(System.IO.TextWriter writer)
        {
            var bootstrap4Mode = false;

#if BOOTSTRAP4
            bootstrap4Mode = true;
#endif
            var controlContext = GetNearestParent <IControlContext>();

            ITagBuilder div = null;
            if (!Inline)
            {
                div = Helper.CreateTagBuilder("div");
                div.AddCssClass(bootstrap4Mode ? "form-check" : "radio");

                if (bootstrap4Mode && controlContext != null)
                {
                    if (controlContext.HasErrors)
                    {
                        div.AddCssClass("has-danger checkbox");
                    }
                    else if (controlContext.HasWarning)
                    {
                        div.AddCssClass("has-warning checkbox");
                    }
                }

                div.WriteStartTag(writer);
            }

            var lbl = Helper.CreateTagBuilder("label");
            if (Inline)
            {
                lbl.AddCssClass(bootstrap4Mode ? "form-check-inline" : "radio-inline");
            }
            else
            {
                if (bootstrap4Mode)
                {
                    lbl.AddCssClass("form-check-label");
                }
            }
            lbl.WriteStartTag(writer);

            var input = Helper.CreateTagBuilder("input");
            input.MergeAttribute("type", "radio", true);
            if (bootstrap4Mode)
            {
                input.AddCssClass("form-check-input");
            }
            if (controlContext != null)
            {
                input.MergeAttribute("id", controlContext.FieldName, true);
                input.MergeAttribute("name", controlContext.FieldName, true);
                input.MergeAttribute("value", Value?.ToString(), true);
                var controlValue = controlContext.FieldValue;
                if (controlValue != null && Value != null && Value.ToString().Equals(controlValue.ToString()))
                {
                    input.MergeAttribute("checked", "checked", true);
                }
            }
            if (Disabled)
            {
                input.MergeAttribute("disabled", "disabled", true);
            }

            ApplyCss(input);
            ApplyAttributes(input);

            ////input.MergeAttributes(helper.HtmlHelper.GetUnobtrusiveValidationAttributes(context.ExpressionText, context.Metadata));

            input.WriteFullTag(writer);

            writer.Write(" "); // writing space to separate text from radio itself

            writer.Write(Helper.HtmlEncode(Text));

            lbl.WriteEndTag(writer);

            if (div != null)
            {
                div.WriteEndTag(writer);
            }
        }
Ejemplo n.º 5
0
        protected override void WriteSelf(System.IO.TextWriter writer)
        {
            var formContext      = GetNearestParent <IForm>();
            var formGroupContext = GetNearestParent <FormGroup>();
            var controlContext   = GetNearestParent <IControlContext>();

            ITagBuilder div = null;

            if (!Size.IsEmpty())
            {
                // Inline forms does not support sized controls (we need 'some other' sizing rules?)
                if (formContext != null && formContext.Type != FormType.Inline)
                {
                    if (formGroupContext != null && formGroupContext.WithSizedControl)
                    {
                        div = Helper.CreateTagBuilder("div");
                        div.AddCssClass(Size.ToCssClass());
                        div.WriteStartTag(writer);
                    }
                    else
                    {
                        throw new InvalidOperationException("Size not allowed - call WithSizedControls() on FormGroup.");
                    }
                }
            }

            var input = Helper.CreateTagBuilder("input");

            input.AddCssClass("form-control");
            var actualType = Type;

            if (actualType != InputType.File && controlContext != null)
            {
                input.MergeAttribute("id", controlContext.FieldName, true);
                input.MergeAttribute("name", controlContext.FieldName, true);
                if (controlContext.IsRequired)
                {
                    input.MergeAttribute("required", "required", true);
                }
                var value = controlContext.FieldValue;
                if (value != null)
                {
                    var valueString = value.ToString();
                    if (Type == InputType.Date || Type == InputType.Datetime || Type == InputType.DatetimeLocal || Type == InputType.Time)
                    {
                        var valueDateTime       = value as DateTime?;
                        var valueDateTimeOffset = value as DateTimeOffset?;
                        var valueTimeSpan       = value as TimeSpan?;
                        if (valueDateTimeOffset.HasValue)
                        {
                            valueDateTime = valueDateTimeOffset.Value.DateTime;
                        }
                        if (valueDateTime.HasValue)
                        {
                            valueTimeSpan = valueDateTime.Value.TimeOfDay;
                        }
                        var asHtml5 = (DateInputMode == BootstrapMvc.DateInputMode.Html5);
                        if (!asHtml5)
                        {
                            actualType = InputType.Text;
                        }
                        switch (Type)
                        {
                        case InputType.Date:
                            if (valueDateTime.HasValue)
                            {
                                valueString = asHtml5
                                        ? valueDateTime.Value.ToString("yyyy-MM-dd")
                                        : valueDateTime.Value.ToString("d");
                            }
                            break;

                        case InputType.DatetimeLocal:
                            if (valueDateTime.HasValue)
                            {
                                valueString = asHtml5
                                        ? valueDateTime.Value.ToString("o")
                                        : valueDateTime.Value.ToString();
                            }
                            break;

                        case InputType.Datetime:
                            if (valueDateTime.HasValue)
                            {
                                valueString = asHtml5
                                        ? valueDateTimeOffset.Value.ToString("o")
                                        : valueDateTimeOffset.Value.ToString();
                            }
                            break;

                        case InputType.Time:
                            if (valueDateTime.HasValue)
                            {
                                valueString = valueTimeSpan.ToString();
                            }
                            break;
                        }
                    }
                    input.MergeAttribute("value", valueString, true);
                }
            }

            if (actualType != InputType.Text)
            {
                input.MergeAttribute("type", actualType.ToType(), true);
            }
            if (Disabled)
            {
                input.MergeAttribute("disabled", "disabled", true);
            }

            ApplyCss(input);
            ApplyAttributes(input);

            ////input.MergeAttributes(helper.HtmlHelper.GetUnobtrusiveValidationAttributes(context.ExpressionText, context.Metadata));

            input.WriteFullTag(writer);

            if (div != null)
            {
                div.WriteEndTag(writer);
            }
        }