rowCtrl.js 4.7 KB

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