search.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'config',
  5. 'jquery'
  6. ],
  7. function (angular, _, config, $) {
  8. 'use strict';
  9. var module = angular.module('grafana.controllers');
  10. module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, datasourceSrv) {
  11. $scope.init = function() {
  12. $scope.giveSearchFocus = 0;
  13. $scope.selectedIndex = -1;
  14. $scope.results = {dashboards: [], tags: [], metrics: []};
  15. $scope.query = { query: 'title:' };
  16. $scope.db = datasourceSrv.getGrafanaDB();
  17. $scope.onAppEvent('open-search', $scope.openSearch);
  18. };
  19. $scope.keyDown = function (evt) {
  20. if (evt.keyCode === 27) {
  21. $element.find('.dropdown-toggle').dropdown('toggle');
  22. }
  23. if (evt.keyCode === 40) {
  24. $scope.selectedIndex++;
  25. }
  26. if (evt.keyCode === 38) {
  27. $scope.selectedIndex--;
  28. }
  29. if (evt.keyCode === 13) {
  30. if ($scope.tagsOnly) {
  31. var tag = $scope.results.tags[$scope.selectedIndex];
  32. if (tag) {
  33. $scope.filterByTag(tag.term);
  34. }
  35. return;
  36. }
  37. var selectedDash = $scope.results.dashboards[$scope.selectedIndex];
  38. if (selectedDash) {
  39. $location.path("/dashboard/elasticsearch/" + selectedDash.id);
  40. setTimeout(function() {
  41. $('body').click(); // hack to force dropdown to close;
  42. });
  43. }
  44. }
  45. };
  46. $scope.shareDashboard = function(title, id) {
  47. var baseUrl = window.location.href.replace(window.location.hash,'');
  48. $scope.share = {
  49. title: title,
  50. url: baseUrl + '#dashboard/elasticsearch/' + encodeURIComponent(id)
  51. };
  52. };
  53. $scope.searchDashboards = function(queryString) {
  54. return $scope.db.searchDashboards(queryString)
  55. .then(function(results) {
  56. $scope.tagsOnly = results.tagsOnly;
  57. $scope.results.dashboards = results.dashboards;
  58. $scope.results.tags = results.tags;
  59. });
  60. };
  61. $scope.filterByTag = function(tag, evt) {
  62. $scope.query.query = "tags:" + tag + " AND title:";
  63. $scope.search();
  64. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  65. if (evt) {
  66. evt.stopPropagation();
  67. evt.preventDefault();
  68. }
  69. };
  70. $scope.showTags = function(evt) {
  71. evt.stopPropagation();
  72. $scope.tagsOnly = !$scope.tagsOnly;
  73. $scope.query.query = $scope.tagsOnly ? "tags!:" : "";
  74. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  75. $scope.selectedIndex = -1;
  76. $scope.search();
  77. };
  78. $scope.search = function() {
  79. $scope.showImport = false;
  80. $scope.selectedIndex = -1;
  81. $scope.searchDashboards($scope.query.query);
  82. };
  83. $scope.openSearch = function (evt) {
  84. if (evt) {
  85. $element.find('.dropdown-toggle').dropdown('toggle');
  86. }
  87. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  88. $scope.query.query = 'title:';
  89. $scope.search();
  90. };
  91. $scope.addMetricToCurrentDashboard = function (metricId) {
  92. $scope.dashboard.rows.push({
  93. title: '',
  94. height: '250px',
  95. editable: true,
  96. panels: [
  97. {
  98. type: 'graphite',
  99. title: 'test',
  100. span: 12,
  101. targets: [{ target: metricId }]
  102. }
  103. ]
  104. });
  105. };
  106. $scope.toggleImport = function ($event) {
  107. $event.stopPropagation();
  108. $scope.showImport = !$scope.showImport;
  109. };
  110. $scope.newDashboard = function() {
  111. $location.url('/dashboard/file/empty.json');
  112. };
  113. });
  114. module.directive('xngFocus', function() {
  115. return function(scope, element, attrs) {
  116. $(element).click(function(e) {
  117. e.stopPropagation();
  118. });
  119. scope.$watch(attrs.xngFocus,function (newValue) {
  120. setTimeout(function() {
  121. newValue && element.focus();
  122. }, 200);
  123. },true);
  124. };
  125. });
  126. });