search.ts 6.1 KB

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