Browse Source

Carousel tag helper

pull/279/head
yekalkan 9 years ago
parent
commit
e775ad2493
  1. 2
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/AbpTagHelperService.cs
  2. 21
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/AbpCarouselItemTagHelper.cs
  3. 74
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/AbpCarouselItemTagHelperService.cs
  4. 19
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/AbpCarouselTagHelper.cs
  5. 135
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/AbpCarouselTagHelperService.cs
  6. 20
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/CarouselItem.cs

2
src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/AbpTagHelperService.cs

@ -20,6 +20,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers
protected const string TabItems = "TabItems";
protected const string AccordionItems = "AccordionItems";
protected const string BreadcrumbItemsContent = "BreadcrumbItemsContent";
protected const string CarouselItemsContent = "CarouselItemsContent";
protected const string TabItemsDataTogglePlaceHolder = "{{data_toggle}}";
protected const string TabItemsVerticalPillPlaceHolder = "{{vertical_pill}}";
protected const string TabItemNamePlaceHolder = "{{Tag_Name}}";
@ -27,6 +28,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers
protected const string AbpTabItemActivePlaceholder = "{_Tab_Active_Placeholder_}";
protected const string AbpTabItemShowActivePlaceholder = "{_Tab_Show_Active_Placeholder_}";
protected const string AbpBreadcrumbItemActivePlaceholder = "{_Breadcrumb_Active_Placeholder_}";
protected const string AbpCarouselItemActivePlaceholder = "{_CarouselItem_Active_Placeholder_}";
protected const string AbpTabItemSelectedPlaceholder = "{_Tab_Selected_Placeholder_}";
protected const string AbpAccordionParentIdPlaceholder = "{_{{Parent_Accordion_Id}}_}";

21
src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/AbpCarouselItemTagHelper.cs

@ -0,0 +1,21 @@
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Carousel
{
public class AbpCarouselItemTagHelper : AbpTagHelper<AbpCarouselItemTagHelper, AbpCarouselItemTagHelperService>
{
public bool? Active { get; set; }
public string Src { get; set; }
public string Alt { get; set; }
public string CaptionTitle { get; set; }
public string Caption { get; set; }
public AbpCarouselItemTagHelper(AbpCarouselItemTagHelperService tagHelperService)
: base(tagHelperService)
{
}
}
}

74
src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/AbpCarouselItemTagHelperService.cs

@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Text;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Carousel
{
public class AbpCarouselItemTagHelperService : AbpTagHelperService<AbpCarouselItemTagHelper>
{
private readonly HtmlEncoder _encoder;
public AbpCarouselItemTagHelperService(HtmlEncoder encoder)
{
_encoder = encoder;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
output.Attributes.AddClass("carousel-item");
output.Attributes.AddClass(AbpCarouselItemActivePlaceholder);
SetInnerImgTag(context, output);
SetActive(context, output);
AddCaption(context, output);
AddToContext(context, output);
output.SuppressOutput();
}
private void AddToContext(TagHelperContext context, TagHelperOutput output)
{
var getOutputAsHtml = RenderTagHelperOutput(output, _encoder);
var itemList = GetValueFromContext<List<CarouselItem>>(context, CarouselItemsContent);
itemList.Add(new CarouselItem(getOutputAsHtml, TagHelper.Active ?? false));
}
protected virtual void SetInnerImgTag(TagHelperContext context, TagHelperOutput output)
{
var imgTag ="<img class=\"d-block w-100\" src=\""+TagHelper.Src+ "\" alt=\"" + TagHelper.Alt + "\">";
output.Content.SetHtmlContent(imgTag);
}
protected virtual void SetActive(TagHelperContext context, TagHelperOutput output)
{
if (TagHelper.Active??false)
{
output.Attributes.AddClass("active");
}
}
protected virtual void AddCaption(TagHelperContext context, TagHelperOutput output)
{
if (string.IsNullOrWhiteSpace(TagHelper.Caption) && string.IsNullOrWhiteSpace(TagHelper.CaptionTitle))
{
return;
}
var html = new StringBuilder("");
html.AppendLine("<div class=\"carousel-caption d-none d-md-block\">");
html.AppendLine("<h5>"+TagHelper.CaptionTitle+"</h5>");
html.AppendLine("<p>" + TagHelper.Caption + "</p>");
html.AppendLine("</div>");
output.PostContent.SetHtmlContent(html.ToString());
}
}
}

19
src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/AbpCarouselTagHelper.cs

