blob: 15c5ab34faf2a3a1c360cffdc907049a50aad0c6 [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019/**
20 * © OpenCORD
21 *
22 * Visit http://guide.xosproject.org/devguide/addview/ for more information
23 *
24 * Created by teone on 4/15/16.
25 */
26
27(function () {
28 'use strict';
29
30 angular.module('xos.uiComponents')
31
32 /**
33 * @ngdoc directive
34 * @name xos.uiComponents.directive:xosPagination
35 * @restrict E
36 * @description The xos-table directive
37 * @param {Number} pageSize Number of elements per page
38 * @param {Number} totalElements Number of total elements in the collection
39 * @param {Function} change The callback to be triggered on page change.
40 * * @element ANY
41 * @scope
42 * @example
43 <example module="samplePagination">
44 <file name="index.html">
45 <div ng-controller="SampleCtrl1 as vm">
46 <xos-pagination
47 page-size="vm.pageSize"
48 total-elements="vm.totalElements"
49 change="vm.change">
50 </xos-pagination>
51 </div>
52 </file>
53 <file name="script.js">
54 angular.module('samplePagination', ['xos.uiComponents'])
55 .controller('SampleCtrl1', function(){
56 this.pageSize = 10;
57 this.totalElements = 35;
58 this.change = (pageNumber) => {
59 console.log(pageNumber);
60 }
61 });
62 </file>
63 </example>
64 **/
65
Arpit Agarwal34b63832016-08-08 11:59:45 -070066 .component('xosPagination', {
67 restrict: 'E',
68 bindings: {
69 pageSize: '=',
70 totalElements: '=',
71 change: '='
72 },
73 template: `
74 <div class="row" ng-if="vm.pageList.length > 1">
75 <div class="col-xs-12 text-center">
76 <ul class="pagination">
77 <li
78 ng-click="vm.goToPage(vm.currentPage - 1)"
79 ng-class="{disabled: vm.currentPage == 0}">
80 <a href="" aria-label="Previous">
81 <span aria-hidden="true">&laquo;</span>
82 </a>
83 </li>
84 <li ng-repeat="i in vm.pageList" ng-class="{active: i === vm.currentPage}">
85 <a href="" ng-click="vm.goToPage(i)">{{i + 1}}</a>
86 </li>
87 <li
88 ng-click="vm.goToPage(vm.currentPage + 1)"
89 ng-class="{disabled: vm.currentPage == vm.pages - 1}">
90 <a href="" aria-label="Next">
91 <span aria-hidden="true">&raquo;</span>
92 </a>
93 </li>
94 </ul>
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070095 </div>
Arpit Agarwal34b63832016-08-08 11:59:45 -070096 </div>
97 `,
98 bindToController: true,
99 controllerAs: 'vm',
100 controller: function($scope){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700101
Arpit Agarwal34b63832016-08-08 11:59:45 -0700102 this.currentPage = 0;
103
104 this.goToPage = (n) => {
105 if(n < 0 || n === this.pages){
106 return;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700107 }
Arpit Agarwal34b63832016-08-08 11:59:45 -0700108 this.currentPage = n;
109 this.change(n);
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700110 }
Arpit Agarwal34b63832016-08-08 11:59:45 -0700111
112 this.createPages = (pages) => {
113 let arr = [];
114 for(let i = 0; i < pages; i++){
115 arr.push(i);
116 }
117 return arr;
118 }
119
120 // watch for data changes
121 $scope.$watch(() => this.totalElements, () => {
122 if(this.totalElements){
123 this.pages = Math.ceil(this.totalElements / this.pageSize);
124 this.pageList = this.createPages(this.pages);
125 }
126 });
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700127 }
128 })
129 .filter('pagination', function(){
130 return function(input, start) {
131 if(!input || !angular.isArray(input)){
132 return input;
133 }
134 start = parseInt(start, 10);
135 return input.slice(start);
136 };
137 });
138})();