rowCtrl.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'app/core/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. isNew: true,
  85. };
  86. $scope.addPanel(panel);
  87. $timeout(function() {
  88. $scope.dashboardViewState.update({fullscreen: true, edit: true, panelId: panel.id });
  89. });
  90. };
  91. $scope.setHeight = function(height) {
  92. $scope.row.height = height;
  93. $scope.$broadcast('render');
  94. };
  95. $scope.removePanel = function(panel) {
  96. $scope.appEvent('confirm-modal', {
  97. title: 'Are you sure you want to remove this panel?',
  98. icon: 'fa-trash',
  99. yesText: 'Delete',
  100. onConfirm: function() {
  101. $scope.row.panels = _.without($scope.row.panels, panel);
  102. }
  103. });
  104. };
  105. $scope.updatePanelSpan = function(panel, span) {
  106. panel.span = Math.min(Math.max(Math.floor(panel.span + span), 1), 12);
  107. };
  108. $scope.replacePanel = function(newPanel, oldPanel) {
  109. var row = $scope.row;
  110. var index = _.indexOf(row.panels, oldPanel);
  111. row.panels.splice(index, 1);
  112. // adding it back needs to be done in next digest
  113. $timeout(function() {
  114. newPanel.id = oldPanel.id;
  115. newPanel.span = oldPanel.span;
  116. row.panels.splice(index, 0, newPanel);
  117. });
  118. };
  119. $scope.init();
  120. });
  121. module.directive('rowHeight', function() {
  122. return function(scope, element) {
  123. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  124. element.css({ minHeight: scope.row.collapse ? '5px' : scope.row.height });
  125. });
  126. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  127. var hasPanel = _.findWhere(scope.row.panels, {id: info.panelId});
  128. if (!hasPanel) {
  129. element.hide();
  130. }
  131. });
  132. scope.onAppEvent('panel-fullscreen-exit', function() {
  133. element.show();
  134. });
  135. };
  136. });
  137. module.directive('panelWidth', function() {
  138. return function(scope, element) {
  139. function updateWidth() {
  140. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  141. }
  142. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  143. if (scope.panel.id !== info.panelId) {
  144. element.hide();
  145. } else {
  146. element[0].style.width = '100%';
  147. }
  148. });
  149. scope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  150. if (scope.panel.id !== info.panelId) {
  151. element.show();
  152. } else {
  153. updateWidth();
  154. }
  155. });
  156. scope.$watch('panel.span', updateWidth);
  157. };
  158. });
  159. module.directive('panelDropZone', function() {
  160. return function(scope, element) {
  161. scope.$on("ANGULAR_DRAG_START", function() {
  162. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  163. if (dropZoneSpan > 0) {
  164. element.find('.panel-container').css('height', scope.row.height);
  165. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  166. element.show();
  167. }
  168. });
  169. scope.$on("ANGULAR_DRAG_END", function() {
  170. element.hide();
  171. });
  172. };
  173. });
  174. });