csharp 像Umbraco的API一样的JQuery
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 像Umbraco的API一样的JQuery相关的知识,希望对你有一定的参考价值。
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
}
以上是关于csharp 像Umbraco的API一样的JQuery的主要内容,如果未能解决你的问题,请参考以下文章
csharp 使用Umbraco Examine API搜索多个术语。