row.js 3.9 KB

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