rowCtrl.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.settingsHover = function(row) {
  29. // Shows/hides the settings button on hover
  30. return row.hoverSettings = ! row.hoverSettings;
  31. };
  32. $scope.addPanel = function(panel) {
  33. $scope.dashboard.addPanel(panel, $scope.row);
  34. };
  35. $scope.deleteRow = function() {
  36. function delete_row() {
  37. $scope.dashboard.rows = _.without($scope.dashboard.rows, $scope.row);
  38. }
  39. if (!$scope.row.panels.length) {
  40. delete_row();
  41. return;
  42. }
  43. $scope.appEvent('confirm-modal', {
  44. title: 'Delete',
  45. text: '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 = _.find(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('panelDropZone', function() {
  127. return function(scope, element) {
  128. scope.$on("ANGULAR_DRAG_START", function() {
  129. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  130. if (dropZoneSpan > 0) {
  131. element.find('.panel-container').css('height', scope.row.height);
  132. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  133. element.show();
  134. }
  135. });
  136. scope.$on("ANGULAR_DRAG_END", function() {
  137. element.hide();
  138. });
  139. };
  140. });
  141. });