search_ctrl.js 3.4 KB

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