search.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. ],
  6. function (angular, _, config) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('SearchCtrl', function($scope, $location, $timeout, backendSrv) {
  10. $scope.init = function() {
  11. $scope.giveSearchFocus = 0;
  12. $scope.selectedIndex = -1;
  13. $scope.results = [];
  14. $scope.query = { query: '', tag: [], starred: false };
  15. $scope.currentSearchId = 0;
  16. if ($scope.dashboardViewState.fullscreen) {
  17. $scope.exitFullscreen();
  18. }
  19. $timeout(function() {
  20. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  21. $scope.query.query = '';
  22. $scope.search();
  23. }, 100);
  24. };
  25. $scope.keyDown = function (evt) {
  26. if (evt.keyCode === 27) {
  27. $scope.dismiss();
  28. }
  29. if (evt.keyCode === 40) {
  30. $scope.moveSelection(1);
  31. }
  32. if (evt.keyCode === 38) {
  33. $scope.moveSelection(-1);
  34. }
  35. if (evt.keyCode === 13) {
  36. if ($scope.tagMode) {
  37. var tag = $scope.results[$scope.selectedIndex];
  38. if (tag) {
  39. $scope.filterByTag(tag.term);
  40. }
  41. return;
  42. }
  43. var selectedDash = $scope.results[$scope.selectedIndex];
  44. if (selectedDash) {
  45. $location.search({});
  46. $location.path(selectedDash.url);
  47. }
  48. }
  49. };
  50. $scope.moveSelection = function(direction) {
  51. var max = ($scope.results || []).length;
  52. var newIndex = $scope.selectedIndex + direction;
  53. $scope.selectedIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  54. };
  55. $scope.searchDashboards = function() {
  56. $scope.tagsMode = false;
  57. $scope.currentSearchId = $scope.currentSearchId + 1;
  58. var localSearchId = $scope.currentSearchId;
  59. return backendSrv.search($scope.query).then(function(results) {
  60. if (localSearchId < $scope.currentSearchId) { return; }
  61. $scope.results = _.map(results, function(dash) {
  62. dash.url = 'dashboard/' + dash.uri;
  63. return dash;
  64. });
  65. if ($scope.queryHasNoFilters()) {
  66. $scope.results.unshift({ title: 'Home', url: config.appSubUrl + '/', type: 'dash-home' });
  67. }
  68. });
  69. };
  70. $scope.queryHasNoFilters = function() {
  71. var query = $scope.query;
  72. return query.query === '' && query.starred === false && query.tag.length === 0;
  73. };
  74. $scope.filterByTag = function(tag, evt) {
  75. $scope.query.tag.push(tag);
  76. $scope.search();
  77. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  78. if (evt) {
  79. evt.stopPropagation();
  80. evt.preventDefault();
  81. }
  82. };
  83. $scope.removeTag = function(tag, evt) {
  84. $scope.query.tag = _.without($scope.query.tag, tag);
  85. $scope.search();
  86. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  87. evt.stopPropagation();
  88. evt.preventDefault();
  89. };
  90. $scope.getTags = function() {
  91. return backendSrv.get('/api/dashboards/tags').then(function(results) {
  92. $scope.tagsMode = true;
  93. $scope.results = results;
  94. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  95. });
  96. };
  97. $scope.showStarred = function() {
  98. $scope.query.starred = !$scope.query.starred;
  99. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  100. $scope.search();
  101. };
  102. $scope.search = function() {
  103. $scope.showImport = false;
  104. $scope.selectedIndex = 0;
  105. $scope.searchDashboards();
  106. };
  107. $scope.newDashboard = function() {
  108. $location.url('dashboard/new');
  109. };
  110. });
  111. });