Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.0 KiB

  1. using System;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.Web.UI.HtmlControls;
  5. using System.Web.UI.WebControls;
  6. using System.Data;
  7. using System.Collections;
  8. using System.Collections.Specialized;
  9. namespace UDDI.VisualStudio
  10. {
  11. internal struct PageInfo
  12. {
  13. public PageInfo( bool showEllipses, bool isCurrentPage, int pageNumber )
  14. {
  15. this.showEllipses = showEllipses;
  16. this.isCurrentPage = isCurrentPage;
  17. this.pageNumber = pageNumber;
  18. }
  19. public bool showEllipses;
  20. public bool isCurrentPage;
  21. public int pageNumber;
  22. }
  23. internal class PagingRenderer
  24. {
  25. private ArrayList pageRange;
  26. private int numResults;
  27. private int currentPage;
  28. private string parentUrl;
  29. public PagingRenderer( int numResults, int currentPage, string parentUrl )
  30. {
  31. this.numResults = numResults;
  32. this.currentPage = currentPage;
  33. this.parentUrl = parentUrl;
  34. pageRange = new ArrayList();
  35. //
  36. // Get the number of pages of results we have
  37. //
  38. int numPages = ( int ) Math.Ceiling( ( double ) Decimal.Divide( ( Decimal )numResults, ( Decimal )Constants.NumResultsPerPage ) );
  39. //
  40. // Start our loop at the current page. Add the current page, the move forward adding pages. We will keep adding
  41. // pages until we have added the number of pages specified by Constants.MaxPagesToShow or we run out of pages to add.
  42. // If we have more pages to add after we reach Constants.MaxPagesToShow, we'll start working our way back from the current
  43. // page.
  44. //
  45. int forwardIndex = 0;
  46. int backIndex = currentPage - 1;
  47. int numPagesAdded = 0;
  48. while( numPagesAdded < Constants.MaxPagesToShow && numPagesAdded < numPages)
  49. {
  50. if( currentPage + forwardIndex <= numPages )
  51. {
  52. //
  53. // The first page added will always be the current page
  54. //
  55. pageRange.Add( new PageInfo(false, forwardIndex == 0 , currentPage + forwardIndex ) );
  56. forwardIndex++;
  57. }
  58. else
  59. {
  60. pageRange.Insert( 0, new PageInfo( false, false, backIndex ) );
  61. backIndex--;
  62. }
  63. numPagesAdded++;
  64. }
  65. if( pageRange.Count > 0 )
  66. {
  67. PageInfo lastPage = ( PageInfo )pageRange[ pageRange.Count - 1];
  68. if( lastPage.pageNumber != numPages )
  69. {
  70. pageRange.Add( new PageInfo( true, false, lastPage.pageNumber + 1 ) );
  71. }
  72. PageInfo firstPage = ( PageInfo )pageRange[ 0 ];
  73. if( firstPage.pageNumber != 1 )
  74. {
  75. pageRange.Insert( 0, new PageInfo( true, false, firstPage.pageNumber - 1 ) );
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// This method will render the row of page numbers that link to the pages of our request. This method assumes it is
  81. /// being called in the context of a HTML table. It will add rows to that table.
  82. /// </summary>
  83. /// <param name="writer">Writer to use to write out our HTML.</param>
  84. public void Render( HtmlTextWriter writer )
  85. {
  86. writer.RenderBeginTag( HtmlTextWriterTag.Table );
  87. writer.RenderBeginTag( HtmlTextWriterTag.Tr );
  88. foreach( PageInfo pageInfo in pageRange )
  89. {
  90. if( true == pageInfo.showEllipses )
  91. {
  92. writer.RenderBeginTag( HtmlTextWriterTag.Td );
  93. RenderEllipses( pageInfo.pageNumber, writer );
  94. writer.RenderEndTag();
  95. }
  96. else
  97. {
  98. writer.RenderBeginTag( HtmlTextWriterTag.Td );
  99. if( true == pageInfo.isCurrentPage )
  100. {
  101. RenderCurrentPageLink( writer );
  102. }
  103. else
  104. {
  105. RenderPageLink( pageInfo.pageNumber, writer );
  106. }
  107. writer.RenderEndTag();
  108. }
  109. }
  110. writer.RenderEndTag();
  111. writer.RenderEndTag();
  112. }
  113. /// <summary>
  114. /// This method will render a previous or next ellipses in our row of page numbers. This is done so we can limit the number
  115. /// of pages to show in that row.
  116. /// </summary>
  117. /// <param name="pageNumber">The page number to link the ellipses to.</param>
  118. /// <param name="writer">Writer to use to write our HTML.</param>
  119. private void RenderEllipses( int pageNumber, HtmlTextWriter writer)
  120. {
  121. string pageLink = string.Format( "{0}?{1}={2}", parentUrl, StateParamNames.CurrentPage, pageNumber );
  122. pageLink += GetKeyedRefData();
  123. writer.AddAttribute( HtmlTextWriterAttribute.Href, pageLink );
  124. writer.RenderBeginTag( HtmlTextWriterTag.A );
  125. writer.Write( "..." );
  126. writer.RenderEndTag();
  127. }
  128. /// <summary>
  129. /// This method will render a page number.
  130. /// </summary>
  131. /// <param name="pageNumber">Page number to render.</param>
  132. /// <param name="writer">Writer to use to write our HTML.</param>
  133. private void RenderPageLink( int pageNumber, HtmlTextWriter writer )
  134. {
  135. string pageLink = string.Format( "{0}?{1}={2}", parentUrl, StateParamNames.CurrentPage, pageNumber );
  136. pageLink += GetKeyedRefData();
  137. writer.AddAttribute( HtmlTextWriterAttribute.Class, "boldBlue" );
  138. writer.AddAttribute( HtmlTextWriterAttribute.Href, pageLink );
  139. writer.RenderBeginTag( HtmlTextWriterTag.A );
  140. writer.Write( pageNumber );
  141. writer.RenderEndTag();
  142. }
  143. /// <summary>
  144. /// Renders the 'current' page. This page number will not be a link.
  145. /// </summary>
  146. /// <param name="writer">Writer to use to write our HTML.</param>
  147. private void RenderCurrentPageLink( HtmlTextWriter writer )
  148. {
  149. writer.AddAttribute( HtmlTextWriterAttribute.Class, "A.navbold" );
  150. writer.RenderBeginTag( HtmlTextWriterTag.Span );
  151. writer.Write( currentPage );
  152. writer.RenderEndTag();
  153. }
  154. private string GetKeyedRefData()
  155. {
  156. if( !Utility.StringEmpty( HttpContext.Current.Request[ StateParamNames.TModelKey ] ) )
  157. {
  158. string urlext = "&{0}={1}&{2}={3}";
  159. return string.Format(
  160. urlext,
  161. StateParamNames.TModelKey,
  162. HttpContext.Current.Request[ StateParamNames.TModelKey ],
  163. StateParamNames.KeyValue,
  164. HttpContext.Current.Request[ StateParamNames.KeyValue ]
  165. );
  166. }
  167. else
  168. {
  169. return string.Empty;
  170. }
  171. }
  172. }
  173. }