Files
Umbraco-CMS/src/Umbraco.Web/Models/DynamicPublishedContent.cs

1319 lines
39 KiB
C#
Raw Normal View History

2013-09-17 18:15:19 +02:00
// fixme - should #define - but when will it be OK?
2013-09-05 17:47:13 +02:00
#undef FIX_GET_PROPERTY_VALUE
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
2013-09-05 17:47:13 +02:00
using System.Runtime.CompilerServices;
using System.Web;
using Umbraco.Core.Dynamics;
using Umbraco.Core.Models;
using Umbraco.Core;
using System.Reflection;
2013-09-05 17:47:13 +02:00
using Umbraco.Core.Models.PublishedContent;
using ContentType = umbraco.cms.businesslogic.ContentType;
namespace Umbraco.Web.Models
{
/// <summary>
/// The base dynamic model for views
/// </summary>
[DebuggerDisplay("Content Id: {Id}, Name: {Name}")]
2013-09-05 17:47:13 +02:00
public class DynamicPublishedContent : DynamicObject, IPublishedContent
{
protected internal IPublishedContent PublishedContent { get; private set; }
2013-09-17 18:15:19 +02:00
private DynamicPublishedContentList _contentList;
2013-09-05 17:47:13 +02:00
2013-09-17 18:15:19 +02:00
// must implement that one if we implement IPublishedContent
2013-09-05 17:47:13 +02:00
public IEnumerable<IPublishedContent> ContentSet
{
2013-09-17 18:15:19 +02:00
// that is a definitively non-efficient way of doing it, though it should work
get { return _contentList ?? (_contentList = new DynamicPublishedContentList(PublishedContent.ContentSet)); }
2013-09-05 17:47:13 +02:00
}
public PublishedContentType ContentType { get { return PublishedContent.ContentType; } }
#region Constructors
public DynamicPublishedContent(IPublishedContent content)
{
if (content == null) throw new ArgumentNullException("content");
PublishedContent = content;
2013-09-17 18:15:19 +02:00
}
2013-09-05 17:47:13 +02:00
2013-09-17 18:15:19 +02:00
internal DynamicPublishedContent(IPublishedContent content, DynamicPublishedContentList contentList)
{
PublishedContent = content;
_contentList = contentList;
}
#endregion
2013-09-05 17:47:13 +02:00
// these two here have leaked in v6 and so we cannot remove them anymore
// without breaking compatibility but... TODO: remove them in v7
public DynamicPublishedContentList ChildrenAsList { get { return Children; } }
public int parentId { get { return PublishedContent.Parent.Id; } }
2013-09-05 17:47:13 +02:00
#region DynamicObject
2013-09-05 17:47:13 +02:00
private readonly ConcurrentDictionary<string, object> _cachedMemberOutput = new ConcurrentDictionary<string, object>();
2013-09-05 17:47:13 +02:00
/// <summary>
/// Attempts to call a method on the dynamic object
/// </summary>
/// <param name="binder"></param>
/// <param name="args"></param>
/// <param name="result"></param>
/// <returns></returns>
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var attempt = DynamicInstanceHelper.TryInvokeMember(this, binder, args, new[]
{
typeof(DynamicPublishedContent)
});
if (attempt.Success)
{
result = attempt.Result.ObjectResult;
//need to check the return type and possibly cast if result is from an extension method found
if (attempt.Result.Reason == DynamicInstanceHelper.TryInvokeMemberSuccessReason.FoundExtensionMethod)
{
//we don't need to cast if it is already DynamicPublishedContent
if (attempt.Result.ObjectResult != null && (!(attempt.Result.ObjectResult is DynamicPublishedContent)))
{
if (attempt.Result.ObjectResult is IPublishedContent)
{
result = new DynamicPublishedContent((IPublishedContent)attempt.Result.ObjectResult);
}
else if (attempt.Result.ObjectResult is IEnumerable<DynamicPublishedContent>)
{
result = new DynamicPublishedContentList((IEnumerable<DynamicPublishedContent>)attempt.Result.ObjectResult);
}
else if (attempt.Result.ObjectResult is IEnumerable<IPublishedContent>)
{
result = new DynamicPublishedContentList((IEnumerable<IPublishedContent>)attempt.Result.ObjectResult);
}
}
}
return true;
}
//this is the result of an extension method execution gone wrong so we return dynamic null
2013-09-17 18:15:19 +02:00
if (attempt.Result != null
&& attempt.Result.Reason == DynamicInstanceHelper.TryInvokeMemberSuccessReason.FoundExtensionMethod
&& attempt.Exception is TargetInvocationException)
{
2013-09-05 17:47:13 +02:00
result = DynamicNull.Null;
return true;
}
result = null;
return false;
}
/// <summary>
/// Attempts to return a custom member (generally based on a string match)
/// </summary>
/// <param name="binder"></param>
/// <returns></returns>
protected virtual Attempt<object> TryGetCustomMember(GetMemberBinder binder)
{
2013-09-17 18:15:19 +02:00
// as of 4.5 the CLR is case-sensitive which means that the default binder
// will handle those methods only when using the proper casing. So what
// this method does is ensure that any casing is supported.
2013-09-05 17:47:13 +02:00
if (binder.Name.InvariantEquals("ChildrenAsList") || binder.Name.InvariantEquals("Children"))
{
return Attempt<object>.Succeed(Children);
}
if (binder.Name.InvariantEquals("parentId"))
{
var parent = ((IPublishedContent) this).Parent;
if (parent == null)
{
throw new InvalidOperationException(string.Format("The node {0} does not have a parent", Id));
}
return Attempt<object>.Succeed(parent.Id);
}
2013-09-17 18:15:19 +02:00
2013-09-04 17:14:06 +02:00
return Attempt<object>.Fail();
}
/// <summary>
/// Attempts to return the children by the document type's alias (for example: CurrentPage.NewsItems where NewsItem is the
/// document type alias)
/// </summary>
/// <param name="binder"></param>
/// <returns></returns>
/// <remarks>
/// This method will work by both the plural and non-plural alias (i.e. NewsItem and NewsItems)
/// </remarks>
protected virtual Attempt<object> TryGetChildrenByAlias(GetMemberBinder binder)
{
var filteredTypeChildren = PublishedContent.Children
.Where(x => x.DocumentTypeAlias.InvariantEquals(binder.Name) || x.DocumentTypeAlias.MakePluralName().InvariantEquals(binder.Name))
.ToArray();
if (filteredTypeChildren.Any())
{
return Attempt<object>.Succeed(
new DynamicPublishedContentList(filteredTypeChildren.Select(x => new DynamicPublishedContent(x))));
}
2013-09-04 17:14:06 +02:00
return Attempt<object>.Fail();
}
/// <summary>
/// Attempts to return a member based on the reflected document property
/// </summary>
/// <param name="binder"></param>
/// <returns></returns>
protected virtual Attempt<object> TryGetDocumentProperty(GetMemberBinder binder)
{
var reflectedProperty = GetReflectedProperty(binder.Name);
var result = reflectedProperty != null
2013-09-17 18:15:19 +02:00
? reflectedProperty.Value
: null;
2013-09-04 17:14:06 +02:00
return Attempt.If(result != null, result);
}
/// <summary>
/// Attempts to return a member based on a user defined umbraco property
/// </summary>
/// <param name="binder"></param>
/// <returns></returns>
protected virtual Attempt<object> TryGetUserProperty(GetMemberBinder binder)
{
var name = binder.Name;
2013-09-05 17:47:13 +02:00
var recurse = false;
if (name.StartsWith("_"))
{
name = name.Substring(1, name.Length - 1);
2013-09-05 17:47:13 +02:00
recurse = true;
}
2013-09-05 17:47:13 +02:00
var value = PublishedContent.GetPropertyValue(name, recurse);
return Attempt<object>.SucceedIf(value != null, value);
}
/// <summary>
/// Returns the member match methods in the correct order and is used in the TryGetMember method.
/// </summary>
/// <returns></returns>
protected virtual IEnumerable<Func<GetMemberBinder, Attempt<object>>> GetMemberMatchMethods()
{
var memberMatchMethods = new List<Func<GetMemberBinder, Attempt<object>>>
{
TryGetCustomMember, //match custom members
TryGetUserProperty, //then match custom user defined umbraco properties
TryGetChildrenByAlias, //then try to match children based on doc type alias
TryGetDocumentProperty //then try to match on a reflected document property
};
return memberMatchMethods;
}
/// <summary>
/// Try to return an object based on the dynamic member accessor
/// </summary>
/// <param name="binder"></param>
/// <param name="result"></param>
/// <returns></returns>
/// <remarks>
/// TODO: SD: This will alwasy return true so that no exceptions are generated, this is only because this is how the
/// old DynamicNode worked, I'm not sure if this is the correct/expected functionality but I've left it like that.
/// IMO I think this is incorrect and it would be better to throw an exception for something that is not supported!
/// </remarks>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (binder == null) throw new ArgumentNullException("binder");
var name = binder.Name;
//check the cache first!
if (_cachedMemberOutput.TryGetValue(name, out result))
{
return true;
}
//loop through each member match method and execute it.
//If it is successful, cache the result and return it.
foreach (var attempt in GetMemberMatchMethods()
.Select(m => m(binder))
.Where(attempt => attempt.Success))
{
result = attempt.Result;
//cache the result so we don't have to re-process the whole thing
_cachedMemberOutput.TryAdd(name, result);
return true;
}
//if property access, type lookup and member invoke all failed
//at this point, we're going to return null
//instead, we return a DynamicNull - see comments in that file
//this will let things like Model.ChildItem work and return nothing instead of crashing
//.Where explictly checks for this type
//and will make it false
//which means backwards equality (&& property != true) will pass
//forwwards equality (&& property or && property == true) will fail
2013-09-05 17:47:13 +02:00
result = DynamicNull.Null;
//alwasy return true if we haven't thrown an exception though I'm wondering if we return 'false' if .Net throws an exception for us??
return true;
}
/// <summary>
/// Returns a property defined on the document object as a member property using reflection
/// </summary>
/// <param name="alias"></param>
/// <returns></returns>
private PropertyResult GetReflectedProperty(string alias)
{
return GetPropertyInternal(alias, PublishedContent, false);
}
/// <summary>
/// Return a user defined property
/// </summary>
/// <param name="alias"></param>
/// <param name="recursive"></param>
/// <returns></returns>
internal PropertyResult GetUserProperty(string alias, bool recursive = false)
{
if (!recursive)
{
return GetPropertyInternal(alias, PublishedContent);
}
var context = this;
var prop = GetPropertyInternal(alias, PublishedContent);
2013-02-18 13:22:12 -01:00
2013-09-05 17:47:13 +02:00
while (prop == null || !prop.HasValue)
{
var parent = ((IPublishedContent) context).Parent;
if (parent == null) break;
2013-02-18 13:22:12 -01:00
// Update the context before attempting to retrieve the property again.
2013-09-05 17:47:13 +02:00
context = parent.AsDynamicOrNull();
prop = context.GetPropertyInternal(alias, context.PublishedContent);
}
2013-02-18 13:22:12 -01:00
return prop;
}
private PropertyResult GetPropertyInternal(string alias, IPublishedContent content, bool checkUserProperty = true)
{
if (alias.IsNullOrWhiteSpace()) throw new ArgumentNullException("alias");
if (content == null) throw new ArgumentNullException("content");
//if we're looking for a user defined property
if (checkUserProperty)
{
var prop = content.GetProperty(alias);
2013-09-05 17:47:13 +02:00
// get wrap the result in a PropertyResult - just so it's an IHtmlString - ?!
return prop == null
? null
: new PropertyResult(prop, PropertyResultType.UserProperty);
}
//reflect
2013-09-17 18:15:19 +02:00
// as of 4.5 the CLR is case-sensitive which means that the default binder
// can handle properties only when using the proper casing. So what this
// does is ensure that any casing is supported.
2013-09-05 17:47:13 +02:00
Func<string, Attempt<object>> getMember =
memberAlias =>
{
try
{
return Attempt<object>.Succeed(
content.GetType().InvokeMember(memberAlias,
System.Reflection.BindingFlags.GetProperty |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public,
null,
content,
null));
}
catch (MissingMethodException ex)
{
2013-09-04 17:14:06 +02:00
return Attempt<object>.Fail(ex);
}
};
//try with the current casing
var attempt = getMember(alias);
if (!attempt.Success)
{
//if we cannot get with the current alias, try changing it's case
attempt = alias[0].IsUpperCase()
? getMember(alias.ConvertCase(StringAliasCaseType.CamelCase))
: getMember(alias.ConvertCase(StringAliasCaseType.PascalCase));
}
return !attempt.Success
2013-09-05 17:47:13 +02:00
? null
: new PropertyResult(alias, attempt.Result, Guid.Empty, PropertyResultType.ReflectedProperty);
}
2013-09-05 17:47:13 +02:00
#endregion
#region Explicit IPublishedContent implementation
IPublishedContent IPublishedContent.Parent
{
get { return PublishedContent.Parent; }
}
int IPublishedContent.Id
{
get { return PublishedContent.Id; }
}
int IPublishedContent.TemplateId
{
get { return PublishedContent.TemplateId; }
}
int IPublishedContent.SortOrder
{
get { return PublishedContent.SortOrder; }
}
string IPublishedContent.Name
{
get { return PublishedContent.Name; }
}
string IPublishedContent.UrlName
{
get { return PublishedContent.UrlName; }
}
string IPublishedContent.DocumentTypeAlias
{
get { return PublishedContent.DocumentTypeAlias; }
}
int IPublishedContent.DocumentTypeId
{
get { return PublishedContent.DocumentTypeId; }
}
string IPublishedContent.WriterName
{
get { return PublishedContent.WriterName; }
}
string IPublishedContent.CreatorName
{
get { return PublishedContent.CreatorName; }
}
int IPublishedContent.WriterId
{
get { return PublishedContent.WriterId; }
}
int IPublishedContent.CreatorId
{
get { return PublishedContent.CreatorId; }
}
string IPublishedContent.Path
{
get { return PublishedContent.Path; }
}
DateTime IPublishedContent.CreateDate
{
get { return PublishedContent.CreateDate; }
}
DateTime IPublishedContent.UpdateDate
{
get { return PublishedContent.UpdateDate; }
}
Guid IPublishedContent.Version
{
get { return PublishedContent.Version; }
}
int IPublishedContent.Level
{
get { return PublishedContent.Level; }
}
bool IPublishedContent.IsDraft
{
get { return PublishedContent.IsDraft; }
}
int IPublishedContent.GetIndex()
{
return PublishedContent.GetIndex();
}
ICollection<IPublishedProperty> IPublishedContent.Properties
{
get { return PublishedContent.Properties; }
}
IEnumerable<IPublishedContent> IPublishedContent.Children
{
get { return PublishedContent.Children; }
}
IPublishedProperty IPublishedContent.GetProperty(string alias)
{
return PublishedContent.GetProperty(alias);
}
#endregion
#region IPublishedContent implementation
public int TemplateId
{
get { return PublishedContent.TemplateId; }
}
public int SortOrder
{
get { return PublishedContent.SortOrder; }
}
public string Name
{
get { return PublishedContent.Name; }
}
public string UrlName
{
get { return PublishedContent.UrlName; }
}
public string DocumentTypeAlias
{
get { return PublishedContent.DocumentTypeAlias; }
}
public string WriterName
{
get { return PublishedContent.WriterName; }
}
public string CreatorName
{
get { return PublishedContent.CreatorName; }
}
public int WriterId
{
get { return PublishedContent.WriterId; }
}
public int CreatorId
{
get { return PublishedContent.CreatorId; }
}
public string Path
{
get { return PublishedContent.Path; }
}
public DateTime CreateDate
{
get { return PublishedContent.CreateDate; }
}
public int Id
{
get { return PublishedContent.Id; }
}
public DateTime UpdateDate
{
get { return PublishedContent.UpdateDate; }
}
public Guid Version
{
get { return PublishedContent.Version; }
}
public int Level
{
get { return PublishedContent.Level; }
}
public string Url
{
get { return PublishedContent.Url; }
}
public PublishedItemType ItemType
{
get { return PublishedContent.ItemType; }
}
2013-09-05 17:47:13 +02:00
// see note in IPublishedContent
//public bool Published
//{
// get { return PublishedContent.Published; }
//}
public IEnumerable<IPublishedProperty> Properties
{
get { return PublishedContent.Properties; }
}
public object this[string propertyAlias]
{
get { return PublishedContent[propertyAlias]; }
}
2013-09-05 17:47:13 +02:00
#endregion
2013-09-05 17:47:13 +02:00
#region GetProperty
2013-09-05 17:47:13 +02:00
// enhanced versions of the extension methods that exist for IPublishedContent,
// here we support the recursive (_) and reflected (@) syntax
2013-09-05 17:47:13 +02:00
public IPublishedProperty GetProperty(string alias)
{
return alias.StartsWith("_")
? GetProperty(alias.Substring(1), true)
: GetProperty(alias, false);
}
2013-09-05 17:47:13 +02:00
public IPublishedProperty GetProperty(string alias, bool recurse)
{
if (alias.StartsWith("@")) return GetReflectedProperty(alias.Substring(1));
2013-09-05 17:47:13 +02:00
// get wrap the result in a PropertyResult - just so it's an IHtmlString - ?!
var property = PublishedContent.GetProperty(alias, recurse);
return property == null ? null : new PropertyResult(property, PropertyResultType.UserProperty);
}
2013-09-05 17:47:13 +02:00
#endregion
2013-09-05 17:47:13 +02:00
// IPublishedContent extension methods:
//
// all these methods are IPublishedContent extension methods so they should in
// theory apply to DynamicPublishedContent since it is an IPublishedContent and
// we look for extension methods. But that lookup has to be pretty slow.
// Duplicating the methods here makes things much faster.
2013-09-05 17:47:13 +02:00
#region IPublishedContent extension methods - Template
2013-09-05 17:47:13 +02:00
public string GetTemplateAlias()
{
2013-09-05 17:47:13 +02:00
return PublishedContentExtensions.GetTemplateAlias(this);
}
2013-09-05 17:47:13 +02:00
#endregion
2013-09-05 17:47:13 +02:00
#region IPublishedContent extension methods - HasProperty
public bool HasProperty(string name)
{
return PublishedContent.HasProperty(name);
}
#endregion
#region IPublishedContent extension methods - HasValue
public bool HasValue(string alias)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.HasValue(alias);
}
2013-09-05 17:47:13 +02:00
public bool HasValue(string alias, bool recursive)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.HasValue(alias, recursive);
}
2013-09-05 17:47:13 +02:00
public IHtmlString HasValue(string alias, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.HasValue(alias, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public IHtmlString HasValue(string alias, bool recursive, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.HasValue(alias, recursive, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public IHtmlString HasValue(string alias, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.HasValue(alias, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public IHtmlString HasValue(string alias, bool recursive, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.HasValue(alias, recursive, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
#endregion
2013-09-05 17:47:13 +02:00
#region IPublishedContent extension methods - GetPropertyValue
2013-09-05 17:47:13 +02:00
// for whatever reason, some methods returning strings were created in DynamicPublishedContent
// and are now considered a "feature" as of v6. So we can't have the proper GetPropertyValue
// methods returning objects, too. And we don't want to change it in v6 as that would be a
// breaking change.
2013-09-05 17:47:13 +02:00
#if FIX_GET_PROPERTY_VALUE
2013-09-05 17:47:13 +02:00
public object GetPropertyValue(string alias)
{
return PublishedContent.GetPropertyValue(alias);
}
2013-09-05 17:47:13 +02:00
public object GetPropertyValue(string alias, string defaultValue)
{
return PublishedContent.GetPropertyValue(alias, defaultValue);
}
2013-09-05 17:47:13 +02:00
public object GetPropertyValue(string alias, object defaultValue)
{
return PublishedContent.GetPropertyValue(alias, defaultValue);
}
2013-09-05 17:47:13 +02:00
public object GetPropertyValue(string alias, bool recurse)
{
return PublishedContent.GetPropertyValue(alias, recurse);
}
2013-09-05 17:47:13 +02:00
public object GetPropertyValue(string alias, bool recurse, object defaultValue)
{
return PublishedContent.GetPropertyValue(alias, recurse, defaultValue);
}
2013-09-05 17:47:13 +02:00
#else
2013-09-05 17:47:13 +02:00
public string GetPropertyValue(string alias)
{
return GetPropertyValue(alias, false);
}
2013-09-05 17:47:13 +02:00
public string GetPropertyValue(string alias, string defaultValue)
{
var value = GetPropertyValue(alias);
return value.IsNullOrWhiteSpace() ? defaultValue : value;
}
2013-09-05 17:47:13 +02:00
public string GetPropertyValue(string alias, bool recurse, string defaultValue)
{
var value = GetPropertyValue(alias, recurse);
return value.IsNullOrWhiteSpace() ? defaultValue : value;
}
2013-09-05 17:47:13 +02:00
public string GetPropertyValue(string alias, bool recursive)
{
var property = GetProperty(alias, recursive);
if (property == null || property.Value == null) return null;
return property.Value.ToString();
}
2013-09-05 17:47:13 +02:00
#endif
2013-09-05 17:47:13 +02:00
#endregion
2013-09-05 17:47:13 +02:00
#region IPublishedContent extension methods - GetPropertyValue<T>
2013-09-05 17:47:13 +02:00
public T GetPropertyValue<T>(string alias)
{
return PublishedContent.GetPropertyValue<T>(alias);
}
2013-09-05 17:47:13 +02:00
public T GetPropertyValue<T>(string alias, T defaultValue)
{
return PublishedContent.GetPropertyValue(alias, defaultValue);
}
2013-09-05 17:47:13 +02:00
public T GetPropertyValue<T>(string alias, bool recurse)
{
return PublishedContent.GetPropertyValue<T>(alias, recurse);
}
2013-09-05 17:47:13 +02:00
public T GetPropertyValue<T>(string alias, bool recurse, T defaultValue)
{
return PublishedContent.GetPropertyValue(alias, recurse, defaultValue);
}
2013-09-05 17:47:13 +02:00
#endregion
2013-09-05 17:47:13 +02:00
#region IPublishedContent extension methods - Search
public DynamicPublishedContentList Search(string term, bool useWildCards = true, string searchProvider = null)
{
return new DynamicPublishedContentList(PublishedContent.Search(term, useWildCards, searchProvider));
}
public DynamicPublishedContentList SearchDescendants(string term, bool useWildCards = true, string searchProvider = null)
{
return new DynamicPublishedContentList(PublishedContent.SearchDescendants(term, useWildCards, searchProvider));
}
public DynamicPublishedContentList SearchChildren(string term, bool useWildCards = true, string searchProvider = null)
{
return new DynamicPublishedContentList(PublishedContent.SearchChildren(term, useWildCards, searchProvider));
}
public DynamicPublishedContentList Search(Examine.SearchCriteria.ISearchCriteria criteria, Examine.Providers.BaseSearchProvider searchProvider = null)
{
return new DynamicPublishedContentList(PublishedContent.Search(criteria, searchProvider));
}
#endregion
2013-09-05 17:47:13 +02:00
#region IPublishedContent extension methods - AsDynamic
public dynamic AsDynamic()
{
return this;
}
public dynamic AsDynamicOrNull()
{
return this;
}
#endregion
#region IPublishedContente extension methods - ContentSet
public int Position()
{
return Index();
}
public int Index()
{
return PublishedContent.GetIndex();
}
#endregion
#region IPublishedContent extension methods - IsSomething: misc
public bool Visible
{
get { return PublishedContent.IsVisible(); }
}
public bool IsVisible()
{
return PublishedContent.IsVisible();
}
public bool IsDocumentType(string docTypeAlias)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsDocumentType(docTypeAlias);
}
public bool IsNull(string alias, bool recursive)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNull(alias, recursive);
}
2013-09-05 17:47:13 +02:00
public bool IsNull(string alias)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNull(alias, false);
}
2013-09-05 17:47:13 +02:00
#endregion
#region IPublishedContent extension methods - IsSomething: position in set
public bool IsFirst()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsFirst();
}
2013-09-05 17:47:13 +02:00
public HtmlString IsFirst(string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsFirst(valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsFirst(string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsFirst(valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsNotFirst()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotFirst();
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotFirst(string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotFirst(valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotFirst(string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotFirst(valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsPosition(int index)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsPosition(index);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsPosition(int index, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsPosition(index, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsPosition(int index, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsPosition(index, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsModZero(int modulus)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsModZero(modulus);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsModZero(int modulus, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsModZero(modulus, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsModZero(int modulus, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsModZero(modulus, valueIfTrue, valueIfFalse);
}
public bool IsNotModZero(int modulus)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotModZero(modulus);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotModZero(int modulus, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotModZero(modulus, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotModZero(int modulus, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotModZero(modulus, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsNotPosition(int index)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotPosition(index);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotPosition(int index, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotPosition(index, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotPosition(int index, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotPosition(index, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsLast()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsLast();
}
2013-09-05 17:47:13 +02:00
public HtmlString IsLast(string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsLast(valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsLast(string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsLast(valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsNotLast()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotLast();
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotLast(string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotLast(valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotLast(string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotLast(valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsEven()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsEven();
}
2013-09-05 17:47:13 +02:00
public HtmlString IsEven(string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsEven(valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsEven(string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsEven(valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsOdd()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsOdd();
}
2013-09-05 17:47:13 +02:00
public HtmlString IsOdd(string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsOdd(valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsOdd(string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsOdd(valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
#endregion
#region IPublishedContent extension methods - IsSomething: equality
public bool IsEqual(DynamicPublishedContent other)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsEqual(other);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsEqual(DynamicPublishedContent other, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsEqual(other, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsEqual(DynamicPublishedContent other, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsEqual(other, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsNotEqual(DynamicPublishedContent other)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotEqual(other);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotEqual(DynamicPublishedContent other, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotEqual(other, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsNotEqual(DynamicPublishedContent other, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsNotEqual(other, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
#endregion
#region IPublishedContent extension methods - IsSomething: ancestors and descendants
public bool IsDescendant(DynamicPublishedContent other)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsDescendant(other);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsDescendant(DynamicPublishedContent other, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsDescendant(other, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsDescendant(DynamicPublishedContent other, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsDescendant(other, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsDescendantOrSelf(DynamicPublishedContent other)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsDescendantOrSelf(other);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsDescendantOrSelf(DynamicPublishedContent other, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsDescendantOrSelf(other, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsDescendantOrSelf(DynamicPublishedContent other, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsDescendantOrSelf(other, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsAncestor(DynamicPublishedContent other)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsAncestor(other);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsAncestor(DynamicPublishedContent other, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsAncestor(other, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsAncestor(DynamicPublishedContent other, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsAncestor(other, valueIfTrue, valueIfFalse);
}
2013-09-05 17:47:13 +02:00
public bool IsAncestorOrSelf(DynamicPublishedContent other)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsAncestorOrSelf(other);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsAncestorOrSelf(DynamicPublishedContent other, string valueIfTrue)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsAncestorOrSelf(other, valueIfTrue);
}
2013-09-05 17:47:13 +02:00
public HtmlString IsAncestorOrSelf(DynamicPublishedContent other, string valueIfTrue, string valueIfFalse)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.IsAncestorOrSelf(other, valueIfTrue, valueIfFalse);
}
#endregion
2013-09-05 17:47:13 +02:00
// all these methods wrap whatever PublishedContent returns in a new
// DynamicPublishedContentList, for dynamic usage.
#region Ancestors
public DynamicPublishedContentList Ancestors(int level)
{
return new DynamicPublishedContentList(PublishedContent.Ancestors(level));
}
public DynamicPublishedContentList Ancestors(string contentTypeAlias)
{
return new DynamicPublishedContentList(PublishedContent.Ancestors(contentTypeAlias));
}
public DynamicPublishedContentList Ancestors()
{
return new DynamicPublishedContentList(PublishedContent.Ancestors());
}
public DynamicPublishedContentList Ancestors(Func<IPublishedContent, bool> func)
{
return new DynamicPublishedContentList(PublishedContent.AncestorsOrSelf(false, func));
}
public DynamicPublishedContent AncestorOrSelf()
{
return PublishedContent.AncestorOrSelf().AsDynamicOrNull();
}
public DynamicPublishedContent AncestorOrSelf(int level)
{
return PublishedContent.AncestorOrSelf(level).AsDynamicOrNull();
}
public DynamicPublishedContent AncestorOrSelf(string contentTypeAlias)
{
return PublishedContent.AncestorOrSelf(contentTypeAlias).AsDynamicOrNull();
}
public DynamicPublishedContent AncestorOrSelf(Func<IPublishedContent, bool> func)
{
return PublishedContent.AncestorsOrSelf(true, func).FirstOrDefault().AsDynamicOrNull();
}
public DynamicPublishedContentList AncestorsOrSelf(Func<IPublishedContent, bool> func)
{
return new DynamicPublishedContentList(PublishedContent.AncestorsOrSelf(true, func));
}
public DynamicPublishedContentList AncestorsOrSelf()
{
return new DynamicPublishedContentList(PublishedContent.AncestorsOrSelf());
}
public DynamicPublishedContentList AncestorsOrSelf(string contentTypeAlias)
{
return new DynamicPublishedContentList(PublishedContent.AncestorsOrSelf(contentTypeAlias));
}
public DynamicPublishedContentList AncestorsOrSelf(int level)
{
return new DynamicPublishedContentList(PublishedContent.AncestorsOrSelf(level));
}
#endregion
#region Descendants
public DynamicPublishedContentList Descendants(string contentTypeAlias)
{
return new DynamicPublishedContentList(PublishedContent.Descendants(contentTypeAlias));
}
public DynamicPublishedContentList Descendants(int level)
{
return new DynamicPublishedContentList(PublishedContent.Descendants(level));
}
public DynamicPublishedContentList Descendants()
{
return new DynamicPublishedContentList(PublishedContent.Descendants());
}
public DynamicPublishedContentList DescendantsOrSelf(int level)
{
return new DynamicPublishedContentList(PublishedContent.DescendantsOrSelf(level));
}
public DynamicPublishedContentList DescendantsOrSelf(string contentTypeAlias)
{
return new DynamicPublishedContentList(PublishedContent.DescendantsOrSelf(contentTypeAlias));
}
public DynamicPublishedContentList DescendantsOrSelf()
{
return new DynamicPublishedContentList(PublishedContent.DescendantsOrSelf());
}
#endregion
#region Traversal
public DynamicPublishedContent Up()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Up().AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Up(int number)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Up(number).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Up(string contentTypeAlias)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Up(contentTypeAlias).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Down()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Down().AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Down(int number)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Down(number).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Down(string contentTypeAlias)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Down(contentTypeAlias).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Next()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Next().AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Next(int number)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Next(number).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Next(string contentTypeAlias)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Next(contentTypeAlias).AsDynamicOrNull();
}
public DynamicPublishedContent Previous()
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Previous().AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Previous(int number)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Previous(number).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Previous(string contentTypeAlias)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Previous(contentTypeAlias).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Sibling(int number)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Previous(number).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
public DynamicPublishedContent Sibling(string contentTypeAlias)
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Previous(contentTypeAlias).AsDynamicOrNull();
}
2013-09-05 17:47:13 +02:00
#endregion
2013-09-05 17:47:13 +02:00
#region Parent
public DynamicPublishedContent Parent
{
get
{
2013-09-05 17:47:13 +02:00
return PublishedContent.Parent != null ? PublishedContent.Parent.AsDynamicOrNull() : null;
}
}
#endregion
2013-09-05 17:47:13 +02:00
#region Children
// we want to cache the dynamic list of children here
// whether PublishedContent.Children itself is cached, is not our concern
private DynamicPublishedContentList _children;
2013-09-05 17:47:13 +02:00
public DynamicPublishedContentList Children
{
get { return _children ?? (_children = new DynamicPublishedContentList(PublishedContent.Children)); }
}
#endregion
2013-09-17 18:15:19 +02:00
// should probably cleanup what's below
2013-09-05 17:47:13 +02:00
#region Where
public HtmlString Where(string predicate, string valueIfTrue)
{
return Where(predicate, valueIfTrue, string.Empty);
}
public HtmlString Where(string predicate, string valueIfTrue, string valueIfFalse)
{
if (Where(predicate))
{
return new HtmlString(valueIfTrue);
}
return new HtmlString(valueIfFalse);
}
public bool Where(string predicate)
{
//Totally gonna cheat here
var dynamicDocumentList = new DynamicPublishedContentList();
dynamicDocumentList.Add(this);
var filtered = dynamicDocumentList.Where<DynamicPublishedContent>(predicate);
if (Queryable.Count(filtered) == 1)
{
//this node matches the predicate
return true;
}
return false;
}
#endregion
2013-09-05 17:47:13 +02:00
}
}