Пример #1
0
        /// <summary>
        /// Process
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="output">Output</param>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            //contextualize IHtmlHelper
            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            if (string.IsNullOrEmpty(Action))
            {
                Action = "Delete";
            }

            string modelName = _htmlHelper.ViewData.ModelMetadata.ModelType.Name.ToLower();

            if (!string.IsNullOrEmpty(Action))
            {
                modelName += "-" + Action;
            }
            string modalId = new HtmlString(modelName + "-delete-confirmation").ToHtmlString();

            if (int.TryParse(ModelId, out int modelId))
            {
                DeleteConfirmationModel deleteConfirmationModel = new DeleteConfirmationModel
                {
                    Id             = modelId,
                    ControllerName = _htmlHelper.ViewContext.RouteData.Values["controller"].ToString(),
                    ActionName     = Action,
                    WindowId       = modalId
                };

                //tag details
                output.TagName = "div";
                output.TagMode = TagMode.StartTagAndEndTag;

                output.Attributes.Add("id", modalId);
                output.Attributes.Add("class", "modal fade");
                output.Attributes.Add("tabindex", "-1");
                output.Attributes.Add("role", "dialog");
                output.Attributes.Add("aria-labelledby", $"{modalId}-title");
                output.Content.SetHtmlContent(await _htmlHelper.PartialAsync("Delete", deleteConfirmationModel));

                //modal script
                TagBuilder script = new TagBuilder("script");
                script.InnerHtml.AppendHtml("$(document).ready(function () {" +
                                            $"$('#{ButtonId}').attr(\"data-toggle\", \"modal\").attr(\"data-target\", \"#{modalId}\")" + "});");
                output.PostContent.SetHtmlContent(script.RenderHtmlContent());
            }
        }
        /// <summary>
        /// Process
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="output">Output</param>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            //contextualize IHtmlHelper
            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            if (string.IsNullOrEmpty(Action))
            {
                Action = _htmlHelper.ViewContext.RouteData.Values["action"].ToString();
            }

            string modalId = new HtmlString(ButtonId + "-action-confirmation").ToHtmlString();

            ActionConfirmationModel actionConfirmationModel = new ActionConfirmationModel()
            {
                ControllerName       = _htmlHelper.ViewContext.RouteData.Values["controller"].ToString(),
                ActionName           = Action,
                WindowId             = modalId,
                AdditonalConfirmText = ConfirmText
            };

            //tag details
            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;

            output.Attributes.Add("id", modalId);
            output.Attributes.Add("class", "modal fade");
            output.Attributes.Add("tabindex", "-1");
            output.Attributes.Add("role", "dialog");
            output.Attributes.Add("aria-labelledby", $"{modalId}-title");
            output.Content.SetHtmlContent(await _htmlHelper.PartialAsync("Confirm", actionConfirmationModel));

            //modal script
            TagBuilder script = new TagBuilder("script");

            script.InnerHtml.AppendHtml("$(document).ready(function () {" +
                                        $"$('#{ButtonId}').attr(\"data-toggle\", \"modal\").attr(\"data-target\", \"#{modalId}\");" +
                                        $"$('#{modalId}-submit-button').attr(\"name\", $(\"#{ButtonId}\").attr(\"name\"));" +
                                        $"$(\"#{ButtonId}\").attr(\"name\", \"\");" +
                                        $"if($(\"#{ButtonId}\").attr(\"type\") == \"submit\")$(\"#{ButtonId}\").attr(\"type\", \"button\");" +
                                        "});");
            output.PostContent.SetHtmlContent(script.RenderHtmlContent());
        }
