rowCtrl.js 5.3 KB

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