Skip to content

Commit

Permalink
Merge branch 'feature/HtmlHelpers' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Sadler committed Jul 25, 2021
2 parents 298fc06 + c810572 commit a180dd7
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 7 deletions.
2 changes: 1 addition & 1 deletion TestSite.V9/App_Plugins/UmbNav/views/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h5>
</umb-control-group>

<umb-control-group label="@umbnav_customClasses"
ng-if="showDisplay">
ng-if="showCustomClasses">
<input type="text"
localize="placeholder"
class="umb-property-editor umb-textstring"
Expand Down
4 changes: 3 additions & 1 deletion TestSite.V9/Views/Partials/Navigation/TopNavigation.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

@foreach (var item in umbNav)
{
<a class="nav-link " href="@item.Url()" target="@item.Target" rel="@item.Noopener @item.Noreferrer">@item.Title</a>
@*<a class="nav-link " href="@item.Url()" target="@item.Target" rel="@item.Noopener @item.Noreferrer">@item.Title</a>*@
<umbnavitem menu-item="item" class="nav-link" rel="test"></umbnavitem>
@item.GetLinkHtml(htmlAttributes: new {Class="nav-link"})
}
2 changes: 2 additions & 0 deletions TestSite.V9/Views/_ViewImports.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
@using TestSite.V9
@using Umbraco.Cms.Web.Common.PublishedModels
@using Umbraco.Cms.Web.Common.Views
@using UmbNav.Core
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, UmbNav.Core
13 changes: 12 additions & 1 deletion TestSite.V9/uSync/v9/Content/umbnav.config
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"icon": "icon-home color-black",
"published": true,
"naviHide": false,
"culture": "undefined"
"culture": "undefined",
"customClasses": "test"
},
{
"id": 1103,
Expand All @@ -38,6 +39,16 @@
"published": true,
"naviHide": false,
"culture": "undefined"
},
{
"description": "http://www.google.com",
"title": "Google",
"target": "_blank",
"noopener": "noopener",
"noreferrer": "noreferrer",
"url": "http://www.google.com",
"children": [],
"icon": "icon-link"
}
]]]></Value>
</umbNavPE>
Expand Down
4 changes: 3 additions & 1 deletion TestSite.V9/uSync/v9/DataTypes/UmbNavUmbNavPEUmbNav.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"HideNoreferrer": false,
"HideIncludeChildren": false,
"AllowDisplay": false,
"HideLabel": false
"AllowCustomClasses": true,
"HideLabel": false,
"AllowImageIcon": false
}]]></Config>
</DataType>
57 changes: 57 additions & 0 deletions TestSite.V9/umbraco/models/UmbNav.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Umbraco.ModelsBuilder.Embedded v9.0.0-rc001+4f06c1f33a54daf4752a439778626202960db68c
//
// Changes to this file will be lost if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Linq.Expressions;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Infrastructure.ModelsBuilder;
using Umbraco.Cms.Core;
using Umbraco.Extensions;

namespace Umbraco.Cms.Web.Common.PublishedModels
{
/// <summary>UmbNav</summary>
[PublishedModel("umbNav")]
public partial class UmbNav : PublishedContentModel
{
// helpers
#pragma warning disable 0109 // new is redundant
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "9.0.0-rc001+4f06c1f33a54daf4752a439778626202960db68c")]
public new const string ModelTypeAlias = "umbNav";
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "9.0.0-rc001+4f06c1f33a54daf4752a439778626202960db68c")]
public new const PublishedItemType ModelItemType = PublishedItemType.Content;
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "9.0.0-rc001+4f06c1f33a54daf4752a439778626202960db68c")]
public new static IPublishedContentType GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor)
=> PublishedModelUtility.GetModelContentType(publishedSnapshotAccessor, ModelItemType, ModelTypeAlias);
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "9.0.0-rc001+4f06c1f33a54daf4752a439778626202960db68c")]
public static IPublishedPropertyType GetModelPropertyType<TValue>(IPublishedSnapshotAccessor publishedSnapshotAccessor, Expression<Func<UmbNav, TValue>> selector)
=> PublishedModelUtility.GetModelPropertyType(GetModelContentType(publishedSnapshotAccessor), selector);
#pragma warning restore 0109

private IPublishedValueFallback _publishedValueFallback;

// ctor
public UmbNav(IPublishedContent content, IPublishedValueFallback publishedValueFallback)
: base(content, publishedValueFallback)
{
_publishedValueFallback = publishedValueFallback;
}

// properties

///<summary>
/// UmbNavPE
///</summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "9.0.0-rc001+4f06c1f33a54daf4752a439778626202960db68c")]
[ImplementPropertyType("umbNavPE")]
public virtual global::System.Collections.Generic.IEnumerable<global::UmbNav.Core.Models.UmbNavItem> UmbNavPE => this.Value<global::System.Collections.Generic.IEnumerable<global::UmbNav.Core.Models.UmbNavItem>>(_publishedValueFallback, "umbNavPE");
}
}
2 changes: 1 addition & 1 deletion TestSite/App_Plugins/UmbNav/views/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h5>
</umb-control-group>