Пример #3
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            output.SuppressOutput();

            Dictionary <string, object> htmlAttributes = new Dictionary <string, object>();

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

            if (IsRequired)
            {
                output.PreElement.SetHtmlContent("<div class='input-group input-group-required'>");
                output.PostElement.SetHtmlContent("<div class=\"input-group-btn\"><span class=\"required\">*</span></div></div>");
            }

            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            bool.TryParse(RenderFormControlClass, out bool renderFormControlClass);
            if ((string.IsNullOrEmpty(RenderFormControlClass) && For.Metadata.ModelType.Name.Equals("String")) || renderFormControlClass)
            {
                htmlAttributes.Add("class", "form-control");
            }

            IViewEngine      viewEngine      = CoreUtility.GetPrivateFieldValue(_htmlHelper, "_viewEngine") as IViewEngine;
            IViewBufferScope bufferScope     = CoreUtility.GetPrivateFieldValue(_htmlHelper, "_bufferScope") as IViewBufferScope;
            TemplateBuilder  templateBuilder = new TemplateBuilder(
                viewEngine,
                bufferScope,
                _htmlHelper.ViewContext,
                _htmlHelper.ViewData,
                For.ModelExplorer,
                For.Name,
                Template,
                readOnly: false,
                additionalViewData: new { htmlAttributes, postfix = Postfix });

            IHtmlContent htmlOutput = templateBuilder.Build();

            output.Content.SetHtmlContent(htmlOutput.ToHtmlString());
        }
Пример #4
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            string modalId = new HtmlString(ButtonId + "-action-confirmation").ToHtmlString();

            ScriptConfirmationModel scriptConfirmationModel = new ScriptConfirmationModel()
            {
                ModalId     = modalId,
                ConfirmText = ConfirmText,
                CallBack    = CallBackFunction
            };

            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.Add("id", modalId);
            output.Attributes.Add("class", $"modal {ModalUiClass} fade");
            output.Attributes.Add("tabindex", "-1");
            output.Attributes.Add("role", "dialog");
            output.Attributes.Add("aria-labelledby", $"{modalId}-title");
            output.Content.SetHtmlContent(await _htmlHelper.PartialAsync("ScriptConfirm", scriptConfirmationModel));

            TagBuilder script = new TagBuilder("script");

            script.InnerHtml.AppendHtml("$(document).ready(function () {" +
                                        $"$('#{ButtonId}').attr(\"data-toggle\", \"modal\").attr(\"data-target\", \"#{modalId}\");" +
                                        $"if($(\"#{ButtonId}\").attr(\"type\") == \"submit\")$(\"#{ButtonId}\").attr(\"type\", \"button\");" +
                                        "});");
            output.PostContent.SetHtmlContent(script.ToHtmlString());
        }
Пример #5
0
        /// <summary>
        /// Process
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="output">Output</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            //clear the output
            output.SuppressOutput();

            //container for additional attributes
            Dictionary <string, object> htmlAttributes = new Dictionary <string, object>();

            //disabled attribute
            bool.TryParse(IsDisabled, out bool disabled);
            if (disabled)
            {
                htmlAttributes.Add("disabled", "disabled");
            }

            //required asterisk
            bool.TryParse(IsRequired, out bool required);
            if (required)
            {
                output.PreElement.SetHtmlContent("<div class='input-group input-group-required'>");
                output.PostElement.SetHtmlContent("<div class=\"input-group-btn\"><span class=\"required\">*</span></div></div>");
            }

            //contextualize IHtmlHelper
            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            //add form-control class
            bool.TryParse(RenderFormControlClass, out bool renderFormControlClass);
            if (string.IsNullOrEmpty(RenderFormControlClass) && For.Metadata.ModelType.Name.Equals("String") || renderFormControlClass)
            {
                htmlAttributes.Add("class", "form-control");
            }

            //generate editor

            //we have to invoke strong typed "EditorFor" method of HtmlHelper<TModel>
            //but we cannot do it because we don't have access to Expression<Func<TModel, TValue>>
            //more info at https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/HtmlHelperOfT.cs

            //so we manually invoke implementation of "GenerateEditor" method of HtmlHelper
            //more info at https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/HtmlHelper.cs

            //little workaround here. we need to access private properties of HtmlHelper
            //just ensure that they are not renamed by asp.net core team in future versions
            IViewEngine      viewEngine      = CommonHelper.GetPrivateFieldValue(_htmlHelper, "_viewEngine") as IViewEngine;
            IViewBufferScope bufferScope     = CommonHelper.GetPrivateFieldValue(_htmlHelper, "_bufferScope") as IViewBufferScope;
            TemplateBuilder  templateBuilder = new TemplateBuilder(
                viewEngine,
                bufferScope,
                _htmlHelper.ViewContext,
                _htmlHelper.ViewData,
                For.ModelExplorer,
                For.Name,
                Template,
                readOnly: false,
                additionalViewData: new { htmlAttributes, postfix = this.Postfix });

            Microsoft.AspNetCore.Html.IHtmlContent htmlOutput = templateBuilder.Build();
            output.Content.SetHtmlContent(htmlOutput.RenderHtmlContent());
        }
Пример #6
0
        /// <summary>
        /// Process
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="output">Output</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            //clear the output
            output.SuppressOutput();

            //required asterisk
            bool.TryParse(IsRequired, out bool required);
            if (required)
            {
                output.PreElement.SetHtmlContent("<div class='input-group input-group-required'>");
                output.PostElement.SetHtmlContent("<div class=\"input-group-btn\"><span class=\"required\">*</span></div></div>");
            }

            //contextualize IHtmlHelper
            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            //get htmlAttributes object
            Dictionary <string, object>    htmlAttributes = new Dictionary <string, object>();
            ReadOnlyTagHelperAttributeList attributes     = context.AllAttributes;

            foreach (TagHelperAttribute attribute in attributes)
            {
                if (!attribute.Name.Equals(ForAttributeName) &&
                    !attribute.Name.Equals(NameAttributeName) &&
                    !attribute.Name.Equals(ItemsAttributeName) &&
                    !attribute.Name.Equals(DisabledAttributeName) &&
                    !attribute.Name.Equals(RequiredAttributeName))
                {
                    htmlAttributes.Add(attribute.Name, attribute.Value);
                }
            }

            //generate editor
            string tagName = For != null ? For.Name : Name;

            bool.TryParse(IsMultiple, out bool multiple);
            if (!string.IsNullOrEmpty(tagName))
            {
                IHtmlContent selectList;
                if (multiple)
                {
                    selectList = _htmlHelper.Editor(tagName, "MultiSelect", new { htmlAttributes, SelectList = Items });
                }
                else
                {
                    if (htmlAttributes.ContainsKey("class"))
                    {
                        htmlAttributes["class"] += " form-control";
                    }
                    else
                    {
                        htmlAttributes.Add("class", "form-control");
                    }

                    selectList = _htmlHelper.DropDownList(tagName, Items, htmlAttributes);
                }
                output.Content.SetHtmlContent(selectList.RenderHtmlContent());
            }
        }
Пример #7
0
        /// <summary>
        /// Process
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="output">Output</param>
        /// <returns>Result</returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            //contextualize IHtmlHelper
            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            //create context item
            List <AgsTabContextItem> tabContext = new List <AgsTabContextItem>();

            context.Items.Add(typeof(AgsTabsTagHelper), tabContext);

            //get tab name which should be selected
            //first try get tab name from query
            Microsoft.Extensions.Primitives.StringValues tabNameToSelect = ViewContext.HttpContext.Request.Query["tabNameToSelect"];

            //then from attribute
            if (!string.IsNullOrEmpty(TabNameToSelect))
            {
                tabNameToSelect = TabNameToSelect;
            }

            //then save tab name in tab context to access it in tab item
            if (!string.IsNullOrEmpty(tabNameToSelect))
            {
                context.Items.Add("tabNameToSelect", tabNameToSelect);
            }

            //execute child tag helpers
            await output.GetChildContentAsync();

            //tabs title
            TagBuilder tabsTitle = new TagBuilder("ul");

            tabsTitle.AddCssClass("nav");
            tabsTitle.AddCssClass("nav-tabs");

            //tabs content
            TagBuilder tabsContent = new TagBuilder("div");

            tabsContent.AddCssClass("tab-content");

            foreach (AgsTabContextItem tabItem in tabContext)
            {
                tabsTitle.InnerHtml.AppendHtml(tabItem.Title);
                tabsContent.InnerHtml.AppendHtml(tabItem.Content);
            }

            //append data
            output.Content.AppendHtml(tabsTitle.RenderHtmlContent());
            output.Content.AppendHtml(tabsContent.RenderHtmlContent());

            bool.TryParse(RenderSelectedTabInput, out bool renderSelectedTabInput);
            if (string.IsNullOrEmpty(RenderSelectedTabInput) || renderSelectedTabInput)
            {
                //render input contains selected tab name
                TagBuilder selectedTabInput = new TagBuilder("input");
                selectedTabInput.Attributes.Add("type", "hidden");
                selectedTabInput.Attributes.Add("id", "selected-tab-name");
                selectedTabInput.Attributes.Add("name", "selected-tab-name");
                selectedTabInput.Attributes.Add("value", _htmlHelper.GetSelectedTabName());
                output.PreContent.SetHtmlContent(selectedTabInput.RenderHtmlContent());

                //render tabs script
                if (output.Attributes.ContainsName("id"))
                {
                    TagBuilder script = new TagBuilder("script");
                    script.InnerHtml.AppendHtml("$(document).ready(function () {bindBootstrapTabSelectEvent('" + output.Attributes["id"].Value + "', 'selected-tab-name');});");
                    output.PostContent.SetHtmlContent(script.RenderHtmlContent());
                }
            }

            output.TagName = "div";

            string itemClass = "nav-tabs-custom";
            //merge classes
            string classValue = output.Attributes.ContainsName("class")
                ? $"{output.Attributes["class"].Value} {itemClass}"
                : itemClass;

            output.Attributes.SetAttribute("class", classValue);
        }
