rowCtrl.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config'
  5. ],
  6. function (angular, _, config) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('RowCtrl', function($scope, $rootScope, $timeout) {
  10. var _d = {
  11. title: "Row",
  12. height: "150px",
  13. collapse: false,
  14. editable: true,
  15. panels: [],
  16. };
  17. _.defaults($scope.row,_d);
  18. $scope.init = function() {
  19. $scope.editor = {index: 0};
  20. };
  21. $scope.togglePanelMenu = function(posX) {
  22. $scope.showPanelMenu = !$scope.showPanelMenu;
  23. $scope.panelMenuPos = posX;
  24. };
  25. $scope.toggle_row = function(row) {
  26. row.collapse = row.collapse ? false : true;
  27. if (!row.collapse) {
  28. $timeout(function() {
  29. $scope.$broadcast('render');
  30. });
  31. }
  32. };
  33. $scope.add_panel = function(panel) {
  34. $scope.dashboard.add_panel(panel, $scope.row);
  35. };
  36. $scope.delete_row = function() {
  37. $scope.appEvent('confirm-modal', {
  38. title: 'Are you sure you want to delete this row?',
  39. icon: 'fa-trash',
  40. yesText: 'Delete',
  41. onConfirm: function() {
  42. $scope.dashboard.rows = _.without($scope.dashboard.rows, $scope.row);
  43. }
  44. });
  45. };
  46. $scope.move_row = function(direction) {
  47. var rowsList = $scope.dashboard.rows;
  48. var rowIndex = _.indexOf(rowsList, $scope.row);
  49. var newIndex = rowIndex + direction;
  50. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  51. _.move(rowsList, rowIndex, rowIndex + direction);
  52. }
  53. };
  54. $scope.add_panel_default = function(type) {
  55. var defaultSpan = 12;
  56. var _as = 12 - $scope.dashboard.rowSpan($scope.row);
  57. var panel = {
  58. title: config.new_panel_title,
  59. error: false,
  60. span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  61. editable: true,
  62. type: type
  63. };
  64. $scope.add_panel(panel);
  65. $timeout(function() {
  66. $scope.$broadcast('render');
  67. });
  68. };
  69. $scope.set_height = function(height) {
  70. $scope.row.height = height;
  71. $scope.$broadcast('render');
  72. };
  73. $scope.removePanel = function(panel) {
  74. $scope.appEvent('confirm-modal', {
  75. title: 'Are you sure you want to remove this panel?',
  76. icon: 'fa-trash',
  77. yesText: 'Delete',
  78. onConfirm: function() {
  79. $scope.row.panels = _.without($scope.row.panels, panel);
  80. }
  81. });
  82. };
  83. $scope.updatePanelSpan = function(panel, span) {
  84. panel.span = Math.min(Math.max(Math.floor(panel.span + span), 1), 12);
  85. };
  86. $scope.replacePanel = function(newPanel, oldPanel) {
  87. var row = $scope.row;
  88. var index = _.indexOf(row.panels, oldPanel);
  89. row.panels.splice(index, 1);
  90. // adding it back needs to be done in next digest
  91. $timeout(function() {
  92. newPanel.id = oldPanel.id;
  93. newPanel.span = oldPanel.span;
  94. row.panels.splice(index, 0, newPanel);
  95. });
  96. };
  97. $scope.init();
  98. });
  99. module.directive('rowHeight', function() {
  100. return function(scope, element) {
  101. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  102. element.css({ minHeight: scope.row.collapse ? '5px' : scope.row.height });
  103. });
  104. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  105. var hasPanel = _.findWhere(scope.row.panels, {id: info.panelId});
  106. if (!hasPanel) {
  107. element.hide();
  108. }
  109. });
  110. scope.onAppEvent('panel-fullscreen-exit', function() {
  111. element.show();
  112. });
  113. };
  114. });
  115. module.directive('panelWidth', function() {
  116. return function(scope, element) {
  117. function updateWidth() {
  118. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  119. }
  120. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  121. if (scope.panel.id !== info.panelId) {
  122. element.hide();
  123. } else {
  124. element[0].style.width = '100%';
  125. }
  126. });
  127. scope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  128. if (scope.panel.id !== info.panelId) {
  129. element.show();
  130. } else {
  131. updateWidth();
  132. }
  133. });
  134. scope.$watch('panel.span', updateWidth);
  135. };
  136. });
  137. module.directive('panelDropZone', function() {
  138. return function(scope, element) {
  139. scope.$on("ANGULAR_DRAG_START", function() {
  140. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  141. if (dropZoneSpan > 0) {
  142. element.find('.panel-container').css('height', scope.row.height);
  143. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  144. element.show();
  145. }
  146. });
  147. scope.$on("ANGULAR_DRAG_END", function() {
  148. element.hide();
  149. });
  150. };
  151. });
  152. });