row.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. define([
  2. 'angular',
  3. 'app',
  4. 'underscore'
  5. ],
  6. function (angular, app, _) {
  7. 'use strict';
  8. var module = angular.module('kibana.controllers');
  9. module.controller('RowCtrl', function($scope, $rootScope, $timeout) {
  10. var _d = {
  11. title: "Row",
  12. height: "150px",
  13. collapse: false,
  14. collapsable: true,
  15. editable: true,
  16. panels: [],
  17. notice: false
  18. };
  19. _.defaults($scope.row,_d);
  20. $scope.init = function() {
  21. $scope.reset_panel();
  22. };
  23. $scope.toggle_row = function(row) {
  24. if(!row.collapsable) {
  25. return;
  26. }
  27. row.collapse = row.collapse ? false : true;
  28. if (!row.collapse) {
  29. $timeout(function() {
  30. $scope.$broadcast('render');
  31. });
  32. } else {
  33. row.notice = false;
  34. }
  35. };
  36. $scope.rowSpan = function(row) {
  37. var panels = _.filter(row.panels, function(p) {
  38. return $scope.isPanel(p);
  39. });
  40. return _.reduce(_.pluck(panels,'span'), function(p,v) {
  41. return p+v;
  42. },0);
  43. };
  44. // This can be overridden by individual panels
  45. $scope.close_edit = function() {
  46. $scope.$broadcast('render');
  47. };
  48. $scope.add_panel = function(panel) {
  49. var rowSpan = $scope.rowSpan($scope.row);
  50. var panelCount = $scope.row.panels.length;
  51. var space = (12 - rowSpan) - panel.span;
  52. // try to make room of there is no space left
  53. if (space <= 0) {
  54. if (panelCount === 1) {
  55. $scope.row.panels[0].span = 6;
  56. panel.span = 6;
  57. }
  58. else if (panelCount === 2) {
  59. $scope.row.panels[0].span = 4;
  60. $scope.row.panels[1].span = 4;
  61. panel.span = 4;
  62. }
  63. }
  64. $scope.row.panels.push(panel);
  65. };
  66. $scope.delete_row = function() {
  67. if (confirm("Are you sure you want to delete this row?")) {
  68. $scope.dashboard.current.rows = _.without($scope.dashboard.current.rows, $scope.row);
  69. }
  70. };
  71. $scope.move_row = function(direction) {
  72. var rowsList = $scope.dashboard.current.rows;
  73. var rowIndex = _.indexOf(rowsList, $scope.row);
  74. var newIndex = rowIndex + direction;
  75. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  76. _.move(rowsList, rowIndex, rowIndex + direction);
  77. }
  78. };
  79. $scope.add_panel_default = function(type) {
  80. $scope.reset_panel(type);
  81. $scope.add_panel($scope.panel);
  82. $timeout(function() {
  83. $scope.$broadcast('render');
  84. });
  85. };
  86. $scope.set_height = function(height) {
  87. $scope.row.height = height;
  88. $scope.$broadcast('render');
  89. };
  90. $scope.remove_panel_from_row = function(row, panel) {
  91. if (confirm('Are you sure you want to remove this ' + panel.type + ' panel?')) {
  92. row.panels = _.without(row.panels,panel);
  93. }
  94. };
  95. $scope.duplicatePanel = function(panel, row) {
  96. row = row || $scope.row;
  97. var currentRowSpan = $scope.rowSpan(row);
  98. if (currentRowSpan <= 9) {
  99. row.panels.push(angular.copy(panel));
  100. }
  101. else {
  102. var rowsList = $scope.dashboard.current.rows;
  103. var rowIndex = _.indexOf(rowsList, row);
  104. if (rowIndex === rowsList.length - 1) {
  105. var newRow = angular.copy($scope.row);
  106. newRow.panels = [];
  107. $scope.dashboard.current.rows.push(newRow);
  108. $scope.duplicatePanel(panel, newRow);
  109. }
  110. else {
  111. $scope.duplicatePanel(panel, rowsList[rowIndex+1]);
  112. }
  113. }
  114. };
  115. /** @scratch /panels/0
  116. * [[panels]]
  117. * = Panels
  118. *
  119. * [partintro]
  120. * --
  121. * *Kibana* dashboards are made up of blocks called +panels+. Panels are organized into rows
  122. * and can serve many purposes, though most are designed to provide the results of a query or
  123. * multiple queries as a visualization. Other panels may show collections of documents or
  124. * allow you to insert instructions for your users.
  125. *
  126. * Panels can be configured easily via the Kibana web interface. For more advanced usage, such
  127. * as templated or scripted dashboards, documentation of panel properties is available in this
  128. * section. You may find settings here which are not exposed via the web interface.
  129. *
  130. * Each panel type has its own properties, hover there are several that are shared.
  131. *
  132. */
  133. $scope.reset_panel = function(type) {
  134. var
  135. defaultSpan = 4,
  136. _as = 12-$scope.rowSpan($scope.row);
  137. $scope.panel = {
  138. error : false,
  139. /** @scratch /panels/1
  140. * span:: A number, 1-12, that describes the width of the panel.
  141. */
  142. span : _as < defaultSpan && _as > 0 ? _as : defaultSpan,
  143. /** @scratch /panels/1
  144. * editable:: Enable or disable the edit button the the panel
  145. */
  146. editable: true,
  147. /** @scratch /panels/1
  148. * type:: The type of panel this object contains. Each panel type will require additional
  149. * properties. See the panel types list to the right.
  150. */
  151. type : type
  152. };
  153. function fixRowHeight(height) {
  154. if (!height) {
  155. return '200px';
  156. }
  157. if (!_.isString(height)) {
  158. return height + 'px';
  159. }
  160. return height;
  161. }
  162. $scope.row.height = fixRowHeight($scope.row.height);
  163. };
  164. /** @scratch /panels/2
  165. * --
  166. */
  167. $scope.init();
  168. });
  169. });