Ejemplo n.º 1
0
        protected virtual IHtmlContent GenerateCheckBox(
            ModelExplorer modelExplorer,
            string expression,
            bool?isChecked,
            object htmlAttributes)
        {
            var checkbox = _htmlGenerator.GenerateCheckBox(
                ViewContext,
                modelExplorer,
                expression,
                isChecked,
                htmlAttributes);

            var hiddenForCheckboxTag = _htmlGenerator.GenerateHiddenForCheckbox(ViewContext, modelExplorer, expression);

            if (checkbox == null || hiddenForCheckboxTag == null)
            {
                return(HtmlString.Empty);
            }

            var checkboxContent = new HtmlContentBuilder().AppendHtml(checkbox);

            if (ViewContext.FormContext.CanRenderAtEndOfForm)
            {
                ViewContext.FormContext.EndOfFormContent.Add(hiddenForCheckboxTag);
            }
            else
            {
                checkboxContent.AppendHtml(hiddenForCheckboxTag);
            }

            return(checkboxContent);
        }
Ejemplo n.º 2
0
        private void WriteCheckBoxInLabel(TextWriter writer, ModelExplorer modelExplorer, string name)
        {
            bool IsSubItem = AttributeHelper.IsAttributeDefined <SubItemAttribute>(
                modelExplorer.Metadata.ContainerType,
                modelExplorer.Metadata.PropertyName);
            var labelBuilder = GenerateLabel(IsSubItem);

            var checkboxBuilder = _htmlGenerator.GenerateCheckBox(
                ViewContext,
                modelExplorer,
                name,
                isChecked: Equals(For.Model, true),
                htmlAttributes: null);

            var htmlBuilder = new HtmlContentBuilder();

            htmlBuilder.AppendHtml(labelBuilder.RenderStartTag());
            htmlBuilder.AppendHtml(checkboxBuilder.RenderStartTag());
            htmlBuilder.AppendHtml(labelBuilder.InnerHtml);
            if (!string.IsNullOrEmpty(For.Metadata.Description))
            {
                htmlBuilder.AppendHtml(InformationSpan.FormatSwedish(For.Metadata.Description));
            }
            if (!string.IsNullOrEmpty(HelpLink))
            {
                htmlBuilder.AppendHtml(HelpAnchor.FormatSwedish(HelpLink));
            }
            htmlBuilder.AppendHtml(labelBuilder.RenderEndTag());
            WritePrefix(writer, PrefixAttribute.Position.Value);
            htmlBuilder.WriteTo(writer, _htmlEncoder);
        }
