search.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import _ from 'lodash';
  2. import coreModule from '../../core_module';
  3. import { SearchSrv } from 'app/core/services/search_srv';
  4. export class SearchCtrl {
  5. isOpen: boolean;
  6. query: any;
  7. giveSearchFocus: number;
  8. selectedIndex: number;
  9. results: any;
  10. currentSearchId: number;
  11. showImport: boolean;
  12. dismiss: any;
  13. ignoreClose: any;
  14. isLoading: boolean;
  15. initialFolderFilterTitle: string;
  16. /** @ngInject */
  17. constructor($scope, private $location, private $timeout, private searchSrv: SearchSrv, $rootScope) {
  18. $rootScope.onAppEvent('show-dash-search', this.openSearch.bind(this), $scope);
  19. $rootScope.onAppEvent('hide-dash-search', this.closeSearch.bind(this), $scope);
  20. this.initialFolderFilterTitle = "All";
  21. }
  22. closeSearch() {
  23. this.isOpen = this.ignoreClose;
  24. }
  25. openSearch(evt, payload) {
  26. if (this.isOpen) {
  27. this.closeSearch();
  28. return;
  29. }
  30. this.isOpen = true;
  31. this.giveSearchFocus = 0;
  32. this.selectedIndex = -1;
  33. this.results = [];
  34. this.query = { query: '', tag: [], starred: false };
  35. this.currentSearchId = 0;
  36. this.ignoreClose = true;
  37. this.isLoading = true;
  38. if (payload && payload.starred) {
  39. this.query.starred = true;
  40. }
  41. this.$timeout(() => {
  42. this.ignoreClose = false;
  43. this.giveSearchFocus = this.giveSearchFocus + 1;
  44. this.search();
  45. }, 100);
  46. }
  47. keyDown(evt) {
  48. if (evt.keyCode === 27) {
  49. this.closeSearch();
  50. }
  51. if (evt.keyCode === 40) {
  52. this.moveSelection(1);
  53. }
  54. if (evt.keyCode === 38) {
  55. this.moveSelection(-1);
  56. }
  57. if (evt.keyCode === 13) {
  58. var selectedDash = this.results[this.selectedIndex];
  59. if (selectedDash) {
  60. this.$location.search({});
  61. this.$location.path(selectedDash.url);
  62. }
  63. }
  64. }
  65. moveSelection(direction) {
  66. var max = (this.results || []).length;
  67. var newIndex = this.selectedIndex + direction;
  68. this.selectedIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  69. }
  70. searchDashboards() {
  71. this.currentSearchId = this.currentSearchId + 1;
  72. var localSearchId = this.currentSearchId;
  73. return this.searchSrv.search(this.query).then(results => {
  74. if (localSearchId < this.currentSearchId) { return; }
  75. this.results = results;
  76. this.isLoading = false;
  77. });
  78. }
  79. queryHasNoFilters() {
  80. var query = this.query;
  81. return query.query === '' && query.starred === false && query.tag.length === 0;
  82. }
  83. filterByTag(tag, evt) {
  84. this.query.tag.push(tag);
  85. this.search();
  86. this.giveSearchFocus = this.giveSearchFocus + 1;
  87. if (evt) {
  88. evt.stopPropagation();
  89. evt.preventDefault();
  90. }
  91. }
  92. removeTag(tag, evt) {
  93. this.query.tag = _.without(this.query.tag, tag);
  94. this.search();
  95. this.giveSearchFocus = this.giveSearchFocus + 1;
  96. evt.stopPropagation();
  97. evt.preventDefault();
  98. }
  99. getTags() {
  100. return this.searchSrv.getDashboardTags().then((results) => {
  101. this.results = results;
  102. this.giveSearchFocus = this.giveSearchFocus + 1;
  103. });
  104. }
  105. showStarred() {
  106. this.query.starred = !this.query.starred;
  107. this.giveSearchFocus = this.giveSearchFocus + 1;
  108. this.search();
  109. }
  110. search() {
  111. this.showImport = false;
  112. this.selectedIndex = 0;
  113. this.searchDashboards();
  114. }
  115. toggleFolder(section) {
  116. this.searchSrv.toggleSection(section);
  117. }
  118. }
  119. export function searchDirective() {
  120. return {
  121. restrict: 'E',
  122. templateUrl: 'public/app/core/components/search/search.html',
  123. controller: SearchCtrl,
  124. bindToController: true,
  125. controllerAs: 'ctrl',
  126. scope: {},
  127. };
  128. }
  129. coreModule.directive('dashboardSearch', searchDirective);