panelSrv.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: 'advanced',
  53. submenu: [
  54. { text: 'Panel JSON', click: 'editPanelJson()' },
  55. ],
  56. condition: true
  57. },
  58. // {
  59. // text: 'remove',
  60. // click: 'remove_panel_from_row(row, panel)',
  61. // condition: true
  62. // }
  63. ];
  64. $scope.inspector = {};
  65. $scope.panelMeta.menu = _.where(menu, { condition: true });
  66. $scope.editPanelJson = function() {
  67. $scope.emitAppEvent('show-json-editor', { object: $scope.panel, updateHandler: $scope.replacePanel });
  68. };
  69. $scope.updateColumnSpan = function(span) {
  70. $scope.panel.span = Math.min(Math.max($scope.panel.span + span, 1), 12);
  71. $timeout(function() {
  72. $scope.$emit('render');
  73. });
  74. };
  75. $scope.addDataQuery = function() {
  76. $scope.panel.targets.push({target: ''});
  77. };
  78. $scope.removeDataQuery = function (query) {
  79. $scope.panel.targets = _.without($scope.panel.targets, query);
  80. $scope.get_data();
  81. };
  82. $scope.setDatasource = function(datasource) {
  83. $scope.panel.datasource = datasource;
  84. $scope.datasource = datasourceSrv.get(datasource);
  85. if (!$scope.datasource) {
  86. $scope.panelMeta.error = "Cannot find datasource " + datasource;
  87. return;
  88. }
  89. };
  90. $scope.changeDatasource = function(datasource) {
  91. $scope.setDatasource(datasource);
  92. $scope.get_data();
  93. };
  94. $scope.toggleFullscreen = function(edit) {
  95. $scope.dashboardViewState.update({ fullscreen: true, edit: edit, panelId: $scope.panel.id });
  96. };
  97. $scope.otherPanelInFullscreenMode = function() {
  98. return $scope.dashboardViewState.fullscreen && !$scope.fullscreen;
  99. };
  100. // Post init phase
  101. $scope.fullscreen = false;
  102. $scope.editor = { index: 1 };
  103. if ($scope.panelMeta.fullEditorTabs) {
  104. $scope.editorTabs = _.pluck($scope.panelMeta.fullEditorTabs, 'title');
  105. }
  106. $scope.datasources = datasourceSrv.getMetricSources();
  107. $scope.setDatasource($scope.panel.datasource);
  108. $scope.dashboardViewState.registerPanel($scope);
  109. if ($scope.get_data) {
  110. var panel_get_data = $scope.get_data;
  111. $scope.get_data = function() {
  112. if ($scope.otherPanelInFullscreenMode()) { return; }
  113. delete $scope.panelMeta.error;
  114. $scope.panelMeta.loading = true;
  115. panel_get_data();
  116. };
  117. if (!$scope.skipDataOnInit) {
  118. $scope.get_data();
  119. }
  120. }
  121. };
  122. });
  123. });