Webiant Logo Webiant Logo
  1. No results found.

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

NopDeleteConfirmationTagHelper.cs

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Nop.Web.Framework.Extensions;
using Nop.Web.Framework.Models;

namespace Nop.Web.Framework.TagHelpers.Admin;

/// 
/// "nop-delete-confirmation" tag helper
/// 
[HtmlTargetElement("nop-delete-confirmation", Attributes = MODEL_ID_ATTRIBUTE_NAME + "," + BUTTON_ID_ATTRIBUTE_NAME, TagStructure = TagStructure.WithoutEndTag)]
public partial class NopDeleteConfirmationTagHelper : TagHelper
{
    #region Constants

    protected const string MODEL_ID_ATTRIBUTE_NAME = "asp-model-id";
    protected const string BUTTON_ID_ATTRIBUTE_NAME = "asp-button-id";
    protected const string ACTION_ATTRIBUTE_NAME = "asp-action";

    #endregion

    #region Fields

    protected readonly IHtmlHelper _htmlHelper;

    #endregion

    #region Ctor

    public NopDeleteConfirmationTagHelper(IHtmlGenerator generator, IHtmlHelper htmlHelper)
    {
        Generator = generator;
        _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);

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

        var modelName = _htmlHelper.ViewData.ModelMetadata.ModelType.Name.ToLowerInvariant();
        if (!string.IsNullOrEmpty(Action))
            modelName += "-" + Action;
        var modalId = await new HtmlString(modelName + "-delete-confirmation").RenderHtmlContentAsync();

        var 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");

        var partialView = await _htmlHelper.PartialAsync("Delete", deleteConfirmationModel);
        output.Content.SetHtmlContent(partialView);

        //modal script
        var script = new TagBuilder("script");
        script.InnerHtml.AppendHtml(
            "$(function() {" +
            $"$('#{ButtonId}').attr(\"data-toggle\", \"modal\").attr(\"data-target\", \"#{modalId}\")" +
            "});");
        var scriptTag = await script.RenderHtmlContentAsync();
        output.PostContent.SetHtmlContent(scriptTag);
    }

    #endregion

    #region Properties

    protected IHtmlGenerator Generator { get; set; }

    /// 
    /// Model identifier
    /// 
    [HtmlAttributeName(MODEL_ID_ATTRIBUTE_NAME)]
    public string ModelId { get; set; }

    /// 
    /// Button identifier
    /// 
    [HtmlAttributeName(BUTTON_ID_ATTRIBUTE_NAME)]
    public string ButtonId { get; set; }

    /// 
    /// Delete action name
    /// 
    [HtmlAttributeName(ACTION_ATTRIBUTE_NAME)]
    public string Action { get; set; }

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

    #endregion
}