rowCtrl.js 4.0 KB

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