panel_directive.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import $ from 'jquery';
  4. import {PanelCtrl} from './panel_ctrl';
  5. export class DefaultPanelCtrl extends PanelCtrl {
  6. /** @ngInject */
  7. constructor($scope, $injector) {
  8. super($scope, $injector);
  9. }
  10. }
  11. export class PanelDirective {
  12. template: string;
  13. templateUrl: string;
  14. bindToController: boolean;
  15. scope: any;
  16. controller: any;
  17. controllerAs: string;
  18. getDirective() {
  19. if (!this.controller) {
  20. this.controller = DefaultPanelCtrl;
  21. }
  22. return {
  23. template: this.template,
  24. templateUrl: this.templateUrl,
  25. controller: this.controller,
  26. controllerAs: 'ctrl',
  27. bindToController: true,
  28. scope: {dashboard: "=", panel: "=", row: "="},
  29. link: (scope, elem, attrs, ctrl) => {
  30. ctrl.init();
  31. this.link(scope, elem, attrs, ctrl);
  32. }
  33. };
  34. }
  35. link(scope, elem, attrs, ctrl) {
  36. return null;
  37. }
  38. }
  39. var module = angular.module('grafana.directives');
  40. module.directive('grafanaPanel', function() {
  41. return {
  42. restrict: 'E',
  43. templateUrl: 'public/app/features/panel/partials/panel.html',
  44. transclude: true,
  45. scope: { ctrl: "=" },
  46. link: function(scope, elem) {
  47. var panelContainer = elem.find('.panel-container');
  48. var ctrl = scope.ctrl;
  49. scope.$watchGroup(['ctrl.fullscreen', 'ctrl.height', 'ctrl.panel.height', 'ctrl.row.height'], function() {
  50. panelContainer.css({ minHeight: ctrl.height || ctrl.panel.height || ctrl.row.height, display: 'block' });
  51. elem.toggleClass('panel-fullscreen', ctrl.fullscreen ? true : false);
  52. });
  53. }
  54. };
  55. });
  56. module.directive('panelResizer', function($rootScope) {
  57. return {
  58. restrict: 'E',
  59. template: '<span class="resize-panel-handle"></span>',
  60. link: function(scope, elem) {
  61. var resizing = false;
  62. var lastPanel;
  63. var ctrl = scope.ctrl;
  64. var handleOffset;
  65. var originalHeight;
  66. var originalWidth;
  67. var maxWidth;
  68. function dragStartHandler(e) {
  69. e.preventDefault();
  70. resizing = true;
  71. handleOffset = $(e.target).offset();
  72. originalHeight = parseInt(ctrl.row.height);
  73. originalWidth = ctrl.panel.span;
  74. maxWidth = $(document).width();
  75. lastPanel = ctrl.row.panels[ctrl.row.panels.length - 1];
  76. $('body').on('mousemove', moveHandler);
  77. $('body').on('mouseup', dragEndHandler);
  78. }
  79. function moveHandler(e) {
  80. ctrl.row.height = originalHeight + (e.pageY - handleOffset.top);
  81. ctrl.panel.span = originalWidth + (((e.pageX - handleOffset.left) / maxWidth) * 12);
  82. ctrl.panel.span = Math.min(Math.max(ctrl.panel.span, 1), 12);
  83. var rowSpan = ctrl.dashboard.rowSpan(ctrl.row);
  84. // auto adjust other panels
  85. if (Math.floor(rowSpan) < 14) {
  86. // last panel should not push row down
  87. if (lastPanel === ctrl.panel && rowSpan > 12) {
  88. lastPanel.span -= rowSpan - 12;
  89. } else if (lastPanel !== ctrl.panel) {
  90. // reduce width of last panel so total in row is 12
  91. lastPanel.span = lastPanel.span - (rowSpan - 12);
  92. lastPanel.span = Math.min(Math.max(lastPanel.span, 1), 12);
  93. }
  94. }
  95. scope.$apply(function() {
  96. scope.$broadcast('render');
  97. });
  98. }
  99. function dragEndHandler() {
  100. // if close to 12
  101. var rowSpan = ctrl.dashboard.rowSpan(ctrl.row);
  102. if (rowSpan < 12 && rowSpan > 11) {
  103. lastPanel.span += 12 - rowSpan;
  104. }
  105. scope.$apply(function() {
  106. $rootScope.$broadcast('render');
  107. });
  108. $('body').off('mousemove', moveHandler);
  109. $('body').off('mouseup', dragEndHandler);
  110. }
  111. elem.on('mousedown', dragStartHandler);
  112. scope.$on("$destroy", function() {
  113. elem.off('mousedown', dragStartHandler);
  114. });
  115. }
  116. };
  117. });