grafana/src/app/services/panelSrv.js

133 lines
3.5 KiB
JavaScript
Raw Normal View History

define([
'angular',
2014-08-07 07:35:19 -05:00
'lodash',
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.services');
module.service('panelSrv', function($rootScope, $timeout, datasourceSrv) {
this.init = function($scope) {
if (!$scope.panel.span) { $scope.panel.span = 12; }
var menu = [
{
2014-09-23 09:11:31 -05:00
text: "view",
2014-09-24 04:58:02 -05:00
icon: "icon-eye-open",
2014-09-23 09:11:31 -05:00
click: 'toggleFullscreen(false)',
condition: $scope.panelMeta.fullscreenView
},
{
text: 'edit',
2014-09-24 04:58:02 -05:00
icon: 'icon-cogs',
2014-09-23 15:30:01 -05:00
click: 'editPanel()',
condition: true,
},
{
2014-09-23 09:11:31 -05:00
text: 'duplicate',
2014-09-24 04:58:02 -05:00
icon: 'icon-copy',
click: 'duplicatePanel(panel)',
condition: true
},
{
text: 'json',
2014-09-24 04:58:02 -05:00
icon: 'icon-code',
click: 'editPanelJson()',
condition: true
},
2014-09-24 04:58:02 -05:00
{
text: 'share',
icon: 'icon-share',
click: 'sharePanel()',
condition: true
},
];
$scope.inspector = {};
$scope.panelMeta.menu = _.where(menu, { condition: true });
2014-09-23 15:30:01 -05:00
$scope.editPanel = function() {
if ($scope.panelMeta.fullscreenEdit) {
$scope.toggleFullscreen(true);
}
else {
$scope.emitAppEvent('show-dash-editor', { src: 'app/partials/paneleditor.html', scope: $scope });
}
};
$scope.editPanelJson = function() {
$scope.emitAppEvent('show-json-editor', { object: $scope.panel, updateHandler: $scope.replacePanel });
};
$scope.updateColumnSpan = function(span) {
2014-09-23 09:11:31 -05:00
$scope.panel.span = Math.min(Math.max($scope.panel.span + span, 1), 12);
$timeout(function() {
$scope.$emit('render');
});
};
$scope.addDataQuery = function() {
$scope.panel.targets.push({target: ''});
};
$scope.removeDataQuery = function (query) {
$scope.panel.targets = _.without($scope.panel.targets, query);
$scope.get_data();
};
$scope.setDatasource = function(datasource) {
$scope.panel.datasource = datasource;
$scope.datasource = datasourceSrv.get(datasource);
if (!$scope.datasource) {
$scope.panelMeta.error = "Cannot find datasource " + datasource;
return;
}
};
$scope.changeDatasource = function(datasource) {
$scope.setDatasource(datasource);
$scope.get_data();
};
$scope.toggleFullscreen = function(edit) {
$scope.dashboardViewState.update({ fullscreen: true, edit: edit, panelId: $scope.panel.id });
};
$scope.otherPanelInFullscreenMode = function() {
return $scope.dashboardViewState.fullscreen && !$scope.fullscreen;
};
// Post init phase
$scope.fullscreen = false;
$scope.editor = { index: 1 };
if ($scope.panelMeta.fullEditorTabs) {
$scope.editorTabs = _.pluck($scope.panelMeta.fullEditorTabs, 'title');
}
$scope.datasources = datasourceSrv.getMetricSources();
$scope.setDatasource($scope.panel.datasource);
2014-08-13 05:16:37 -05:00
$scope.dashboardViewState.registerPanel($scope);
if ($scope.get_data) {
var panel_get_data = $scope.get_data;
$scope.get_data = function() {
if ($scope.otherPanelInFullscreenMode()) { return; }
delete $scope.panelMeta.error;
$scope.panelMeta.loading = true;
panel_get_data();
};
if (!$scope.skipDataOnInit) {
$scope.get_data();
}
}
};
});
});