PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/pub/js/vendors/backbone/backbone-paginator.js

https://github.com/oliver96/adsys
JavaScript | 80 lines | 76 code | 4 blank | 0 comment | 18 complexity | 38d0dd375d167ae132d8b4aca6fd3960 MD5 | raw file
  1. Backbone.Paginator = (function ( Backbone, _, $ ) {
  2. 'use strict';
  3. var Paginator = Backbone.View.extend({
  4. el: null
  5. , tpl: '<% if (totalPage > 0) { %>' +
  6. '<ul>' +
  7. '<% if(page > 1) { %>' +
  8. '<% if (showFirst == true) { %>' +
  9. '<li><a href="#" class="first">&#171;</a></li>' +
  10. '<% } %>' +
  11. '<li><a href="#" class="prev">&#8249;</a></li>' +
  12. '<% } %>' +
  13. '<% var mod = page % pageSize, startPage = 0; endPage = 0; %>' +
  14. '<% startPage = (mod == 0 ? page - pageSize : page - mod) + 1; %>' +
  15. '<% endPage = startPage + pageSize; %>' +
  16. '<% if(endPage > totalPage + 1) endPage = totalPage + 1; %>' +
  17. '<% for(var p = startPage; p < endPage; p ++) { %>' +
  18. '<% if(p == page) { %>' +
  19. '<li class="active"><a href="#" class="page"><%=p%></a></li>' +
  20. '<% } else { %>' +
  21. '<li><a href="#" class="page"><%=p%></a></li>' +
  22. '<% } %>' +
  23. '<% } %>' +
  24. '<% if(page > 0 && page < totalPage) { %>' +
  25. '<li><a href="#" class="next">&#8250;</a></li>' +
  26. '<% if(showLast == true) { %>' +
  27. '<li><a href="#" class="last">&#187;</a></li>' +
  28. '<% } %>' +
  29. '<% } %>' +
  30. '<% if (showInfo == true) { %>' +
  31. '<li><span class="page-info">第&nbsp;<%=page%>&nbsp;&#8260;&nbsp;<%=totalPage%>&nbsp;页</span></li>' +
  32. '<% } %>' +
  33. '</ul>' +
  34. '<% } %>'
  35. , template: null
  36. , events: {
  37. 'click a.first': 'firstPage',
  38. 'click a.prev': 'prevPage',
  39. 'click a.page': 'changePage',
  40. 'click a.next': 'nextPage',
  41. 'click a.last': 'lastPage'
  42. }
  43. , initialize: function () {
  44. if(this.el == null) return;
  45. this.template = _.template(this.tpl);
  46. this.listenTo(this.collection, 'reset', this.render);
  47. this.listenTo(this.collection, 'change', this.render);
  48. }
  49. , render: function () {
  50. var html = this.template(this.collection.pageInfo);
  51. this.$el.html(html);
  52. }
  53. , firstPage: function(e) {
  54. e.preventDefault();
  55. this.collection.firstPage();
  56. }
  57. , prevPage: function(e) {
  58. e.preventDefault();
  59. this.collection.prevPage();
  60. }
  61. , changePage: function(e) {
  62. e.preventDefault();
  63. var page = parseInt($(e.target).text());
  64. this.collection.changePage(page);
  65. }
  66. , nextPage: function(e) {
  67. e.preventDefault();
  68. this.collection.nextPage();
  69. }
  70. , lastPage: function(e) {
  71. e.preventDefault();
  72. this.collection.lastPage();
  73. }
  74. });
  75. return Paginator
  76. }( Backbone, _, jQuery ));