rowCtrl.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.editRow = function() {
  54. $scope.appEvent('show-dash-editor', {
  55. src: 'public/app/partials/roweditor.html',
  56. scope: $scope.$new()
  57. });
  58. };
  59. $scope.moveRow = function(direction) {
  60. var rowsList = $scope.dashboard.rows;
  61. var rowIndex = _.indexOf(rowsList, $scope.row);
  62. var newIndex = rowIndex;
  63. switch(direction) {
  64. case 'up': {
  65. newIndex = rowIndex - 1;
  66. break;
  67. }
  68. case 'down': {
  69. newIndex = rowIndex + 1;
  70. break;
  71. }
  72. case 'top': {
  73. newIndex = 0;
  74. break;
  75. }
  76. case 'bottom': {
  77. newIndex = rowsList.length - 1;
  78. break;
  79. }
  80. default: {
  81. newIndex = rowIndex;
  82. }
  83. }
  84. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  85. _.move(rowsList, rowIndex, newIndex);
  86. }
  87. };
  88. $scope.addPanelDefault = function(type) {
  89. var defaultSpan = 12;
  90. var _as = 12 - $scope.dashboard.rowSpan($scope.row);
  91. var panel = {
  92. title: config.new_panel_title,
  93. error: false,
  94. span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  95. editable: true,
  96. type: type,
  97. isNew: true,
  98. };
  99. $scope.addPanel(panel);
  100. $timeout(function() {
  101. $scope.dashboardViewState.update({fullscreen: true, edit: true, panelId: panel.id });
  102. });
  103. };
  104. $scope.setHeight = function(height) {
  105. $scope.row.height = height;
  106. $scope.$broadcast('render');
  107. };
  108. $scope.init();
  109. });
  110. module.directive('rowHeight', function() {
  111. return function(scope, element) {
  112. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  113. element.css({ minHeight: scope.row.collapse ? '5px' : scope.row.height });
  114. });
  115. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  116. var hasPanel = _.findWhere(scope.row.panels, {id: info.panelId});
  117. if (!hasPanel) {
  118. element.hide();
  119. }
  120. });
  121. scope.onAppEvent('panel-fullscreen-exit', function() {
  122. element.show();
  123. });
  124. };
  125. });
  126. module.directive('panelWidth', function() {
  127. return function(scope, element) {
  128. function updateWidth() {
  129. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  130. }
  131. scope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  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. if (scope.panel.id !== info.panelId) {
  140. element.show();
  141. } else {
  142. updateWidth();
  143. }
  144. });
  145. scope.$watch('panel.span', updateWidth);
  146. };
  147. });
  148. module.directive('panelDropZone', function() {
  149. return function(scope, element) {
  150. scope.$on("ANGULAR_DRAG_START", function() {
  151. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  152. if (dropZoneSpan > 0) {
  153. element.find('.panel-container').css('height', scope.row.height);
  154. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  155. element.show();
  156. }
  157. });
  158. scope.$on("ANGULAR_DRAG_END", function() {
  159. element.hide();
  160. });
  161. };
  162. });
  163. });