Пример #8
0
        /// <summary>
        /// Process
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="output">Output</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            //contextualize IHtmlHelper
            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            bool.TryParse(IsDefault, out bool isDefaultTab);

            //get name of the tab should be selected
            string tabNameToSelect = context.Items.ContainsKey("tabNameToSelect")
                ? context.Items["tabNameToSelect"].ToString()
                : "";

            if (string.IsNullOrEmpty(tabNameToSelect))
            {
                tabNameToSelect = _htmlHelper.GetSelectedTabName();
            }

            if (string.IsNullOrEmpty(tabNameToSelect) && isDefaultTab)
            {
                tabNameToSelect = Name;
            }

            //tab title
            TagBuilder tabTitle = new TagBuilder("li");
            TagBuilder a        = new TagBuilder("a")
            {
                Attributes =
                {
                    new KeyValuePair <string, string>("data-tab-name", Name),
                    new KeyValuePair <string, string>("href",          $"#{Name}"),
                    new KeyValuePair <string, string>("data-toggle",   "tab"),
                }
            };

            a.InnerHtml.AppendHtml(Title);

            //merge classes
            if (context.AllAttributes.ContainsName("class"))
            {
                tabTitle.Attributes.Add("class", context.AllAttributes["class"].Value.ToString());
            }
            tabTitle.InnerHtml.AppendHtml(a.RenderHtmlContent());

            //tab content
            TagBuilder tabContent = new TagBuilder("div");

            tabContent.AddCssClass("tab-pane");
            tabContent.Attributes.Add("id", Name);
            tabContent.InnerHtml.AppendHtml(output.GetChildContentAsync().Result.GetContent());

            //active class
            string itemClass = string.Empty;

            if (tabNameToSelect == Name)
            {
                tabTitle.AddCssClass("active");
                tabContent.AddCssClass("active");
            }

            //add to context
            List <AgsTabContextItem> tabContext = (List <AgsTabContextItem>)context.Items[typeof(AgsTabsTagHelper)];

            tabContext.Add(new AgsTabContextItem()
            {
                Title     = tabTitle.RenderHtmlContent(),
                Content   = tabContent.RenderHtmlContent(),
                IsDefault = isDefaultTab
            });

            //generate nothing
            output.SuppressOutput();
        }
