dashLoader.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'moment'
  5. ],
  6. function (angular, _, moment) {
  7. 'use strict';
  8. var module = angular.module('kibana.controllers');
  9. module.controller('dashLoader', function($scope, $rootScope, $http, dashboard, alertSrv, $location, filterSrv, playlistSrv) {
  10. $scope.loader = dashboard.current.loader;
  11. $scope.init = function() {
  12. $scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  13. $scope.gist = $scope.gist || {};
  14. $scope.elasticsearch = $scope.elasticsearch || {};
  15. $rootScope.$on('save-dashboard', function() {
  16. $scope.elasticsearch_save('dashboard', false);
  17. });
  18. $rootScope.$on('zoom-out', function() {
  19. $scope.zoom(2);
  20. });
  21. };
  22. $scope.exitFullscreen = function() {
  23. $rootScope.$emit('panel-fullscreen-exit');
  24. };
  25. $scope.showDropdown = function(type) {
  26. if(_.isUndefined(dashboard.current.loader)) {
  27. return true;
  28. }
  29. var _l = dashboard.current.loader;
  30. if(type === 'load') {
  31. return (_l.load_elasticsearch || _l.load_gist || _l.load_local);
  32. }
  33. if(type === 'save') {
  34. return (_l.save_elasticsearch || _l.save_gist || _l.save_local || _l.save_default);
  35. }
  36. if(type === 'share') {
  37. return (_l.save_temp);
  38. }
  39. return false;
  40. };
  41. $scope.set_default = function() {
  42. if(dashboard.set_default($location.path())) {
  43. alertSrv.set('Home Set','This page has been set as your default Kibana dashboard','success',5000);
  44. } else {
  45. alertSrv.set('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  46. }
  47. };
  48. $scope.purge_default = function() {
  49. if(dashboard.purge_default()) {
  50. alertSrv.set('Local Default Clear','Your Kibana default dashboard has been reset to the default',
  51. 'success',5000);
  52. } else {
  53. alertSrv.set('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  54. }
  55. };
  56. $scope.elasticsearch_save = function(type,ttl) {
  57. dashboard.elasticsearch_save(
  58. type,
  59. ($scope.elasticsearch.title || dashboard.current.title),
  60. ($scope.loader.save_temp_ttl_enable ? ttl : false)
  61. ).then(function(result) {
  62. if(_.isUndefined(result._id)) {
  63. alertSrv.set('Save failed','Dashboard could not be saved to Elasticsearch','error',5000);
  64. return;
  65. }
  66. alertSrv.set('Dashboard Saved', 'This dashboard has been saved to Elasticsearch as "' + result._id + '"','success', 5000);
  67. if(type === 'temp') {
  68. $scope.share = dashboard.share_link(dashboard.current.title,'temp',result._id);
  69. }
  70. $rootScope.$emit('dashboard-saved');
  71. });
  72. };
  73. $scope.elasticsearch_delete = function(id) {
  74. if (!confirm('Are you sure you want to delete dashboard?')) {
  75. return;
  76. }
  77. dashboard.elasticsearch_delete(id).then(
  78. function(result) {
  79. if(!_.isUndefined(result)) {
  80. if(result.found) {
  81. alertSrv.set('Dashboard Deleted',id+' has been deleted','success',5000);
  82. // Find the deleted dashboard in the cached list and remove it
  83. var toDelete = _.where($scope.elasticsearch.dashboards,{_id:id})[0];
  84. $scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,toDelete);
  85. } else {
  86. alertSrv.set('Dashboard Not Found','Could not find '+id+' in Elasticsearch','warning',5000);
  87. }
  88. } else {
  89. alertSrv.set('Dashboard Not Deleted','An error occurred deleting the dashboard','error',5000);
  90. }
  91. }
  92. );
  93. };
  94. $scope.save_gist = function() {
  95. dashboard.save_gist($scope.gist.title).then(
  96. function(link) {
  97. if(!_.isUndefined(link)) {
  98. $scope.gist.last = link;
  99. alertSrv.set('Gist saved','You will be able to access your exported dashboard file at '+
  100. '<a href="'+link+'">'+link+'</a> in a moment','success');
  101. } else {
  102. alertSrv.set('Save failed','Gist could not be saved','error',5000);
  103. }
  104. });
  105. };
  106. $scope.gist_dblist = function(id) {
  107. dashboard.gist_list(id).then(
  108. function(files) {
  109. if(files && files.length > 0) {
  110. $scope.gist.files = files;
  111. } else {
  112. alertSrv.set('Gist Failed','Could not retrieve dashboard list from gist','error',5000);
  113. }
  114. });
  115. };
  116. // function $scope.zoom
  117. // factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan
  118. $scope.zoom = function(factor) {
  119. var _range = filterSrv.timeRange();
  120. var _timespan = (_range.to.valueOf() - _range.from.valueOf());
  121. var _center = _range.to.valueOf() - _timespan/2;
  122. var _to = (_center + (_timespan*factor)/2);
  123. var _from = (_center - (_timespan*factor)/2);
  124. // If we're not already looking into the future, don't.
  125. if(_to > Date.now() && _range.to < Date.now()) {
  126. var _offset = _to - Date.now();
  127. _from = _from - _offset;
  128. _to = Date.now();
  129. }
  130. filterSrv.setTime({
  131. from:moment.utc(_from).toDate(),
  132. to:moment.utc(_to).toDate(),
  133. });
  134. };
  135. $scope.openSaveDropdown = function() {
  136. $scope.isFavorite = playlistSrv.isCurrentFavorite();
  137. };
  138. $scope.markAsFavorite = function() {
  139. playlistSrv.markAsFavorite();
  140. $scope.isFavorite = true;
  141. };
  142. $scope.removeAsFavorite = function() {
  143. playlistSrv.removeAsFavorite(dashboard.current);
  144. $scope.isFavorite = false;
  145. };
  146. $scope.stopPlaylist = function() {
  147. playlistSrv.stop(1);
  148. };
  149. });
  150. });