Try your search with a different keyword or use * as a wildcard.
using System.Globalization;
using System.Text;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Nop.Services.Localization;
using Nop.Web.Framework.Extensions;
namespace Nop.Web.Framework.TagHelpers.Shared;
///
/// "nop-date-picker" tag helper
///
[HtmlTargetElement("nop-date-picker", Attributes = DAY_NAME_ATTRIBUTE_NAME + "," + MONTH_NAME_ATTRIBUTE_NAME + "," + YEAR_NAME_ATTRIBUTE_NAME, TagStructure = TagStructure.WithoutEndTag)]
public partial class NopDatePickerTagHelper : TagHelper
{
#region Constants
protected const string DAY_NAME_ATTRIBUTE_NAME = "asp-day-name";
protected const string MONTH_NAME_ATTRIBUTE_NAME = "asp-month-name";
protected const string YEAR_NAME_ATTRIBUTE_NAME = "asp-year-name";
protected const string BEGIN_YEAR_ATTRIBUTE_NAME = "asp-begin-year";
protected const string END_YEAR_ATTRIBUTE_NAME = "asp-end-year";
protected const string SELECTED_DATE_ATTRIBUTE_NAME = "asp-selected-date";
protected const string WRAP_TAGS_ATTRIBUTE_NAME = "asp-wrap-tags";
#endregion
#region Fields
protected readonly IHtmlHelper _htmlHelper;
protected readonly ILocalizationService _localizationService;
#endregion
#region Ctor
public NopDatePickerTagHelper(IHtmlGenerator generator, IHtmlHelper htmlHelper, ILocalizationService localizationService)
{
Generator = generator;
_htmlHelper = htmlHelper;
_localizationService = localizationService;
}
#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);
//tag details
output.TagName = "div";
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("class", "date-picker-wrapper");
//build date selects
var daysList = new TagBuilder("select");
var monthsList = new TagBuilder("select");
var yearsList = new TagBuilder("select");
daysList.Attributes.Add("name", DayName);
monthsList.Attributes.Add("name", MonthName);
yearsList.Attributes.Add("name", YearName);
var tagHelperAttributes = new List
{
DAY_NAME_ATTRIBUTE_NAME,
MONTH_NAME_ATTRIBUTE_NAME,
YEAR_NAME_ATTRIBUTE_NAME,
BEGIN_YEAR_ATTRIBUTE_NAME,
END_YEAR_ATTRIBUTE_NAME,
SELECTED_DATE_ATTRIBUTE_NAME,
WRAP_TAGS_ATTRIBUTE_NAME
};
var customerAttributes = new Dictionary();
foreach (var attribute in context.AllAttributes)
{
if (!tagHelperAttributes.Contains(attribute.Name))
customerAttributes.Add(attribute.Name, attribute.Value);
}
var htmlAttributesDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(customerAttributes);
daysList.MergeAttributes(htmlAttributesDictionary, true);
monthsList.MergeAttributes(htmlAttributesDictionary, true);
yearsList.MergeAttributes(htmlAttributesDictionary, true);
var currentCalendar = CultureInfo.CurrentCulture.Calendar;
var days = new StringBuilder();
var months = new StringBuilder();
var years = new StringBuilder();
days.AppendFormat("", "0", await _localizationService.GetResourceAsync("Common.Day"));
for (var i = 1; i <= 31; i++)
days.AppendFormat("", i,
(SelectedDate.HasValue && currentCalendar.GetDayOfMonth(SelectedDate.Value) == i) ? " selected=\"selected\"" : null);
months.AppendFormat("", "0", await _localizationService.GetResourceAsync("Common.Month"));
for (var i = 1; i <= 12; i++)
{
months.AppendFormat("",
i,
(SelectedDate.HasValue && currentCalendar.GetMonth(SelectedDate.Value) == i) ? " selected=\"selected\"" : null,
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i));
}
years.AppendFormat("", "0", await _localizationService.GetResourceAsync("Common.Year"));
BeginYear ??= DateTime.UtcNow.AddYears(-100);
EndYear ??= DateTime.UtcNow;
if (EndYear > BeginYear)
{
for (var i = currentCalendar.GetYear(BeginYear.Value); i <= currentCalendar.GetYear(EndYear.Value); i++)
years.AppendFormat("", i,
(SelectedDate.HasValue && currentCalendar.GetYear(SelectedDate.Value) == i) ? " selected=\"selected\"" : null);
}
else
{
for (var i = currentCalendar.GetYear(BeginYear.Value); i >= currentCalendar.GetYear(EndYear.Value); i--)
years.AppendFormat("", i,
(SelectedDate.HasValue && currentCalendar.GetYear(SelectedDate.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 var wrapTags) && wrapTags)
{
var wrapDaysList = "" + await daysList.RenderHtmlContentAsync() + "";
var wrapMonthsList = "" + await monthsList.RenderHtmlContentAsync() + "";
var wrapYearsList = "" + await yearsList.RenderHtmlContentAsync() + "";
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);
}
}
#endregion
#region Properties
protected IHtmlGenerator Generator { get; set; }
///
/// Day name
///
[HtmlAttributeName(DAY_NAME_ATTRIBUTE_NAME)]
public string DayName { get; set; }
///
/// Month name
///
[HtmlAttributeName(MONTH_NAME_ATTRIBUTE_NAME)]
public string MonthName { get; set; }
///
/// Year name
///
[HtmlAttributeName(YEAR_NAME_ATTRIBUTE_NAME)]
public string YearName { get; set; }
///
/// Begin year
///
[HtmlAttributeName(BEGIN_YEAR_ATTRIBUTE_NAME)]
public DateTime? BeginYear { get; set; }
///
/// End year
///
[HtmlAttributeName(END_YEAR_ATTRIBUTE_NAME)]
public DateTime? EndYear { get; set; }
///
/// Selected day
///
[HtmlAttributeName(SELECTED_DATE_ATTRIBUTE_NAME)]
public DateTime? SelectedDate { get; set; }
///
/// Wrap tags
///
[HtmlAttributeName(WRAP_TAGS_ATTRIBUTE_NAME)]
public string WrapTags { get; set; }
///
/// ViewContext
///
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
#endregion
}