Created
February 21, 2019 22:26
-
-
Save danielmackay/564b68ca9e28be16008c3dfac7b830f9 to your computer and use it in GitHub Desktop.
Dynamic Page Type Selector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ContentType( | |
DisplayName = "Global Search", | |
GUID = "1B83AF7C-BC4B-4A13-8AAB-B9313D25D284", | |
GroupName = Global.PageGroups.Content, | |
Description = "")] | |
public class GlobalSearchPage : SitePageBase, IAllowedPageType | |
{ | |
[Display( | |
Name = "Page Types", | |
GroupName = SystemTabNames.Content, | |
Order = 10)] | |
[SelectMany(SelectionFactoryType = typeof(SearchPageTypeSelectionFactory))] | |
public virtual string PageTypes { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InterfaceHelper | |
{ | |
public static IEnumerable<Type> GetAllTypes<TInterface>() | |
{ | |
return AppDomain.CurrentDomain | |
.GetAssemblies() | |
.SelectMany(x => x.GetTypes()) | |
.Where(x => typeof(TInterface).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract && !x.Name.Contains("Proxy")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SearchPageTypeSelectionFactory : ISelectionFactory | |
{ | |
private static IEnumerable<Type> Posts => InterfaceHelper.GetAllTypes<IPostSearch>(); | |
private static IEnumerable<Type> Pages => InterfaceHelper.GetAllTypes<IPageSearch>(); | |
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata) | |
{ | |
var types = Posts.Union(Pages); | |
return types.Select(t => new SelectItem { Text = t.Name.ToProperCase(), Value = t }); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StringExtensions | |
{ | |
// Capitalize the first character and add a space before each capitalized letter (except the first character). | |
public static string ToProperCase(this string strText) | |
{ | |
// If there are 0 or 1 characters, just return the string. | |
if (strText == null) return strText; | |
if (strText.Length < 2) return strText.ToUpper(); | |
// Start with the first character. | |
var result = strText.Substring(0, 1).ToUpper(); | |
// Add the remaining characters. | |
for (var i = 1; i < strText.Length; i++) | |
{ | |
if (char.IsUpper(strText[i])) result += " "; | |
result += strText[i]; | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment