panelSrv.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.services');
  8. module.service('panelSrv', function($rootScope, $timeout, datasourceSrv) {
  9. this.init = function($scope) {
  10. if (!$scope.panel.span) {
  11. $scope.panel.span = 12;
  12. }
  13. var menu = [
  14. {
  15. text: 'Edit',
  16. configModal: "app/partials/paneleditor.html",
  17. condition: !$scope.panelMeta.fullscreenEdit
  18. },
  19. {
  20. text: 'Edit',
  21. click: "toggleFullscreen(true)",
  22. condition: $scope.panelMeta.fullscreenEdit
  23. },
  24. {
  25. text: "Fullscreen",
  26. click: 'toggleFullscreen(false)',
  27. condition: $scope.panelMeta.fullscreenView
  28. },
  29. {
  30. text: 'Duplicate',
  31. click: 'duplicatePanel(panel)',
  32. condition: true
  33. },
  34. {
  35. text: 'Span',
  36. submenu: [
  37. { text: '1', click: 'updateColumnSpan(1)' },
  38. { text: '2', click: 'updateColumnSpan(2)' },
  39. { text: '3', click: 'updateColumnSpan(3)' },
  40. { text: '4', click: 'updateColumnSpan(4)' },
  41. { text: '5', click: 'updateColumnSpan(5)' },
  42. { text: '6', click: 'updateColumnSpan(6)' },
  43. { text: '7', click: 'updateColumnSpan(7)' },
  44. { text: '8', click: 'updateColumnSpan(8)' },
  45. { text: '9', click: 'updateColumnSpan(9)' },
  46. { text: '10', click: 'updateColumnSpan(10)' },
  47. { text: '11', click: 'updateColumnSpan(11)' },
  48. { text: '12', click: 'updateColumnSpan(12)' },
  49. ],
  50. condition: true
  51. },
  52. {
  53. text: 'Remove',
  54. click: 'remove_panel_from_row(row, panel)',
  55. condition: true
  56. }
  57. ];
  58. $scope.inspector = {};
  59. $scope.panelMeta.menu = _.where(menu, { condition: true });
  60. $scope.updateColumnSpan = function(span) {
  61. $scope.panel.span = span;
  62. $timeout(function() {
  63. $scope.$emit('render');
  64. });
  65. };
  66. $scope.addDataQuery = function() {
  67. $scope.panel.targets.push({target: ''});
  68. };
  69. $scope.removeDataQuery = function (query) {
  70. $scope.panel.targets = _.without($scope.panel.targets, query);
  71. $scope.get_data();
  72. };
  73. $scope.setDatasource = function(datasource) {
  74. $scope.panel.datasource = datasource;
  75. $scope.datasource = datasourceSrv.get(datasource);
  76. if (!$scope.datasource) {
  77. $scope.panel.error = "Cannot find datasource " + datasource;
  78. return;
  79. }
  80. };
  81. $scope.changeDatasource = function(datasource) {
  82. $scope.setDatasource(datasource);
  83. $scope.get_data();
  84. };
  85. $scope.toggleFullscreen = function(edit) {
  86. $scope.dashboardViewState.update({ fullscreen: true, edit: edit, panelId: $scope.panel.id });
  87. };
  88. $scope.otherPanelInFullscreenMode = function() {
  89. return $scope.dashboardViewState.fullscreen && !$scope.fullscreen;
  90. };
  91. // Post init phase
  92. $scope.fullscreen = false;
  93. $scope.editor = { index: 1 };
  94. if ($scope.panelMeta.fullEditorTabs) {
  95. $scope.editorTabs = _.pluck($scope.panelMeta.fullEditorTabs, 'title');
  96. }
  97. $scope.datasources = datasourceSrv.getMetricSources();
  98. $scope.setDatasource($scope.panel.datasource);
  99. $scope.dashboardViewState.registerPanel($scope);
  100. if ($scope.get_data) {
  101. var panel_get_data = $scope.get_data;
  102. $scope.get_data = function() {
  103. if ($scope.otherPanelInFullscreenMode()) { return; }
  104. delete $scope.panel.error;
  105. $scope.panelMeta.loading = true;
  106. panel_get_data();
  107. };
  108. if (!$scope.skipDataOnInit) {
  109. $scope.get_data();
  110. }
  111. }
  112. $scope.performance.panelsInitialized++;
  113. if ($scope.performance.panelsInitialized === $scope.dashboard.rows.length) {
  114. var timeTaken = new Date().getTime() - $scope.performance.dashboardLoadStart;
  115. console.log("Dashboard::Performance - All panels initialized in " + timeTaken + " ms");
  116. }
  117. };
  118. });
  119. });