2020-08-05 08:44:22 +02:00
using System.Net ;
2020-06-28 12:35:20 +02:00
using Umbraco.Core.Strings ;
2019-12-04 10:39:49 +01:00
namespace Umbraco.Core.Models.PublishedContent
2016-06-09 20:13:20 +02:00
{
2020-06-19 11:41:16 +02:00
/// <summary>
/// Represents an item in an array that stores its own index and the total count.
/// </summary>
/// <typeparam name="TContent">The type of the content.</typeparam>
2016-06-09 20:13:20 +02:00
public class IndexedArrayItem < TContent >
{
2020-06-19 11:41:16 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="IndexedArrayItem{TContent}" /> class.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="index">The index.</param>
2016-06-09 20:13:20 +02:00
public IndexedArrayItem ( TContent content , int index )
{
Content = content ;
Index = index ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Gets the content.
/// </summary>
/// <value>
/// The content.
/// </value>
2016-06-09 20:13:20 +02:00
public TContent Content { get ; }
2020-06-19 11:41:16 +02:00
/// <summary>
/// Gets the index.
/// </summary>
/// <value>
/// The index.
/// </value>
2016-06-09 20:13:20 +02:00
public int Index { get ; }
2020-06-19 11:41:16 +02:00
/// <summary>
/// Gets the total count.
/// </summary>
/// <value>
/// The total count.
/// </value>
2019-12-09 14:12:06 +01:00
public int TotalCount { get ; set ; }
2016-06-09 20:13:20 +02:00
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is the first.
/// </summary>
/// <returns>
/// <c>true</c> if this item is the first; otherwise, <c>false</c>.
/// </returns>
2016-06-09 20:13:20 +02:00
public bool IsFirst ( )
{
return Index = = 0 ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is the first, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsFirst ( string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsFirst ( valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is the first, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsFirst ( string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsFirst ( ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is not the first.
/// </summary>
/// <returns>
/// <c>true</c> if this item is not the first; otherwise, <c>false</c>.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2016-06-09 20:13:20 +02:00
public bool IsNotFirst ( )
{
return IsFirst ( ) = = false ;
}
2020-06-28 12:35:20 +02:00
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is not the first, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsNotFirst ( string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsNotFirst ( valueIfTrue , string . Empty ) ;
}
2020-06-28 12:35:20 +02:00
/// <summary>
2020-06-19 11:41:16 +02:00
/// If this item is not the first, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsNotFirst ( string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsNotFirst ( ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is at the specified <paramref name="index" />.
/// </summary>
/// <param name="index">The index.</param>
/// <returns>
/// <c>true</c> if this item is at the specified <paramref name="index" />; otherwise, <c>false</c>.
/// </returns>
2016-06-09 20:13:20 +02:00
public bool IsIndex ( int index )
{
return Index = = index ;
}
2020-06-28 12:35:20 +02:00
/// <summary>
2020-06-19 11:41:16 +02:00
/// If this item is at the specified <paramref name="index" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsIndex ( int index , string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsIndex ( index , valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is at the specified <paramref name="index" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsIndex ( int index , string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsIndex ( index ) ? valueIfTrue : valueIfFalse ) ) ;
2020-06-28 12:35:20 +02:00
}
2016-06-09 20:13:20 +02:00
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is at an index that can be divided by the specified <paramref name="modulus" />.
/// </summary>
/// <param name="modulus">The modulus.</param>
/// <returns>
/// <c>true</c> if this item is at an index that can be divided by the specified <paramref name="modulus" />; otherwise, <c>false</c>.
/// </returns>
2016-06-09 20:13:20 +02:00
public bool IsModZero ( int modulus )
{
return Index % modulus = = 0 ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is at an index that can be divided by the specified <paramref name="modulus" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="modulus">The modulus.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsModZero ( int modulus , string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsModZero ( modulus , valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is at an index that can be divided by the specified <paramref name="modulus" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="modulus">The modulus.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsModZero ( int modulus , string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsModZero ( modulus ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is not at an index that can be divided by the specified <paramref name="modulus" />.
/// </summary>
/// <param name="modulus">The modulus.</param>
/// <returns>
/// <c>true</c> if this item is not at an index that can be divided by the specified <paramref name="modulus" />; otherwise, <c>false</c>.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2016-06-09 20:13:20 +02:00
public bool IsNotModZero ( int modulus )
{
return IsModZero ( modulus ) = = false ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is not at an index that can be divided by the specified <paramref name="modulus" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="modulus">The modulus.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsNotModZero ( int modulus , string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsNotModZero ( modulus , valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is not at an index that can be divided by the specified <paramref name="modulus" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="modulus">The modulus.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsNotModZero ( int modulus , string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsNotModZero ( modulus ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is not at the specified <paramref name="index" />.
/// </summary>
/// <param name="index">The index.</param>
/// <returns>
/// <c>true</c> if this item is not at the specified <paramref name="index" />; otherwise, <c>false</c>.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2016-06-09 20:13:20 +02:00
public bool IsNotIndex ( int index )
{
return IsIndex ( index ) = = false ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is not at the specified <paramref name="index" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsNotIndex ( int index , string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsNotIndex ( index , valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is at the specified <paramref name="index" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsNotIndex ( int index , string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsNotIndex ( index ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is the last.
/// </summary>
/// <returns>
/// <c>true</c> if this item is the last; otherwise, <c>false</c>.
/// </returns>
2016-06-09 20:13:20 +02:00
public bool IsLast ( )
{
return Index = = TotalCount - 1 ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is the last, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsLast ( string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsLast ( valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is the last, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsLast ( string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsLast ( ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is not the last.
/// </summary>
/// <returns>
/// <c>true</c> if this item is not the last; otherwise, <c>false</c>.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2016-06-09 20:13:20 +02:00
public bool IsNotLast ( )
{
return IsLast ( ) = = false ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is not the last, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsNotLast ( string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsNotLast ( valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is not the last, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsNotLast ( string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsNotLast ( ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is at an even index.
/// </summary>
/// <returns>
/// <c>true</c> if this item is at an even index; otherwise, <c>false</c>.
/// </returns>
2016-06-09 20:13:20 +02:00
public bool IsEven ( )
{
return Index % 2 = = 0 ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is at an even index, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsEven ( string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsEven ( valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is at an even index, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsEven ( string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsEven ( ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// Determines whether this item is at an odd index.
/// </summary>
/// <returns>
/// <c>true</c> if this item is at an odd index; otherwise, <c>false</c>.
/// </returns>
2016-06-09 20:13:20 +02:00
public bool IsOdd ( )
{
return Index % 2 = = 1 ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is at an odd index, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsOdd ( string valueIfTrue )
2016-06-09 20:13:20 +02:00
{
return IsOdd ( valueIfTrue , string . Empty ) ;
}
2020-06-19 11:41:16 +02:00
/// <summary>
/// If this item is at an odd index, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
// TODO: This method should be removed or moved to an extension method on HtmlHelper.
2019-11-27 08:27:18 +01:00
public IHtmlEncodedString IsOdd ( string valueIfTrue , string valueIfFalse )
2016-06-09 20:13:20 +02:00
{
2020-08-05 08:44:22 +02:00
return new HtmlEncodedString ( WebUtility . HtmlEncode ( IsOdd ( ) ? valueIfTrue : valueIfFalse ) ) ;
2016-06-09 20:13:20 +02:00
}
}
}