wazuh-kibana-app/public/directives/wz-data-table/wz-data-table-directive.js

140 lines
4.0 KiB
JavaScript
Raw Normal View History

/*
2018-12-07 10:01:51 +00:00
* Wazuh app - Wazuh table with data as input parameter directive
* 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,
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);
/**
* This loads data for table, that has been provided by parameter
*/
2018-12-07 10:01:51 +00:00
const fetch = () => {
try {
2018-12-07 11:54:41 +00:00
$scope.filterTable();
$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 10:01:51 +00:00
return;
};
2018-12-07 10:01:51 +00:00
$scope.sortValue = '';
$scope.sortReverse = false;
$scope.searchTerm = '';
$scope.sort = key => {
2018-12-07 11:54:41 +00:00
if (key !== $scope.sortValue) {
$scope.sortReverse = false;
}
$scope.sortValue = key;
$scope.sortReverse = !$scope.sortReverse;
2018-12-07 11:54:41 +00:00
$scope.filterTable();
}
/**
* 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();
}
/**
* On controller loads
*/
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
};
});