rowCtrl.js 4.4 KB

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