<umb-control-group label="@umbnav_customClasses"
ng-if="showDisplay">
ng-if="showCustomClasses">
<input type="text"
localize="placeholder"
class="umb-property-editor umb-textstring"
Expand Down
76 changes: 75 additions & 1 deletion src/UmbNav.Core/Extensions/UmbNavItemExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,90 @@
using System;
using System.Collections.Generic;
using UmbNav.Core.Models;
#if NETCOREAPP
using Umbraco.Cms.Core.Models.PublishedContent;
using HtmlHelper = Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper;
using TagBuilder = Microsoft.AspNetCore.Mvc.Rendering.TagBuilder;
using Microsoft.AspNetCore.Html;
#else
using System.Web;
using Umbraco.Core.Models.PublishedContent;

using System.Web.Mvc;
#endif

namespace UmbNav.Core.Extensions
{
public static class UmbNavItemExtensions
{
#if NETCOREAPP
[Obsolete("I see your using Umbraco V9, Why not use the TagHelper <umbnavitem> instead.")]
public static IHtmlContent GetLinkHtml(this UmbNavItem item, string culture = null, UrlMode mode = UrlMode.Default, object htmlAttributes = null)
#else
public static HtmlString GetLinkHtml(this UmbNavItem item, string culture = null, UrlMode mode = UrlMode.Default, object htmlAttributes = null)
#endif
{
var htmlAttributesConverted = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
var tagBuilder = new TagBuilder("a");
tagBuilder.Attributes.Add("href", item.Url(culture, mode));

#if NETCOREAPP
tagBuilder.InnerHtml.Append(item.Title);
#else
tagBuilder.InnerHtml = item.Title;
#endif

if (!string.IsNullOrEmpty(item.Target))
{
tagBuilder.Attributes.Add("target", item.Target);
}

if (!string.IsNullOrEmpty(item.Noopener) || !string.IsNullOrEmpty(item.Noreferrer))
{
var rel = new List<string>();

if (!string.IsNullOrEmpty(item.Noopener))
{
rel.Add(item.Noopener);
}

if (!string.IsNullOrEmpty(item.Noreferrer))
{
rel.Add(item.Noreferrer);
}

if (htmlAttributesConverted.ContainsKey("rel"))
{
var originalRelValue = htmlAttributesConverted["rel"] as string;
htmlAttributesConverted["rel"] = string.Format("{0} {1}", originalRelValue, string.Join(" ", rel));
}
else
{
htmlAttributesConverted.Add("rel", string.Join(" ", rel));
}
}

if (!string.IsNullOrEmpty(item.CustomClasses))
{
if (htmlAttributesConverted.ContainsKey("class"))
{
var originalRelValue = htmlAttributesConverted["class"] as string;
htmlAttributesConverted["class"] = string.Format("{0} {1}", originalRelValue, string.Join(" ", item.CustomClasses));
}
else
{
htmlAttributesConverted.Add("rel", string.Join(" ", item.CustomClasses));
}
}

tagBuilder.MergeAttributes(htmlAttributesConverted);

#if NETCOREAPP
return tagBuilder;
#else
return MvcHtmlString.Create(tagBuilder.ToString());
#endif
}

public static string Url(this UmbNavItem item, string culture = null, UrlMode mode = UrlMode.Default)
{
if (item.Udi != null)
Expand Down
68 changes: 68 additions & 0 deletions src/UmbNav.Core/TagHelpers/UmbnavItemTagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#if NETCOREAPP
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.TagHelpers;
using UmbNav.Core.Extensions;
using UmbNav.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace UmbNav.Core.TagHelpers
{
public class UmbnavitemTagHelper : TagHelper
{
public UmbNavItem MenuItem { get; set; }
public UrlMode Mode { get; set; }
public string Culture { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "a";
output.Attributes.SetAttribute("href", MenuItem.Url(Culture, Mode));
output.Content.SetContent(MenuItem.Title);

if (!string.IsNullOrEmpty(MenuItem.Target))
{
output.Attributes.SetAttribute("target", MenuItem.Target);
}

if (!string.IsNullOrEmpty(MenuItem.Noopener) || !string.IsNullOrEmpty(MenuItem.Noreferrer))
{
var rel = new List<string>();

if (!string.IsNullOrEmpty(MenuItem.Noopener))
{
rel.Add(MenuItem.Noopener);
}

if (!string.IsNullOrEmpty(MenuItem.Noreferrer))
{
rel.Add(MenuItem.Noreferrer);
}

if (output.Attributes["rel"] != null)
{
var originalRelValue = output.Attributes["rel"].Value;
output.Attributes.SetAttribute("rel", string.Format("{0} {1}", originalRelValue, string.Join(" ", rel)));
}
else
{
output.Attributes.SetAttribute("rel", string.Join(" ", rel));
}
}

if (!string.IsNullOrEmpty(MenuItem.CustomClasses))
{
if (output.Attributes["class"] != null)
{
var originalRelValue = output.Attributes["class"].Value;
output.Attributes.SetAttribute("class", string.Format("{0} {1}", originalRelValue, string.Join(" ", MenuItem.CustomClasses)));
}
else
{
output.Attributes.SetAttribute("rel", string.Join(" ", MenuItem.CustomClasses));
}
}

}
}
}
#endif
2 changes: 1 addition & 1 deletion src/UmbNav.Web/App_Plugins/UmbNav/views/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h5>
</umb-control-group>

<umb-control-group label="@umbnav_customClasses"
ng-if="showDisplay">
ng-if="showCustomClasses">
<input type="text"
localize="placeholder"
class="umb-property-editor umb-textstring"
Expand Down

0 comments on commit a180dd7

Please sign in to comment.