row.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.duplicatePanel = function(panel, row) {
  66. $scope.dashboard.duplicatePanel(panel, row || $scope.row);
  67. };
  68. $scope.reset_panel = function(type) {
  69. var defaultSpan = 12;
  70. var _as = 12 - $scope.dashboard.rowSpan($scope.row);
  71. $scope.panel = {
  72. error : false,
  73. span : _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  74. editable: true,
  75. type : type
  76. };
  77. function fixRowHeight(height) {
  78. if (!height) {
  79. return '200px';
  80. }
  81. if (!_.isString(height)) {
  82. return height + 'px';
  83. }
  84. return height;
  85. }
  86. $scope.row.height = fixRowHeight($scope.row.height);
  87. };
  88. $scope.init();
  89. });
  90. module.directive('rowHeight', function() {
  91. return function(scope, element) {
  92. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  93. element[0].style.minHeight = scope.row.collapse ? '5px' : scope.row.height;
  94. });
  95. };
  96. });
  97. module.directive('panelWidth', function() {
  98. return function(scope, element) {
  99. scope.$watch('panel.span', function() {
  100. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  101. });
  102. };
  103. });
  104. module.directive('panelDropZone', function() {
  105. return function(scope, element) {
  106. scope.$watch('dashboard.$$panelDragging', function(newVal) {
  107. if (newVal && scope.dashboard.rowSpan(scope.row) < 10) {
  108. element.show();
  109. }
  110. else {
  111. element.hide();
  112. }
  113. });
  114. };
  115. });
  116. });