@ -0,0 +1,19 @@
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Carousel
{
public class AbpCarouselTagHelper : AbpTagHelper<AbpCarouselTagHelper, AbpCarouselTagHelperService>
{
public string Id { get; set; }
public bool? Crossfade { get; set; }
public bool? Controls { get; set; }
public bool? Indicators { get; set; }
public AbpCarouselTagHelper(AbpCarouselTagHelperService tagHelperService)
: base(tagHelperService)
{
}
}
}

135
src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/AbpCarouselTagHelperService.cs

@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Carousel
{
public class AbpCarouselTagHelperService : AbpTagHelperService<AbpCarouselTagHelper>
{
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
SetRandomIdIfNotProvided();
AddBasicAttributes(context, output);
var itemList = InitilizeCarouselItemsContentsContext(context, output);
await output.GetChildContentAsync();
SetOneItemAsActive(context, output, itemList);
SetItems(context, output, itemList);
SetControls(context, output, itemList);
SetIndicators(context, output, itemList);
}
protected virtual List<CarouselItem> InitilizeCarouselItemsContentsContext(TagHelperContext context, TagHelperOutput output)
{
var items = new List<CarouselItem>();
context.Items[CarouselItemsContent] = items;
return items;
}
protected virtual void SetItems(TagHelperContext context, TagHelperOutput output, List<CarouselItem> itemList)
{
var itemsHtml = new StringBuilder("");
foreach (var carouselItem in itemList)
{
SetActiveIfActive(carouselItem);
itemsHtml.AppendLine(carouselItem.Html);
}
output.Content.SetHtmlContent(itemsHtml.ToString());
}
protected virtual void SetControls(TagHelperContext context, TagHelperOutput output, List<CarouselItem> itemList)
{
var html = new StringBuilder("");
html.AppendLine("<a class=\"carousel-control-prev\" href=\"#" + TagHelper.Id + "\" role=\"button\" data-slide=\"prev\">");
html.AppendLine("<span class=\"carousel-control-prev-icon\" aria-hidden=\"true\"></span>");
html.AppendLine("<span class=\"sr-only\">Previous</span>");
html.AppendLine("</a>");
html.AppendLine("<a class=\"carousel-control-next\" href=\"#" + TagHelper.Id + "\" role=\"button\" data-slide=\"next\">");
html.AppendLine("<span class=\"carousel-control-next-icon\" aria-hidden=\"true\"></span>");
html.AppendLine("<span class=\"sr-only\">Next</span>");
html.AppendLine("</a>");
output.PostContent.SetHtmlContent(html.ToString());
}
protected virtual void SetIndicators(TagHelperContext context, TagHelperOutput output, List<CarouselItem> itemList)
{
if (TagHelper.Indicators??false)
{
return;
}
var html = new StringBuilder("<ol class=\"carousel-indicators\">");
for (var i = 0; i < itemList.Count; i++)
{
html.AppendLine(
"<li " +
"data-target=\"#"+TagHelper.Id+"\"" +
" data-slide-to=\""+i+"\"" +
(itemList[i].Active?" class=\"active\">":"") +
"</li>");
}
html.AppendLine("</ol>");
output.PreContent.SetHtmlContent(html.ToString());
}
protected virtual void SetOneItemAsActive(TagHelperContext context, TagHelperOutput output, List<CarouselItem> itemList)
{
if (!itemList.Any(it=> it.Active) && itemList.Count > 0)
{
itemList.FirstOrDefault().Active = true;
}
}
protected virtual void AddBasicAttributes(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.Add("data-ride", "carousel");
output.Attributes.Add("id", TagHelper.Id);
AddBasicClasses(context, output);
}
protected virtual void AddBasicClasses(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.AddClass("carousel");
output.Attributes.AddClass("slide");
SetFadeAnimation(context, output);
}
protected virtual void SetFadeAnimation(TagHelperContext context, TagHelperOutput output)
{
if (TagHelper.Crossfade??false)
{
output.Attributes.AddClass("carousel-fade");
}
}
protected virtual void SetRandomIdIfNotProvided()
{
if (string.IsNullOrWhiteSpace(TagHelper.Id))
{
TagHelper.Id = Guid.NewGuid().ToString("N");
}
}
protected virtual void SetActiveIfActive(CarouselItem item)
{
item.Html = item.Html.Replace(AbpCarouselItemActivePlaceholder, item.Active ? "active" : "");
}
}
}

20
src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Carousel/CarouselItem.cs

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Carousel
{
public class CarouselItem
{
public CarouselItem(string html, bool active)
{
Html = html;
Active = active;
}
public string Html { get; set; }
public bool Active { get; set; }
}
}
Loading…
Cancel
Save