2018-12-07 09:31:14 +00:00
|
|
|
/*
|
2018-12-07 10:01:51 +00:00
|
|
|
* Wazuh app - Wazuh table with data as input parameter directive
|
2019-01-14 16:36:47 +00:00
|
|
|
* Copyright (C) 2015-2019 Wazuh, Inc.
|
2018-12-07 09:31:14 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Find more information about this on the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import template from './wz-data-table.html';
|
|
|
|
import { uiModules } from 'ui/modules';
|
|
|
|
import { KeyEquivalenece } from '../../../util/csv-key-equivalence';
|
|
|
|
import { calcTableRows } from '../wz-table/lib/rows';
|
|
|
|
import * as pagination from '../wz-table/lib/pagination';
|
|
|
|
import { checkGap } from '../wz-table/lib/check-gap';
|
|
|
|
|
|
|
|
const app = uiModules.get('app/wazuh', []);
|
|
|
|
|
2019-02-22 16:19:37 +00:00
|
|
|
app.directive('wzDataTable', function() {
|
2018-12-07 09:31:14 +00:00
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
rowSizes: '=rowSizes',
|
2019-02-07 11:41:16 +00:00
|
|
|
data: '=',
|
|
|
|
keys: '='
|
2018-12-07 09:31:14 +00:00
|
|
|
},
|
2018-12-13 10:02:53 +00:00
|
|
|
controller($scope, $filter, errorHandler, $window) {
|
2018-12-07 09:31:14 +00:00
|
|
|
/**
|
|
|
|
* Init variables
|
|
|
|
*/
|
|
|
|
$scope.keyEquivalence = KeyEquivalenece;
|
|
|
|
$scope.totalItems = 0;
|
|
|
|
$scope.wazuh_table_loading = true;
|
|
|
|
$scope.items = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizing. Calculate number of table rows depending on the screen height
|
|
|
|
*/
|
|
|
|
const rowSizes = $scope.rowSizes || [15, 13, 11];
|
|
|
|
let doit;
|
|
|
|
let resizing = false;
|
|
|
|
$window.onresize = () => {
|
|
|
|
if (resizing) return;
|
|
|
|
resizing = true;
|
|
|
|
clearTimeout(doit);
|
|
|
|
doit = setTimeout(() => {
|
|
|
|
$scope.rowsPerPage = calcTableRows($window.innerHeight, rowSizes);
|
|
|
|
$scope.itemsPerPage = $scope.rowsPerPage;
|
2018-12-13 10:02:53 +00:00
|
|
|
init()
|
|
|
|
.then(() => (resizing = false))
|
|
|
|
.catch(() => (resizing = false));
|
2018-12-07 09:31:14 +00:00
|
|
|
}, 150);
|
|
|
|
};
|
|
|
|
$scope.rowsPerPage = calcTableRows($window.innerHeight, rowSizes);
|
|
|
|
|
2018-12-12 11:47:05 +00:00
|
|
|
/**
|
2018-12-13 10:02:53 +00:00
|
|
|
* This loads data for table, that has been provided by parameter
|
2018-12-12 11:47:05 +00:00
|
|
|
*/
|
2018-12-07 10:01:51 +00:00
|
|
|
const fetch = () => {
|
2018-12-07 09:31:14 +00:00
|
|
|
try {
|
2018-12-07 11:54:41 +00:00
|
|
|
$scope.filterTable();
|
2018-12-07 09:31:14 +00:00
|
|
|
} catch (error) {
|
2018-12-07 11:54:41 +00:00
|
|
|
errorHandler.handle(error, 'Error loading table');
|
2018-12-07 09:31:14 +00:00
|
|
|
}
|
2018-12-07 10:01:51 +00:00
|
|
|
return;
|
2018-12-07 09:31:14 +00:00
|
|
|
};
|
|
|
|
|
2018-12-07 10:01:51 +00:00
|
|
|
$scope.sortValue = '';
|
|
|
|
$scope.sortReverse = false;
|
|
|
|
$scope.searchTerm = '';
|
2018-12-07 09:31:14 +00:00
|
|
|
$scope.sort = key => {
|
2018-12-07 11:54:41 +00:00
|
|
|
if (key !== $scope.sortValue) {
|
|
|
|
$scope.sortReverse = false;
|
|
|
|
}
|
2018-12-07 09:31:14 +00:00
|
|
|
$scope.sortValue = key;
|
|
|
|
$scope.sortReverse = !$scope.sortReverse;
|
2018-12-07 11:54:41 +00:00
|
|
|
$scope.filterTable();
|
2018-12-13 10:02:53 +00:00
|
|
|
};
|
2018-12-07 11:54:41 +00:00
|
|
|
|
2018-12-12 11:47:05 +00:00
|
|
|
/**
|
|
|
|
* This apply filter and sorting to table data
|
|
|
|
*/
|
2018-12-07 11:54:41 +00:00
|
|
|
$scope.filterTable = () => {
|
2018-12-13 10:02:53 +00:00
|
|
|
items = $filter('orderBy')(
|
|
|
|
$filter('filter')($scope.data, $scope.searchTerm),
|
|
|
|
$scope.sortValue,
|
|
|
|
$scope.sortReverse
|
|
|
|
);
|
2018-12-07 11:54:41 +00:00
|
|
|
$scope.totalItems = items.length;
|
|
|
|
$scope.items = items;
|
|
|
|
checkGap($scope, items);
|
|
|
|
$scope.searchTable();
|
2018-12-13 10:02:53 +00:00
|
|
|
};
|
2018-12-07 09:31:14 +00:00
|
|
|
|
2018-12-12 11:47:05 +00:00
|
|
|
/**
|
|
|
|
* On controller loads
|
|
|
|
*/
|
2018-12-07 09:31:14 +00:00
|
|
|
const init = async () => {
|
|
|
|
$scope.error = false;
|
|
|
|
$scope.wazuh_table_loading = true;
|
|
|
|
await fetch();
|
|
|
|
$scope.wazuh_table_loading = false;
|
2018-12-13 10:02:53 +00:00
|
|
|
};
|
2018-12-07 09:31:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pagination variables and functions
|
|
|
|
*/
|
|
|
|
$scope.itemsPerPage = $scope.rowsPerPage || 10;
|
|
|
|
$scope.pagedItems = [];
|
|
|
|
$scope.currentPage = 0;
|
|
|
|
let items = [];
|
|
|
|
$scope.gap = 0;
|
|
|
|
$scope.searchTable = () => pagination.searchTable($scope, items);
|
|
|
|
$scope.groupToPages = () => pagination.groupToPages($scope);
|
|
|
|
$scope.range = (size, start, end) =>
|
|
|
|
pagination.range(size, start, end, $scope.gap);
|
|
|
|
$scope.prevPage = () => pagination.prevPage($scope);
|
|
|
|
$scope.nextPage = async currentPage =>
|
|
|
|
pagination.nextPage(currentPage, $scope, errorHandler, fetch);
|
2019-02-22 16:19:37 +00:00
|
|
|
$scope.setPage = function() {
|
2018-12-07 09:31:14 +00:00
|
|
|
$scope.currentPage = this.n;
|
|
|
|
$scope.nextPage(this.n);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listeners
|
|
|
|
*/
|
|
|
|
$scope.$on('$destroy', () => {
|
|
|
|
$window.onresize = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
init();
|
|
|
|
},
|
|
|
|
template
|
|
|
|
};
|
|
|
|
});
|