Matteo Scandolo | 320ce2c | 2016-04-15 12:20:14 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Visit http://guide.xosproject.org/devguide/addview/ for more information |
| 5 | * |
| 6 | * Created by teone on 4/15/16. |
| 7 | */ |
| 8 | |
| 9 | (function () { |
| 10 | 'use strict'; |
| 11 | |
| 12 | angular.module('xos.uiComponents') |
| 13 | .directive('xosPagination', function(){ |
| 14 | return { |
| 15 | restrict: 'E', |
| 16 | scope: { |
| 17 | pageSize: '=', |
| 18 | totalElements: '=', |
| 19 | change: '=' |
| 20 | }, |
| 21 | template: ` |
| 22 | <div class="row"> |
| 23 | <div class="col-xs-12 text-center"> |
| 24 | <ul class="pagination"> |
| 25 | <li |
| 26 | ng-click="vm.goToPage(vm.currentPage - 1)" |
| 27 | ng-class="{disabled: vm.currentPage == 0}"> |
| 28 | <a href="" aria-label="Previous"> |
| 29 | <span aria-hidden="true">«</span> |
| 30 | </a> |
| 31 | </li> |
| 32 | <li ng-repeat="i in vm.pageList" ng-class="{active: i === vm.currentPage}"> |
| 33 | <a href="" ng-click="vm.goToPage(i)">{{i + 1}}</a> |
| 34 | </li> |
| 35 | <li |
| 36 | ng-click="vm.goToPage(vm.currentPage + 1)" |
| 37 | ng-class="{disabled: vm.currentPage == vm.pages - 1}"> |
| 38 | <a href="" aria-label="Next"> |
| 39 | <span aria-hidden="true">»</span> |
| 40 | </a> |
| 41 | </li> |
| 42 | </ul> |
| 43 | </div> |
| 44 | </div> |
| 45 | `, |
| 46 | bindToController: true, |
| 47 | controllerAs: 'vm', |
| 48 | controller: function($scope){ |
| 49 | |
| 50 | this.currentPage = 0; |
| 51 | |
| 52 | this.goToPage = (n) => { |
| 53 | if(n < 0 || n === this.pages){ |
| 54 | return; |
| 55 | } |
| 56 | this.currentPage = n; |
| 57 | this.change(n); |
| 58 | } |
| 59 | |
| 60 | this.createPages = (pages) => { |
| 61 | let arr = []; |
| 62 | for(var i = 0; i < pages; i++){ |
| 63 | arr.push(i); |
| 64 | } |
| 65 | return arr; |
| 66 | } |
| 67 | |
| 68 | // watch for data changes |
| 69 | $scope.$watch(() => this.totalElements, () => { |
| 70 | if(this.totalElements){ |
| 71 | this.pages = Math.round(this.totalElements / this.pageSize); |
| 72 | this.pageList = this.createPages(this.pages); |
| 73 | } |
| 74 | // scope.getPages(); |
| 75 | }); |
| 76 | } |
| 77 | } |
| 78 | }) |
| 79 | .filter('pagination', function(){ |
| 80 | return function(input, start) { |
| 81 | if(!input || !angular.isArray(input)){ |
| 82 | return input; |
| 83 | } |
| 84 | start = parseInt(start, 10); |
| 85 | return input.slice(start); |
| 86 | }; |
| 87 | }); |
| 88 | })(); |