Skip to content

Commit

Permalink
Changed attributes collection to be <string, object> rather than <str…
Browse files Browse the repository at this point in the history
…ing, string> to allow arbitrary objects to be attached to a node. See #169.
  • Loading branch information
NightOwl888 committed Jun 19, 2013
1 parent 2261784 commit fd4cf02
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/MvcSiteMapProvider/MvcMusicStore/Code/BlingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static string Bling(this MvcSiteMapHtmlHelper helper)
var node = SiteMaps.Current.CurrentNode;
if (node != null && node.Attributes.ContainsKey("bling"))
{
return node.Attributes["bling"];
return node.Attributes["bling"].GetType().Equals(typeof(string)) ? node.Attributes["bling"].ToString() : string.Empty;
}
return string.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromProviderNode(ISiteMap siteMap,
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
protected virtual void AcquireAttributesFrom(System.Web.SiteMapNode node, IDictionary<string, string> attributes)
protected virtual void AcquireAttributesFrom(System.Web.SiteMapNode node, IDictionary<string, object> attributes)
{
// Unfortunately, the ASP.NET implementation uses a protected member variable to store
// the attributes, so there is no way to loop through them without reflection or some
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMa
/// </summary>
/// <param name="attribute">The attribute.</param>
/// <returns></returns>
protected virtual void AcquireAttributesFrom(IMvcSiteMapNodeAttribute attribute, IDictionary<string, string> attributes)
protected virtual void AcquireAttributesFrom(IMvcSiteMapNodeAttribute attribute, IDictionary<string, object> attributes)
{
foreach (var att in attribute.Attributes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromXmlElement(ISiteMap siteMap, XE
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
protected virtual void AcquireAttributesFrom(XElement node, IDictionary<string, string> attributes)
protected virtual void AcquireAttributesFrom(XElement node, IDictionary<string, object> attributes)
{
foreach (XAttribute attribute in node.Attributes())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace MvcSiteMapProvider.Collections.Specialized
/// localization of custom attributes.
/// </summary>
public class AttributeCollection
: RequestCacheableDictionary<string, string>, IAttributeCollection
: RequestCacheableDictionary<string, object>, IAttributeCollection
{
public AttributeCollection(
ISiteMap siteMap,
Expand All @@ -29,72 +29,82 @@ IRequestCache requestCache

protected readonly ILocalizationService localizationService;

public override void Add(string key, string value)
public override void Add(string key, object value)
{
if (this.IsReadOnly)
{
throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly));
}
value = localizationService.ExtractExplicitResourceKey(key, value);
ThrowIfReadOnly();
if (value.GetType().Equals(typeof(string)))
value = localizationService.ExtractExplicitResourceKey(key, value.ToString());
base.Add(key, value);
}

public override void Clear()
{
if (this.IsReadOnly)
{
throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly));
}
ThrowIfReadOnly();
foreach (var key in this.Keys)
{
localizationService.RemoveResourceKey(key);
}
base.Clear();
}

protected override void Insert(string key, string value, bool add)
protected override void Insert(string key, object value, bool add)
{
if (this.IsReadOnly)
{
throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly));
}
value = localizationService.ExtractExplicitResourceKey(key, value);
ThrowIfReadOnly();
if (value.GetType().Equals(typeof(string)))
value = localizationService.ExtractExplicitResourceKey(key, value.ToString());
base.Insert(key, value, add);
}

public override bool Remove(KeyValuePair<string, string> item)
public override bool Remove(KeyValuePair<string, object> item)
{
if (this.IsReadOnly)
{
throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly));
}
ThrowIfReadOnly();
localizationService.RemoveResourceKey(item.Key);
return base.Remove(item);
}

public override bool Remove(string key)
{
if (this.IsReadOnly)
{
throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly));
}
ThrowIfReadOnly();
localizationService.RemoveResourceKey(key);
return base.Remove(key);
}

public override string this[string key]
public override object this[string key]
{
get
{
return localizationService.GetResourceString(key, base[key], base.siteMap);
var value = base[key];
if (value.GetType().Equals(typeof(string)))
{
return localizationService.GetResourceString(key, base[key].ToString(), base.siteMap);
}
else
{
return value;
}
}
set
{
if (this.IsReadOnly)
{
throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapNodeReadOnly, key));
}
base[key] = localizationService.ExtractExplicitResourceKey(key, value);
if (value.GetType().Equals(typeof(string)))
{
base[key] = localizationService.ExtractExplicitResourceKey(key, value.ToString());
}
else
{
base[key] = value;
}
}
}

protected virtual void ThrowIfReadOnly()
{
if (this.IsReadOnly)
{
throw new InvalidOperationException(String.Format(Resources.Messages.SiteMapReadOnly));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace MvcSiteMapProvider.Collections.Specialized
/// Contract for the specialized collection for dealing with custom site map attributes.
/// </summary>
public interface IAttributeCollection
: IDictionary<string, string>
: IDictionary<string, object>
{
void CopyTo(IDictionary<string, string> destination);
void CopyTo(IDictionary<string, object> destination);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public class FilteredSiteMapNodeVisibilityProvider
public override bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata)
{
// Is a visibility attribute specified?
string visibility = node.Attributes["visibility"];
string visibility = string.Empty;
if (node.Attributes.ContainsKey("visibility"))
{
visibility = node.Attributes["visibility"].GetType().Equals(typeof(string)) ? node.Attributes["visibility"].ToString() : string.Empty;
}
if (string.IsNullOrEmpty(visibility))
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public SiteMapNodeModel(ISiteMapNode node, IDictionary<string, object> sourceMet
/// Gets or sets the meta attributes.
/// </summary>
/// <value>The meta attributes.</value>
public IDictionary<string, string> Attributes { get; protected set; }
public IDictionary<string, object> Attributes { get; protected set; }

/// <summary>
/// Gets or sets the source metadata generated by the HtmlHelper.
Expand Down

0 comments on commit fd4cf02

Please sign in to comment.