panelSrv.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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) { $scope.panel.span = 12; }
  11. if (!$scope.panel.title) { $scope.panel.title = 'No title'; }
  12. var menu = [
  13. {
  14. text: "view",
  15. click: 'toggleFullscreen(false)',
  16. condition: $scope.panelMeta.fullscreenView
  17. },
  18. {
  19. text: 'edit',
  20. editorLink: "app/partials/paneleditor.html",
  21. condition: !$scope.panelMeta.fullscreenEdit
  22. },
  23. {
  24. text: 'edit',
  25. click: "toggleFullscreen(true)",
  26. condition: $scope.panelMeta.fullscreenEdit
  27. },
  28. {
  29. text: 'duplicate',
  30. click: 'duplicatePanel(panel)',
  31. condition: true
  32. },
  33. // {
  34. // text: 'span',
  35. // submenu: [
  36. // { text: '1', click: 'updateColumnSpan(1)' },
  37. // { text: '2', click: 'updateColumnSpan(2)' },
  38. // { text: '3', click: 'updateColumnSpan(3)' },
  39. // { text: '4', click: 'updateColumnSpan(4)' },
  40. // { text: '5', click: 'updateColumnSpan(5)' },
  41. // { text: '6', click: 'updateColumnSpan(6)' },
  42. // { text: '7', click: 'updateColumnSpan(7)' },
  43. // { text: '8', click: 'updateColumnSpan(8)' },
  44. // { text: '9', click: 'updateColumnSpan(9)' },
  45. // { text: '10', click: 'updateColumnSpan(10)' },
  46. // { text: '11', click: 'updateColumnSpan(11)' },
  47. // { text: '12', click: 'updateColumnSpan(12)' },
  48. // ],
  49. // condition: true
  50. // },
  51. {
  52. text: 'json',
  53. click: 'editPanelJson()',
  54. condition: true
  55. },
  56. // {
  57. // text: 'remove',
  58. // click: 'remove_panel_from_row(row, panel)',
  59. // condition: true
  60. // }
  61. ];
  62. $scope.inspector = {};
  63. $scope.panelMeta.menu = _.where(menu, { condition: true });
  64. $scope.editPanelJson = function() {
  65. $scope.emitAppEvent('show-json-editor', { object: $scope.panel, updateHandler: $scope.replacePanel });
  66. };
  67. $scope.updateColumnSpan = function(span) {
  68. $scope.panel.span = Math.min(Math.max($scope.panel.span + span, 1), 12);
  69. $timeout(function() {
  70. $scope.$emit('render');
  71. });
  72. };
  73. $scope.addDataQuery = function() {
  74. $scope.panel.targets.push({target: ''});
  75. };
  76. $scope.removeDataQuery = function (query) {
  77. $scope.panel.targets = _.without($scope.panel.targets, query);
  78. $scope.get_data();
  79. };
  80. $scope.setDatasource = function(datasource) {
  81. $scope.panel.datasource = datasource;
  82. $scope.datasource = datasourceSrv.get(datasource);
  83. if (!$scope.datasource) {
  84. $scope.panelMeta.error = "Cannot find datasource " + datasource;
  85. return;
  86. }
  87. };
  88. $scope.changeDatasource = function(datasource) {
  89. $scope.setDatasource(datasource);
  90. $scope.get_data();
  91. };
  92. $scope.toggleFullscreen = function(edit) {
  93. $scope.dashboardViewState.update({ fullscreen: true, edit: edit, panelId: $scope.panel.id });
  94. };
  95. $scope.otherPanelInFullscreenMode = function() {
  96. return $scope.dashboardViewState.fullscreen && !$scope.fullscreen;
  97. };
  98. // Post init phase
  99. $scope.fullscreen = false;
  100. $scope.editor = { index: 1 };
  101. if ($scope.panelMeta.fullEditorTabs) {
  102. $scope.editorTabs = _.pluck($scope.panelMeta.fullEditorTabs, 'title');
  103. }
  104. $scope.datasources = datasourceSrv.getMetricSources();
  105. $scope.setDatasource($scope.panel.datasource);
  106. $scope.dashboardViewState.registerPanel($scope);
  107. if ($scope.get_data) {
  108. var panel_get_data = $scope.get_data;
  109. $scope.get_data = function() {
  110. if ($scope.otherPanelInFullscreenMode()) { return; }
  111. delete $scope.panelMeta.error;
  112. $scope.panelMeta.loading = true;
  113. panel_get_data();
  114. };
  115. if (!$scope.skipDataOnInit) {
  116. $scope.get_data();
  117. }
  118. }
  119. };
  120. });
  121. });