2017-09-26 14:57:50 +02:00
using System.Web ;
2016-06-09 20:13:20 +02:00
namespace Umbraco.Core.Models.PublishedContent
{
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>
2016-06-09 20:13:20 +02:00
public int TotalCount { get ; internal set ; }
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsFirst ( string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsFirst ( string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . 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-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.
2016-06-09 20:13:20 +02:00
public HtmlString IsNotFirst ( string valueIfTrue )
{
return IsNotFirst ( valueIfTrue , string . Empty );
}
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, <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.
2016-06-09 20:13:20 +02:00
public HtmlString IsNotFirst ( string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . 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-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, <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.
2016-06-09 20:13:20 +02:00
public HtmlString IsIndex ( int index , string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsIndex ( int index , string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . HtmlEncode ( IsIndex ( 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 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.
2016-06-09 20:13:20 +02:00
public HtmlString IsModZero ( int modulus , string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsModZero ( int modulus , string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . 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.
2016-06-09 20:13:20 +02:00
public HtmlString IsNotModZero ( int modulus , string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsNotModZero ( int modulus , string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . 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.
2016-06-09 20:13:20 +02:00
public HtmlString IsNotIndex ( int index , string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsNotIndex ( int index , string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . 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.
2016-06-09 20:13:20 +02:00
public HtmlString IsLast ( string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsLast ( string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . 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.
2016-06-09 20:13:20 +02:00
public HtmlString IsNotLast ( string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsNotLast ( string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . 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.
2016-06-09 20:13:20 +02:00
public HtmlString IsEven ( string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsEven ( string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . 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.
2016-06-09 20:13:20 +02:00
public HtmlString IsOdd ( string valueIfTrue )
{
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.
2016-06-09 20:13:20 +02:00
public HtmlString IsOdd ( string valueIfTrue , string valueIfFalse )
{
2020-06-19 11:41:16 +02:00
return new HtmlString ( HttpUtility . HtmlEncode ( IsOdd () ? valueIfTrue : valueIfFalse ));
2016-06-09 20:13:20 +02:00
}
}
}