panelSrv.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. ],
  6. function (angular, _, $) {
  7. 'use strict';
  8. var module = angular.module('grafana.services');
  9. module.service('panelSrv', function($rootScope, $timeout, datasourceSrv) {
  10. this.init = function($scope) {
  11. if (!$scope.panel.span) {
  12. $scope.panel.span = 12;
  13. }
  14. var menu = [
  15. {
  16. text: 'Edit',
  17. configModal: "app/partials/paneleditor.html",
  18. condition: !$scope.panelMeta.fullscreenEdit
  19. },
  20. {
  21. text: 'Edit',
  22. click: "toggleFullscreen(true)",
  23. condition: $scope.panelMeta.fullscreenEdit
  24. },
  25. {
  26. text: "Fullscreen",
  27. click: 'toggleFullscreen(false)',
  28. condition: $scope.panelMeta.fullscreenView
  29. },
  30. {
  31. text: 'Duplicate',
  32. click: 'duplicatePanel(panel)',
  33. condition: true
  34. },
  35. {
  36. text: 'Span',
  37. submenu: [
  38. { text: '1', click: 'updateColumnSpan(1)' },
  39. { text: '2', click: 'updateColumnSpan(2)' },
  40. { text: '3', click: 'updateColumnSpan(3)' },
  41. { text: '4', click: 'updateColumnSpan(4)' },
  42. { text: '5', click: 'updateColumnSpan(5)' },
  43. { text: '6', click: 'updateColumnSpan(6)' },
  44. { text: '7', click: 'updateColumnSpan(7)' },
  45. { text: '8', click: 'updateColumnSpan(8)' },
  46. { text: '9', click: 'updateColumnSpan(9)' },
  47. { text: '10', click: 'updateColumnSpan(10)' },
  48. { text: '11', click: 'updateColumnSpan(11)' },
  49. { text: '12', click: 'updateColumnSpan(12)' },
  50. ],
  51. condition: true
  52. },
  53. {
  54. text: 'Remove',
  55. click: 'remove_panel_from_row(row, panel)',
  56. condition: true
  57. }
  58. ];
  59. $scope.inspector = {};
  60. $scope.panelMeta.menu = _.where(menu, { condition: true });
  61. $scope.updateColumnSpan = function(span) {
  62. $scope.panel.span = span;
  63. $timeout(function() {
  64. $scope.$emit('render');
  65. });
  66. };
  67. $scope.enterFullscreenMode = function(options) {
  68. var docHeight = $(window).height();
  69. var editHeight = Math.floor(docHeight * 0.3);
  70. var fullscreenHeight = Math.floor(docHeight * 0.7);
  71. var oldTimeRange = $scope.range;
  72. $scope.height = options.edit ? editHeight : fullscreenHeight;
  73. $scope.editMode = options.edit;
  74. if (!$scope.fullscreen) {
  75. var closeEditMode = $rootScope.$on('panel-fullscreen-exit', function() {
  76. $scope.editMode = false;
  77. $scope.fullscreen = false;
  78. delete $scope.height;
  79. closeEditMode();
  80. $timeout(function() {
  81. if (oldTimeRange !== $scope.range) {
  82. $scope.dashboard.emit_refresh();
  83. }
  84. else {
  85. $scope.$emit('render');
  86. }
  87. });
  88. });
  89. }
  90. $(window).scrollTop(0);
  91. $scope.dashboardViewState.update({ fullscreen: true, edit: $scope.editMode, panelId: $scope.panel.id });
  92. $scope.fullscreen = true;
  93. $timeout(function() {
  94. $scope.$emit('render');
  95. });
  96. };
  97. $scope.addDataQuery = function() {
  98. $scope.panel.targets.push({target: ''});
  99. };
  100. $scope.removeDataQuery = function (query) {
  101. $scope.panel.targets = _.without($scope.panel.targets, query);
  102. $scope.get_data();
  103. };
  104. $scope.setDatasource = function(datasource) {
  105. $scope.panel.datasource = datasource;
  106. $scope.datasource = datasourceSrv.get(datasource);
  107. if (!$scope.datasource) {
  108. $scope.panel.error = "Cannot find datasource " + datasource;
  109. return;
  110. }
  111. };
  112. $scope.changeDatasource = function(datasource) {
  113. $scope.setDatasource(datasource);
  114. $scope.get_data();
  115. };
  116. $scope.toggleFullscreen = function(edit) {
  117. $scope.dashboardViewState.update({ fullscreen: true, edit: edit, panelId: $scope.panel.id });
  118. };
  119. $scope.otherPanelInFullscreenMode = function() {
  120. return $scope.dashboardViewState.fullscreen && !$scope.fullscreen;
  121. };
  122. // Post init phase
  123. $scope.fullscreen = false;
  124. $scope.editor = { index: 1 };
  125. if ($scope.panelMeta.fullEditorTabs) {
  126. $scope.editorTabs = _.pluck($scope.panelMeta.fullEditorTabs, 'title');
  127. }
  128. $scope.datasources = datasourceSrv.getMetricSources();
  129. $scope.setDatasource($scope.panel.datasource);
  130. $scope.dashboardViewState.registerPanel($scope);
  131. if ($scope.get_data) {
  132. var panel_get_data = $scope.get_data;
  133. $scope.get_data = function() {
  134. if ($scope.otherPanelInFullscreenMode()) { return; }
  135. delete $scope.panel.error;
  136. $scope.panelMeta.loading = true;
  137. panel_get_data();
  138. };
  139. if (!$scope.skipDataOnInit) {
  140. $scope.get_data();
  141. }
  142. }
  143. };
  144. });
  145. });