예제 #1
0
        protected override void WriteSelf(System.IO.TextWriter writer)
        {
            var controlContext = GetNearestParent <IControlContext>();

            ITagBuilder div = null;

            if (!Inline)
            {
                div = Helper.CreateTagBuilder("div");
                div.AddCssClass("checkbox");
                div.WriteStartTag(writer);
            }

            var lbl = Helper.CreateTagBuilder("label");

            if (Inline)
            {
                lbl.AddCssClass("checkbox-inline");
            }
            lbl.WriteStartTag(writer);

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

            input.MergeAttribute("type", "checkbox", true);
            if (controlContext != null)
            {
                input.MergeAttribute("id", controlContext.FieldName, true);
                input.MergeAttribute("name", controlContext.FieldName, true);
                input.MergeAttribute("value", "true", true);
                var controlValue = controlContext.FieldValue;
                if (controlValue != null && bool.Parse(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 checkbox itself

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

            lbl.WriteEndTag(writer);

            if (div != null)
            {
                div.WriteEndTag(writer);
            }
        }
예제 #2
0
        protected override void WriteSelf(System.IO.TextWriter writer)
        {
            var bootstrap4Mode = false;

#if BOOTSTRAP4
            bootstrap4Mode = true;
#endif

            var controlContext = GetNearestParent <IControlContext>();

            ITagBuilder div = null;
            ITagBuilder lbl = null;

            if (bootstrap4Mode)
            {
                div = Helper.CreateTagBuilder("div");
                div.AddCssClass("form-check");
                if (Inline)
                {
                    div.AddCssClass("form-check-inline");
                }

                if (controlContext != null)
                {
                    if (controlContext.HasErrors)
                    {
                        div.AddCssClass("has-danger");
                    }

                    else if (controlContext.HasWarning)
                    {
                        div.AddCssClass("has-warning");
                    }
                }

                lbl = Helper.CreateTagBuilder("label");
                lbl.AddCssClass("form-check-label");
            }
            else
            {
                if (!Inline)
                {
                    div = Helper.CreateTagBuilder("div");
                    div.AddCssClass("radio");
                }

                lbl = Helper.CreateTagBuilder("label");
                if (Inline)
                {
                    lbl.AddCssClass("radio-inline");
                }
            }

            div?.WriteStartTag(writer);
            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 ?? controlContext?.DisplayName));

            lbl?.WriteEndTag(writer);
            div?.WriteEndTag(writer);
        }
예제 #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 input = Helper.CreateTagBuilder("input");

#if BOOTSTRAP3
            input.AddCssClass("form-control");
#endif
#if BOOTSTRAP4
            if (Type == InputType.File)
            {
                input.AddCssClass("form-control-file");
            }
            else
            {
                input.AddCssClass("form-control");
            }

            if (controlContext != null)
            {
                if (controlContext.HasErrors)
                {
                    input.AddCssClass("form-control-danger");
                }
                else if (controlContext.HasWarning)
                {
                    input.AddCssClass("form-control-warning");
                }
            }
#endif
            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 (actualType == InputType.Datetime)
                    {
                        actualType = InputType.DatetimeLocal;
                    }

                    if (actualType == InputType.Date || actualType == InputType.DatetimeLocal || actualType == 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", CultureInfo.InvariantCulture)
                                        : valueDateTime.Value.ToString(TextDateInputModeDateFormat);
                            }
                            break;

                        case InputType.DatetimeLocal:
                            if (valueDateTime.HasValue)
                            {
                                valueString = asHtml5
                                        ? valueDateTime.Value.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture)
                                        : valueDateTime.Value.ToString(TextDateInputModeDateTimeLocalFormat);
                            }
                            break;

                        case InputType.Time:
                            if (valueTimeSpan.HasValue)
                            {
                                valueString = DateTime.MinValue.Add(valueTimeSpan.Value).ToString("HH:mm:ss", CultureInfo.InvariantCulture);
                            }
                            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);
            }
        }
        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);
            }
        }
예제 #5
0
        protected override void WriteSelf(System.IO.TextWriter writer, IBootstrapContext context)
        {
            var formGroup = context.PeekNearest <FormGroup>();

            if (formGroup != null && ControlContextValue == null)
            {
                ControlContextValue = formGroup.ControlContextValue;
            }

            ITagBuilder div = null;

            if (!InlineValue)
            {
                div = context.CreateTagBuilder("div");
                div.AddCssClass("checkbox");
                div.WriteStartTag(writer);
            }

            var lbl = context.CreateTagBuilder("label");

            if (InlineValue)
            {
                lbl.AddCssClass("checkbox-inline");
            }
            lbl.WriteStartTag(writer);

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

            input.MergeAttribute("type", "checkbox");
            if (ControlContextValue != null)
            {
                input.MergeAttribute("id", ControlContextValue.Name);
                input.MergeAttribute("name", ControlContextValue.Name);
                input.MergeAttribute("value", "true");
                var controlValue = ControlContextValue.Value;
                if (controlValue != null && bool.Parse(controlValue.ToString()))
                {
                    input.MergeAttribute("checked", "checked");
                }
            }
            if (DisabledValue)
            {
                input.MergeAttribute("disabled", "disabled");
            }

            ApplyCss(input);
            ApplyAttributes(input);

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

            input.WriteFullTag(writer);

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

            writer.Write(context.HtmlEncode(TextValue));

            lbl.WriteEndTag(writer);

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