Пример #9
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.SetAttribute("class", "date-picker-wrapper");

            TagBuilder daysList   = new TagBuilder("select");
            TagBuilder monthsList = new TagBuilder("select");
            TagBuilder yearsList  = new TagBuilder("select");

            daysList.Attributes.Add("name", DayName);
            monthsList.Attributes.Add("name", MonthName);
            yearsList.Attributes.Add("name", YearName);

            List <string> tagHelperAttributes = new List <string>
            {
                DayNameAttributeName,
                MonthNameAttributeName,
                YearNameAttributeName,
                BeginYearAttributeName,
                EndYearAttributeName,
                SelectedDayAttributeName,
                SelectedMonthAttributeName,
                SelectedYearAttributeName,
                WrapTagsAttributeName
            };
            Dictionary <string, object> customerAttributes = new Dictionary <string, object>();

            foreach (TagHelperAttribute attribute in context.AllAttributes)
            {
                if (!tagHelperAttributes.Contains(attribute.Name))
                {
                    customerAttributes.Add(attribute.Name, attribute.Value);
                }
            }
            IDictionary <string, object> htmlAttributesDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(customerAttributes);

            daysList.MergeAttributes(htmlAttributesDictionary, true);
            monthsList.MergeAttributes(htmlAttributesDictionary, true);
            yearsList.MergeAttributes(htmlAttributesDictionary, true);

            StringBuilder days   = new StringBuilder();
            StringBuilder months = new StringBuilder();
            StringBuilder years  = new StringBuilder();

            days.AppendFormat("<option value='{0}'>{1}</option>", "0", "Chọn ngày");
            for (int i = 1; i <= 31; i++)
            {
                days.AppendFormat("<option value='{0}'{1}>{0}</option>", i, (SelectedDay.HasValue && SelectedDay.Value == i) ? " selected=\"selected\"" : null);
            }

            months.AppendFormat("<option value='{0}'>{1}</option>", "0", "Chọn tháng");
            for (int i = 1; i <= 12; i++)
            {
                months.AppendFormat("<option value='{0}'{1}>{2}</option>", i, (SelectedMonth.HasValue && SelectedMonth.Value == i) ? " selected=\"selected\"" : null, CultureInfo.CurrentUICulture.DateTimeFormat.GetMonthName(i));
            }

            years.AppendFormat("<option value='{0}'>{1}</option>", "0", "Chọn năm");

            if (BeginYear == null)
            {
                BeginYear = DateTime.UtcNow.Year - 10;
            }

            if (EndYear == null)
            {
                EndYear = DateTime.UtcNow.Year;
            }

            if (EndYear > BeginYear)
            {
                for (int i = BeginYear.Value; i <= EndYear.Value; i++)
                {
                    years.AppendFormat("<option value='{0}'{1}>{0}</option>", i, (SelectedYear.HasValue && SelectedYear.Value == i) ? " selected=\"selected\"" : null);
                }
            }
            else
            {
                for (int i = BeginYear.Value; i >= EndYear.Value; i--)
                {
                    years.AppendFormat("<option value='{0}'{1}>{0}</option>", i, (SelectedYear.HasValue && SelectedYear.Value == i) ? " selected=\"selected\"" : null);
                }
            }

            daysList.InnerHtml.AppendHtml(days.ToString());
            monthsList.InnerHtml.AppendHtml(months.ToString());
            yearsList.InnerHtml.AppendHtml(years.ToString());

            if (bool.TryParse(WrapTags, out bool wrapTags) && wrapTags)
            {
                string wrapDaysList   = "<span class=\"days-list select-wrapper\">" + daysList.ToHtmlString() + "</span>";
                string wrapMonthsList = "<span class=\"months-list select-wrapper\">" + monthsList.ToHtmlString() + "</span>";
                string wrapYearsList  = "<span class=\"years-list select-wrapper\">" + yearsList.ToHtmlString() + "</span>";

                output.Content.AppendHtml(wrapDaysList);
                output.Content.AppendHtml(wrapMonthsList);
                output.Content.AppendHtml(wrapYearsList);
            }
            else
            {
                output.Content.AppendHtml(daysList);
                output.Content.AppendHtml(monthsList);
                output.Content.AppendHtml(yearsList);
            }
        }
