dashLoader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. define([
  2. 'angular',
  3. 'underscore'
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('kibana.controllers');
  8. module.controller('dashLoader', function($scope, $http, timer, dashboard, alertSrv) {
  9. $scope.loader = dashboard.current.loader;
  10. $scope.init = function() {
  11. $scope.advancedLoad = false;
  12. $scope.advancedSave = false;
  13. $scope.gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  14. $scope.gist = $scope.gist || {};
  15. $scope.elasticsearch = $scope.elasticsearch || {};
  16. };
  17. $scope.showDropdown = function(type) {
  18. if(_.isUndefined(dashboard.current.loader)) {
  19. return true;
  20. }
  21. var _l = dashboard.current.loader;
  22. if(type === 'load') {
  23. return (_l.load_elasticsearch || _l.load_gist || _l.load_local);
  24. }
  25. if(type === 'save') {
  26. return (_l.save_elasticsearch || _l.save_gist || _l.save_local || _l.save_default);
  27. }
  28. if(type === 'share') {
  29. return (_l.save_temp);
  30. }
  31. return false;
  32. };
  33. $scope.set_default = function() {
  34. if(dashboard.set_default()) {
  35. alertSrv.set('Local Default Set',dashboard.current.title+' has been set as your local default','success',5000);
  36. } else {
  37. alertSrv.set('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  38. }
  39. };
  40. $scope.purge_default = function() {
  41. if(dashboard.purge_default()) {
  42. alertSrv.set('Local Default Clear','Your local default dashboard has been cleared','success',5000);
  43. } else {
  44. alertSrv.set('Incompatible Browser','Sorry, your browser is too old for this feature','error',5000);
  45. }
  46. };
  47. $scope.elasticsearch_save = function(type,ttl) {
  48. dashboard.elasticsearch_save(
  49. type,
  50. ($scope.elasticsearch.title || dashboard.current.title),
  51. ($scope.loader.save_temp_ttl_enable ? ttl : false)
  52. ).then(
  53. function(result) {
  54. if(!_.isUndefined(result._id)) {
  55. alertSrv.set('Dashboard Saved','This dashboard has been saved to Elasticsearch as "' +
  56. result._id + '"','success',5000);
  57. if(type === 'temp') {
  58. $scope.share = dashboard.share_link(dashboard.current.title,'temp',result._id);
  59. }
  60. } else {
  61. alertSrv.set('Save failed','Dashboard could not be saved to Elasticsearch','error',5000);
  62. }
  63. });
  64. };
  65. $scope.elasticsearch_delete = function(id) {
  66. dashboard.elasticsearch_delete(id).then(
  67. function(result) {
  68. if(!_.isUndefined(result)) {
  69. if(result.found) {
  70. alertSrv.set('Dashboard Deleted',id+' has been deleted','success',5000);
  71. // Find the deleted dashboard in the cached list and remove it
  72. var toDelete = _.where($scope.elasticsearch.dashboards,{_id:id})[0];
  73. $scope.elasticsearch.dashboards = _.without($scope.elasticsearch.dashboards,toDelete);
  74. } else {
  75. alertSrv.set('Dashboard Not Found','Could not find '+id+' in Elasticsearch','warning',5000);
  76. }
  77. } else {
  78. alertSrv.set('Dashboard Not Deleted','An error occurred deleting the dashboard','error',5000);
  79. }
  80. }
  81. );
  82. };
  83. $scope.elasticsearch_dblist = function(query) {
  84. dashboard.elasticsearch_list(query,$scope.loader.load_elasticsearch_size).then(
  85. function(result) {
  86. if(!_.isUndefined(result.hits)) {
  87. $scope.hits = result.hits.total;
  88. $scope.elasticsearch.dashboards = result.hits.hits;
  89. }
  90. });
  91. };
  92. $scope.save_gist = function() {
  93. dashboard.save_gist($scope.gist.title).then(
  94. function(link) {
  95. if(!_.isUndefined(link)) {
  96. $scope.gist.last = link;
  97. alertSrv.set('Gist saved','You will be able to access your exported dashboard file at '+
  98. '<a href="'+link+'">'+link+'</a> in a moment','success');
  99. } else {
  100. alertSrv.set('Save failed','Gist could not be saved','error',5000);
  101. }
  102. });
  103. };
  104. $scope.gist_dblist = function(id) {
  105. dashboard.gist_list(id).then(
  106. 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. });
  115. });