Last active
August 29, 2015 13:57
-
-
Save mattbrailsford/9494173 to your computer and use it in GitHub Desktop.
A JQuery like API for Umbraco
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.WebPages; | |
using Umbraco.Core.Models; | |
using Umbraco.Web; | |
namespace Our.Umbraco.Api.Jq | |
{ | |
public static class PublishedContentExtensions | |
{ | |
#region Parents | |
public static IEnumerable<IPublishedContent> Parents(this IPublishedContent content) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Ancestors(content); | |
} | |
public static IEnumerable<IPublishedContent> Parents(this IPublishedContent content, int level) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Ancestors(content, level); | |
} | |
public static IEnumerable<IPublishedContent> Parents(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Ancestors(content, contentTypeAlias); | |
} | |
public static IEnumerable<IPublishedContent> Parents(this IPublishedContent content, | |
Func<IPublishedContent, bool> filter) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Ancestors(content).Where(filter); | |
} | |
#endregion | |
#region Closest | |
public static IPublishedContent Closest(this IPublishedContent content, int level) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.AncestorOrSelf(content, level); | |
} | |
public static IPublishedContent Closest(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.AncestorOrSelf(content, contentTypeAlias); | |
} | |
public static IPublishedContent Closest(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.AncestorsOrSelf(content).FirstOrDefault(filter); | |
} | |
#endregion | |
#region Children | |
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content) | |
{ | |
return content.Children; | |
} | |
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return content.Children.Where(x => x.DocumentTypeAlias == contentTypeAlias); | |
} | |
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
return content.Children.Where(filter); | |
} | |
#endregion | |
#region Find | |
public static IEnumerable<IPublishedContent> Find(this IPublishedContent content, int level) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Descendants(content, level); | |
} | |
public static IEnumerable<IPublishedContent> Find(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Descendants(content, contentTypeAlias); | |
} | |
public static IEnumerable<IPublishedContent> Find(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Descendants(content).Where(filter); | |
} | |
#endregion | |
#region Siblings | |
public static IEnumerable<IPublishedContent> Siblings(this IPublishedContent content) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Siblings(content); | |
} | |
public static IEnumerable<IPublishedContent> Siblings(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Siblings(content).Where(x => x.DocumentTypeAlias == contentTypeAlias); | |
} | |
public static IEnumerable<IPublishedContent> Siblings(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Siblings(content).Where(filter); | |
} | |
#endregion | |
#region Next | |
public static IPublishedContent Next(this IPublishedContent content) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Next(content); | |
} | |
public static IPublishedContent Next(this IPublishedContent content, int index) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Next(content, index); | |
} | |
public static IPublishedContent Next(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Next(content, contentTypeAlias); | |
} | |
public static IPublishedContent Next(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
return content.NextAll().FirstOrDefault(filter); | |
} | |
#endregion | |
#region NextAll | |
public static IEnumerable<IPublishedContent> NextAll(this IPublishedContent content) | |
{ | |
var siblings = global::Umbraco.Web.PublishedContentExtensions.Siblings(content).ToList(); | |
var index = siblings.FindIndex(x => x.Id == content.Id); | |
return siblings.Skip(index + 1); | |
} | |
public static IEnumerable<IPublishedContent> NextAll(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return content.NextAll().Where(x => x.DocumentTypeAlias == contentTypeAlias); | |
} | |
public static IEnumerable<IPublishedContent> NextAll(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
return content.NextAll().Where(filter); | |
} | |
#endregion | |
#region NextUntil | |
public static IEnumerable<IPublishedContent> NextUntil(this IPublishedContent content, int index) | |
{ | |
var target = Next(content, index); | |
return content.NextAll().TakeWhile(next => target == null || next.Id != target.Id); | |
} | |
public static IEnumerable<IPublishedContent> NextUntil(this IPublishedContent content, string contentTypeAlias) | |
{ | |
var target = Next(content, contentTypeAlias); | |
return content.NextAll().TakeWhile(next => target == null || next.Id != target.Id); | |
} | |
public static IEnumerable<IPublishedContent> NextUntil(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
var target = Next(content, filter); | |
return content.NextAll().TakeWhile(next => target == null || next.Id != target.Id); | |
} | |
#endregion | |
#region Prev | |
public static IPublishedContent Prev(this IPublishedContent content) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Previous(content); | |
} | |
public static IPublishedContent Prev(this IPublishedContent content, int index) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Previous(content, index); | |
} | |
public static IPublishedContent Prev(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return global::Umbraco.Web.PublishedContentExtensions.Previous(content, contentTypeAlias); | |
} | |
public static IPublishedContent Prev(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
return content.PrevAll().FirstOrDefault(filter); | |
} | |
#endregion | |
#region PrevAll | |
public static IEnumerable<IPublishedContent> PrevAll(this IPublishedContent content) | |
{ | |
var siblings = global::Umbraco.Web.PublishedContentExtensions.Siblings(content).ToList(); | |
siblings.Reverse(); | |
var index = siblings.FindIndex(x => x.Id == content.Id); | |
return siblings.Skip(index + 1); | |
} | |
public static IEnumerable<IPublishedContent> PrevAll(this IPublishedContent content, string contentTypeAlias) | |
{ | |
return content.PrevAll().Where(x => x.DocumentTypeAlias == contentTypeAlias); | |
} | |
public static IEnumerable<IPublishedContent> PrevAll(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
return content.PrevAll().Where(filter); | |
} | |
#endregion | |
#region PrevUntil | |
public static IEnumerable<IPublishedContent> PrevUntil(this IPublishedContent content, int index) | |
{ | |
var target = Prev(content, index); | |
return content.PrevAll().TakeWhile(next => target == null || next.Id != target.Id); | |
} | |
public static IEnumerable<IPublishedContent> PrevUntil(this IPublishedContent content, string contentTypeAlias) | |
{ | |
var target = Prev(content, contentTypeAlias); | |
return content.PrevAll().TakeWhile(next => target == null || next.Id != target.Id); | |
} | |
public static IEnumerable<IPublishedContent> PrevUntil(this IPublishedContent content, Func<IPublishedContent, bool> filter) | |
{ | |
var target = Prev(content, filter); | |
return content.PrevAll().TakeWhile(next => target == null || next.Id != target.Id); | |
} | |
#endregion | |
#region Get | |
public static object Get(this IPublishedContent content, string alias, bool recursive = false, object defaultValue = null) | |
{ | |
return content.GetPropertyValue(alias, recursive, defaultValue); | |
} | |
public static T Get<T>(this IPublishedContent content, string alias, bool recursive = false, T defaultValue = default(T)) | |
{ | |
return content.GetPropertyValue<T>(alias, recursive, defaultValue); | |
} | |
#endregion | |
#region Each | |
public static void Each<T>(this IEnumerable<T> source, | |
Action<IndexedItem<T>> action) | |
{ | |
var count = 0; | |
foreach (var item in source) | |
{ | |
action(new IndexedItem<T>(count++, item)); | |
} | |
} | |
public static HelperResult Each<T>(this IEnumerable<T> source, | |
Func<IndexedItem<T>, HelperResult> template) | |
{ | |
return new HelperResult(writer => | |
{ | |
var count = 0; | |
foreach (var item in source) | |
{ | |
template(new IndexedItem<T>(count++, item)) | |
.WriteTo(writer); | |
} | |
}); | |
} | |
#endregion | |
} | |
#region Helper Classes | |
public class IndexedItem<TModel> | |
{ | |
public IndexedItem(int index, TModel item) | |
{ | |
Index = index; | |
Item = item; | |
} | |
public int Index { get; private set; } | |
public TModel Item { get; private set; } | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment