rowCtrl.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.toggleRow = 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.addPanel = function(panel) {
  34. $scope.dashboard.addPanel(panel, $scope.row);
  35. };
  36. $scope.deleteRow = 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.moveRow = function(direction) {
  47. var rowsList = $scope.dashboard.rows;
  48. var rowIndex = _.indexOf(rowsList, $scope.row);
  49. var newIndex = rowIndex;
  50. switch(direction) {
  51. case 'up': {
  52. newIndex = rowIndex - 1;
  53. break;
  54. }
  55. case 'down': {
  56. newIndex = rowIndex + 1;
  57. break;
  58. }
  59. case 'top': {
  60. newIndex = 0;
  61. break;
  62. }
  63. case 'bottom': {
  64. newIndex = rowsList.length - 1;
  65. break;
  66. }
  67. default: {
  68. newIndex = rowIndex;
  69. }
  70. }
  71. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  72. _.move(rowsList, rowIndex, newIndex);
  73. }
  74. };
  75. $scope.addPanelDefault = function(type) {
  76. var defaultSpan = 12;
  77. var _as = 12 - $scope.dashboard.rowSpan($scope.row);
  78. var panel = {
  79. title: config.new_panel_title,
  80. error: false,
  81. span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  82. editable: true,
  83. type: type
  84. };
  85. $scope.addPanel(panel);
  86. $timeout(function() {
  87. $scope.$broadcast('render');
  88. });
  89. };
  90. $scope.setHeight = function(height) {
  91. $scope.row.height = height;
  92. $scope.$broadcast('render');
  93. };
  94. $scope.removePanel = function(panel) {
  95. $scope.appEvent('confirm-modal', {
  96. title: 'Are you sure you want to remove this panel?',
  97. icon: 'fa-trash',
  98. yesText: 'Delete',
  99. onConfirm: function() {
  100. $scope.row.panels = _.without($scope.row.panels, panel);
  101. }
  102. });
  103. };
  104. $scope.updatePanelSpan = function(panel, span) {
  105. panel.span = Math.min(Math.max(Math.floor(panel.span + span), 1), 12);
  106. };
  107. $scope.replacePanel = function(newPanel, oldPanel) {
  108. var row = $scope.row;
  109. var index = _.indexOf(row.panels, oldPanel);
  110. row.panels.splice(index, 1);
  111. // adding it back needs to be done in next digest
  112. $timeout(function() {
  113. newPanel.id = oldPanel.id;
  114. newPanel.span = oldPanel.span;
  115. row.panels.splice(index, 0, newPanel);
  116. });
  117. };
  118. $scope.init();
  119. });
  120. module.directive('rowHeight', function() {
  121. return function(scope, element) {
  122. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  123. element.css({ minHeight: scope.row.collapse ? '5px' : scope.row.height });
  124. });
  125. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  126. var hasPanel = _.findWhere(scope.row.panels, {id: info.panelId});
  127. if (!hasPanel) {
  128. element.hide();
  129. }
  130. });
  131. scope.onAppEvent('panel-fullscreen-exit', function() {
  132. element.show();
  133. });
  134. };
  135. });
  136. module.directive('panelWidth', function() {
  137. return function(scope, element) {
  138. function updateWidth() {
  139. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  140. }
  141. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  142. if (scope.panel.id !== info.panelId) {
  143. element.hide();
  144. } else {
  145. element[0].style.width = '100%';
  146. }
  147. });
  148. scope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  149. if (scope.panel.id !== info.panelId) {
  150. element.show();
  151. } else {
  152. updateWidth();
  153. }
  154. });
  155. scope.$watch('panel.span', updateWidth);
  156. };
  157. });
  158. module.directive('panelDropZone', function() {
  159. return function(scope, element) {
  160. scope.$on("ANGULAR_DRAG_START", function() {
  161. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  162. if (dropZoneSpan > 0) {
  163. element.find('.panel-container').css('height', scope.row.height);
  164. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  165. element.show();
  166. }
  167. });
  168. scope.$on("ANGULAR_DRAG_END", function() {
  169. element.hide();
  170. });
  171. };
  172. });
  173. });