Ejemplo n.º 3
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            context.ThrowIfNull();
            output.ThrowIfNull();

            var selectListItems = new List <SelectListItem>();
            var kvList          = For?.Metadata.EnumGroupedDisplayNamesAndValues;

            if (kvList != null)
            {
                selectListItems = For.Metadata.EnumGroupedDisplayNamesAndValues.Select(a => new SelectListItem {
                    Text = a.Key.Name.ToString(), Value = a.Value.ToString(), Selected = a.Value == ((int)((For.Model) ?? -1)).ToString()
                }).ToList();
            }

            if (kvList == null && Data != null)
            {
                selectListItems = (List <SelectListItem>)Data;
            }

            var idStr = TagBuilder.CreateSanitizedId(For.Name,
                                                     _generator.IdAttributeDotReplacement);

            var tagBuilder = new StringBuilder(32);

            selectListItems.ForEach(item =>
            {
                var attributes = new Dictionary <string, object>
                {
                    { "title", item.Text },
                    { "lay-skin", "primary" }
                };
                var inputTagBuilder = _generator.GenerateCheckBox(ViewContext,
                                                                  For.ModelExplorer,
                                                                  For.Name,
                                                                  item.Value == (string)For.ModelExplorer.Model,
                                                                  attributes);
                tagBuilder.Append(inputTagBuilder.GetString());
            });

            var outpuHtml = $@"
              <label class='layui-form-label'>{Text ?? For.Metadata.DisplayName}</label>
            <div id='{idStr}_checkGroup' class='layui-input-block'>
              {tagBuilder}
            </div>";

            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.Add("class", InputDisplay.GetDisplayName());
            output.Attributes.Add("id", $"{idStr}_div");
            output.Content.SetHtmlContent(outpuHtml);
            if (kvList == null && !Url.IsNullOrEmpty())
            {
                output.PostElement.SetHtmlContent($@"<script type='text/javascript'>
                 $(function () {{
                    $('#{idStr}_checkGroup').bindCheckBoxData('{Url}','{For.Model}','{For.Name}');
                    }});
                </script>");
            }
        }
        private void WriteInput(TextWriter writer)
        {
            var tagBuilder = htmlGenerator.GenerateCheckBox(
                ViewContext,
                For.ModelExplorer,
                For.Name,
                isChecked: false,
                htmlAttributes: new { @class = $"{Class}" });

            tagBuilder.WriteTo(writer, htmlEncoder);
        }
        /// <summary>
        /// Generates the CheckBox.
        /// </summary>
        /// <param name="modelExplorer">The model explorer.</param>
        /// <param name="output">The output.</param>
        protected void GenerateCheckBox(ModelExplorer modelExplorer, TagHelperOutput output, ModelExpression forAttribute)
        {
            var htmlAttributes = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            for (var i = 0; i < output.Attributes.Count; i++)
            {
                var attribute = output.Attributes[i];
                if (!htmlAttributes.ContainsKey(attribute.Name))
                {
                    htmlAttributes.Add(attribute.Name, attribute.Value);
                }
            }

            if (!htmlAttributes.ContainsKey("disabled"))
            {
                htmlAttributes.Add("disabled", "disabled");
            }

            var checkBoxTag = generator.GenerateCheckBox(
                ViewContext,
                modelExplorer,
                forAttribute.Name, null, htmlAttributes);

            if (checkBoxTag != null)
            {
                //output.Attributes.Clear();
                //output.TagName = null;

                //var renderingMode =
                //	output.TagMode == TagMode.SelfClosing ? TagRenderMode.SelfClosing : TagRenderMode.StartTag;
                checkBoxTag.TagRenderMode = TagRenderMode.SelfClosing;
                output.Content.SetHtmlContent(checkBoxTag);

                //var hiddenForCheckboxTag = generator.GenerateHiddenForCheckbox(ViewContext, modelExplorer, forAttribute.Name);
                //if(hiddenForCheckboxTag != null) {
                //	hiddenForCheckboxTag.TagRenderMode = TagRenderMode.SelfClosing;

                //	if(ViewContext.FormContext.CanRenderAtEndOfForm) {
                //		ViewContext.FormContext.EndOfFormContent.Add(hiddenForCheckboxTag);
                //	} else {
                //		output.Content.SetHtmlContent(hiddenForCheckboxTag);
                //	}
                //}
            }
        }
Ejemplo n.º 6
0
        protected virtual HtmlString GenerateCheckBox(ModelMetadata metadata, string name, bool?isChecked,
                                                      object htmlAttributes)
        {
            var checkbox = _htmlGenerator.GenerateCheckBox(
                ViewContext,
                metadata,
                name,
                isChecked,
                htmlAttributes);
            var hidden = _htmlGenerator.GenerateHiddenForCheckbox(ViewContext, metadata, name);

            if (checkbox == null || hidden == null)
            {
                return(HtmlString.Empty);
            }

            var elements = checkbox.ToString(TagRenderMode.SelfClosing) + hidden.ToString(TagRenderMode.SelfClosing);

            return(new HtmlString(elements));
        }
