search.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'config',
  5. 'jquery'
  6. ],
  7. function (angular, _, config, $) {
  8. 'use strict';
  9. var module = angular.module('kibana.controllers');
  10. module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, elastic) {
  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.onAppEvent('open-search', $scope.openSearch);
  17. };
  18. $scope.keyDown = function (evt) {
  19. if (evt.keyCode === 27) {
  20. $element.find('.dropdown-toggle').dropdown('toggle');
  21. }
  22. if (evt.keyCode === 40) {
  23. $scope.selectedIndex++;
  24. }
  25. if (evt.keyCode === 38) {
  26. $scope.selectedIndex--;
  27. }
  28. if (evt.keyCode === 13) {
  29. if ($scope.tagsOnly) {
  30. var tag = $scope.results.tags[$scope.selectedIndex];
  31. if (tag) {
  32. $scope.filterByTag(tag.term);
  33. }
  34. return;
  35. }
  36. var selectedDash = $scope.results.dashboards[$scope.selectedIndex];
  37. if (selectedDash) {
  38. $location.path("/dashboard/elasticsearch/" + encodeURIComponent(selectedDash._id));
  39. setTimeout(function() {
  40. $('body').click(); // hack to force dropdown to close;
  41. });
  42. }
  43. }
  44. };
  45. $scope.shareDashboard = function(title, id) {
  46. var baseUrl = window.location.href.replace(window.location.hash,'');
  47. $scope.share = {
  48. title: title,
  49. url: baseUrl + '#dashboard/elasticsearch/' + encodeURIComponent(id)
  50. };
  51. };
  52. $scope.searchDasboards = function(queryString) {
  53. var tagsOnly = queryString.indexOf('tags!:') === 0;
  54. if (tagsOnly) {
  55. var tagsQuery = queryString.substring(6, queryString.length);
  56. queryString = 'tags:' + tagsQuery + '*';
  57. }
  58. else {
  59. if (queryString.length === 0) {
  60. queryString = 'title:';
  61. }
  62. if (queryString[queryString.length - 1] !== '*') {
  63. queryString += '*';
  64. }
  65. }
  66. var query = {
  67. query: { query_string: { query: queryString } },
  68. facets: { tags: { terms: { field: "tags", order: "term", size: 50 } } },
  69. size: 20,
  70. sort: ["_uid"]
  71. };
  72. return elastic.post('/dashboard/_search', query)
  73. .then(function(results) {
  74. if(_.isUndefined(results.hits)) {
  75. $scope.results.dashboards = [];
  76. $scope.results.tags = [];
  77. return;
  78. }
  79. $scope.tagsOnly = tagsOnly;
  80. $scope.results.dashboards = results.hits.hits;
  81. $scope.results.tags = results.facets.tags.terms;
  82. });
  83. };
  84. $scope.filterByTag = function(tag, evt) {
  85. $scope.query.query = "tags:" + tag + " AND title:";
  86. $scope.search();
  87. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  88. if (evt) {
  89. evt.stopPropagation();
  90. evt.preventDefault();
  91. }
  92. };
  93. $scope.showTags = function(evt) {
  94. evt.stopPropagation();
  95. $scope.tagsOnly = !$scope.tagsOnly;
  96. $scope.query.query = $scope.tagsOnly ? "tags!:" : "";
  97. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  98. $scope.selectedIndex = -1;
  99. $scope.search();
  100. };
  101. $scope.search = function() {
  102. $scope.showImport = false;
  103. $scope.selectedIndex = -1;
  104. var queryStr = $scope.query.query.toLowerCase();
  105. if (queryStr.indexOf('m:') !== 0) {
  106. queryStr = queryStr.replace(' and ', ' AND ');
  107. $scope.searchDasboards(queryStr);
  108. return;
  109. }
  110. };
  111. $scope.openSearch = function (evt) {
  112. if (evt) {
  113. $element.find('.dropdown-toggle').dropdown('toggle');
  114. }
  115. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  116. $scope.query.query = 'title:';
  117. $scope.search();
  118. };
  119. $scope.addMetricToCurrentDashboard = function (metricId) {
  120. $scope.dashboard.rows.push({
  121. title: '',
  122. height: '250px',
  123. editable: true,
  124. panels: [
  125. {
  126. type: 'graphite',
  127. title: 'test',
  128. span: 12,
  129. targets: [{ target: metricId }]
  130. }
  131. ]
  132. });
  133. };
  134. $scope.toggleImport = function ($event) {
  135. $event.stopPropagation();
  136. $scope.showImport = !$scope.showImport;
  137. };
  138. $scope.newDashboard = function() {
  139. $location.url('/dashboard/file/empty.json');
  140. };
  141. });
  142. module.directive('xngFocus', function() {
  143. return function(scope, element, attrs) {
  144. $(element).click(function(e) {
  145. e.stopPropagation();
  146. });
  147. scope.$watch(attrs.xngFocus,function (newValue) {
  148. setTimeout(function() {
  149. newValue && element.focus();
  150. }, 200);
  151. },true);
  152. };
  153. });
  154. });