dashLoader.js 5.6 KB

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