rowCtrl.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '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.toggle_row = function(row) {
  26. row.collapse = row.collapse ? false : true;
  27. if (!row.collapse) {
  28. $timeout(function() {
  29. $scope.$broadcast('render');
  30. });
  31. }
  32. };
  33. $scope.add_panel = function(panel) {
  34. $scope.dashboard.add_panel(panel, $scope.row);
  35. };
  36. $scope.delete_row = function() {
  37. $scope.appEvent('confirm-modal', {
  38. title: 'Are you sure you want to delete this row?',
  39. icon: 'fa-trash',
  40. yesText: 'Delete',
  41. onConfirm: function() {
  42. $scope.dashboard.rows = _.without($scope.dashboard.rows, $scope.row);
  43. }
  44. });
  45. };
  46. $scope.move_row = function(direction) {
  47. var rowsList = $scope.dashboard.rows;
  48. var rowIndex = _.indexOf(rowsList, $scope.row);
  49. var newIndex = rowIndex + direction;
  50. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  51. _.move(rowsList, rowIndex, rowIndex + direction);
  52. }
  53. };
  54. $scope.add_panel_default = function(type) {
  55. var defaultSpan = 12;
  56. var _as = 12 - $scope.dashboard.rowSpan($scope.row);
  57. var panel = {
  58. title: config.new_panel_title,
  59. error: false,
  60. span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  61. editable: true,
  62. type: type
  63. };
  64. $scope.add_panel(panel);
  65. $timeout(function() {
  66. $scope.$broadcast('render');
  67. });
  68. };
  69. $scope.set_height = function(height) {
  70. $scope.row.height = height;
  71. $scope.$broadcast('render');
  72. };
  73. $scope.removePanel = function(panel) {
  74. $scope.appEvent('confirm-modal', {
  75. title: 'Are you sure you want to remove this panel?',
  76. icon: 'fa-trash',
  77. yesText: 'Delete',
  78. onConfirm: function() {
  79. $scope.row.panels = _.without($scope.row.panels, panel);
  80. }
  81. });
  82. };
  83. $scope.updatePanelSpan = function(panel, span) {
  84. panel.span = Math.min(Math.max(Math.floor(panel.span + span), 1), 12);
  85. };
  86. $scope.replacePanel = function(newPanel, oldPanel) {
  87. var row = $scope.row;
  88. var index = _.indexOf(row.panels, oldPanel);
  89. row.panels.splice(index, 1);
  90. // adding it back needs to be done in next digest
  91. $timeout(function() {
  92. newPanel.id = oldPanel.id;
  93. newPanel.span = oldPanel.span;
  94. row.panels.splice(index, 0, newPanel);
  95. });
  96. };
  97. $scope.init();
  98. });
  99. module.directive('rowHeight', function() {
  100. return function(scope, element) {
  101. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  102. element.css({ minHeight: scope.row.collapse ? '5px' : scope.row.height });
  103. });
  104. };
  105. });
  106. module.directive('panelWidth', function() {
  107. return function(scope, element) {
  108. function updateWidth() {
  109. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  110. }
  111. scope.$watch('panel.span', updateWidth);
  112. };
  113. });
  114. module.directive('panelDropZone', function() {
  115. return function(scope, element) {
  116. scope.$on("ANGULAR_DRAG_START", function() {
  117. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  118. if (dropZoneSpan > 0) {
  119. element.find('.panel-container').css('height', scope.row.height);
  120. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  121. element.show();
  122. }
  123. });
  124. scope.$on("ANGULAR_DRAG_END", function() {
  125. element.hide();
  126. });
  127. };
  128. });
  129. });