/// <summary> /// Get a value that indicates whether the current pager item should be disabled. /// </summary> /// <param name="context">The pager item generation context.</param> /// <returns>If the current pager item should be disabled, returns <c>true</c>; otherwise returns <c>false</c>.</returns> private bool ItemShouldBeDisabled(PagerItemGenerationContext context) { switch (context.PagerItem.ItemType) { case PagerItemType.First: case PagerItemType.Last: switch (context.PagerItemOptions.ActiveMode) { case FirstAndLastPagerItemActiveMode.Always: return false; case FirstAndLastPagerItemActiveMode.NotInCurrentPage: return context.CurrentPage == context.PagerItem.PageNumber; case FirstAndLastPagerItemActiveMode.NotInVisiblePageList: return IsPageVisible(context); default: throw new InvalidOperationException( $"The value of '{nameof(context.PagerItemOptions.ActiveMode)}' property cannot be a valid enum item for a pager item of type '{context.PagerItem.ItemType}'."); } case PagerItemType.Next: return context.CurrentPage == context.TotalPage; case PagerItemType.Previous: return context.CurrentPage == 1; default: return false; } }
/// <summary> /// Get the rendering state of the current pager item. /// </summary> /// <param name="context">The pager item generation context.</param> /// <returns>The rendering item of the current pager item.</returns> private PagerRenderingItemState GetRenderingItemState(PagerItemGenerationContext context) { switch (context.PagerItem.ItemType) { case PagerItemType.First: case PagerItemType.Last: case PagerItemType.Previous: case PagerItemType.Next: var disabled = ItemShouldBeDisabled(context); if (!disabled) { return PagerRenderingItemState.Normal; } switch (context.PagerItemOptions.InactiveBehavior) { case SpecialPagerItemInactiveBehavior.Disable: return PagerRenderingItemState.Disabled; case SpecialPagerItemInactiveBehavior.Hide: return PagerRenderingItemState.Hidden; default: throw new InvalidOperationException( $"The value of '{nameof(context.PagerItemOptions.InactiveBehavior)}' property cannot be a valid enum item for a pager item of type '{context.PagerItem.ItemType}'."); } case PagerItemType.Current: return PagerRenderingItemState.Active; default: return PagerRenderingItemState.Normal; } }
/// <summary> /// Generate a <see cref="PagerRenderingList" /> for a series of <see cref="PagerItem" />. /// </summary> /// <param name="items">The collection of <see cref="PagerItem" /> which are included in the list.</param> /// <param name="context">The generation context.</param> /// <returns>The generated <see cref="PagerRenderingList" /> instance.</returns> private PagerRenderingList GenerateRenderingListCore(IEnumerable <PagerItem> items, PagerGenerationContext context) { var optionsCache = new Dictionary <PagerItemType, PagerItemOptions>(); var renderingItemList = new List <PagerRenderingItem>(); // Owner var result = new PagerRenderingList { Settings = new Dictionary <string, string>(context.Options.AdditionalSettings) }; foreach (var item in items) { PagerItemOptions itemOptions; // Try to get cached options, or create a new option instance if (!optionsCache.TryGetValue(item.ItemType, out itemOptions)) { itemOptions = context.Options.ItemOptions.GetMergedOptionsFor(item); optionsCache[item.ItemType] = itemOptions; } // Generate item context var itemContext = new PagerItemGenerationContext(context, item, itemOptions); // Generate item and add to list var renderingItem = GenerateRenderingItem(result, itemContext); renderingItemList.Add(renderingItem); } // Result result.Items = new ReadOnlyCollection <PagerRenderingItem>(renderingItemList); return(result); }
/// <summary> /// Get a value that indicates whether the current pager item should be disabled. /// </summary> /// <param name="context">The pager item generation context.</param> /// <returns>If the current pager item should be disabled, returns <c>true</c>; otherwise returns <c>false</c>.</returns> private bool ItemShouldBeDisabled(PagerItemGenerationContext context) { switch (context.PagerItem.ItemType) { case PagerItemType.First: case PagerItemType.Last: switch (context.PagerItemOptions.ActiveMode) { case FirstAndLastPagerItemActiveMode.Always: return(false); case FirstAndLastPagerItemActiveMode.NotInCurrentPage: return(context.CurrentPage == context.PagerItem.PageNumber); case FirstAndLastPagerItemActiveMode.NotInVisiblePageList: return(IsPageVisible(context)); default: throw new InvalidOperationException( $"The value of '{nameof(context.PagerItemOptions.ActiveMode)}' property cannot be a valid enum item for a pager item of type '{context.PagerItem.ItemType}'."); } case PagerItemType.Next: return(context.CurrentPage == context.TotalPage); case PagerItemType.Previous: return(context.CurrentPage == 1); default: return(false); } }
/// <summary> /// Get the rendering state of the current pager item. /// </summary> /// <param name="context">The pager item generation context.</param> /// <returns>The rendering item of the current pager item.</returns> private PagerRenderingItemState GetRenderingItemState(PagerItemGenerationContext context) { switch (context.PagerItem.ItemType) { case PagerItemType.First: case PagerItemType.Last: case PagerItemType.Previous: case PagerItemType.Next: var disabled = ItemShouldBeDisabled(context); if (!disabled) { return(PagerRenderingItemState.Normal); } switch (context.PagerItemOptions.InactiveBehavior) { case SpecialPagerItemInactiveBehavior.Disable: return(PagerRenderingItemState.Disabled); case SpecialPagerItemInactiveBehavior.Hide: return(PagerRenderingItemState.Hidden); default: throw new InvalidOperationException( $"The value of '{nameof(context.PagerItemOptions.InactiveBehavior)}' property cannot be a valid enum item for a pager item of type '{context.PagerItem.ItemType}'."); } case PagerItemType.Current: return(PagerRenderingItemState.Active); default: return(PagerRenderingItemState.Normal); } }
/// <summary> /// Determine if the page is in visible page number list. /// </summary> /// <param name="context"> /// The <see cref="PagerItemGenerationContext" /> which contains all informations for the pager and /// current page. /// </param> /// <returns>If the current pager item is in the visible list, returns <c>true</c>; otherwise, returns <c>false</c>.</returns> private static bool IsPageVisible(PagerItemGenerationContext context) { var number = context.PagerItem.PageNumber; return number <= context.Options.PagerItemsForEndings || number >= context.TotalPage - context.Options.PagerItemsForEndings + 1 || number >= context.CurrentPage - context.Options.ExpandPageItemsForCurrentPage || number <= context.CurrentPage + context.Options.ExpandPageItemsForCurrentPage; }
/// <summary> /// The core method for handling the current uri. /// </summary> /// <param name="currentUri">The URL to handle, this URL is ensured in absolute mode.</param> /// <param name="context">The generation context.</param> /// <returns>The generated URL string.</returns> /// <remarks> /// Most URI handling method requirest the URI be absolute format, however in the view page relative URI is /// recommended. The <paramref name="currentUri" /> argument in this method has been handled and is ensured to be /// absolute. The generator will correctly recover it to the original format after handling. /// </remarks> protected override Uri HandleUriCore(Uri currentUri, PagerItemGenerationContext context) { // Get name and value var name = GenerateQueryParameterName(context); var value = GenerateQueryParameterValue(context); // Generate result return(ChangeQueryParameterValue(currentUri, name, value)); }
/// <summary> /// Determine if the page is in visible page number list. /// </summary> /// <param name="context"> /// The <see cref="PagerItemGenerationContext" /> which contains all informations for the pager and /// current page. /// </param> /// <returns>If the current pager item is in the visible list, returns <c>true</c>; otherwise, returns <c>false</c>.</returns> private static bool IsPageVisible(PagerItemGenerationContext context) { var number = context.PagerItem.PageNumber; return(number <= context.Options.PagerItemsForEndings || number >= context.TotalPage - context.Options.PagerItemsForEndings + 1 || number >= context.CurrentPage - context.Options.ExpandPageItemsForCurrentPage || number <= context.CurrentPage + context.Options.ExpandPageItemsForCurrentPage); }
/// <summary> /// The core method for handling the current uri. /// </summary> /// <param name="currentUri">The URL to handle, this URL is ensured in absolute mode.</param> /// <param name="context">The generation context.</param> /// <returns></returns> /// <remarks> /// Most URI handling method requirest the URI be absolute format, however in the view page relative URI is /// recommended. The <paramref name="currentUri" /> argument in this method has been handled and is ensured to be /// absolute. The generator will correctly recover it to the original format after handling. /// </remarks> protected override Uri HandleUriCore(Uri currentUri, PagerItemGenerationContext context) { // Create URI and change fragment var uriBuilder = new UriBuilder(currentUri) { Fragment = GenerateFragment(context) }; return(uriBuilder.Uri); }
/// <summary> /// The core method for handling the current uri. /// </summary> /// <param name="currentUri">The URL to handle, this URL is ensured in absolute mode.</param> /// <param name="context">The generation context.</param> /// <returns></returns> /// <remarks> /// Most URI handling method requirest the URI be absolute format, however in the view page relative URI is /// recommended. The <paramref name="currentUri" /> argument in this method has been handled and is ensured to be /// absolute. The generator will correctly recover it to the original format after handling. /// </remarks> protected override string HandleUriCore(string currentUri, PagerItemGenerationContext context) { // Create URI and change fragment var uriBuilder = new UriBuilder(currentUri) { Fragment = GenerateFragment(context) }; return uriBuilder.ToString(); }
/// <summary> /// Generate a <see cref="PagerRenderingItem" /> for the current pager item. /// </summary> /// <param name="list">The ownner list of the new item.</param> /// <param name="context">The generation context.</param> /// <returns>The generated <see cref="PagerRenderingItem" /> instance.</returns> private PagerRenderingItem GenerateRenderingItem(PagerRenderingList list, PagerItemGenerationContext context) { return(new PagerRenderingItem(list) { Content = context.PagerItemOptions.Content?.GenerateContent(context), Link = context.PagerItemOptions.Link?.GenerateLink(context), Settings = new Dictionary <string, string>(context.PagerItemOptions.AdditionalSettings), State = GetRenderingItemState(context) }); }
/// <summary> /// The core method for handling the current uri. /// </summary> /// <param name="currentUri">The URL to handle, this URL is ensured in absolute mode.</param> /// <param name="context">The generation context.</param> /// <returns>The generated URL string.</returns> /// <remarks> /// Most URI handling method requirest the URI be absolute format, however in the view page relative URI is /// recommended. The <paramref name="currentUri" /> argument in this method has been handled and is ensured to be /// absolute. The generator will correctly recover it to the original format after handling. /// </remarks> protected override string HandleUriCore(string currentUri, PagerItemGenerationContext context) { // Get name and value var name = GenerateQueryParameterName(context); var value = GenerateQueryParameterValue(context); // Get base uri var baseUri = GetCurrentUriWithQuery(context.ViewContext); // Generate result return(ChangeQueryParameterValue(baseUri, name, value)); }
public string GenerateLink(PagerItemGenerationContext context) { var baseUri = GetRealBaseUri(context.ViewContext); // Hosted uri for API compatibility var hostedUri = new Uri("http://test.local/"); // Generate uri object var uri = new Uri(baseUri, UriKind.RelativeOrAbsolute); // Start with slash detection. var isStartWithSlash = false; // If the uri is not absolute uri, convert it to an absolute one var isAbsolute = uri.IsAbsoluteUri; if (!isAbsolute) { isStartWithSlash = !string.IsNullOrEmpty(baseUri) && baseUri[0] == '/'; uri = new Uri(hostedUri, uri); } // Core method to handle URI. var finalUri = HandleUriCore(uri, context); // If the original uri is not absolute, remove the hosted part if (!isAbsolute) { finalUri = hostedUri.MakeRelativeUri(finalUri); } var result = UriHelper.Encode(finalUri); // Add start slash if necessary if (!isAbsolute && isStartWithSlash) { result = "/" + result; } return(result); }
/// <summary> /// The core method for handling the base uri. /// </summary> /// <param name="baseUri">The URL to handle, this URL is ensured in absolute mode.</param> /// <param name="context">The generation context.</param> /// <returns></returns> /// <remarks> /// Most URI handling method requires the URI be absolute format, however in the view page relative URI is /// recommended. The <paramref name="baseUri" /> argument in this method has been handled and is ensured to be /// absolute. The generator will correctly recover it to the original format after handling. /// </remarks> protected abstract Uri HandleUriCore([NotNull] Uri baseUri, [NotNull] PagerItemGenerationContext context);
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) { return(LinkGenerator(context)); }
/// <summary> /// When be derived, generate the content string. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The generated content string.</returns> protected override string GenerateContentString(PagerItemGenerationContext context) => string.Format(FormatProvider, Format, context.PagerItem.PageNumber);
/// <summary> /// Generate the query parameter value for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The query parameter name for current pager item.</returns> public override string GenerateQueryParameterValue(PagerItemGenerationContext context) { return(QueryValueGenerator(context)); }
/// <summary> /// When derived, generate the query parameter value for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The query parameter name for current pager item.</returns> public override string GenerateQueryParameterValue(PagerItemGenerationContext context) => QueryValueGenerator(context);
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) => LinkGenerator(context);
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) { return(Text); }
/// <summary> /// When derived, generate the query parameter name for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The query parameter name for current pager item.</returns> public override string GenerateQueryParameterName(PagerItemGenerationContext context) { return(QueryParameterName); }
/// <summary> /// Generate the query parameter value for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The query parameter name for current pager item.</returns> public override string GenerateQueryParameterValue(PagerItemGenerationContext context) => string.Format(FormatProvider, Format, context.PagerItem.PageNumber);
/// <summary> /// When be derived, generate the content string. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The generated content string.</returns> protected abstract string GenerateContentString(PagerItemGenerationContext context);
/// <summary> /// Generate the content for a specified pager item. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The generated HTML content for the pager item.</returns> public IHtmlContent GenerateContent(PagerItemGenerationContext context) => GenerateContentString(context).ToHtmlContent(EncodeText);
/// <summary> /// Generate the content string. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The generated content string.</returns> protected override string GenerateContentString(PagerItemGenerationContext context) { return(StringContentGenerator(context)); }
/// <summary> /// Generate the content for a specified pager item. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The generated HTML content for the pager item.</returns> public IHtmlContent GenerateContent(PagerItemGenerationContext context) => HtmlContentGenerator(context);
/// <summary> /// Generate the content string. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The generated content string.</returns> protected override string GenerateContentString(PagerItemGenerationContext context) => StringContentGenerator(context);
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) { var currentUri = GetCurrentUriWithQuery(context.ViewContext); return HandleUriCore(currentUri, context); }
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) => string.Format(FormatProvider, Format, context.PagerItem.PageNumber);
/// <summary> /// When derived, generate the query parameter value for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The query parameter name for current pager item.</returns> public abstract string GenerateQueryParameterValue([NotNull] PagerItemGenerationContext context);
/// <summary> /// The core method for handling the current uri. /// </summary> /// <param name="currentUri">The URL to handle, this URL is ensured in absolute mode.</param> /// <param name="context">The generation context.</param> /// <returns></returns> /// <remarks> /// Most URI handling method requirest the URI be absolute format, however in the view page relative URI is /// recommended. The <paramref name="currentUri" /> argument in this method has been handled and is ensured to be /// absolute. The generator will correctly recover it to the original format after handling. /// </remarks> protected abstract string HandleUriCore([NotNull] string currentUri, [NotNull] PagerItemGenerationContext context);
/// <summary> /// When derived, generate the query parameter name for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The query parameter name for current pager item.</returns> public override string GenerateQueryParameterName(PagerItemGenerationContext context) => QueryParameterName;
/// <summary> /// The core method for handling the current uri. /// </summary> /// <param name="currentUri">The URL to handle, this URL is ensured in absolute mode.</param> /// <param name="context">The generation context.</param> /// <returns>The generated URL string.</returns> /// <remarks> /// Most URI handling method requirest the URI be absolute format, however in the view page relative URI is /// recommended. The <paramref name="currentUri" /> argument in this method has been handled and is ensured to be /// absolute. The generator will correctly recover it to the original format after handling. /// </remarks> protected override string HandleUriCore(string currentUri, PagerItemGenerationContext context) { // Get name and value var name = GenerateQueryParameterName(context); var value = GenerateQueryParameterValue(context); // Get base uri var baseUri = GetCurrentUriWithQuery(context.ViewContext); // Generate result return ChangeQueryParameterValue(baseUri, name, value); }
/// <summary> /// When derived, generate the fragment part for a specified pager item. /// </summary> /// <param name="context">The generation context.</param> /// <returns></returns> protected abstract string GenerateFragment([NotNull] PagerItemGenerationContext context);
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) { return(string.Format(FormatProvider, Format, context.PagerItem.PageNumber)); }
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) { var currentUri = GetCurrentUriWithQuery(context.ViewContext); return(HandleUriCore(currentUri, context)); }
/// <summary> /// Generate a <see cref="PagerRenderingItem" /> for the current pager item. /// </summary> /// <param name="list">The ownner list of the new item.</param> /// <param name="context">The generation context.</param> /// <returns>The generated <see cref="PagerRenderingItem" /> instance.</returns> private PagerRenderingItem GenerateRenderingItem(PagerRenderingList list, PagerItemGenerationContext context) { return new PagerRenderingItem(list) { Content = context.PagerItemOptions.Content?.GenerateContent(context), Link = context.PagerItemOptions.Link?.GenerateLink(context), Settings = new Dictionary<string, string>(context.PagerItemOptions.AdditionalSettings), State = GetRenderingItemState(context) }; }
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) => Text;
/// <summary> /// Generate the content for a specified pager item. /// </summary> /// <param name="context">The generation context.</param> /// <returns>The generated HTML content for the pager item.</returns> public IHtmlContent GenerateContent(PagerItemGenerationContext context) { return(HtmlContentGenerator(context)); }
/// <summary> /// Generate a <see cref="PagerRenderingList" /> for a series of <see cref="PagerItem" />. /// </summary> /// <param name="items">The collection of <see cref="PagerItem" /> which are included in the list.</param> /// <param name="context">The generation context.</param> /// <returns>The generated <see cref="PagerRenderingList" /> instance.</returns> private PagerRenderingList GenerateRenderingListCore(IEnumerable<PagerItem> items, PagerGenerationContext context) { var optionsCache = new Dictionary<PagerItemType, PagerItemOptions>(); var renderingItemList = new List<PagerRenderingItem>(); // Owner var result = new PagerRenderingList { Settings = new Dictionary<string, string>(context.Options.AdditionalSettings) }; foreach (var item in items) { PagerItemOptions itemOptions; // Try to get cached options, or create a new option instance if (!optionsCache.TryGetValue(item.ItemType, out itemOptions)) { itemOptions = context.Options.ItemOptions.GetMergedOptionsFor(item); optionsCache[item.ItemType] = itemOptions; } // Generate item context var itemContext = new PagerItemGenerationContext(context, item, itemOptions); // Generate item and add to list var renderingItem = GenerateRenderingItem(result, itemContext); renderingItemList.Add(renderingItem); } // Result result.Items = new ReadOnlyCollection<PagerRenderingItem>(renderingItemList); return result; }
/// <summary> /// Generate the link url for the specified <see cref="PagerItem" />. /// </summary> /// <param name="context">The generation context.</param> public string GenerateLink(PagerItemGenerationContext context) => null;