panel_directive.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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('datasourceEditorView', function(dynamicDirectiveSrv) {
  41. return {
  42. restrict: 'E',
  43. link: function(scope, elem, attrs) {
  44. dynamicDirectiveSrv.define({
  45. datasourceProperty: attrs.datasource,
  46. name: attrs.name,
  47. scope: scope,
  48. parentElem: elem,
  49. });
  50. }
  51. };
  52. });
  53. module.directive('queryEditorLoader', function($compile, $parse, datasourceSrv) {
  54. return {
  55. restrict: 'E',
  56. link: function(scope, elem) {
  57. var editorScope;
  58. scope.$watch("panel.datasource", function() {
  59. var datasource = scope.target.datasource || scope.panel.datasource;
  60. datasourceSrv.get(datasource).then(function(ds) {
  61. if (editorScope) {
  62. editorScope.$destroy();
  63. elem.empty();
  64. }
  65. editorScope = scope.$new();
  66. editorScope.datasource = ds;
  67. if (!scope.target.refId) {
  68. scope.target.refId = 'A';
  69. }
  70. var panelEl = angular.element(document.createElement('metric-query-editor-' + ds.meta.id));
  71. elem.append(panelEl);
  72. $compile(panelEl)(editorScope);
  73. });
  74. });
  75. }
  76. };
  77. });
  78. module.directive('panelResizer', function($rootScope) {
  79. return {
  80. restrict: 'E',
  81. template: '<span class="resize-panel-handle"></span>',
  82. link: function(scope, elem) {
  83. var resizing = false;
  84. var lastPanel = false;
  85. var handleOffset;
  86. var originalHeight;
  87. var originalWidth;
  88. var maxWidth;
  89. function dragStartHandler(e) {
  90. e.preventDefault();
  91. resizing = true;
  92. handleOffset = $(e.target).offset();
  93. originalHeight = parseInt(scope.row.height);
  94. originalWidth = scope.panel.span;
  95. maxWidth = $(document).width();
  96. lastPanel = scope.row.panels[scope.row.panels.length - 1];
  97. $('body').on('mousemove', moveHandler);
  98. $('body').on('mouseup', dragEndHandler);
  99. }
  100. function moveHandler(e) {
  101. scope.row.height = originalHeight + (e.pageY - handleOffset.top);
  102. scope.panel.span = originalWidth + (((e.pageX - handleOffset.left) / maxWidth) * 12);
  103. scope.panel.span = Math.min(Math.max(scope.panel.span, 1), 12);
  104. var rowSpan = scope.dashboard.rowSpan(scope.row);
  105. // auto adjust other panels
  106. if (Math.floor(rowSpan) < 14) {
  107. // last panel should not push row down
  108. if (lastPanel === scope.panel && rowSpan > 12) {
  109. lastPanel.span -= rowSpan - 12;
  110. }
  111. // reduce width of last panel so total in row is 12
  112. else if (lastPanel !== scope.panel) {
  113. lastPanel.span = lastPanel.span - (rowSpan - 12);
  114. lastPanel.span = Math.min(Math.max(lastPanel.span, 1), 12);
  115. }
  116. }
  117. scope.$apply(function() {
  118. scope.$broadcast('render');
  119. });
  120. }
  121. function dragEndHandler() {
  122. // if close to 12
  123. var rowSpan = scope.dashboard.rowSpan(scope.row);
  124. if (rowSpan < 12 && rowSpan > 11) {
  125. lastPanel.span += 12 - rowSpan;
  126. }
  127. scope.$apply(function() {
  128. $rootScope.$broadcast('render');
  129. });
  130. $('body').off('mousemove', moveHandler);
  131. $('body').off('mouseup', dragEndHandler);
  132. }
  133. elem.on('mousedown', dragStartHandler);
  134. scope.$on("$destroy", function() {
  135. elem.off('mousedown', dragStartHandler);
  136. });
  137. }
  138. };
  139. });
  140. });