search.ts 4.1 KB

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