search.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import _ from 'lodash';
  2. import coreModule from '../../core_module';
  3. import { SearchSrv } from 'app/core/services/search_srv';
  4. import { contextSrv } from 'app/core/services/context_srv';
  5. import appEvents from 'app/core/app_events';
  6. export class SearchCtrl {
  7. isOpen: boolean;
  8. query: any;
  9. giveSearchFocus: number;
  10. selectedIndex: number;
  11. results: any;
  12. currentSearchId: number;
  13. showImport: boolean;
  14. dismiss: any;
  15. ignoreClose: any;
  16. isLoading: boolean;
  17. initialFolderFilterTitle: string;
  18. isEditor: string;
  19. /** @ngInject */
  20. constructor($scope, private $location, private $timeout, private searchSrv: SearchSrv) {
  21. appEvents.on('show-dash-search', this.openSearch.bind(this), $scope);
  22. appEvents.on('hide-dash-search', this.closeSearch.bind(this), $scope);
  23. this.initialFolderFilterTitle = 'All';
  24. this.getTags = this.getTags.bind(this);
  25. this.onTagSelect = this.onTagSelect.bind(this);
  26. this.isEditor = contextSrv.isEditor;
  27. }
  28. closeSearch() {
  29. this.isOpen = this.ignoreClose;
  30. }
  31. openSearch(evt, payload) {
  32. if (this.isOpen) {
  33. this.closeSearch();
  34. return;
  35. }
  36. this.isOpen = true;
  37. this.giveSearchFocus = 0;
  38. this.selectedIndex = -1;
  39. this.results = [];
  40. this.query = { query: '', tag: [], starred: false };
  41. this.currentSearchId = 0;
  42. this.ignoreClose = true;
  43. this.isLoading = true;
  44. if (payload && payload.starred) {
  45. this.query.starred = true;
  46. }
  47. this.$timeout(() => {
  48. this.ignoreClose = false;
  49. this.giveSearchFocus = this.giveSearchFocus + 1;
  50. this.search();
  51. }, 100);
  52. }
  53. keyDown(evt) {
  54. if (evt.keyCode === 27) {
  55. this.closeSearch();
  56. }
  57. if (evt.keyCode === 40) {
  58. this.moveSelection(1);
  59. }
  60. if (evt.keyCode === 38) {
  61. this.moveSelection(-1);
  62. }
  63. if (evt.keyCode === 13) {
  64. const flattenedResult = this.getFlattenedResultForNavigation();
  65. const currentItem = flattenedResult[this.selectedIndex];
  66. if (currentItem) {
  67. if (currentItem.dashboardIndex !== undefined) {
  68. const selectedDash = this.results[currentItem.folderIndex].items[currentItem.dashboardIndex];
  69. if (selectedDash) {
  70. this.$location.search({});
  71. this.$location.path(selectedDash.url);
  72. this.closeSearch();
  73. }
  74. } else {
  75. const selectedFolder = this.results[currentItem.folderIndex];
  76. if (selectedFolder) {
  77. selectedFolder.toggle(selectedFolder);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. onFilterboxClick() {
  84. this.giveSearchFocus = 0;
  85. this.preventClose();
  86. }
  87. preventClose() {
  88. this.ignoreClose = true;
  89. this.$timeout(() => {
  90. this.ignoreClose = false;
  91. }, 100);
  92. }
  93. moveSelection(direction) {
  94. if (this.results.length === 0) {
  95. return;
  96. }
  97. const flattenedResult = this.getFlattenedResultForNavigation();
  98. const currentItem = flattenedResult[this.selectedIndex];
  99. if (currentItem) {
  100. if (currentItem.dashboardIndex !== undefined) {
  101. this.results[currentItem.folderIndex].items[currentItem.dashboardIndex].selected = false;
  102. } else {
  103. this.results[currentItem.folderIndex].selected = false;
  104. }
  105. }
  106. if (direction === 0) {
  107. this.selectedIndex = -1;
  108. return;
  109. }
  110. const max = flattenedResult.length;
  111. let newIndex = this.selectedIndex + direction;
  112. this.selectedIndex = (newIndex %= max) < 0 ? newIndex + max : newIndex;
  113. const selectedItem = flattenedResult[this.selectedIndex];
  114. if (selectedItem.dashboardIndex === undefined && this.results[selectedItem.folderIndex].id === 0) {
  115. this.moveSelection(direction);
  116. return;
  117. }
  118. if (selectedItem.dashboardIndex !== undefined) {
  119. if (!this.results[selectedItem.folderIndex].expanded) {
  120. this.moveSelection(direction);
  121. return;
  122. }
  123. this.results[selectedItem.folderIndex].items[selectedItem.dashboardIndex].selected = true;
  124. return;
  125. }
  126. if (this.results[selectedItem.folderIndex].hideHeader) {
  127. this.moveSelection(direction);
  128. return;
  129. }
  130. this.results[selectedItem.folderIndex].selected = true;
  131. }
  132. searchDashboards() {
  133. this.currentSearchId = this.currentSearchId + 1;
  134. var localSearchId = this.currentSearchId;
  135. return this.searchSrv.search(this.query).then(results => {
  136. if (localSearchId < this.currentSearchId) {
  137. return;
  138. }
  139. this.results = results || [];
  140. this.isLoading = false;
  141. this.moveSelection(1);
  142. });
  143. }
  144. queryHasNoFilters() {
  145. var query = this.query;
  146. return query.query === '' && query.starred === false && query.tag.length === 0;
  147. }
  148. filterByTag(tag) {
  149. if (_.indexOf(this.query.tag, tag) === -1) {
  150. this.query.tag.push(tag);
  151. this.search();
  152. }
  153. }
  154. removeTag(tag, evt) {
  155. this.query.tag = _.without(this.query.tag, tag);
  156. this.search();
  157. this.giveSearchFocus = this.giveSearchFocus + 1;
  158. evt.stopPropagation();
  159. evt.preventDefault();
  160. }
  161. getTags() {
  162. return this.searchSrv.getDashboardTags();
  163. }
  164. onTagSelect(newTags) {
  165. this.query.tag = _.map(newTags, tag => tag.value);
  166. this.search();
  167. }
  168. clearSearchFilter() {
  169. this.query.tag = [];
  170. this.search();
  171. }
  172. showStarred() {
  173. this.query.starred = !this.query.starred;
  174. this.giveSearchFocus = this.giveSearchFocus + 1;
  175. this.search();
  176. }
  177. search() {
  178. this.showImport = false;
  179. this.selectedIndex = -1;
  180. this.searchDashboards();
  181. }
  182. folderExpanding() {
  183. this.moveSelection(0);
  184. }
  185. private getFlattenedResultForNavigation() {
  186. let folderIndex = 0;
  187. return _.flatMap(this.results, s => {
  188. let result = [];
  189. result.push({
  190. folderIndex: folderIndex,
  191. });
  192. let dashboardIndex = 0;
  193. result = result.concat(
  194. _.map(s.items || [], i => {
  195. return {
  196. folderIndex: folderIndex,
  197. dashboardIndex: dashboardIndex++,
  198. };
  199. })
  200. );
  201. folderIndex++;
  202. return result;
  203. });
  204. }
  205. }
  206. export function searchDirective() {
  207. return {
  208. restrict: 'E',
  209. templateUrl: 'public/app/core/components/search/search.html',
  210. controller: SearchCtrl,
  211. bindToController: true,
  212. controllerAs: 'ctrl',
  213. scope: {},
  214. };
  215. }
  216. coreModule.directive('dashboardSearch', searchDirective);