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
|
2018-12-07 09:31:14 +00:00
|
|
|
* Copyright (C) 2018 Wazuh, Inc.
|
|
|
|
*
|
|
|
|
* 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', []);
|
|
|
|
|
|
|
|
app.directive('wzDataTable', function () {
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
rowSizes: '=rowSizes',
|
|
|
|
data: '='
|
|
|
|
},
|
|
|
|
controller(
|
|
|
|
$scope,
|
2018-12-07 11:54:41 +00:00
|
|
|
$filter,
|
2018-12-07 09:31:14 +00:00
|
|
|
errorHandler,
|
|
|
|
$window,
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
init().then(() => resizing = false).catch(() => resizing = false);
|
|
|
|
}, 150);
|
|
|
|
};
|
|
|
|
$scope.rowsPerPage = calcTableRows($window.innerHeight, rowSizes);
|
|
|
|
|
2018-12-12 11:47:05 +00:00
|
|
|
/**
|
|
|
|
* This loads data for table, that has been provided by parameter
|
|
|
|
*/
|
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
|
|
|
$scope.keys = Object.keys(items[0]);
|
|
|
|
return;
|
|
|
|
} 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-12 11:47:05 +00:00
|
|
|
/**
|
|
|
|
* This apply filter and sorting to table data
|
|
|
|
*/
|
2018-12-07 11:54:41 +00:00
|
|
|
$scope.filterTable = () => {
|
|
|
|
items = $filter('orderBy')($filter('filter')($scope.data, $scope.searchTerm), $scope.sortValue, $scope.sortReverse);
|
|
|
|
$scope.totalItems = items.length;
|
|
|
|
$scope.items = items;
|
|
|
|
checkGap($scope, items);
|
|
|
|
$scope.searchTable();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
$scope.setPage = function () {
|
|
|
|
$scope.currentPage = this.n;
|
|
|
|
$scope.nextPage(this.n);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listeners
|
|
|
|
*/
|
|
|
|
$scope.$on('$destroy', () => {
|
|
|
|
$window.onresize = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
init();
|
|
|
|
},
|
|
|
|
template
|
|
|
|
};
|
|
|
|
});
|