|
| 1 | +module.exports = ['$scope', 'EventLogDataService', |
| 2 | + function ($scope, EventLogDataService) { |
| 3 | + |
| 4 | + // Defaults. |
| 5 | + var options = { |
| 6 | + pageIndex: 0, |
| 7 | + pageSize: 10, |
| 8 | + eventType: undefined, |
| 9 | + severity: undefined |
| 10 | + }; |
| 11 | + |
| 12 | + // Initialise scope variables. |
| 13 | + $scope.currentPage = 1; |
| 14 | + $scope.pageCount = 1; |
| 15 | + $scope.eventLogs = []; |
| 16 | + |
| 17 | + // Add functions to scope. |
| 18 | + $scope.hasNextPage = hasNextPage; |
| 19 | + $scope.hasPrevPage = hasPrevPage; |
| 20 | + $scope.nextPage = nextPage; |
| 21 | + $scope.prevPage = prevPage; |
| 22 | + $scope.loadPage = loadPage; |
| 23 | + $scope.getPages = getPages; |
| 24 | + |
| 25 | + // Fetch first page. |
| 26 | + fetchEvents(options); |
| 27 | + |
| 28 | + // Fetch events. |
| 29 | + function fetchEvents(opts) { |
| 30 | + |
| 31 | + // Load events based on options. |
| 32 | + EventLogDataService.browseEvents(opts).then( |
| 33 | + function (response) { |
| 34 | + |
| 35 | + // Place the events on the scope. |
| 36 | + $scope.eventLogs = response.Data; |
| 37 | + |
| 38 | + // Place page count on scope. |
| 39 | + $scope.pageCount = response.Pagination.Pages; |
| 40 | + |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + // Is there a next page? |
| 45 | + function hasNextPage() { |
| 46 | + return $scope.currentPage < $scope.pageCount; |
| 47 | + } |
| 48 | + |
| 49 | + // Is there a previous page? |
| 50 | + function hasPrevPage() { |
| 51 | + return $scope.currentPage > 1; |
| 52 | + } |
| 53 | + |
| 54 | + // Load the next page. |
| 55 | + function nextPage() { |
| 56 | + |
| 57 | + // Is there a next page? |
| 58 | + if (hasNextPage()) { |
| 59 | + |
| 60 | + // Yes, increment and fetch events. |
| 61 | + $scope.currentPage++; |
| 62 | + |
| 63 | + options.pageIndex = $scope.currentPage - 1; |
| 64 | + |
| 65 | + fetchEvents(options); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Load the next page. |
| 70 | + function prevPage() { |
| 71 | + |
| 72 | + // Is there a previous page? |
| 73 | + if (hasPrevPage()) { |
| 74 | + |
| 75 | + // Yes, decrement and fetch events. |
| 76 | + $scope.currentPage--; |
| 77 | + |
| 78 | + options.pageIndex = $scope.currentPage - 1; |
| 79 | + |
| 80 | + fetchEvents(options); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Load specified page. |
| 85 | + function loadPage(pageNo) { |
| 86 | + |
| 87 | + // Page exists? |
| 88 | + if (pageNo > 0 && pageNo <= $scope.pageCount) { |
| 89 | + |
| 90 | + $scope.currentPage = pageNo; |
| 91 | + |
| 92 | + options.pageIndex = pageNo - 1; |
| 93 | + |
| 94 | + fetchEvents(options); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Get array for pagination. |
| 99 | + function getPages(number) { |
| 100 | + return new Array(number); |
| 101 | + } |
| 102 | + |
| 103 | + }]; |
0 commit comments