rowCtrl.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.reset_panel();
  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. $scope.reset_panel(type);
  57. $scope.add_panel($scope.panel);
  58. $timeout(function() {
  59. $scope.$broadcast('render');
  60. });
  61. };
  62. $scope.set_height = function(height) {
  63. $scope.row.height = height;
  64. $scope.$broadcast('render');
  65. };
  66. $scope.removePanel = function(panel) {
  67. $scope.appEvent('confirm-modal', {
  68. title: 'Are you sure you want to remove this panel?',
  69. icon: 'fa-trash',
  70. yesText: 'Delete',
  71. onConfirm: function() {
  72. $scope.row.panels = _.without($scope.row.panels, panel);
  73. }
  74. });
  75. };
  76. $scope.updatePanelSpan = function(panel, span) {
  77. panel.span = Math.min(Math.max(panel.span + span, 1), 12);
  78. };
  79. $scope.replacePanel = function(newPanel, oldPanel) {
  80. var row = $scope.row;
  81. var index = _.indexOf(row.panels, oldPanel);
  82. row.panels.splice(index, 1);
  83. // adding it back needs to be done in next digest
  84. $timeout(function() {
  85. newPanel.id = oldPanel.id;
  86. newPanel.span = oldPanel.span;
  87. row.panels.splice(index, 0, newPanel);
  88. });
  89. };
  90. $scope.reset_panel = function(type) {
  91. var defaultSpan = 12;
  92. var _as = 12 - $scope.dashboard.rowSpan($scope.row);
  93. $scope.panel = {
  94. title: config.new_panel_title,
  95. error: false,
  96. span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  97. editable: true,
  98. type: type
  99. };
  100. function fixRowHeight(height) {
  101. if (!height) {
  102. return '200px';
  103. }
  104. if (!_.isString(height)) {
  105. return height + 'px';
  106. }
  107. return height;
  108. }
  109. $scope.row.height = fixRowHeight($scope.row.height);
  110. };
  111. $scope.init();
  112. });
  113. module.directive('rowHeight', function() {
  114. return function(scope, element) {
  115. scope.$watchGroup(['row.collapse', 'row.height'], function() {
  116. element[0].style.minHeight = scope.row.collapse ? '5px' : scope.row.height;
  117. });
  118. };
  119. });
  120. module.directive('panelWidth', function() {
  121. return function(scope, element) {
  122. function updateWidth() {
  123. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  124. }
  125. scope.$watch('panel.span', updateWidth);
  126. };
  127. });
  128. module.directive('panelDropZone', function() {
  129. return function(scope, element) {
  130. scope.$on("ANGULAR_DRAG_START", function() {
  131. var dropZoneSpan = 12 - scope.dashboard.rowSpan(scope.row);
  132. if (dropZoneSpan > 0) {
  133. element.find('.panel-container').css('height', scope.row.height);
  134. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  135. element.show();
  136. }
  137. });
  138. scope.$on("ANGULAR_DRAG_END", function() {
  139. element.hide();
  140. });
  141. };
  142. });
  143. });