Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -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 | |
| 14 | /** |
| 15 | * @ngdoc directive |
| 16 | * @name xos.uiComponents.directive:xosPagination |
| 17 | * @restrict E |
| 18 | * @description The xos-table directive |
| 19 | * @param {Number} pageSize Number of elements per page |
| 20 | * @param {Number} totalElements Number of total elements in the collection |
| 21 | * @param {Function} change The callback to be triggered on page change. |
| 22 | * * @element ANY |
| 23 | * @scope |
| 24 | * @example |
| 25 | <example module="samplePagination"> |
| 26 | <file name="index.html"> |
| 27 | <div ng-controller="SampleCtrl1 as vm"> |
| 28 | <xos-pagination |
| 29 | page-size="vm.pageSize" |
| 30 | total-elements="vm.totalElements" |
| 31 | change="vm.change"> |
| 32 | </xos-pagination> |
| 33 | </div> |
| 34 | </file> |
| 35 | <file name="script.js"> |
| 36 | angular.module('samplePagination', ['xos.uiComponents']) |
| 37 | .controller('SampleCtrl1', function(){ |
| 38 | this.pageSize = 10; |
| 39 | this.totalElements = 35; |
| 40 | this.change = (pageNumber) => { |
| 41 | console.log(pageNumber); |
| 42 | } |
| 43 | }); |
| 44 | </file> |
| 45 | </example> |
| 46 | **/ |
| 47 | |
Arpit Agarwal | 34b6383 | 2016-08-08 11:59:45 -0700 | [diff] [blame] | 48 | .component('xosPagination', { |
| 49 | restrict: 'E', |
| 50 | bindings: { |
| 51 | pageSize: '=', |
| 52 | totalElements: '=', |
| 53 | change: '=' |
| 54 | }, |
| 55 | template: ` |
| 56 | <div class="row" ng-if="vm.pageList.length > 1"> |
| 57 | <div class="col-xs-12 text-center"> |
| 58 | <ul class="pagination"> |
| 59 | <li |
| 60 | ng-click="vm.goToPage(vm.currentPage - 1)" |
| 61 | ng-class="{disabled: vm.currentPage == 0}"> |
| 62 | <a href="" aria-label="Previous"> |
| 63 | <span aria-hidden="true">«</span> |
| 64 | </a> |
| 65 | </li> |
| 66 | <li ng-repeat="i in vm.pageList" ng-class="{active: i === vm.currentPage}"> |
| 67 | <a href="" ng-click="vm.goToPage(i)">{{i + 1}}</a> |
| 68 | </li> |
| 69 | <li |
| 70 | ng-click="vm.goToPage(vm.currentPage + 1)" |
| 71 | ng-class="{disabled: vm.currentPage == vm.pages - 1}"> |
| 72 | <a href="" aria-label="Next"> |
| 73 | <span aria-hidden="true">»</span> |
| 74 | </a> |
| 75 | </li> |
| 76 | </ul> |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 77 | </div> |
Arpit Agarwal | 34b6383 | 2016-08-08 11:59:45 -0700 | [diff] [blame] | 78 | </div> |
| 79 | `, |
| 80 | bindToController: true, |
| 81 | controllerAs: 'vm', |
| 82 | controller: function($scope){ |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 83 | |
Arpit Agarwal | 34b6383 | 2016-08-08 11:59:45 -0700 | [diff] [blame] | 84 | this.currentPage = 0; |
| 85 | |
| 86 | this.goToPage = (n) => { |
| 87 | if(n < 0 || n === this.pages){ |
| 88 | return; |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 89 | } |
Arpit Agarwal | 34b6383 | 2016-08-08 11:59:45 -0700 | [diff] [blame] | 90 | this.currentPage = n; |
| 91 | this.change(n); |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 92 | } |
Arpit Agarwal | 34b6383 | 2016-08-08 11:59:45 -0700 | [diff] [blame] | 93 | |
| 94 | this.createPages = (pages) => { |
| 95 | let arr = []; |
| 96 | for(let i = 0; i < pages; i++){ |
| 97 | arr.push(i); |
| 98 | } |
| 99 | return arr; |
| 100 | } |
| 101 | |
| 102 | // watch for data changes |
| 103 | $scope.$watch(() => this.totalElements, () => { |
| 104 | if(this.totalElements){ |
| 105 | this.pages = Math.ceil(this.totalElements / this.pageSize); |
| 106 | this.pageList = this.createPages(this.pages); |
| 107 | } |
| 108 | }); |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 109 | } |
| 110 | }) |
| 111 | .filter('pagination', function(){ |
| 112 | return function(input, start) { |
| 113 | if(!input || !angular.isArray(input)){ |
| 114 | return input; |
| 115 | } |
| 116 | start = parseInt(start, 10); |
| 117 | return input.slice(start); |
| 118 | }; |
| 119 | }); |
| 120 | })(); |