U4-6674 - Kill ToContentSet, becomes ToIndexedArray
This commit is contained in:
@@ -452,28 +452,14 @@ namespace Umbraco.Web
|
||||
|
||||
#endregion
|
||||
|
||||
#region ToContentSet
|
||||
#region ToIndexedArray
|
||||
|
||||
/// <summary>
|
||||
/// Returns the content enumerable as a content set.
|
||||
/// </summary>
|
||||
/// <param name="source">The content enumerable.</param>
|
||||
/// <returns>A content set wrapping the content enumerable.</returns>
|
||||
public static PublishedContentSet<T> ToContentSet<T>(this IEnumerable<T> source)
|
||||
where T : class, IPublishedContent
|
||||
public static IndexedArrayItem<TContent>[] ToIndexedArray<TContent>(this IEnumerable<TContent> source)
|
||||
where TContent : class, IPublishedContent
|
||||
{
|
||||
return new PublishedContentSet<T>(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the ordered content enumerable as an ordered content set.
|
||||
/// </summary>
|
||||
/// <param name="source">The ordered content enumerable.</param>
|
||||
/// <returns>A ordered content set wrapping the ordered content enumerable.</returns>
|
||||
public static PublishedContentOrderedSet<T> ToContentSet<T>(this IOrderedEnumerable<T> source)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return new PublishedContentOrderedSet<T>(source);
|
||||
var set = source.Select((content, index) => new IndexedArrayItem<TContent>(content, index)).ToArray();
|
||||
foreach (var setItem in set) setItem.TotalCount = set.Length;
|
||||
return set;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -552,28 +538,6 @@ namespace Umbraco.Web
|
||||
|
||||
#endregion
|
||||
|
||||
#region ContentSet
|
||||
|
||||
public static int Position(this IPublishedContent content)
|
||||
{
|
||||
return content.GetIndex();
|
||||
}
|
||||
|
||||
public static int Index(this IPublishedContent content)
|
||||
{
|
||||
return content.GetIndex();
|
||||
}
|
||||
|
||||
private static int GetIndex(this IPublishedContent content, IEnumerable<IPublishedContent> set)
|
||||
{
|
||||
var index = set.FindIndex(n => n.Id == content.Id);
|
||||
if (index < 0)
|
||||
throw new IndexOutOfRangeException("Could not find content in the content set.");
|
||||
return index;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IsSomething: misc.
|
||||
|
||||
/// <summary>
|
||||
@@ -646,160 +610,6 @@ namespace Umbraco.Web
|
||||
|
||||
#endregion
|
||||
|
||||
#region IsSomething: position in set
|
||||
|
||||
public static bool IsFirst(this IPublishedContent content)
|
||||
{
|
||||
return content.GetIndex() == 0;
|
||||
}
|
||||
|
||||
public static HtmlString IsFirst(this IPublishedContent content, string valueIfTrue)
|
||||
{
|
||||
return content.IsFirst(valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsFirst(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsFirst() ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsNotFirst(this IPublishedContent content)
|
||||
{
|
||||
return content.IsFirst() == false;
|
||||
}
|
||||
|
||||
public static HtmlString IsNotFirst(this IPublishedContent content, string valueIfTrue)
|
||||
{
|
||||
return content.IsNotFirst(valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsNotFirst(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsNotFirst() ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsPosition(this IPublishedContent content, int index)
|
||||
{
|
||||
return content.GetIndex() == index;
|
||||
}
|
||||
|
||||
public static HtmlString IsPosition(this IPublishedContent content, int index, string valueIfTrue)
|
||||
{
|
||||
return content.IsPosition(index, valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsPosition(this IPublishedContent content, int index, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsPosition(index) ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsModZero(this IPublishedContent content, int modulus)
|
||||
{
|
||||
return content.GetIndex() % modulus == 0;
|
||||
}
|
||||
|
||||
public static HtmlString IsModZero(this IPublishedContent content, int modulus, string valueIfTrue)
|
||||
{
|
||||
return content.IsModZero(modulus, valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsModZero(this IPublishedContent content, int modulus, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsModZero(modulus) ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsNotModZero(this IPublishedContent content, int modulus)
|
||||
{
|
||||
return content.IsModZero(modulus) == false;
|
||||
}
|
||||
|
||||
public static HtmlString IsNotModZero(this IPublishedContent content, int modulus, string valueIfTrue)
|
||||
{
|
||||
return content.IsNotModZero(modulus, valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsNotModZero(this IPublishedContent content, int modulus, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsNotModZero(modulus) ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsNotPosition(this IPublishedContent content, int index)
|
||||
{
|
||||
return content.IsPosition(index) == false;
|
||||
}
|
||||
|
||||
public static HtmlString IsNotPosition(this IPublishedContent content, int index, string valueIfTrue)
|
||||
{
|
||||
return content.IsNotPosition(index, valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsNotPosition(this IPublishedContent content, int index, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsNotPosition(index) ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsLast(this IPublishedContent content)
|
||||
{
|
||||
return content.GetIndex() == content.ContentSet.Count() - 1;
|
||||
}
|
||||
|
||||
public static HtmlString IsLast(this IPublishedContent content, string valueIfTrue)
|
||||
{
|
||||
return content.IsLast(valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsLast(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsLast() ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsNotLast(this IPublishedContent content)
|
||||
{
|
||||
return content.IsLast() == false;
|
||||
}
|
||||
|
||||
public static HtmlString IsNotLast(this IPublishedContent content, string valueIfTrue)
|
||||
{
|
||||
return content.IsNotLast(valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsNotLast(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsNotLast() ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsEven(this IPublishedContent content)
|
||||
{
|
||||
return content.GetIndex() % 2 == 0;
|
||||
}
|
||||
|
||||
public static HtmlString IsEven(this IPublishedContent content, string valueIfTrue)
|
||||
{
|
||||
return content.IsEven(valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsEven(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsEven() ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
public static bool IsOdd(this IPublishedContent content)
|
||||
{
|
||||
return content.GetIndex() % 2 == 1;
|
||||
}
|
||||
|
||||
public static HtmlString IsOdd(this IPublishedContent content, string valueIfTrue)
|
||||
{
|
||||
return content.IsOdd(valueIfTrue, string.Empty);
|
||||
}
|
||||
|
||||
public static HtmlString IsOdd(this IPublishedContent content, string valueIfTrue, string valueIfFalse)
|
||||
{
|
||||
return new HtmlString(content.IsOdd() ? valueIfTrue : valueIfFalse);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IsSomething: equality
|
||||
|
||||
public static bool IsEqual(this IPublishedContent content, IPublishedContent other)
|
||||
@@ -1440,250 +1250,6 @@ namespace Umbraco.Web
|
||||
return content.Descendant(contentTypeAlias);
|
||||
}
|
||||
|
||||
// next pseudo-axe ~ following within the content set
|
||||
// bogus, kept for backward compatibility but we should get rid of it
|
||||
|
||||
public static IPublishedContent Next(this IPublishedContent content)
|
||||
{
|
||||
return content.ContentSet.ElementAtOrDefault(content.GetIndex() + 1);
|
||||
}
|
||||
|
||||
public static IPublishedContent Next(this IPublishedContent current, Func<IPublishedContent, bool> func) {
|
||||
IPublishedContent next = current.Next();
|
||||
while (next != null) {
|
||||
if (func(next)) return next;
|
||||
next = next.Next();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IPublishedContent Next(this IPublishedContent content, int number)
|
||||
{
|
||||
if (number < 0)
|
||||
throw new ArgumentOutOfRangeException("number", "Must be greater than, or equal to, zero.");
|
||||
return number == 0 ? content : content.ContentSet.ElementAtOrDefault(content.GetIndex() + number);
|
||||
}
|
||||
|
||||
public static IPublishedContent Next(this IPublishedContent content, string contentTypeAlias)
|
||||
{
|
||||
return content.Next(contentTypeAlias, false);
|
||||
}
|
||||
|
||||
public static IPublishedContent Next(this IPublishedContent content, string contentTypeAlias, bool wrap)
|
||||
{
|
||||
return content.Next(content.ContentSet, x => x.DocumentTypeAlias.InvariantEquals(contentTypeAlias), wrap);
|
||||
}
|
||||
|
||||
public static T Next<T>(this IPublishedContent content)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Next<T>(false);
|
||||
}
|
||||
|
||||
public static T Next<T>(this IPublishedContent content, bool wrap)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Next(content.ContentSet, x => x is T, wrap) as T;
|
||||
}
|
||||
|
||||
static IPublishedContent Next(this IPublishedContent content, IEnumerable<IPublishedContent> axis, Func<IPublishedContent, bool> predicate, bool wrap)
|
||||
{
|
||||
var b4 = true;
|
||||
IPublishedContent wrapped = null;
|
||||
foreach (var c in axis)
|
||||
{
|
||||
if (b4)
|
||||
{
|
||||
if (c.Id == content.Id)
|
||||
b4 = false;
|
||||
else if (wrap && wrapped == null && predicate(c))
|
||||
wrapped = c;
|
||||
continue;
|
||||
}
|
||||
if (predicate(c))
|
||||
return c;
|
||||
}
|
||||
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
// previous pseudo-axe ~ preceding within the content set
|
||||
// bogus, kept for backward compatibility but we should get rid of it
|
||||
|
||||
public static IPublishedContent Previous(this IPublishedContent content)
|
||||
{
|
||||
return content.ContentSet.ElementAtOrDefault(content.GetIndex() - 1);
|
||||
}
|
||||
|
||||
public static IPublishedContent Previous(this IPublishedContent current, Func<IPublishedContent, bool> func) {
|
||||
IPublishedContent prev = current.Previous();
|
||||
while (prev != null) {
|
||||
if (func(prev)) return prev;
|
||||
prev = prev.Previous();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IPublishedContent Previous(this IPublishedContent content, int number)
|
||||
{
|
||||
if (number < 0)
|
||||
throw new ArgumentOutOfRangeException("number", "Must be greater than, or equal to, zero.");
|
||||
return number == 0 ? content : content.ContentSet.ElementAtOrDefault(content.GetIndex() - number);
|
||||
}
|
||||
|
||||
public static IPublishedContent Previous(this IPublishedContent content, string contentTypeAlias)
|
||||
{
|
||||
return content.Previous(contentTypeAlias, false);
|
||||
}
|
||||
|
||||
public static IPublishedContent Previous(this IPublishedContent content, string contentTypeAlias, bool wrap)
|
||||
{
|
||||
return content.Next(content.ContentSet.Reverse(), x => x.DocumentTypeAlias.InvariantEquals(contentTypeAlias), wrap);
|
||||
}
|
||||
|
||||
public static T Previous<T>(this IPublishedContent content)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Previous<T>(false);
|
||||
}
|
||||
|
||||
public static T Previous<T>(this IPublishedContent content, bool wrap)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Next(content.ContentSet.Reverse(), x => x is T, wrap) as T;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
[Obsolete("Obsolete, use FollowingSibling or PrecedingSibling instead.")]
|
||||
public static IPublishedContent Sibling(this IPublishedContent content, int number)
|
||||
{
|
||||
if (number < 0)
|
||||
throw new ArgumentOutOfRangeException("number", "Must be greater than, or equal to, zero.");
|
||||
number += 1; // legacy is zero-based
|
||||
return content.FollowingSibling(number);
|
||||
}
|
||||
|
||||
// contentTypeAlias is case-insensitive
|
||||
[Obsolete("Obsolete, use FollowingSibling or PrecedingSibling instead.")]
|
||||
public static IPublishedContent Sibling(this IPublishedContent content, string contentTypeAlias)
|
||||
{
|
||||
// note: the original implementation seems to loop on all siblings
|
||||
// ie if it reaches the end of the set, it starts again at the beginning.
|
||||
// so here we wrap, although it's not consistent... but anyway those
|
||||
// methods should be obsoleted.
|
||||
|
||||
return content.FollowingSibling(contentTypeAlias, true);
|
||||
}
|
||||
|
||||
// following-sibling, preceding-sibling axes
|
||||
|
||||
public static IPublishedContent FollowingSibling(this IPublishedContent content)
|
||||
{
|
||||
return content.Siblings().ElementAtOrDefault(content.GetIndex(content.Siblings()) + 1);
|
||||
}
|
||||
|
||||
public static IPublishedContent FollowingSibling(this IPublishedContent content, int number)
|
||||
{
|
||||
if (number < 0)
|
||||
throw new ArgumentOutOfRangeException("number", "Must be greater than, or equal to, zero.");
|
||||
return number == 0 ? content : content.Siblings().ElementAtOrDefault(content.GetIndex(content.Siblings()) + number);
|
||||
}
|
||||
|
||||
// contentTypeAlias is case-insensitive
|
||||
public static IPublishedContent FollowingSibling(this IPublishedContent content, string contentTypeAlias)
|
||||
{
|
||||
return content.FollowingSibling(contentTypeAlias, false);
|
||||
}
|
||||
|
||||
// contentTypeAlias is case-insensitive
|
||||
// note: not sure that one makes a lot of sense but it is here for backward compatibility
|
||||
public static IPublishedContent FollowingSibling(this IPublishedContent content, string contentTypeAlias, bool wrap)
|
||||
{
|
||||
return content.Next(content.Siblings(), x => x.DocumentTypeAlias.InvariantEquals(contentTypeAlias), wrap);
|
||||
}
|
||||
|
||||
public static T FollowingSibling<T>(this IPublishedContent content)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.FollowingSibling<T>(false);
|
||||
}
|
||||
|
||||
public static T FollowingSibling<T>(this IPublishedContent content, bool wrap)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Next(content.Siblings(), x => x is T, wrap) as T;
|
||||
}
|
||||
|
||||
public static IPublishedContent PrecedingSibling(this IPublishedContent content)
|
||||
{
|
||||
return content.Siblings().ElementAtOrDefault(content.GetIndex(content.Siblings()) - 1);
|
||||
}
|
||||
|
||||
public static IPublishedContent PrecedingSibling(this IPublishedContent content, int number)
|
||||
{
|
||||
if (number < 0)
|
||||
throw new ArgumentOutOfRangeException("number", "Must be greater than, or equal to, zero.");
|
||||
return number == 0 ? content : content.Siblings().ElementAtOrDefault(content.GetIndex(content.Siblings()) - number);
|
||||
}
|
||||
|
||||
// contentTypeAlias is case-insensitive
|
||||
public static IPublishedContent PrecedingSibling(this IPublishedContent content, string contentTypeAlias)
|
||||
{
|
||||
return content.PrecedingSibling(contentTypeAlias, false);
|
||||
}
|
||||
|
||||
// contentTypeAlias is case-insensitive
|
||||
// note: not sure that one makes a lot of sense but it is here for backward compatibility
|
||||
public static IPublishedContent PrecedingSibling(this IPublishedContent content, string contentTypeAlias, bool wrap)
|
||||
{
|
||||
return content.Next(content.Siblings().Reverse(), x => x.DocumentTypeAlias.InvariantEquals(contentTypeAlias), wrap);
|
||||
}
|
||||
|
||||
public static T PrecedingSibling<T>(this IPublishedContent content)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.PrecedingSibling<T>(false);
|
||||
}
|
||||
|
||||
public static T PrecedingSibling<T>(this IPublishedContent content, bool wrap)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Next(content.Siblings().Reverse(), x => x is T, wrap) as T;
|
||||
}
|
||||
|
||||
// following, preceding axes - NOT IMPLEMENTED
|
||||
|
||||
// utilities
|
||||
|
||||
public static IEnumerable<IPublishedContent> Siblings(this IPublishedContent content)
|
||||
{
|
||||
// content.Parent, content.Children and cache.GetAtRoot() should be fast enough,
|
||||
// or cached by the content cache, so that we don't have to implement cache here.
|
||||
|
||||
// returns the true tree siblings, even if the content is in a set
|
||||
// get the root docs if parent is null
|
||||
|
||||
// note: I don't like having to refer to the "current" content cache here, but
|
||||
// what else? would need root content to have a special, non-null but hidden,
|
||||
// parent...
|
||||
|
||||
|
||||
|
||||
var siblings = content.Parent == null
|
||||
? content.ItemType == PublishedItemType.Media ? UmbracoContext.Current.MediaCache.GetAtRoot() : UmbracoContext.Current.ContentCache.GetAtRoot()
|
||||
: content.Parent.Children;
|
||||
|
||||
// make sure we run it once
|
||||
return siblings.ToArray();
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Siblings<T>(this IPublishedContent content)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Siblings().OfType<T>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Axes: parent
|
||||
|
||||
Reference in New Issue
Block a user