search.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. hasEditPermissionInFolders: boolean;
  20. /** @ngInject */
  21. constructor($scope, private $location, private $timeout, private searchSrv: SearchSrv) {
  22. appEvents.on('show-dash-search', this.openSearch.bind(this), $scope);
  23. appEvents.on('hide-dash-search', this.closeSearch.bind(this), $scope);
  24. this.initialFolderFilterTitle = 'All';
  25. this.isEditor = contextSrv.isEditor;
  26. this.hasEditPermissionInFolders = contextSrv.hasEditPermissionInFolders;
  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. const newIndex = (this.selectedIndex + direction) % max;
  112. this.selectedIndex = newIndex < 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. const localSearchId = this.currentSearchId;
  135. const query = {
  136. ...this.query,
  137. tag: this.query.tag,
  138. };
  139. return this.searchSrv.search(query).then(results => {
  140. if (localSearchId < this.currentSearchId) {
  141. return;
  142. }
  143. this.results = results || [];
  144. this.isLoading = false;
  145. this.moveSelection(1);
  146. });
  147. }
  148. queryHasNoFilters() {
  149. const query = this.query;
  150. return query.query === '' && query.starred === false && query.tag.length === 0;
  151. }
  152. filterByTag(tag) {
  153. if (_.indexOf(this.query.tag, tag) === -1) {
  154. this.query.tag.push(tag);
  155. this.search();
  156. }
  157. }
  158. removeTag(tag, evt) {
  159. this.query.tag = _.without(this.query.tag, tag);
  160. this.search();
  161. this.giveSearchFocus = this.giveSearchFocus + 1;
  162. evt.stopPropagation();
  163. evt.preventDefault();
  164. }
  165. getTags = () => {
  166. return this.searchSrv.getDashboardTags();
  167. };
  168. onTagFiltersChanged = (tags: string[]) => {
  169. this.query.tag = tags;
  170. this.search();
  171. };
  172. clearSearchFilter() {
  173. this.query.tag = [];
  174. this.search();
  175. }
  176. showStarred() {
  177. this.query.starred = !this.query.starred;
  178. this.giveSearchFocus = this.giveSearchFocus + 1;
  179. this.search();
  180. }
  181. search() {
  182. this.showImport = false;
  183. this.selectedIndex = -1;
  184. this.searchDashboards();
  185. }
  186. folderExpanding() {
  187. this.moveSelection(0);
  188. }
  189. private getFlattenedResultForNavigation() {
  190. let folderIndex = 0;
  191. return _.flatMap(this.results, s => {
  192. let result = [];
  193. result.push({
  194. folderIndex: folderIndex,
  195. });
  196. let dashboardIndex = 0;
  197. result = result.concat(
  198. _.map(s.items || [], i => {
  199. return {
  200. folderIndex: folderIndex,
  201. dashboardIndex: dashboardIndex++,
  202. };
  203. })
  204. );
  205. folderIndex++;
  206. return result;
  207. });
  208. }
  209. }
  210. export function searchDirective() {
  211. return {
  212. restrict: 'E',
  213. templateUrl: 'public/app/core/components/search/search.html',
  214. controller: SearchCtrl,
  215. bindToController: true,
  216. controllerAs: 'ctrl',
  217. scope: {},
  218. };
  219. }
  220. coreModule.directive('dashboardSearch', searchDirective);