panel_directive.js 5.6 KB

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