wazuh-kibana-app/public/directives/wz-menu/wz-menu.js

128 lines
5.5 KiB
JavaScript
Raw Normal View History

2018-02-15 12:18:00 +00:00
import menuTemplate from './wz-menu.html'
const app = require('ui/modules').get('app/wazuh', []);
app.directive('wzMenu',function(){
return {
2018-04-10 13:08:47 +00:00
controller: function ($scope, $window, $rootScope, appState, patternHandler, courier, errorHandler,genericReq,$location) {
2018-03-13 14:18:25 +00:00
$rootScope.showSelector = appState.getPatternSelector();
2018-03-20 15:20:50 +00:00
2018-03-13 14:18:25 +00:00
if(!$rootScope.$$phase) $rootScope.$digest();
2018-03-20 15:20:50 +00:00
2018-02-15 12:18:00 +00:00
if(appState.getCurrentAPI()) {
$scope.theresAPI = true;
$scope.currentAPI = JSON.parse(appState.getCurrentAPI()).name;
}
else {
$scope.theresAPI = false;
}
2018-03-20 15:20:50 +00:00
2018-02-15 12:18:00 +00:00
$scope.goToClick = path => {
$window.location.href = path;
}
2018-03-20 15:20:50 +00:00
2018-02-15 12:18:00 +00:00
const load = async () => {
try {
// Get the configuration to check if pattern selector is enabled
const config = await genericReq.request('GET', '/api/wazuh-api/configuration', {});
appState.setPatternSelector(typeof config.data.data['ip.selector'] !== 'undefined' ? config.data.data['ip.selector'] : true)
// Abort if we have disabled the pattern selector
if(!appState.getPatternSelector()) return;
// Show the pattern selector
$rootScope.showSelector = true;
let filtered = false;
// If there is no current pattern, fetch it
if(!appState.getCurrentPattern()) {
const currentPattern = await genericReq.request('GET', '/get-list');
if(!currentPattern.data.data.length){
$rootScope.blankScreenError = 'Sorry but no valid index patterns were found'
$location.search('tab',null);
$location.path('/blank-screen');
return;
}
appState.setCurrentPattern(currentPattern.data.data[0].id);
} else {
2018-04-10 13:08:47 +00:00
// If there is current pattern, check if there is some pattern
const patternList = await genericReq.request('GET', '/get-list');
if(!patternList.data.data.length){
$rootScope.blankScreenError = 'Sorry but no valid index patterns were found'
$location.search('tab',null);
$location.path('/blank-screen');
return;
}
// Check if the current pattern cookie is valid
filtered = patternList.data.data.filter(item => item.id.includes(appState.getCurrentPattern()))
if(!filtered.length) appState.setCurrentPattern(patternList.data.data[0].id)
}
const data = filtered ? filtered : await courier.indexPatterns.get(appState.getCurrentPattern());
2018-02-15 12:18:00 +00:00
$scope.theresPattern = true;
$scope.currentPattern = data.title;
const list = await patternHandler.getPatternList();
// Getting the list of index patterns
if(list) {
$scope.patternList = list;
$scope.currentSelectedPattern = appState.getCurrentPattern();
}
if(!$scope.$$phase) $scope.$digest();
if(!$rootScope.$$phase) $rootScope.$digest();
2018-02-15 12:18:00 +00:00
return;
} catch (error) {
errorHandler.handle(error,'Directives - Menu');
$scope.theresPattern = false;
if(!$rootScope.$$phase) $rootScope.$digest();
}
}
load();
// Function to change the current index pattern on the app
$scope.changePattern = async selectedPattern => {
try{
if(!appState.getPatternSelector()) return;
2018-02-15 12:18:00 +00:00
$scope.currentSelectedPattern = await patternHandler.changePattern(selectedPattern);
if(!$scope.$$phase) $scope.$digest();
$window.location.reload();
return;
}catch(error){
errorHandler.handle(error,'Directives - Menu');
if(!$rootScope.$$phase) $rootScope.$digest();
2018-03-20 15:20:50 +00:00
}
2018-02-15 12:18:00 +00:00
}
$scope.$on('updateAPI', () => {
if(appState.getCurrentAPI())
{
$scope.theresAPI = true;
$scope.currentAPI = JSON.parse(appState.getCurrentAPI()).name;
}
else {
$scope.theresAPI = false;
}
});
$scope.$on('updatePattern', () => {
if(!appState.getPatternSelector()) return;
2018-02-15 12:18:00 +00:00
courier.indexPatterns.get(appState.getCurrentPattern())
.then(data => {
$scope.theresPattern = true;
$scope.currentSelectedPattern = appState.getCurrentPattern();
})
.catch(error => {
errorHandler.handle(error,'Directives - Menu');
if(!$rootScope.$$phase) $rootScope.$digest();
$scope.theresPattern = false;
});
});
},
template: menuTemplate
};
2018-03-20 15:20:50 +00:00
});