Ejemplo n.º 7
0
        protected virtual IHtmlContent GenerateCheckBox(
            ModelExplorer modelExplorer,
            string expression,
            bool?isChecked,
            object htmlAttributes)
        {
            var checkbox = _htmlGenerator.GenerateCheckBox(
                ViewContext,
                modelExplorer,
                expression,
                isChecked,
                htmlAttributes);

            var hidden = _htmlGenerator.GenerateHiddenForCheckbox(ViewContext, modelExplorer, expression);

            if (checkbox == null || hidden == null)
            {
                return(HtmlString.Empty);
            }

            return(new BufferedHtmlContent().Append(checkbox).Append(hidden));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 访问并呈现当前标签实例。
        /// </summary>
        /// <param name="context">当前HTML标签上下文,包含当前HTML相关信息。</param>
        /// <param name="output">当前标签输出实例,用于呈现标签相关信息。</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var classes = new List <string> {
                "onoffswitch"
            };

            if (IsBlank)
            {
                classes.Add("onoffswitch-blank");
            }
            output.TagName = "div";
            output.MergeClassNames(context, classes.ToArray());
            if (For != null)
            {
                output.Content.AppendHtml(_generator.GenerateCheckBox(ViewContext, For.ModelExplorer, For.Name, null,
                                                                      new { @class = "onoffswitch-checkbox" }));
                output.Content.AppendHtml(
                    $"<label class=\"onoffswitch-label\" for=\"{For.Name}\"><span class=\"onoffswitch-inner\"></span><span class=\"onoffswitch-switch\"></span></label>");
            }
            else
            {
                var builder = new TagBuilder("input");
                builder.MergeAttribute("type", "checkbox");
                if (Checked)
                {
                    builder.MergeAttribute("checked", "checked");
                }
                builder.AddCssClass("onoffswitch-checkbox");
                builder.MergeAttribute("name", Name);
                builder.MergeAttribute("id", Name);
                builder.MergeAttribute("value", "true");
                output.Content.AppendHtml(builder);
                output.Content.AppendHtml(
                    $"<label class=\"onoffswitch-label\" for=\"{Name}\"><span class=\"onoffswitch-inner\"></span><span class=\"onoffswitch-switch\"></span></label>");
            }
        }
Ejemplo n.º 9
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var text       = output.Attributes.SingleOrDefault(a => a.Name == "text");
            var isDisabled = output.Attributes.SingleOrDefault(a => a.Name == "disabled") != null;
            var isReadOnly = output.Attributes.SingleOrDefault(a => a.Name == "readonly") != null;
            var isRequired = output.Attributes.SingleOrDefault(a => a.Name == "required") != null;

            output.Attributes.RemoveAll("disabled");
            output.Attributes.RemoveAll("readonly");
            output.Attributes.RemoveAll("required");

            var labelAttributes = new Dictionary <string, object>
            {
                { "class", "pmd-checkbox checkbox-pmd-ripple-effect" }
            };

            var checkboxAttributes = new Dictionary <string, object>
            {
                { "type", "checkbox" }
            };

            if (isDisabled)
            {
                checkboxAttributes.Add("disabled", "disabled");
            }
            if (isReadOnly)
            {
                checkboxAttributes.Add("readonly", "readonly");
            }
            if (isRequired)
            {
                checkboxAttributes.Add("required", "required");
            }

            var inputPre  = "";
            var inputPost = "";

            var value = For.ModelExplorer.Model == null ? false : (bool)For.ModelExplorer.Model;

            var labelText = text == null ? For.Metadata.DisplayName : text.Value.ToString();

            var input  = _generator.GenerateCheckBox(ViewContext, For.ModelExplorer, For.Name, value, checkboxAttributes);
            var label  = _generator.GenerateLabel(ViewContext, For.ModelExplorer, For.Name, "", labelAttributes);
            var hidden = _generator.GenerateHiddenForCheckbox(ViewContext, For.ModelExplorer, For.Name);

            // Strip end tag from label
            var labelStart = "";

            using (var writer = new StringWriter())
            {
                label.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                labelStart = writer.ToString();
                labelStart = labelStart.Replace("</label>", "");
            }

            inputPre  = $"{labelStart}{inputPre}";
            inputPost = $"{inputPost}<span class=\"pmd-checkbox-label\">&nbsp;</span><span class=\"pmd-checkbox\">{labelText}</span></label>";

            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.SetAttribute("class", "checkbox pmd-default-theme");

            string textboxOutput;

            using (var writer = new StringWriter())
            {
                writer.Write(inputPre);
                input.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                hidden.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                writer.Write(inputPost);
                textboxOutput = writer.ToString();
            }

            output.Content.SetHtmlContent(textboxOutput);
        }
Ejemplo n.º 10
0
 public TagBuilder GenerateCheckBox(ViewContext viewContext, ModelExplorer modelExplorer, string expression, bool?isChecked, object htmlAttributes)
 {
     return(_htmlGenerator.GenerateCheckBox(viewContext, modelExplorer, expression, isChecked, htmlAttributes));
 }