search.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. const flattenedResult = this.getFlattenedResultForNavigation();
  59. const currentItem = flattenedResult[this.selectedIndex];
  60. if (currentItem) {
  61. if (currentItem.dashboardIndex !== undefined) {
  62. const selectedDash = this.results[currentItem.folderIndex].items[currentItem.dashboardIndex];
  63. if (selectedDash) {
  64. this.$location.search({});
  65. this.$location.path(selectedDash.url);
  66. }
  67. } else {
  68. const selectedFolder = this.results[currentItem.folderIndex];
  69. if (selectedFolder) {
  70. selectedFolder.toggle(selectedFolder);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. moveSelection(direction) {
  77. if (this.results.length === 0) {
  78. return;
  79. }
  80. const flattenedResult = this.getFlattenedResultForNavigation();
  81. const currentItem = flattenedResult[this.selectedIndex];
  82. if (currentItem) {
  83. if (currentItem.dashboardIndex !== undefined) {
  84. this.results[currentItem.folderIndex].items[currentItem.dashboardIndex].selected = false;
  85. } else {
  86. this.results[currentItem.folderIndex].selected = false;
  87. }
  88. }
  89. if (direction === 0) {
  90. this.selectedIndex = -1;
  91. return;
  92. }
  93. const max = flattenedResult.length;
  94. let newIndex = this.selectedIndex + direction;
  95. this.selectedIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  96. const selectedItem = flattenedResult[this.selectedIndex];
  97. if (selectedItem.dashboardIndex === undefined && this.results[selectedItem.folderIndex].id === 0) {
  98. this.moveSelection(direction);
  99. return;
  100. }
  101. if (selectedItem.dashboardIndex !== undefined) {
  102. if (!this.results[selectedItem.folderIndex].expanded) {
  103. this.moveSelection(direction);
  104. return;
  105. }
  106. this.results[selectedItem.folderIndex].items[selectedItem.dashboardIndex].selected = true;
  107. return;
  108. }
  109. if (this.results[selectedItem.folderIndex].hideHeader) {
  110. this.moveSelection(direction);
  111. return;
  112. }
  113. this.results[selectedItem.folderIndex].selected = true;
  114. }
  115. searchDashboards() {
  116. this.currentSearchId = this.currentSearchId + 1;
  117. var localSearchId = this.currentSearchId;
  118. return this.searchSrv.search(this.query).then(results => {
  119. if (localSearchId < this.currentSearchId) { return; }
  120. this.results = results || [];
  121. this.isLoading = false;
  122. this.moveSelection(1);
  123. });
  124. }
  125. queryHasNoFilters() {
  126. var query = this.query;
  127. return query.query === '' && query.starred === false && query.tag.length === 0;
  128. }
  129. filterByTag(tag) {
  130. if (_.indexOf(this.query.tag, tag) === -1) {
  131. this.query.tag.push(tag);
  132. this.search();
  133. this.giveSearchFocus = this.giveSearchFocus + 1;
  134. }
  135. }
  136. removeTag(tag, evt) {
  137. this.query.tag = _.without(this.query.tag, tag);
  138. this.search();
  139. this.giveSearchFocus = this.giveSearchFocus + 1;
  140. evt.stopPropagation();
  141. evt.preventDefault();
  142. }
  143. getTags() {
  144. return this.searchSrv.getDashboardTags().then((results) => {
  145. this.results = results;
  146. this.giveSearchFocus = this.giveSearchFocus + 1;
  147. });
  148. }
  149. showStarred() {
  150. this.query.starred = !this.query.starred;
  151. this.giveSearchFocus = this.giveSearchFocus + 1;
  152. this.search();
  153. }
  154. search() {
  155. this.showImport = false;
  156. this.selectedIndex = -1;
  157. this.searchDashboards();
  158. }
  159. folderExpanding() {
  160. this.moveSelection(0);
  161. }
  162. private getFlattenedResultForNavigation() {
  163. let folderIndex = 0;
  164. return _.flatMap(this.results, (s) => {
  165. let result = [];
  166. result.push({
  167. folderIndex: folderIndex
  168. });
  169. let dashboardIndex = 0;
  170. result = result.concat(_.map(s.items || [], (i) => {
  171. return {
  172. folderIndex: folderIndex,
  173. dashboardIndex: dashboardIndex++
  174. };
  175. }));
  176. folderIndex++;
  177. return result;
  178. });
  179. }
  180. }
  181. export function searchDirective() {
  182. return {
  183. restrict: 'E',
  184. templateUrl: 'public/app/core/components/search/search.html',
  185. controller: SearchCtrl,
  186. bindToController: true,
  187. controllerAs: 'ctrl',
  188. scope: {},
  189. };
  190. }
  191. coreModule.directive('dashboardSearch', searchDirective);