panel_directive.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. define([
  2. 'angular',
  3. 'jquery',
  4. ],
  5. function (angular, $) {
  6. 'use strict';
  7. var module = angular.module('grafana.directives');
  8. module.directive('grafanaPanel', function() {
  9. return {
  10. restrict: 'E',
  11. templateUrl: 'app/features/panel/partials/panel.html',
  12. transclude: true,
  13. link: function(scope, elem) {
  14. var panelContainer = elem.find('.panel-container');
  15. scope.$watchGroup(['fullscreen', 'height', 'panel.height', 'row.height'], function() {
  16. panelContainer.css({ minHeight: scope.height || scope.panel.height || scope.row.height, display: 'block' });
  17. elem.toggleClass('panel-fullscreen', scope.fullscreen ? true : false);
  18. });
  19. }
  20. };
  21. });
  22. module.directive('queryEditorLoader', function($compile, $parse, datasourceSrv) {
  23. return {
  24. restrict: 'E',
  25. link: function(scope, elem) {
  26. var editorScope;
  27. scope.$watch("panel.datasource", function() {
  28. var datasource = scope.target.datasource || scope.panel.datasource;
  29. datasourceSrv.get(datasource).then(function(ds) {
  30. if (editorScope) {
  31. editorScope.$destroy();
  32. elem.empty();
  33. }
  34. editorScope = scope.$new();
  35. editorScope.datasource = ds;
  36. if (!scope.target.refId) {
  37. scope.target.refId = 'A';
  38. }
  39. var panelEl = angular.element(document.createElement('metric-query-editor-' + ds.meta.id));
  40. elem.append(panelEl);
  41. $compile(panelEl)(editorScope);
  42. });
  43. });
  44. }
  45. };
  46. });
  47. module.directive('panelResizer', function($rootScope) {
  48. return {
  49. restrict: 'E',
  50. template: '<span class="resize-panel-handle"></span>',
  51. link: function(scope, elem) {
  52. var resizing = false;
  53. var lastPanel = false;
  54. var handleOffset;
  55. var originalHeight;
  56. var originalWidth;
  57. var maxWidth;
  58. function dragStartHandler(e) {
  59. e.preventDefault();
  60. resizing = true;
  61. handleOffset = $(e.target).offset();
  62. originalHeight = parseInt(scope.row.height);
  63. originalWidth = scope.panel.span;
  64. maxWidth = $(document).width();
  65. lastPanel = scope.row.panels[scope.row.panels.length - 1];
  66. $('body').on('mousemove', moveHandler);
  67. $('body').on('mouseup', dragEndHandler);
  68. }
  69. function moveHandler(e) {
  70. scope.row.height = originalHeight + (e.pageY - handleOffset.top);
  71. scope.panel.span = originalWidth + (((e.pageX - handleOffset.left) / maxWidth) * 12);
  72. scope.panel.span = Math.min(Math.max(scope.panel.span, 1), 12);
  73. var rowSpan = scope.dashboard.rowSpan(scope.row);
  74. // auto adjust other panels
  75. if (Math.floor(rowSpan) < 14) {
  76. // last panel should not push row down
  77. if (lastPanel === scope.panel && rowSpan > 12) {
  78. lastPanel.span -= rowSpan - 12;
  79. }
  80. // reduce width of last panel so total in row is 12
  81. else if (lastPanel !== scope.panel) {
  82. lastPanel.span = lastPanel.span - (rowSpan - 12);
  83. lastPanel.span = Math.min(Math.max(lastPanel.span, 1), 12);
  84. }
  85. }
  86. scope.$apply(function() {
  87. scope.$broadcast('render');
  88. });
  89. }
  90. function dragEndHandler() {
  91. // if close to 12
  92. var rowSpan = scope.dashboard.rowSpan(scope.row);
  93. if (rowSpan < 12 && rowSpan > 11) {
  94. lastPanel.span += 12 - rowSpan;
  95. }
  96. scope.$apply(function() {
  97. $rootScope.$broadcast('render');
  98. });
  99. $('body').off('mousemove', moveHandler);
  100. $('body').off('mouseup', dragEndHandler);
  101. }
  102. elem.on('mousedown', dragStartHandler);
  103. scope.$on("$destroy", function() {
  104. elem.off('mousedown', dragStartHandler);
  105. });
  106. }
  107. };
  108. });
  109. });