Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

NopTabTagHelper.cs

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Nop.Web.Framework.Extensions;

namespace Nop.Web.Framework.TagHelpers.Admin;

/// 
/// "nop-tab" tag helper
/// 
[HtmlTargetElement("nop-tab", ParentTag = "nop-tabs", Attributes = NAME_ATTRIBUTE_NAME)]
public partial class NopTabTagHelper : TagHelper
{
    #region Constants

    protected const string NAME_ATTRIBUTE_NAME = "asp-name";
    protected const string TITLE_ATTRIBUTE_NAME = "asp-title";
    protected const string DEFAULT_ATTRIBUTE_NAME = "asp-default";

    #endregion

    #region Fields

    protected readonly IHtmlHelper _htmlHelper;

    #endregion

    #region Ctor

    public NopTabTagHelper(IHtmlHelper htmlHelper)
    {
        _htmlHelper = htmlHelper;
    }

    #endregion

    #region Methods

    /// 
    /// Asynchronously executes the tag helper with the given context and output
    /// 
    /// Contains information associated with the current HTML tag
    /// A stateful HTML element used to generate an HTML tag
    /// A task that represents the asynchronous operation
    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        ArgumentNullException.ThrowIfNull(context);

        ArgumentNullException.ThrowIfNull(output);

        //contextualize IHtmlHelper
        var viewContextAware = _htmlHelper as IViewContextAware;
        viewContextAware?.Contextualize(ViewContext);

        _ = bool.TryParse(IsDefault, out var isDefaultTab);

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

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

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

        //tab title
        var tabTitle = new TagBuilder("li");
        tabTitle.AddCssClass("nav-item");
        var linkTag = new TagBuilder("a")
        {
            Attributes =
            {
                new KeyValuePair("data-tab-name", Name),
                new KeyValuePair("href", $"#{Name}"),
                new KeyValuePair("data-toggle", "pill"),
                new KeyValuePair("role", "tab"),
                new KeyValuePair("aria-selected", "false"),
            }
        };
        //active class
        if (tabNameToSelect == Name)
            linkTag.AddCssClass("nav-link active");
        else
            linkTag.AddCssClass("nav-link");

        linkTag.InnerHtml.AppendHtml(Title);

        //merge classes
        if (context.AllAttributes.ContainsName("class"))
            tabTitle.AddCssClass(context.AllAttributes["class"].Value.ToString());

        tabTitle.InnerHtml.AppendHtml(await linkTag.RenderHtmlContentAsync());

        //tab content
        var tabContenttop = new TagBuilder("div");
        tabContenttop.AddCssClass("card-body");

        var tabContent = new TagBuilder("div");
        tabContent.AddCssClass("tab-pane fade{0}");
        tabContent.Attributes.Add("id", Name);
        tabContent.Attributes.Add("role", "tabpanel");

        var childContent = await output.GetChildContentAsync();
        tabContent.InnerHtml.AppendHtml(childContent.GetContent());

        tabContenttop.InnerHtml.AppendHtml(await tabContent.RenderHtmlContentAsync());

        //active class
        if (tabNameToSelect == Name)
            tabContent.AddCssClass("active");

        //add to context
        var tabContext = (List)context.Items[typeof(NopTabsTagHelper)];
        tabContext.Add(new NopTabContextItem
        {
            Title = await tabTitle.RenderHtmlContentAsync(),
            Content = await tabContent.RenderHtmlContentAsync(),
            IsDefault = isDefaultTab
        });

        //generate nothing
        output.SuppressOutput();
    }

    #endregion

    #region Properties

    /// 
    /// Title of the tab
    /// 
    [HtmlAttributeName(TITLE_ATTRIBUTE_NAME)]
    public string Title { set; get; }

    /// 
    /// Indicates whether the tab is default
    /// 
    [HtmlAttributeName(DEFAULT_ATTRIBUTE_NAME)]
    public string IsDefault { set; get; }

    /// 
    /// Name of the tab
    /// 
    [HtmlAttributeName(NAME_ATTRIBUTE_NAME)]
    public string Name { set; get; }

    /// 
    /// ViewContext
    /// 
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }

    #endregion
}