rowCtrl.js 4.7 KB

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