panel_directive.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'app/core/config',
  5. ],
  6. function (angular, $, config) {
  7. 'use strict';
  8. var module = angular.module('grafana.directives');
  9. module.directive('panelLoader', function($compile, $parse) {
  10. return {
  11. restrict: 'E',
  12. link: function(scope, elem, attr) {
  13. var getter = $parse(attr.type), panelType = getter(scope);
  14. var module = config.panels[panelType].module;
  15. System.import(module).then(function() {
  16. var panelEl = angular.element(document.createElement('grafana-panel-' + panelType));
  17. elem.append(panelEl);
  18. $compile(panelEl)(scope);
  19. }).catch(function(err) {
  20. console.log('Failed to load panel:', err);
  21. scope.appEvent('alert-error', ['Panel Load Error', 'Failed to load panel ' + panelType + ', ' + err]);
  22. });
  23. }
  24. };
  25. });
  26. module.directive('grafanaPanel', function() {
  27. return {
  28. restrict: 'E',
  29. templateUrl: 'app/features/panel/partials/panel.html',
  30. transclude: true,
  31. link: function(scope, elem) {
  32. var panelContainer = elem.find('.panel-container');
  33. scope.$watchGroup(['fullscreen', 'height', 'panel.height', 'row.height'], function() {
  34. panelContainer.css({ minHeight: scope.height || scope.panel.height || scope.row.height, display: 'block' });
  35. elem.toggleClass('panel-fullscreen', scope.fullscreen ? true : false);
  36. });
  37. }
  38. };
  39. });
  40. module.directive('datasourceCustomSettingsView', function($compile) {
  41. return {
  42. restrict: 'E',
  43. scope: {
  44. dsMeta: "=",
  45. current: "=",
  46. },
  47. link: function(scope, elem) {
  48. scope.$watch("dsMeta.module", function() {
  49. if (!scope.dsMeta) {
  50. return;
  51. }
  52. System.import(scope.dsMeta.module).then(function() {
  53. elem.empty();
  54. var panelEl = angular.element(document.createElement('datasource-custom-settings-view-' + scope.dsMeta.id));
  55. elem.append(panelEl);
  56. $compile(panelEl)(scope);
  57. }).catch(function(err) {
  58. console.log('Failed to load plugin:', err);
  59. scope.appEvent('alert-error', ['Plugin Load Error', 'Failed to load plugin ' + scope.dsMeta.id + ', ' + err]);
  60. });
  61. });
  62. }
  63. };
  64. });
  65. module.directive('datasourceEditorView', function(dynamicDirectiveSrv) {
  66. return {
  67. restrict: 'E',
  68. link: function(scope, elem, attrs) {
  69. dynamicDirectiveSrv.define({
  70. datasourceProperty: attrs.datasource,
  71. name: attrs.name,
  72. scope: scope,
  73. parentElem: elem,
  74. });
  75. }
  76. };
  77. });
  78. module.directive('queryEditorLoader', function($compile, $parse, datasourceSrv) {
  79. return {
  80. restrict: 'E',
  81. link: function(scope, elem) {
  82. var editorScope;
  83. scope.$watch("panel.datasource", function() {
  84. var datasource = scope.target.datasource || scope.panel.datasource;
  85. datasourceSrv.get(datasource).then(function(ds) {
  86. if (editorScope) {
  87. editorScope.$destroy();
  88. elem.empty();
  89. }
  90. editorScope = scope.$new();
  91. editorScope.datasource = ds;
  92. if (!scope.target.refId) {
  93. scope.target.refId = 'A';
  94. }
  95. var panelEl = angular.element(document.createElement('metric-query-editor-' + ds.meta.id));
  96. elem.append(panelEl);
  97. $compile(panelEl)(editorScope);
  98. });
  99. });
  100. }
  101. };
  102. });
  103. module.directive('panelResizer', function($rootScope) {
  104. return {
  105. restrict: 'E',
  106. template: '<span class="resize-panel-handle"></span>',
  107. link: function(scope, elem) {
  108. var resizing = false;
  109. var lastPanel = false;
  110. var handleOffset;
  111. var originalHeight;
  112. var originalWidth;
  113. var maxWidth;
  114. function dragStartHandler(e) {
  115. e.preventDefault();
  116. resizing = true;
  117. handleOffset = $(e.target).offset();
  118. originalHeight = parseInt(scope.row.height);
  119. originalWidth = scope.panel.span;
  120. maxWidth = $(document).width();
  121. lastPanel = scope.row.panels[scope.row.panels.length - 1];
  122. $('body').on('mousemove', moveHandler);
  123. $('body').on('mouseup', dragEndHandler);
  124. }
  125. function moveHandler(e) {
  126. scope.row.height = originalHeight + (e.pageY - handleOffset.top);
  127. scope.panel.span = originalWidth + (((e.pageX - handleOffset.left) / maxWidth) * 12);
  128. scope.panel.span = Math.min(Math.max(scope.panel.span, 1), 12);
  129. var rowSpan = scope.dashboard.rowSpan(scope.row);
  130. // auto adjust other panels
  131. if (Math.floor(rowSpan) < 14) {
  132. // last panel should not push row down
  133. if (lastPanel === scope.panel && rowSpan > 12) {
  134. lastPanel.span -= rowSpan - 12;
  135. }
  136. // reduce width of last panel so total in row is 12
  137. else if (lastPanel !== scope.panel) {
  138. lastPanel.span = lastPanel.span - (rowSpan - 12);
  139. lastPanel.span = Math.min(Math.max(lastPanel.span, 1), 12);
  140. }
  141. }
  142. scope.$apply(function() {
  143. scope.$broadcast('render');
  144. });
  145. }
  146. function dragEndHandler() {
  147. // if close to 12
  148. var rowSpan = scope.dashboard.rowSpan(scope.row);
  149. if (rowSpan < 12 && rowSpan > 11) {
  150. lastPanel.span += 12 - rowSpan;
  151. }
  152. scope.$apply(function() {
  153. $rootScope.$broadcast('render');
  154. });
  155. $('body').off('mousemove', moveHandler);
  156. $('body').off('mouseup', dragEndHandler);
  157. }
  158. elem.on('mousedown', dragStartHandler);
  159. scope.$on("$destroy", function() {
  160. elem.off('mousedown', dragStartHandler);
  161. });
  162. }
  163. };
  164. });
  165. });