panel_directive.js 5.8 KB

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