Skip to content
This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using uComponents.Core;
using umbraco;
using uComponents.DataTypes.Shared.Enums;
using umbraco.interfaces;

namespace uComponents.DataTypes.EnumCheckBoxList
Expand Down Expand Up @@ -103,49 +101,13 @@ internal EnumCheckBoxListDataEditor(IData data, EnumCheckBoxListOptions options)
/// </summary>
protected override void CreateChildControls()
{
FieldInfo fieldInfo;
ListItem checkBoxListItem;

try
{
var assembly = Helper.IO.GetAssembly(this.options.Assembly);
var type = assembly.GetType(this.options.Enum);

// Loop though enum to create drop down list items
foreach (string name in Enum.GetNames(type))
{
checkBoxListItem = new ListItem(name, name); // Default to the enum item name

fieldInfo = type.GetField(name);

// Loop though any custom attributes that may have been applied the the curent enum item
foreach (var customAttributeData in CustomAttributeData.GetCustomAttributes(fieldInfo))
{
if (customAttributeData.Constructor.DeclaringType != null && customAttributeData.Constructor.DeclaringType.Name == "EnumCheckBoxListAttribute" && customAttributeData.NamedArguments != null)
{
// Loop though each property on the EnumCheckBoxListAttribute
foreach (var customAttributeNamedArguement in customAttributeData.NamedArguments)
{
switch (customAttributeNamedArguement.MemberInfo.Name)
{
case "Text":
checkBoxListItem.Text = customAttributeNamedArguement.TypedValue.Value.ToString();
break;

case "Value":
checkBoxListItem.Value = customAttributeNamedArguement.TypedValue.Value.ToString();
break;

case "Enabled":
checkBoxListItem.Enabled = (bool)customAttributeNamedArguement.TypedValue.Value;
break;
}
}
}
}

this.CheckBoxList.Items.Add(checkBoxListItem);
}
var items = EnumHelper.GetEnumListAttributeValues(type).ToArray();
this.CheckBoxList.Items.AddRange(items);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System;
using System;
using System.Linq;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using uComponents.Core;
using uComponents.DataTypes.Shared.Enums;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.web;
using umbraco.interfaces;

namespace uComponents.DataTypes.EnumDropDownList
Expand Down Expand Up @@ -112,40 +110,8 @@ protected override void CreateChildControls()
var assembly = Helper.IO.GetAssembly(this.options.Assembly);
var type = assembly.GetType(this.options.Enum);

// Loop though enum to create drop down list items
foreach (var name in Enum.GetNames(type))
{
var dropDownListItem = new ListItem(name, name);
var fieldInfo = type.GetField(name);

// Loop though any custom attributes that may have been applied the the curent enum item
foreach (var customAttributeData in CustomAttributeData.GetCustomAttributes(fieldInfo))
{
if (customAttributeData.Constructor.DeclaringType != null && customAttributeData.Constructor.DeclaringType.Name == "EnumDropDownListAttribute" && customAttributeData.NamedArguments != null)
{
// Loop though each property on the EnumDropDownListAttribute
foreach (var customAttributeNamedArguement in customAttributeData.NamedArguments)
{
switch (customAttributeNamedArguement.MemberInfo.Name)
{
case "Text":
dropDownListItem.Text = customAttributeNamedArguement.TypedValue.Value.ToString();
break;

case "Value":
dropDownListItem.Value = customAttributeNamedArguement.TypedValue.Value.ToString();
break;

case "Enabled":
dropDownListItem.Enabled = (bool)customAttributeNamedArguement.TypedValue.Value;
break;
}
}
}
}

this.dropDownList.Items.Add(dropDownListItem);
}
var items = EnumHelper.GetEnumListAttributeValues(type).ToArray();
this.DropDownList.Items.AddRange(items);
}
catch
{
Expand Down
63 changes: 63 additions & 0 deletions src/uComponents.DataTypes/Shared/Enums/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.UI.WebControls;

namespace uComponents.DataTypes.Shared.Enums
{
/// <summary>
/// Helper methods for working with Enums
/// </summary>
public class EnumHelper
{

/// <summary>
/// Gets the Enum of the supplied type and returns its attribute values as a ListItem
/// </summary>
/// <param name="enumType">The full type name of the enum to parse</param>
/// <returns>List of ListItems representing the Enum's attribute values</returns>
public static IEnumerable<ListItem> GetEnumListAttributeValues(Type enumType)
{
var enums = new List<ListItem>();

foreach (var enumName in Enum.GetNames(enumType))
{
var fieldInfo = enumType.GetField(enumName);
var listItem = new ListItem(enumName, enumName); // Default to the enum item name

// Loop though any custom attributes that may have been applied the the curent enum item
foreach (var customAttributeData in CustomAttributeData.GetCustomAttributes(fieldInfo))
{
if (customAttributeData.Constructor.DeclaringType != null && (new []{ "EnumDropDownListAttribute", "EnumCheckBoxListAttribute" }.Contains(customAttributeData.Constructor.DeclaringType.Name) && customAttributeData.NamedArguments != null))
{
// Loop though each property on the EnumDropDownListAttribute
foreach (var customAttributeNamedArguement in customAttributeData.NamedArguments)
{
switch (customAttributeNamedArguement.MemberInfo.Name)
{
case "Text":
listItem.Text = customAttributeNamedArguement.TypedValue.Value.ToString();
break;

case "Value":
listItem.Value = customAttributeNamedArguement.TypedValue.Value.ToString();
break;

case "Enabled":
listItem.Enabled = (bool)customAttributeNamedArguement.TypedValue.Value;
break;
}
}

enums.Add(listItem);
}
}

}

return enums;
}

}
}
1 change: 1 addition & 0 deletions src/uComponents.DataTypes/uComponents.DataTypes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
<Compile Include="Settings.cs" />
<Compile Include="Shared\AjaxUpload\AjaxUploadHandler.cs" />
<Compile Include="Shared\AjaxUpload\AjaxUploadHandlerResource.Designer.cs" />
<Compile Include="Shared\Enums\EnumHelper.cs" />
<Compile Include="Shared\Pages\DirectoryBrowser.aspx.cs">
<DependentUpon>DirectoryBrowser.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
Expand Down
54 changes: 54 additions & 0 deletions src/uComponents.XsltExtensions/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Xml;
using System.Xml.XPath;
using uComponents.Core;
using uComponents.DataTypes.Shared.Enums;
using umbraco;
using Umbraco.Core;

namespace uComponents.XsltExtensions
{
/// <summary>
/// The Enums class exposes XSLT extensions to access data from EnumCheckBoxListAttributes and EnumDropDownListAttributes
/// </summary>
[XsltExtension("ucomponents.enums")]
public class Enums
{

/// <summary>
/// Gets the Enum of the supplied type and returns its attribute values as a ListItem
/// </summary>
/// <param name="assemblyName">Full name of the assembly containing the enum</param>
/// <param name="typeName">The enum's type name</param>
/// <returns>List of ListItems representing the Enum's attribute values</returns>
public static XPathNodeIterator GetEnumListAttributeValues(string assemblyName, string typeName)
{
if (!assemblyName.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
assemblyName += ".dll";

var assembly = Helper.IO.GetAssembly(assemblyName);
var type = assembly.GetType(typeName);
var enumListItems= EnumHelper.GetEnumListAttributeValues(type);

// create the XML document
var xd = new XmlDocument();
xd.LoadXml("<ListItems/>");

// loop through each of the directories
foreach (var enumListItem in enumListItems)
{
var enumNode = XmlHelper.AddTextNode(xd, "ListItem", "");
enumNode.Attributes.Append(XmlHelper.AddAttribute(xd, "Text", enumListItem.Text));
enumNode.Attributes.Append(XmlHelper.AddAttribute(xd, "Value", enumListItem.Value));
enumNode.Attributes.Append(XmlHelper.AddAttribute(xd, "Enabled", enumListItem.Enabled.ToString()));

// add the node to the XML document
xd.DocumentElement.AppendChild(enumNode);
}

// return the XML document
return xd.CreateNavigator().Select("/ListItems");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
<Compile Include="Cms.cs" />
<Compile Include="Dates.cs" />
<Compile Include="Email.cs" />
<Compile Include="Enums.cs" />
<Compile Include="Exceptions.cs" />
<Compile Include="IO.cs" />
<Compile Include="Media.cs" />
Expand All @@ -296,6 +297,10 @@
<Project>{431971FF-DF3B-42AA-9E57-FD5D65ACAD51}</Project>
<Name>uComponents.Core</Name>
</ProjectReference>
<ProjectReference Include="..\uComponents.DataTypes\uComponents.DataTypes.csproj">
<Project>{E83250BD-27B0-4B39-90C8-F07538058938}</Project>
<Name>uComponents.DataTypes</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down