Ejemplo n.º 1
0
        /// <summary>
        /// Creates an an individual checkbox
        /// </summary>
        /// <param name="sb">String builder of checkbox list</param>
        /// <param name="modelMetadata">Model Metadata</param>
        /// <param name="htmlWrapper">MVC Html helper class that is being extended</param>
        /// <param name="htmlAttributesForCheckBox">Each checkbox HTML tag attributes (e.g. 'new { class="somename" }')</param>
        /// <param name="selectedValues">List of strings of selected values</param>
        /// <param name="disabledValues">List of strings of disabled values</param>
        /// <param name="name">Name of the checkbox list (same for all checkboxes)</param>
        /// <param name="itemValue">Value of the checkbox</param>
        /// <param name="itemText">Text to be displayed next to checkbox</param>
        /// <param name="htmlHelper">HtmlHelper passed from view model</param>
        /// <param name="textLayout">Sets layout of a checkbox for right-to-left languages</param>
        /// <returns>String builder of checkbox list</returns>
        private static StringBuilder CreateCheckBoxListElement(StringBuilder sb, HtmlHelper htmlHelper, ModelMetadata modelMetadata, HtmlWrapperInfo htmlWrapper, object htmlAttributesForCheckBox, IEnumerable <string> selectedValues, IEnumerable <string> disabledValues, string name, string itemValue, string itemText, TextLayout textLayout)
        {
            // get full name from view model
            var fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            // create checkbox tag
            var checkbox_builder = new TagBuilder("input");

            if (selectedValues.Any(x => x == itemValue))
            {
                checkbox_builder.MergeAttribute("checked", "checked");
            }
            checkbox_builder.MergeAttributes(htmlAttributesForCheckBox.ToDictionary());
            checkbox_builder.MergeAttribute("type", "checkbox");
            checkbox_builder.MergeAttribute("value", itemValue);
            checkbox_builder.MergeAttribute("name", fullName);

            // create linked label tag
            var link_name = name + linkedLabelCount++;

            checkbox_builder.GenerateId(link_name);
            var linked_label_builder = new TagBuilder("label");

            linked_label_builder.MergeAttribute("for", link_name.Replace(".", "_"));
            linked_label_builder.MergeAttributes(htmlAttributesForCheckBox.ToDictionary());
            linked_label_builder.InnerHtml = itemText;

            // if there are any errors for a named field, we add the css attribute
            ModelState modelState;

            if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    checkbox_builder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
                }
            }
            checkbox_builder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, modelMetadata));

            // open checkbox tag wrapper
            if (textLayout == TextLayout.RightToLeft)
            {
                // then set style for displaying checkbox for right-to-left languages
                var defaultSectionStyle = "style=\"text-align: right;\"";
                sb.Append(htmlWrapper.WrapElement != HtmlElementTag.None
                            ? "<" + htmlWrapper.WrapElement + " " + defaultSectionStyle + ">"
                            : "");
            }
            else
            {
                sb.Append(htmlWrapper.WrapElement != HtmlElementTag.None
                            ? "<" + htmlWrapper.WrapElement + ">"
                            : "");
            }

            // build hidden tag for disabled checkbox (so the value will post)
            if (disabledValues != null && disabledValues.ToList().Any(x => x == itemValue))
            {
                // set main checkbox to be disabled
                checkbox_builder.MergeAttribute("disabled", "disabled");

                // create a hidden input with checkbox value
                // so it can be posted if checked
                if (selectedValues.Any(x => x == itemValue))
                {
                    var hidden_input_builder = new TagBuilder("input");
                    hidden_input_builder.MergeAttribute("type", "hidden");
                    hidden_input_builder.MergeAttribute("value", itemValue);
                    hidden_input_builder.MergeAttribute("name", name);
                    sb.Append(hidden_input_builder.ToString(TagRenderMode.Normal));
                }
            }

            // create checkbox and tag combination
            if (textLayout == TextLayout.RightToLeft)
            {
                // then display checkbox for right-to-left languages
                sb.Append(linked_label_builder.ToString(TagRenderMode.Normal));
                sb.Append(checkbox_builder.ToString(TagRenderMode.Normal));
            }
            else
            {
                sb.Append(checkbox_builder.ToString(TagRenderMode.Normal));
                sb.Append(linked_label_builder.ToString(TagRenderMode.Normal));
            }

            // close checkbox tag wrapper
            sb.Append(htmlWrapper.WrapElement != HtmlElementTag.None
                        ? "</" + htmlWrapper.WrapElement + ">"
                        : "");

            // add element ending
            sb.Append(htmlWrapper.AppendToElement);

            // add table column break, if applicable
            htmlwrapRowbreakCount += 1;
            if (htmlwrapRowbreakCount == htmlWrapper.SeparatorMaxCount)
            {
                sb.Append(htmlWrapper.WrapRowbreak);
                htmlwrapRowbreakCount = 0;
            }

            // return string builder with checkbox html markup
            return(sb);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates an HTML wrapper for the checkbox list
        /// </summary>
        /// <param name="htmlListInfo">Settings for HTML wrapper of the list (e.g. 'new HtmlListInfo2(HtmlTag2.vertical_columns, 2, new { style="color:green;" })')</param>
        /// <param name="numberOfItems">Count of all items in the list</param>
        /// <param name="position">Direction of the list (e.g. 'Position2.Horizontal' or 'Position2.Vertical')</param>
        /// <param name="textLayout">Sets layout of a checkbox for right-to-left languages</param>
        /// <returns>HTML wrapper information</returns>
        private static HtmlWrapperInfo CreateHtmlWrapper(HtmlListInfo htmlListInfo, int numberOfItems, Position position, TextLayout textLayout)
        {
            var htmlWrapInfo = new HtmlWrapperInfo();

            if (htmlListInfo != null)
            {
                // creating custom layouts
                switch (htmlListInfo.HtmlTag)
                {
                // creates user selected number of float sections with
                // vertically sorted checkboxes
                case HtmlTag.VerticalColumns:
                {
                    if (htmlListInfo.Columns <= 0)
                    {
                        htmlListInfo.Columns = 1;
                    }
                    // calculate number of rows
                    var rows = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(numberOfItems)
                                                            / Convert.ToDecimal(htmlListInfo.Columns)));
                    if (numberOfItems <= 4 &&
                        (numberOfItems <= htmlListInfo.Columns || numberOfItems - htmlListInfo.Columns == 1))
                    {
                        rows = numberOfItems;
                    }
                    htmlWrapInfo.SeparatorMaxCount = rows;

                    // create wrapped raw html tag
                    var wrapRow              = HtmlElementTag.Div;
                    var wrapHtml_builder     = new TagBuilder(wrapRow.ToString());
                    var user_html_attributes = htmlListInfo.HtmlAttributes.ToDictionary();

                    // create raw style and merge it with user provided style (if applicable)
                    var defaultSectionStyle = "float:left;";     // margin-right:30px; line-height:25px;
                    if (textLayout == TextLayout.RightToLeft)
                    {
                        defaultSectionStyle += " text-align: right;";
                    }

                    object style;
                    user_html_attributes.TryGetValue("style", out style);
                    if (style != null)     // if user style is set, use it
                    {
                        wrapHtml_builder.MergeAttribute("style", defaultSectionStyle + " " + style);
                    }
                    else     // if not set, add only default style
                    {
                        wrapHtml_builder.MergeAttribute("style", defaultSectionStyle);
                    }

                    // merge it with other user provided attributes (e.g.: class)
                    user_html_attributes.Remove("style");
                    wrapHtml_builder.MergeAttributes(user_html_attributes);

                    // build wrapped raw html tag
                    htmlWrapInfo.WrapOpen     = wrapHtml_builder.ToString(TagRenderMode.StartTag);
                    htmlWrapInfo.WrapRowbreak = "</" + wrapRow + "> " +
                                                wrapHtml_builder.ToString(TagRenderMode.StartTag);
                    htmlWrapInfo.WrapClose = wrapHtml_builder.ToString(TagRenderMode.EndTag) +
                                             " <div style=\"clear:both;\"></div>";
                    htmlWrapInfo.AppendToElement = "<br/>";
                }
                break;

                // creates an html <table> with checkboxes sorted horizontally
                case HtmlTag.Table:
                {
                    if (htmlListInfo.Columns <= 0)
                    {
                        htmlListInfo.Columns = 1;
                    }
                    htmlWrapInfo.SeparatorMaxCount = htmlListInfo.Columns;

                    var wrapHtml_builder = new TagBuilder(HtmlElementTag.Table.ToString());
                    wrapHtml_builder.MergeAttributes(htmlListInfo.HtmlAttributes.ToDictionary());
                    wrapHtml_builder.MergeAttribute("cellspacing", "0");     // for IE7 compatibility

                    var wrapRow = HtmlElementTag.Tr;
                    htmlWrapInfo.WrapElement = HtmlElementTag.Td;
                    htmlWrapInfo.WrapOpen    = wrapHtml_builder.ToString(TagRenderMode.StartTag) +
                                               "<" + wrapRow + ">";
                    htmlWrapInfo.WrapRowbreak = "</" + wrapRow + "><" + wrapRow + ">";
                    htmlWrapInfo.WrapClose    = "</" + wrapRow + ">" +
                                                wrapHtml_builder.ToString(TagRenderMode.EndTag);
                }
                break;
                    //// creates an html unordered (bulleted) list of checkboxes in one column
                    //case HtmlTag.Ul:
                    //    {
                    //        var wrapHtml_builder = new TagBuilder(htmlElementTag.ul.ToString());
                    //        wrapHtml_builder.MergeAttributes(wrapInfo.htmlAttributes.ToDictionary());
                    //        wrapHtml_builder.MergeAttribute("cellspacing", "0"); // for IE7 compatibility

                    //        w.wrap_element = htmlElementTag.li;
                    //        w.wrap_open = wrapHtml_builder.ToString(TagRenderMode.StartTag);
                    //        w.wrap_close = wrapHtml_builder.ToString(TagRenderMode.EndTag);
                    //    }
                    //    break;
                }
            }
            // default setting creates vertical or horizontal column of checkboxes
            else
            {
                if (position == Position.Horizontal || position == Position.Horizontal_RightToLeft)
                {
                    htmlWrapInfo.AppendToElement = " &nbsp; ";
                }
                if (position == Position.Vertical || position == Position.Vertical_RightToLeft)
                {
                    htmlWrapInfo.AppendToElement = "<br/>";
                }

                if (textLayout == TextLayout.RightToLeft)
                {
                    // lean text to right for right-to-left languages
                    var defaultSectionStyle = "style=\"text-align: right;\"";
                    var wrapRow             = HtmlElementTag.Div;
                    htmlWrapInfo.WrapOpen     = "<" + wrapRow + " " + defaultSectionStyle + ">";
                    htmlWrapInfo.WrapRowbreak = string.Empty;
                    htmlWrapInfo.WrapClose    = "</" + wrapRow + ">";
                }
            }

            // return completed check box list wrapper
            return(htmlWrapInfo);
        }