UtcTimeTagHelper.cs 994 Bytes
Newer Older
Dmytro Bogatov's avatar
Dmytro Bogatov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace StatusMonitor.Web.TagHelpers
{
	[HtmlTargetElement("utc-time", Attributes = "time")]
	/// <summary>
	/// Underlying class for <local-time> tag.
	/// </summary>
	public class UtcTimeTagHelper : TagHelper
	{
Dmytro Bogatov's avatar
Fix.  
Dmytro Bogatov committed
15 16
		public bool ShowDate { get; set; } = false;

Dmytro Bogatov's avatar
Dmytro Bogatov committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30
		public DateTime Time { get; set; }

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

		/// <summary>
		/// Called by the framework. Renders tag helper.
		/// </summary>
		public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
		{
			output.TagName = "span";
			output.TagMode = TagMode.StartTagAndEndTag;

			output.Attributes.Clear();
Dmytro Bogatov's avatar
Fix.  
Dmytro Bogatov committed
31 32
			
			output.Attributes.Add("class", ShowDate ? "utc-date" : "utc-time");
Dmytro Bogatov's avatar
Dmytro Bogatov committed
33 34 35 36 37 38 39

			output.Content.SetHtmlContent(Time.ToString());

			await Task.CompletedTask;
		}
	}
}