Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional parameter to add a default a-tag class to the MvcLink… #21

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
6 changes: 4 additions & 2 deletions src/DeloitteDigital.Atlas/Mvc/HtmlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public static MvcEditFrame BeginEditFrame<T>(this HtmlHelper<T> helper, string d
return frame;
}

public static IDisposable BeginLink(this HtmlHelper htmlHelper, ILinkFieldRenderingString linkField, string alternateTag = null, bool skipInPageEditor = false)
public static IDisposable BeginLink(this HtmlHelper htmlHelper, ILinkFieldRenderingString linkField, string alternateTag = null, bool skipInPageEditor = false, bool alternateTagInPageEditor = false, string linkTagClass = null)
{
if (skipInPageEditor && global::Sitecore.Context.PageMode.IsExperienceEditor)
return new EmptyMvcLink();
return new MvcLink(htmlHelper.ViewContext, linkField, alternateTag);
if (alternateTagInPageEditor && global::Sitecore.Context.PageMode.IsExperienceEditor)
return new MvcLink(htmlHelper.ViewContext, null, alternateTag, linkTagClass);
return new MvcLink(htmlHelper.ViewContext, linkField, alternateTag, linkTagClass);
}
}
}
23 changes: 18 additions & 5 deletions src/DeloitteDigital.Atlas/Mvc/MvcLink.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using DeloitteDigital.Atlas.FieldRendering;

Expand All @@ -16,24 +17,36 @@ internal class MvcLink : IDisposable
private readonly ViewContext viewContext;
private readonly TagBuilder tagBuilder;

public MvcLink(ViewContext viewContext, ILinkFieldRenderingString linkField, string alternateTag)
public MvcLink(ViewContext viewContext, ILinkFieldRenderingString linkField, string alternateTag, string linkTagClass = null)
{
this.viewContext = viewContext;

if (linkField?.Url != null)
if (!string.IsNullOrWhiteSpace(linkField?.Url))
{
// link given - render an anchor tag
this.tagBuilder = new TagBuilder("a");
this.tagBuilder.Attributes.Add("href", linkField.Url);
// add optional attributes
if (!string.IsNullOrWhiteSpace(linkField.Target))
this.tagBuilder.Attributes.Add("target", linkField.Target);
if (!string.IsNullOrWhiteSpace(linkField.Class))
this.tagBuilder.Attributes.Add("class", linkField.Class);
if (!string.IsNullOrWhiteSpace(linkField.Description))
this.tagBuilder.Attributes.Add("title", linkField.Description);

if (!string.IsNullOrWhiteSpace(linkField.Class) && !string.IsNullOrWhiteSpace(linkTagClass))
{
var classes = new HashSet<string>();
foreach (var linkClass in linkField.Class.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
classes.Add(linkClass);
foreach (var tagClass in linkTagClass.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
classes.Add(tagClass);
this.tagBuilder.Attributes.Add("class", string.Join(" ", classes));
}
else if (!string.IsNullOrWhiteSpace(linkField.Class))
this.tagBuilder.Attributes.Add("class", linkField.Class);
else if (!string.IsNullOrWhiteSpace(linkTagClass))
this.tagBuilder.Attributes.Add("class", linkTagClass);
}
else if (string.IsNullOrWhiteSpace(alternateTag))
else if (!string.IsNullOrWhiteSpace(alternateTag))
{
// no link given - render the alternate tag if provided
this.tagBuilder = new TagBuilder(alternateTag);
Expand Down
6 changes: 3 additions & 3 deletions src/DeloitteDigital.Atlas/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Deloitte Digital")]
[assembly: AssemblyProduct("DeloitteDigital.Atlas")]
[assembly: AssemblyCopyright("Copyright © Deloitte Digital 2016")]
[assembly: AssemblyCopyright("Copyright © Deloitte Digital 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]