PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/microsoft/xzsyw/Syw/Syw/Syw.Framework/Mvc/Models/PagerModel.cs

http://jqbird.googlecode.com/
C# | 414 lines | 297 code | 38 blank | 79 comment | 13 complexity | 6743c41d9c90f1c577bbefb12d8ca900 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Syw.Framework.Mvc.Models
  6. {
  7. public class PagerModel
  8. {
  9. #region Constructors
  10. public PagerModel()
  11. {
  12. }
  13. #endregion Constructors
  14. #region Fields
  15. private int individualPagesDisplayedCount;
  16. private int pageIndex = -2;
  17. private int pageSize;
  18. private bool? showFirst;
  19. private bool? showIndividualPages;
  20. private bool? showLast;
  21. private bool? showNext;
  22. private bool? showPagerItems;
  23. private bool? showPrevious;
  24. private bool? showTotalSummary;
  25. private int totalRecords;
  26. private string firstButtonText;
  27. private string lastButtonText;
  28. private string nextButtonText;
  29. private string previousButtonText;
  30. private string currentPageText;
  31. #endregion Fields
  32. #region Properties
  33. /// <summary>
  34. /// Gets the current page index
  35. /// </summary>
  36. public int CurrentPage
  37. {
  38. get
  39. {
  40. return (this.PageIndex + 1);
  41. }
  42. }
  43. /// <summary>
  44. /// Gets or sets a count of individual pages to be displayed
  45. /// </summary>
  46. public int IndividualPagesDisplayedCount
  47. {
  48. get
  49. {
  50. if (individualPagesDisplayedCount <= 0)
  51. return 5;
  52. else
  53. return individualPagesDisplayedCount;
  54. }
  55. set
  56. {
  57. individualPagesDisplayedCount = value;
  58. }
  59. }
  60. /// <summary>
  61. /// Gets the current page index
  62. /// </summary>
  63. public int PageIndex
  64. {
  65. get
  66. {
  67. if (this.pageIndex < 0)
  68. {
  69. return 0;
  70. }
  71. return this.pageIndex;
  72. }
  73. set
  74. {
  75. this.pageIndex = value;
  76. }
  77. }
  78. /// <summary>
  79. /// Gets or sets a page size
  80. /// </summary>
  81. public int PageSize
  82. {
  83. get
  84. {
  85. return (pageSize <= 0) ? 10 : pageSize;
  86. }
  87. set
  88. {
  89. pageSize = value;
  90. }
  91. }
  92. /// <summary>
  93. /// Gets or sets a value indicating whether to show "first"
  94. /// </summary>
  95. public bool ShowFirst
  96. {
  97. get
  98. {
  99. return showFirst ?? true;
  100. }
  101. set
  102. {
  103. showFirst = value;
  104. }
  105. }
  106. /// <summary>
  107. /// Gets or sets a value indicating whether to show "individual pages"
  108. /// </summary>
  109. public bool ShowIndividualPages
  110. {
  111. get
  112. {
  113. return showIndividualPages ?? true;
  114. }
  115. set
  116. {
  117. showIndividualPages = value;
  118. }
  119. }
  120. /// <summary>
  121. /// Gets or sets a value indicating whether to show "last"
  122. /// </summary>
  123. public bool ShowLast
  124. {
  125. get
  126. {
  127. return showLast ?? true;
  128. }
  129. set
  130. {
  131. showLast = value;
  132. }
  133. }
  134. /// <summary>
  135. /// Gets or sets a value indicating whether to show "next"
  136. /// </summary>
  137. public bool ShowNext
  138. {
  139. get
  140. {
  141. return showNext ?? true;
  142. }
  143. set
  144. {
  145. showNext = value;
  146. }
  147. }
  148. /// <summary>
  149. /// Gets or sets a value indicating whether to show pager items
  150. /// </summary>
  151. public bool ShowPagerItems
  152. {
  153. get
  154. {
  155. return showPagerItems ?? true;
  156. }
  157. set
  158. {
  159. showPagerItems = value;
  160. }
  161. }
  162. /// <summary>
  163. /// Gets or sets a value indicating whether to show "previous"
  164. /// </summary>
  165. public bool ShowPrevious
  166. {
  167. get
  168. {
  169. return showPrevious ?? true;
  170. }
  171. set
  172. {
  173. showPrevious = value;
  174. }
  175. }
  176. /// <summary>
  177. /// Gets or sets a value indicating whether to show "total summary"
  178. /// </summary>
  179. public bool ShowTotalSummary
  180. {
  181. get
  182. {
  183. return showTotalSummary ?? false;
  184. }
  185. set
  186. {
  187. showTotalSummary = value;
  188. }
  189. }
  190. /// <summary>
  191. /// Gets a total pages count
  192. /// </summary>
  193. public int TotalPages
  194. {
  195. get
  196. {
  197. if ((this.TotalRecords == 0) || (this.PageSize == 0))
  198. {
  199. return 0;
  200. }
  201. int num = this.TotalRecords / this.PageSize;
  202. if ((this.TotalRecords % this.PageSize) > 0)
  203. {
  204. num++;
  205. }
  206. return num;
  207. }
  208. }
  209. /// <summary>
  210. /// Gets or sets a total records count
  211. /// </summary>
  212. public int TotalRecords
  213. {
  214. get
  215. {
  216. return totalRecords;
  217. }
  218. set
  219. {
  220. totalRecords = value;
  221. }
  222. }
  223. /// <summary>
  224. /// Gets or sets the first button text
  225. /// </summary>
  226. public string FirstButtonText
  227. {
  228. get
  229. {
  230. return (!string.IsNullOrEmpty(firstButtonText)) ?
  231. firstButtonText :
  232. "???";
  233. }
  234. set
  235. {
  236. firstButtonText = value;
  237. }
  238. }
  239. /// <summary>
  240. /// Gets or sets the last button text
  241. /// </summary>
  242. public string LastButtonText
  243. {
  244. get
  245. {
  246. return (!string.IsNullOrEmpty(lastButtonText)) ?
  247. lastButtonText :
  248. "??";
  249. }
  250. set
  251. {
  252. lastButtonText = value;
  253. }
  254. }
  255. /// <summary>
  256. /// Gets or sets the next button text
  257. /// </summary>
  258. public string NextButtonText
  259. {
  260. get
  261. {
  262. return (!string.IsNullOrEmpty(nextButtonText)) ?
  263. nextButtonText :
  264. "???";
  265. }
  266. set
  267. {
  268. nextButtonText = value;
  269. }
  270. }
  271. /// <summary>
  272. /// Gets or sets the previous button text
  273. /// </summary>
  274. public string PreviousButtonText
  275. {
  276. get
  277. {
  278. return (!string.IsNullOrEmpty(previousButtonText)) ?
  279. previousButtonText :
  280. "???";
  281. }
  282. set
  283. {
  284. previousButtonText = value;
  285. }
  286. }
  287. /// <summary>
  288. /// Gets or sets the current page text
  289. /// </summary>
  290. public string CurrentPageText
  291. {
  292. get
  293. {
  294. return (!string.IsNullOrEmpty(currentPageText)) ?
  295. currentPageText :
  296. "? {0} of {1} ( ? {2})";
  297. }
  298. set
  299. {
  300. currentPageText = value;
  301. }
  302. }
  303. /// <summary>
  304. /// Gets or sets the route name or action name
  305. /// </summary>
  306. public string RouteActionName { get; set; }
  307. /// <summary>
  308. /// Gets or sets whether the links are created using RouteLink instead of Action Link
  309. /// (for additional route values such as slugs or page numbers)
  310. /// </summary>
  311. public bool UseRouteLinks { get; set; }
  312. /// <summary>
  313. /// Gets or sets the RouteValues object. Allows for custom route values other than page.
  314. /// </summary>
  315. public IRouteValues RouteValues { get; set; }
  316. #endregion Properties
  317. #region Methods
  318. /// <summary>
  319. /// Gets first individual page index
  320. /// </summary>
  321. /// <returns>Page index</returns>
  322. public int GetFirstIndividualPageIndex()
  323. {
  324. if ((this.TotalPages < this.IndividualPagesDisplayedCount) ||
  325. ((this.PageIndex - (this.IndividualPagesDisplayedCount / 2)) < 0))
  326. {
  327. return 0;
  328. }
  329. if ((this.PageIndex + (this.IndividualPagesDisplayedCount / 2)) >= this.TotalPages)
  330. {
  331. return (this.TotalPages - this.IndividualPagesDisplayedCount);
  332. }
  333. return (this.PageIndex - (this.IndividualPagesDisplayedCount / 2));
  334. }
  335. /// <summary>
  336. /// Get last individual page index
  337. /// </summary>
  338. /// <returns>Page index</returns>
  339. public int GetLastIndividualPageIndex()
  340. {
  341. int num = this.IndividualPagesDisplayedCount / 2;
  342. if ((this.IndividualPagesDisplayedCount % 2) == 0)
  343. {
  344. num--;
  345. }
  346. if ((this.TotalPages < this.IndividualPagesDisplayedCount) ||
  347. ((this.PageIndex + num) >= this.TotalPages))
  348. {
  349. return (this.TotalPages - 1);
  350. }
  351. if ((this.PageIndex - (this.IndividualPagesDisplayedCount / 2)) < 0)
  352. {
  353. return (this.IndividualPagesDisplayedCount - 1);
  354. }
  355. return (this.PageIndex + num);
  356. }
  357. #endregion Methods
  358. }
  359. /// <summary>
  360. /// Interface for custom RouteValues objects
  361. /// </summary>
  362. public interface IRouteValues
  363. {
  364. int page { get; set; }
  365. }
  366. /// <summary>
  367. /// Class that has a slug and page for route values. Used for Topic (posts) and
  368. /// Forum (topics) pagination
  369. /// </summary>
  370. public class RouteValues : IRouteValues
  371. {
  372. public int id { get; set; }
  373. public string slug { get; set; }
  374. public int page { get; set; }
  375. }
  376. }