panel_directive.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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('panelResizer', function($rootScope) {
  23. return {
  24. restrict: 'E',
  25. template: '<span class="resize-panel-handle"></span>',
  26. link: function(scope, elem) {
  27. var resizing = false;
  28. var lastPanel = false;
  29. var handleOffset;
  30. var originalHeight;
  31. var originalWidth;
  32. var maxWidth;
  33. function dragStartHandler(e) {
  34. e.preventDefault();
  35. resizing = true;
  36. handleOffset = $(e.target).offset();
  37. originalHeight = parseInt(scope.row.height);
  38. originalWidth = scope.panel.span;
  39. maxWidth = $(document).width();
  40. lastPanel = scope.row.panels[scope.row.panels.length - 1];
  41. $('body').on('mousemove', moveHandler);
  42. $('body').on('mouseup', dragEndHandler);
  43. }
  44. function moveHandler(e) {
  45. scope.row.height = originalHeight + (e.pageY - handleOffset.top);
  46. scope.panel.span = originalWidth + (((e.pageX - handleOffset.left) / maxWidth) * 12);
  47. scope.panel.span = Math.min(Math.max(scope.panel.span, 1), 12);
  48. var rowSpan = scope.dashboard.rowSpan(scope.row);
  49. // auto adjust other panels
  50. if (Math.floor(rowSpan) < 14) {
  51. // last panel should not push row down
  52. if (lastPanel === scope.panel && rowSpan > 12) {
  53. lastPanel.span -= rowSpan - 12;
  54. }
  55. // reduce width of last panel so total in row is 12
  56. else if (lastPanel !== scope.panel) {
  57. lastPanel.span = lastPanel.span - (rowSpan - 12);
  58. lastPanel.span = Math.min(Math.max(lastPanel.span, 1), 12);
  59. }
  60. }
  61. scope.$apply(function() {
  62. scope.$broadcast('render');
  63. });
  64. }
  65. function dragEndHandler() {
  66. // if close to 12
  67. var rowSpan = scope.dashboard.rowSpan(scope.row);
  68. if (rowSpan < 12 && rowSpan > 11) {
  69. lastPanel.span += 12 - rowSpan;
  70. }
  71. scope.$apply(function() {
  72. $rootScope.$broadcast('render');
  73. });
  74. $('body').off('mousemove', moveHandler);
  75. $('body').off('mouseup', dragEndHandler);
  76. }
  77. elem.on('mousedown', dragStartHandler);
  78. scope.$on("$destroy", function() {
  79. elem.off('mousedown', dragStartHandler);
  80. });
  81. }
  82. };
  83. });
  84. });