/
NopActionConfirmationTagHelper.cs
128 lines (100 loc) · 4.2 KB
/
NopActionConfirmationTagHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;
/// <summary>
/// "nop-action-confirmation" tag helper
/// </summary>
[HtmlTargetElement("nop-action-confirmation", Attributes = BUTTON_ID_ATTRIBUTE_NAME, TagStructure = TagStructure.WithoutEndTag)]
public partial class NopActionConfirmationTagHelper : TagHelper
{
#region Constants
protected const string BUTTON_ID_ATTRIBUTE_NAME = "asp-button-id";
protected const string ACTION_ATTRIBUTE_NAME = "asp-action";
protected const string ADDITIONAL_CONFIRM_TEXT = "asp-additional-confirm";
#endregion
#region Fields
protected readonly IHtmlHelper _htmlHelper;
#endregion
#region Ctor
public NopActionConfirmationTagHelper(IHtmlGenerator generator, IHtmlHelper htmlHelper)
{
Generator = generator;
_htmlHelper = htmlHelper;
}
#endregion
#region Methods
/// <summary>
/// Asynchronously executes the tag helper with the given context and output
/// </summary>
/// <param name="context">Contains information associated with the current HTML tag</param>
/// <param name="output">A stateful HTML element used to generate an HTML tag</param>
/// <returns>A task that represents the asynchronous operation</returns>
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 = _htmlHelper.ViewContext.RouteData.Values["action"].ToString();
var modalId = await new HtmlString(ButtonId + "-action-confirmation").RenderHtmlContentAsync();
var 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");
var partialView = await _htmlHelper.PartialAsync("Confirm", actionConfirmationModel);
output.Content.SetHtmlContent(partialView);
//modal script
var script = new TagBuilder("script");
script.InnerHtml.AppendHtml(
"$(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\");" +
"});");
var scriptTag = await script.RenderHtmlContentAsync();
output.PostContent.SetHtmlContent(scriptTag);
}
#endregion
#region Properties
protected IHtmlGenerator Generator { get; set; }
/// <summary>
/// Button identifier
/// </summary>
[HtmlAttributeName(BUTTON_ID_ATTRIBUTE_NAME)]
public string ButtonId { get; set; }
/// <summary>
/// Delete action name
/// </summary>
[HtmlAttributeName(ACTION_ATTRIBUTE_NAME)]
public string Action { get; set; }
/// <summary>
/// Additional confirm text
/// </summary>
[HtmlAttributeName(ADDITIONAL_CONFIRM_TEXT)]
public string ConfirmText { get; set; }
/// <summary>
/// ViewContext
/// </summary>
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
#endregion
}