rowCtrl.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. define([
  2. 'angular',
  3. 'app',
  4. 'lodash',
  5. 'config'
  6. ],
  7. function (angular, app, _, config) {
  8. 'use strict';
  9. var module = angular.module('grafana.controllers');
  10. module.controller('RowCtrl', function($scope, $rootScope, $timeout) {
  11. var _d = {
  12. title: "Row",
  13. height: "150px",
  14. collapse: false,
  15. editable: true,
  16. panels: [],
  17. };
  18. _.defaults($scope.row,_d);
  19. $scope.init = function() {
  20. $scope.reset_panel();
  21. };
  22. $scope.togglePanelMenu = function(posX) {
  23. $scope.showPanelMenu = !$scope.showPanelMenu;
  24. $scope.panelMenuPos = posX;
  25. };
  26. $scope.toggle_row = function(row) {
  27. row.collapse = row.collapse ? false : true;
  28. if (!row.collapse) {
  29. $timeout(function() {
  30. $scope.$broadcast('render');
  31. });
  32. }
  33. };
  34. // This can be overridden by individual panels
  35. $scope.close_edit = function() {
  36. $scope.$broadcast('render');
  37. };
  38. $scope.add_panel = function(panel) {
  39. $scope.dashboard.add_panel(panel, $scope.row);
  40. };
  41. $scope.delete_row = function() {
  42. $scope.appEvent('confirm-modal', {
  43. title: 'Are you sure you want to delete this row?',
  44. icon: 'fa-trash',
  45. yesText: 'delete',
  46. onConfirm: function() {
  47. $scope.dashboard.rows = _.without($scope.dashboard.rows, $scope.row);
  48. }
  49. });
  50. };
  51. $scope.move_row = function(direction) {
  52. var rowsList = $scope.dashboard.rows;
  53. var rowIndex = _.indexOf(rowsList, $scope.row);
  54. var newIndex = rowIndex + direction;
  55. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  56. _.move(rowsList, rowIndex, rowIndex + direction);
  57. }
  58. };
  59. $scope.add_panel_default = function(type) {
  60. $scope.reset_panel(type);
  61. $scope.add_panel($scope.panel);
  62. $timeout(function() {
  63. $scope.$broadcast('render');
  64. });
  65. };
  66. $scope.set_height = function(height) {
  67. $scope.row.height = height;
  68. $scope.$broadcast('render');
  69. };
  70. $scope.remove_panel_from_row = function(row, panel) {
  71. $scope.appEvent('confirm-modal', {
  72. title: 'Are you sure you want to remove this panel?',
  73. icon: 'fa-trash',
  74. yesText: 'Delete',
  75. onConfirm: function() {
  76. row.panels = _.without(row.panels, panel);
  77. }
  78. });
  79. };
  80. $scope.replacePanel = function(newPanel, oldPanel) {
  81. var row = $scope.row;
  82. var index = _.indexOf(row.panels, oldPanel);
  83. row.panels.splice(index, 1);
  84. // adding it back needs to be done in next digest
  85. $timeout(function() {
  86. newPanel.id = oldPanel.id;
  87. newPanel.span = oldPanel.span;
  88. row.panels.splice(index, 0, newPanel);
  89. });
  90. };
  91. $scope.reset_panel = function(type) {
  92. var defaultSpan = 12;
  93. var _as = 12 - $scope.dashboard.rowSpan($scope.row);
  94. $scope.panel = {
  95. title: config.new_panel_title,
  96. error: false,
  97. span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  98. editable: true,
  99. type: type
  100. };
  101. function fixRowHeight(height) {
  102. if (!height) {
  103. return '200px';
  104. }
  105. if (!_.isString(height)) {
  106. return height + 'px';
  107. }
  108. return height;
  109. }
  110. $scope.row.height = fixRowHeight($scope.row.height);
  111. };
  112. $scope.init();
  113. });
  114. module.directive('rowHeight', function() {
  115. return function(scope, element) {
  116. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  117. element[0].style.minHeight = scope.row.collapse ? '5px' : scope.row.height;
  118. });
  119. };
  120. });
  121. module.directive('panelWidth', function() {
  122. return function(scope, element) {
  123. scope.$watch('panel.span', function() {
  124. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  125. });
  126. };
  127. });
  128. module.directive('panelDropZone', function() {
  129. return function(scope, element) {
  130. scope.$on("ANGULAR_DRAG_START", function() {
  131. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  132. if (dropZoneSpan > 0) {
  133. element.find('.panel-container').css('height', scope.row.height);
  134. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  135. element.show();
  136. }
  137. });
  138. scope.$on("ANGULAR_DRAG_END", function() {
  139. element.hide();
  140. });
  141. };
  142. });
  143. });