row.js 3.8 KB

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