pagination.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. rangeStop: function() {
  28. var rangeStop = this.get('rangeStart') + this.get('pageSize'),
  29. total = this.get('total');
  30. if (rangeStop < total) {
  31. return rangeStop;
  32. }
  33. return total;
  34. }.property('total', 'rangeStart', 'pageSize').cacheable(),
  35. hasPrevious: function() {
  36. return this.get('rangeStart') > 0;
  37. }.property('rangeStart').cacheable(),
  38. hasNext: function() {
  39. return this.get('rangeStop') < this.get('total');
  40. }.property('rangeStop', 'total').cacheable(),
  41. nextPage: function() {
  42. if (this.get('hasNext')) {
  43. this.incrementProperty('rangeStart', this.get('pageSize'));
  44. }
  45. },
  46. previousPage: function() {
  47. if (this.get('hasPrevious')) {
  48. this.decrementProperty('rangeStart', this.get('pageSize'));
  49. }
  50. },
  51. currentPage: function () {
  52. return this.get('rangeStop') / this.get('pageSize');
  53. }.property('rangeStop', 'pageSize').cacheable(),
  54. startPosition: function() {
  55. if (this.get('total') == 0)
  56. return 0;
  57. return this.get('rangeStart') + 1;
  58. }.property('rangeStart', 'total').cacheable(),
  59. totalPages: function() {
  60. return Math.ceil(this.get('total') / this.get('pageSize'));
  61. }.property('total', 'pageSize').cacheable(),
  62. // changeContent: function() {
  63. //// this.didRequestRange(this.get('rangeStart'), this.get('rangeStop'));
  64. // }.observes('total', 'rangeStart', 'rangeStop'),
  65. pageSizeChange: function() {
  66. this.set('rangeStart', 0);
  67. }.observes('pageSize')
  68. });