controllers.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. 'use strict';
  4. angular.module('kibana.controllers', [])
  5. .controller('DashCtrl', function($scope, $rootScope, $http, $timeout, $route, ejsResource,
  6. fields, dashboard, alertSrv) {
  7. $scope.editor = {
  8. index: 0
  9. };
  10. $scope.init = function() {
  11. $scope.config = config;
  12. // Make underscore.js available to views
  13. $scope._ = _;
  14. $scope.dashboard = dashboard;
  15. $scope.dashAlerts = alertSrv;
  16. alertSrv.clearAll();
  17. // Provide a global list of all see fields
  18. $scope.fields = fields;
  19. $scope.reset_row();
  20. var ejs = $scope.ejs = ejsResource(config.elasticsearch);
  21. };
  22. $scope.add_row = function(dash,row) {
  23. dash.rows.push(row);
  24. };
  25. $scope.reset_row = function() {
  26. $scope.row = {
  27. title: '',
  28. height: '150px',
  29. editable: true,
  30. };
  31. };
  32. $scope.row_style = function(row) {
  33. return { 'min-height': row.collapse ? '5px' : row.height };
  34. };
  35. $scope.edit_path = function(type) {
  36. if(type) {
  37. return 'panels/'+type+'/editor.html';
  38. } else {
  39. return false;
  40. }
  41. };
  42. $scope.setEditorTabs = function(panelMeta) {
  43. $scope.editorTabs = ['General','Panel'];
  44. if(!_.isUndefined(panelMeta.editorTabs)) {
  45. $scope.editorTabs = _.union($scope.editorTabs,_.pluck(panelMeta.editorTabs,'title'));
  46. }
  47. return $scope.editorTabs;
  48. };
  49. // This is whoafully incomplete, but will do for now
  50. $scope.parse_error = function(data) {
  51. var _error = data.match("nested: (.*?);");
  52. return _.isNull(_error) ? data : _error[1];
  53. };
  54. $scope.init();
  55. })
  56. .controller('RowCtrl', function($scope, $rootScope, $timeout, ejsResource, querySrv) {
  57. var _d = {
  58. title: "Row",
  59. height: "150px",
  60. collapse: false,
  61. collapsable: true,
  62. editable: true,
  63. panels: [],
  64. };
  65. _.defaults($scope.row,_d);
  66. $scope.init = function() {
  67. $scope.querySrv = querySrv;
  68. $scope.reset_panel();
  69. };
  70. $scope.toggle_row = function(row) {
  71. if(!row.collapsable) {
  72. return;
  73. }
  74. row.collapse = row.collapse ? false : true;
  75. if (!row.collapse) {
  76. $timeout(function() {
  77. $scope.$broadcast('render');
  78. });
  79. }
  80. };
  81. // This can be overridden by individual panels
  82. $scope.close_edit = function() {
  83. $scope.$broadcast('render');
  84. };
  85. $scope.add_panel = function(row,panel) {
  86. $scope.row.panels.push(panel);
  87. };
  88. $scope.reset_panel = function(type) {
  89. $scope.panel = {
  90. error : false,
  91. span : 3,
  92. editable: true,
  93. type : type
  94. };
  95. };
  96. $scope.init();
  97. })
  98. .controller('dashLoader', function($scope, $http, timer, dashboard, alertSrv) {
  99. $scope.loader = dashboard.current.loader;
  100. $scope.init = function() {
  101. $scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  102. $scope.gist = $scope.gist || {};
  103. $scope.elasticsearch = $scope.elasticsearch || {};
  104. };
  105. $scope.showDropdown = function(type) {
  106. var _l = $scope.loader;
  107. if(type === 'load') {
  108. return (_l.load_elasticsearch || _l.load_gist || _l.load_local);
  109. }
  110. if(type === 'save') {
  111. return (_l.save_elasticsearch || _l.save_gist || _l.local_local || _l.save_default);
  112. }
  113. if(type === 'share') {
  114. return (_l.save_temp);
  115. }
  116. return false;
  117. };
  118. $scope.set_default = function() {
  119. if(dashboard.set_default()) {
  120. alertSrv.set('Local Default Set',dashboard.current.title+' has been set as your local default','success',5000);
  121. } else {
  122. alertSrv.set('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  123. }
  124. };
  125. $scope.purge_default = function() {
  126. if(dashboard.purge_default()) {
  127. alertSrv.set('Local Default Clear','Your local default dashboard has been cleared','success',5000);
  128. } else {
  129. alertSrv.set('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  130. }
  131. };
  132. $scope.elasticsearch_save = function(type,ttl) {
  133. dashboard.elasticsearch_save(
  134. type,
  135. ($scope.elasticsearch.title || dashboard.current.title),
  136. ($scope.loader.save_temp_ttl_enable ? ttl : false)
  137. ).then(
  138. function(result) {
  139. if(!_.isUndefined(result._id)) {
  140. alertSrv.set('Dashboard Saved','This dashboard has been saved to Elasticsearch as "' +
  141. result._id + '"','success',5000);
  142. if(type === 'temp') {
  143. $scope.share = dashboard.share_link(dashboard.current.title,'temp',result._id);
  144. }
  145. } else {
  146. alertSrv.set('Save failed','Dashboard could not be saved to Elasticsearch','error',5000);
  147. }
  148. });
  149. };
  150. $scope.elasticsearch_delete = function(id) {
  151. dashboard.elasticsearch_delete(id).then(
  152. function(result) {
  153. if(!_.isUndefined(result)) {
  154. if(result.found) {
  155. alertSrv.set('Dashboard Deleted',id+' has been deleted','success',5000);
  156. // Find the deleted dashboard in the cached list and remove it
  157. var toDelete = _.where($scope.elasticsearch.dashboards,{_id:id})[0];
  158. $scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,toDelete);
  159. } else {
  160. alertSrv.set('Dashboard Not Found','Could not find '+id+' in Elasticsearch','warning',5000);
  161. }
  162. } else {
  163. alertSrv.set('Dashboard Not Deleted','An error occurred deleting the dashboard','error',5000);
  164. }
  165. }
  166. );
  167. };
  168. $scope.elasticsearch_dblist = function(query) {
  169. dashboard.elasticsearch_list(query,$scope.loader.load_elasticsearch_size).then(
  170. function(result) {
  171. if(!_.isUndefined(result.hits)) {
  172. $scope.hits = result.hits.total;
  173. $scope.elasticsearch.dashboards = result.hits.hits;
  174. }
  175. });
  176. };
  177. $scope.save_gist = function() {
  178. dashboard.save_gist($scope.gist.title).then(
  179. function(link) {
  180. if(!_.isUndefined(link)) {
  181. $scope.gist.last = link;
  182. alertSrv.set('Gist saved','You will be able to access your exported dashboard file at '+
  183. '<a href="'+link+'">'+link+'</a> in a moment','success');
  184. } else {
  185. alertSrv.set('Save failed','Gist could not be saved','error',5000);
  186. }
  187. });
  188. };
  189. $scope.gist_dblist = function(id) {
  190. dashboard.gist_list(id).then(
  191. function(files) {
  192. if(files && files.length > 0) {
  193. $scope.gist.files = files;
  194. } else {
  195. alertSrv.set('Gist Failed','Could not retrieve dashboard list from gist','error',5000);
  196. }
  197. });
  198. };
  199. });