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

148 lines
4.3 KiB
JavaScript
Raw Normal View History

/*
* Wazuh app - Top nav bar 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.
*/
2018-09-10 08:32:49 +00:00
import menuTemplate from './wz-menu.html';
import { uiModules } from 'ui/modules';
2018-02-15 12:18:00 +00:00
2018-08-20 07:00:50 +00:00
const app = uiModules.get('app/wazuh', []);
2018-02-15 12:18:00 +00:00
2018-08-20 07:00:50 +00:00
class WzMenu {
2018-09-10 08:32:49 +00:00
constructor() {
this.template = menuTemplate;
}
controller(
$scope,
$window,
$rootScope,
appState,
patternHandler,
indexPatterns,
errorHandler,
wazuhConfig
) {
$rootScope.showSelector = appState.getPatternSelector();
if (!$rootScope.$$phase) $rootScope.$digest();
if (appState.getCurrentAPI()) {
$scope.theresAPI = true;
$scope.currentAPI = JSON.parse(appState.getCurrentAPI()).name;
} else {
$scope.theresAPI = false;
2018-08-20 07:00:50 +00:00
}
2018-09-10 08:32:49 +00:00
$scope.goToClick = path => {
$window.location.href = path;
};
const load = async () => {
try {
2018-10-25 12:56:47 +00:00
const list = await patternHandler.getPatternList();
2018-09-10 08:32:49 +00:00
// Get the configuration to check if pattern selector is enabled
const config = wazuhConfig.getConfig();
appState.setPatternSelector(config['ip.selector']);
// 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()) {
2018-10-25 12:56:47 +00:00
appState.setCurrentPattern(list[0].id);
2018-09-10 08:32:49 +00:00
} else {
// Check if the current pattern cookie is valid
2018-10-25 12:56:47 +00:00
filtered = list.filter(item =>
2018-09-10 08:32:49 +00:00
item.id.includes(appState.getCurrentPattern())
);
2018-10-25 16:12:58 +00:00
if (!filtered.length) appState.setCurrentPattern(list[0].id);
2018-08-20 07:00:50 +00:00
}
2018-03-20 15:20:50 +00:00
2018-09-10 08:32:49 +00:00
const data = filtered
? filtered
: await indexPatterns.get(appState.getCurrentPattern());
$scope.theresPattern = true;
$scope.currentPattern = data.title;
// Getting the list of index patterns
if (list) {
$scope.patternList = list;
$scope.currentSelectedPattern = appState.getCurrentPattern();
2018-08-20 07:00:50 +00:00
}
2018-09-10 08:32:49 +00:00
if (!$scope.$$phase) $scope.$digest();
if (!$rootScope.$$phase) $rootScope.$digest();
return;
} catch (error) {
errorHandler.handle(error, 'Directives - Menu');
$scope.theresPattern = false;
}
};
load();
// Function to change the current index pattern on the app
$scope.changePattern = async selectedPattern => {
try {
if (!appState.getPatternSelector()) return;
$scope.currentSelectedPattern = await patternHandler.changePattern(
selectedPattern
);
if (!$scope.$$phase) $scope.$digest();
$window.location.reload();
return;
} catch (error) {
errorHandler.handle(error, 'Directives - Menu');
}
};
$scope.$on('updateAPI', (evt, params) => {
const current = appState.getCurrentAPI();
if (current) {
const parsed = JSON.parse(current);
// If we've received cluster info as parameter, it means we must update our stored cookie
if (params && params.cluster_info) {
if (params.cluster_info.status === 'enabled') {
parsed.name = params.cluster_info.cluster;
} else {
parsed.name = params.cluster_info.manager;
}
appState.setCurrentAPI(JSON.stringify(parsed));
2018-08-20 07:00:50 +00:00
}
2018-09-10 08:32:49 +00:00
$scope.theresAPI = true;
$scope.currentAPI = parsed.name;
} else {
$scope.theresAPI = false;
}
});
$scope.$on('updatePattern', () => {
if (!appState.getPatternSelector()) return;
indexPatterns
.get(appState.getCurrentPattern())
.then(() => {
$scope.theresPattern = true;
$scope.currentSelectedPattern = appState.getCurrentPattern();
})
.catch(error => {
errorHandler.handle(error, 'Directives - Menu');
$scope.theresPattern = false;
2018-08-20 07:00:50 +00:00
});
2018-09-10 08:32:49 +00:00
});
}
2018-08-20 07:00:50 +00:00
}
2018-02-15 12:18:00 +00:00
2018-09-10 08:32:49 +00:00
app.directive('wzMenu', () => new WzMenu());