rowCtrl.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.init();
  103. });
  104. module.directive('rowHeight', function() {
  105. return function(scope, element) {
  106. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  107. element.css({ minHeight: scope.row.collapse ? '5px' : scope.row.height });
  108. });
  109. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  110. var hasPanel = _.findWhere(scope.row.panels, {id: info.panelId});
  111. if (!hasPanel) {
  112. element.hide();
  113. }
  114. });
  115. scope.onAppEvent('panel-fullscreen-exit', function() {
  116. element.show();
  117. });
  118. };
  119. });
  120. module.directive('panelWidth', function() {
  121. return function(scope, element) {
  122. function updateWidth() {
  123. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  124. }
  125. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  126. if (scope.panel.id !== info.panelId) {
  127. element.hide();
  128. } else {
  129. element[0].style.width = '100%';
  130. }
  131. });
  132. scope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  133. if (scope.panel.id !== info.panelId) {
  134. element.show();
  135. } else {
  136. updateWidth();
  137. }
  138. });
  139. scope.$watch('panel.span', updateWidth);
  140. };
  141. });
  142. module.directive('panelDropZone', function() {
  143. return function(scope, element) {
  144. scope.$on("ANGULAR_DRAG_START", function() {
  145. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  146. if (dropZoneSpan > 0) {
  147. element.find('.panel-container').css('height', scope.row.height);
  148. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  149. element.show();
  150. }
  151. });
  152. scope.$on("ANGULAR_DRAG_END", function() {
  153. element.hide();
  154. });
  155. };
  156. });
  157. });