pagination.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. @extends Ember.Mixin
  21. Implements common pagination management properties for controllers.
  22. */
  23. App.Pagination = Em.Mixin.create({
  24. total: 0,
  25. rangeStart: 0,
  26. pageSize: 0,
  27. // didRequestRange: Em.K,
  28. rangeStop: function() {
  29. var rangeStop = this.get('rangeStart') + this.get('pageSize'),
  30. total = this.get('total');
  31. if (rangeStop < total) {
  32. return rangeStop;
  33. }
  34. return total;
  35. }.property('total', 'rangeStart', 'pageSize').cacheable(),
  36. hasPrevious: function() {
  37. return this.get('rangeStart') > 0;
  38. }.property('rangeStart').cacheable(),
  39. hasNext: function() {
  40. return this.get('rangeStop') < this.get('total');
  41. }.property('rangeStop', 'total').cacheable(),
  42. nextPage: function() {
  43. if (this.get('hasNext')) {
  44. this.incrementProperty('rangeStart', this.get('pageSize'));
  45. }
  46. },
  47. previousPage: function() {
  48. if (this.get('hasPrevious')) {
  49. this.decrementProperty('rangeStart', this.get('pageSize'));
  50. }
  51. },
  52. currentPage: function () {
  53. return this.get('rangeStop') / this.get('pageSize');
  54. }.property('rangeStop', 'pageSize').cacheable(),
  55. startPosition: function() {
  56. return this.get('rangeStart') + 1;
  57. }.property('rangeStart').cacheable(),
  58. totalPages: function() {
  59. return Math.ceil(this.get('total') / this.get('pageSize'));
  60. }.property('total', 'pageSize').cacheable(),
  61. // changeContent: function() {
  62. //// this.didRequestRange(this.get('rangeStart'), this.get('rangeStop'));
  63. // }.observes('total', 'rangeStart', 'rangeStop'),
  64. pageSizeChange: function() {
  65. this.set('rangeStart', 0);
  66. }.observes('pageSize')
  67. });