Пример #10
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            List <TabContextItem> tabContext = new List <TabContextItem>();

            context.Items.Add(typeof(TabsTagHelper), tabContext);
            StringValues tabNameToSelect = TabNameToSelect;

            if (string.IsNullOrEmpty(tabNameToSelect))
            {
                tabNameToSelect = ViewContext.HttpContext.Request.Query["tabNameToSelect"];
            }

            if (string.IsNullOrEmpty(tabNameToSelect))
            {
                tabNameToSelect = _htmlHelper.GetSelectedTabName();
            }

            if (!string.IsNullOrEmpty(tabNameToSelect))
            {
                context.Items.Add("tabNameToSelect", tabNameToSelect);
            }

            await output.GetChildContentAsync();

            TagBuilder tabsTitle = new TagBuilder("ul");

            tabsTitle.AddCssClass("nav");
            tabsTitle.AddCssClass("nav-tabs");

            TagBuilder tabsContent = new TagBuilder("div");

            tabsContent.AddCssClass("tab-content");

            foreach (TabContextItem tabItem in tabContext)
            {
                tabsTitle.InnerHtml.AppendHtml(tabItem.Title);
                tabsContent.InnerHtml.AppendHtml(tabItem.Content);
            }

            output.Content.AppendHtml(tabsTitle.ToHtmlString());
            output.Content.AppendHtml(tabsContent.ToHtmlString());

            string hiddenName = "selected-tab-name";

            if (!string.IsNullOrEmpty(TabNamePerfix))
            {
                hiddenName += $"-{TabNamePerfix}";
            }

            TagBuilder selectedTabInput = new TagBuilder("input");

            selectedTabInput.Attributes.Add("type", "hidden");
            selectedTabInput.Attributes.Add("id", hiddenName);
            selectedTabInput.Attributes.Add("name", hiddenName);
            selectedTabInput.Attributes.Add("value", tabNameToSelect);
            output.PreContent.SetHtmlContent(selectedTabInput.ToHtmlString());

            if (output.Attributes.ContainsName("id"))
            {
                TagBuilder script = new TagBuilder("script");
                script.InnerHtml.AppendHtml("$(document).ready(function () {bindBootstrapTabSelectEvent('" + output.Attributes["id"].Value + "', '" + hiddenName + "');});");
                output.PostContent.SetHtmlContent(script.ToHtmlString());
            }

            output.TagName = "div";
            const string itemClass  = "nav-tabs-custom";
            string       classValue = output.Attributes.ContainsName("class") ? $"{output.Attributes["class"].Value} {itemClass}" : itemClass;

            output.Attributes.SetAttribute("class", classValue);
        }
Пример #11
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            IViewContextAware viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            string tabNameToSelect = context.Items.ContainsKey("tabNameToSelect") ? context.Items["tabNameToSelect"].ToString() : "";

            if (string.IsNullOrEmpty(tabNameToSelect))
            {
                tabNameToSelect = _htmlHelper.GetSelectedTabName();
            }

            if (string.IsNullOrEmpty(tabNameToSelect) && IsDefault)
            {
                tabNameToSelect = Name;
            }

            TagBuilder tabTitle = new TagBuilder("li");
            TagBuilder a        = new TagBuilder("a")
            {
                Attributes =
                {
                    new KeyValuePair <string, string>("data-tab-name", Name),
                    new KeyValuePair <string, string>("href",          $"#{Name}"),
                    new KeyValuePair <string, string>("data-toggle",   "tab"),
                }
            };

            a.InnerHtml.AppendHtml(Title);

            if (context.AllAttributes.ContainsName("class"))
            {
                tabTitle.Attributes.Add("class", context.AllAttributes["class"].Value.ToString());
            }

            tabTitle.InnerHtml.AppendHtml(a.ToHtmlString());

            TagBuilder tabContent = new TagBuilder("div");

            tabContent.AddCssClass("tab-pane");
            tabContent.Attributes.Add("id", Name);
            tabContent.InnerHtml.AppendHtml(output.GetChildContentAsync().Result.GetContent());
            if (tabNameToSelect == Name)
            {
                tabTitle.AddCssClass("active");
                tabContent.AddCssClass("active");
            }

            List <TabContextItem> tabContext = (List <TabContextItem>)context.Items[typeof(TabsTagHelper)];

            tabContext.Add(new TabContextItem()
            {
                Title     = tabTitle.ToHtmlString(),
                Content   = tabContent.ToHtmlString(),
                IsDefault = IsDefault
            });

            output.SuppressOutput();
        }