search.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import _, { debounce } 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. import { parse, SearchParserOptions, SearchParserResult } from 'search-query-parser';
  7. import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
  8. export interface SearchQuery {
  9. query: string;
  10. parsedQuery: SearchParserResult;
  11. tags: string[];
  12. starred: boolean;
  13. }
  14. class SearchQueryParser {
  15. config: SearchParserOptions;
  16. constructor(config: SearchParserOptions) {
  17. this.config = config;
  18. }
  19. parse(query: string) {
  20. const parsedQuery = parse(query, this.config);
  21. if (typeof parsedQuery === 'string') {
  22. return {
  23. text: parsedQuery,
  24. } as SearchParserResult;
  25. }
  26. return parsedQuery;
  27. }
  28. }
  29. interface SelectedIndicies {
  30. dashboardIndex?: number;
  31. folderIndex?: number;
  32. }
  33. export class SearchCtrl {
  34. isOpen: boolean;
  35. query: SearchQuery;
  36. giveSearchFocus: boolean;
  37. selectedIndex: number;
  38. results: any;
  39. currentSearchId: number;
  40. showImport: boolean;
  41. dismiss: any;
  42. ignoreClose: any;
  43. isLoading: boolean;
  44. initialFolderFilterTitle: string;
  45. isEditor: string;
  46. hasEditPermissionInFolders: boolean;
  47. queryParser: SearchQueryParser;
  48. /** @ngInject */
  49. constructor($scope: any, private $location: any, private $timeout: any, private searchSrv: SearchSrv) {
  50. appEvents.on('show-dash-search', this.openSearch.bind(this), $scope);
  51. appEvents.on('hide-dash-search', this.closeSearch.bind(this), $scope);
  52. appEvents.on('search-query', debounce(this.search.bind(this), 500), $scope);
  53. this.initialFolderFilterTitle = 'All';
  54. this.isEditor = contextSrv.isEditor;
  55. this.hasEditPermissionInFolders = contextSrv.hasEditPermissionInFolders;
  56. this.onQueryChange = this.onQueryChange.bind(this);
  57. this.onKeyDown = this.onKeyDown.bind(this);
  58. this.query = {
  59. query: '',
  60. parsedQuery: { text: '' },
  61. tags: [],
  62. starred: false,
  63. };
  64. this.queryParser = new SearchQueryParser({
  65. keywords: ['folder'],
  66. });
  67. }
  68. closeSearch() {
  69. this.isOpen = this.ignoreClose;
  70. }
  71. onQueryChange(query: SearchQuery | string) {
  72. if (typeof query === 'string') {
  73. this.query = {
  74. ...this.query,
  75. parsedQuery: this.queryParser.parse(query),
  76. query: query,
  77. };
  78. } else {
  79. this.query = query;
  80. }
  81. appEvents.emit('search-query');
  82. }
  83. openSearch(evt: any, payload: any) {
  84. if (this.isOpen) {
  85. this.closeSearch();
  86. return;
  87. }
  88. this.isOpen = true;
  89. this.giveSearchFocus = true;
  90. this.selectedIndex = -1;
  91. this.results = [];
  92. this.query = {
  93. query: evt ? `${evt.query} ` : '',
  94. parsedQuery: this.queryParser.parse(evt && evt.query),
  95. tags: [],
  96. starred: false,
  97. };
  98. this.currentSearchId = 0;
  99. this.ignoreClose = true;
  100. this.isLoading = true;
  101. if (payload && payload.starred) {
  102. this.query.starred = true;
  103. }
  104. this.$timeout(() => {
  105. this.ignoreClose = false;
  106. this.giveSearchFocus = true;
  107. this.search();
  108. }, 100);
  109. }
  110. onKeyDown(evt: KeyboardEvent) {
  111. if (evt.keyCode === 27) {
  112. this.closeSearch();
  113. }
  114. if (evt.keyCode === 40) {
  115. this.moveSelection(1);
  116. }
  117. if (evt.keyCode === 38) {
  118. this.moveSelection(-1);
  119. }
  120. if (evt.keyCode === 13) {
  121. const flattenedResult = this.getFlattenedResultForNavigation();
  122. const currentItem = flattenedResult[this.selectedIndex];
  123. if (currentItem) {
  124. if (currentItem.dashboardIndex !== undefined) {
  125. const selectedDash = this.results[currentItem.folderIndex].items[currentItem.dashboardIndex];
  126. if (selectedDash) {
  127. this.$location.search({});
  128. this.$location.path(selectedDash.url);
  129. this.closeSearch();
  130. }
  131. } else {
  132. const selectedFolder = this.results[currentItem.folderIndex];
  133. if (selectedFolder) {
  134. selectedFolder.toggle(selectedFolder);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. onFilterboxClick() {
  141. this.giveSearchFocus = false;
  142. this.preventClose();
  143. }
  144. preventClose() {
  145. this.ignoreClose = true;
  146. this.$timeout(() => {
  147. this.ignoreClose = false;
  148. }, 100);
  149. }
  150. moveSelection(direction: number) {
  151. if (this.results.length === 0) {
  152. return;
  153. }
  154. const flattenedResult = this.getFlattenedResultForNavigation();
  155. const currentItem = flattenedResult[this.selectedIndex];
  156. if (currentItem) {
  157. if (currentItem.dashboardIndex !== undefined) {
  158. this.results[currentItem.folderIndex].items[currentItem.dashboardIndex].selected = false;
  159. } else {
  160. this.results[currentItem.folderIndex].selected = false;
  161. }
  162. }
  163. if (direction === 0) {
  164. this.selectedIndex = -1;
  165. return;
  166. }
  167. const max = flattenedResult.length;
  168. const newIndex = (this.selectedIndex + direction) % max;
  169. this.selectedIndex = newIndex < 0 ? newIndex + max : newIndex;
  170. const selectedItem = flattenedResult[this.selectedIndex];
  171. if (selectedItem.dashboardIndex === undefined && this.results[selectedItem.folderIndex].id === 0) {
  172. this.moveSelection(direction);
  173. return;
  174. }
  175. if (selectedItem.dashboardIndex !== undefined) {
  176. if (!this.results[selectedItem.folderIndex].expanded) {
  177. this.moveSelection(direction);
  178. return;
  179. }
  180. this.results[selectedItem.folderIndex].items[selectedItem.dashboardIndex].selected = true;
  181. return;
  182. }
  183. if (this.results[selectedItem.folderIndex].hideHeader) {
  184. this.moveSelection(direction);
  185. return;
  186. }
  187. this.results[selectedItem.folderIndex].selected = true;
  188. }
  189. searchDashboards(folderContext?: string) {
  190. this.currentSearchId = this.currentSearchId + 1;
  191. const localSearchId = this.currentSearchId;
  192. const folderIds = [];
  193. const { parsedQuery } = this.query;
  194. if (folderContext === 'current') {
  195. folderIds.push(getDashboardSrv().getCurrent().meta.folderId);
  196. }
  197. const query = {
  198. ...this.query,
  199. query: parsedQuery.text,
  200. tag: this.query.tags,
  201. folderIds,
  202. };
  203. return this.searchSrv
  204. .search({
  205. ...query,
  206. })
  207. .then(results => {
  208. if (localSearchId < this.currentSearchId) {
  209. return;
  210. }
  211. this.results = results || [];
  212. this.isLoading = false;
  213. this.moveSelection(1);
  214. });
  215. }
  216. queryHasNoFilters() {
  217. const query = this.query;
  218. return query.query === '' && query.starred === false && query.tags.length === 0;
  219. }
  220. filterByTag(tag: string) {
  221. if (_.indexOf(this.query.tags, tag) === -1) {
  222. this.query.tags.push(tag);
  223. this.search();
  224. }
  225. }
  226. removeTag(tag: string, evt: any) {
  227. this.query.tags = _.without(this.query.tags, tag);
  228. this.search();
  229. this.giveSearchFocus = true;
  230. evt.stopPropagation();
  231. evt.preventDefault();
  232. }
  233. getTags = () => {
  234. return this.searchSrv.getDashboardTags();
  235. };
  236. onTagFiltersChanged = (tags: string[]) => {
  237. this.query.tags = tags;
  238. this.search();
  239. };
  240. clearSearchFilter() {
  241. this.query.query = '';
  242. this.query.tags = [];
  243. this.search();
  244. }
  245. showStarred() {
  246. this.query.starred = !this.query.starred;
  247. this.giveSearchFocus = true;
  248. this.search();
  249. }
  250. search() {
  251. this.showImport = false;
  252. this.selectedIndex = -1;
  253. this.searchDashboards(this.query.parsedQuery['folder']);
  254. }
  255. folderExpanding() {
  256. this.moveSelection(0);
  257. }
  258. private getFlattenedResultForNavigation(): SelectedIndicies[] {
  259. let folderIndex = 0;
  260. return _.flatMap(this.results, (s: any) => {
  261. let result: SelectedIndicies[] = [];
  262. result.push({
  263. folderIndex: folderIndex,
  264. });
  265. let dashboardIndex = 0;
  266. result = result.concat(
  267. _.map(s.items || [], i => {
  268. return {
  269. folderIndex: folderIndex,
  270. dashboardIndex: dashboardIndex++,
  271. };
  272. })
  273. );
  274. folderIndex++;
  275. return result;
  276. });
  277. }
  278. }
  279. export function searchDirective() {
  280. return {
  281. restrict: 'E',
  282. templateUrl: 'public/app/core/components/search/search.html',
  283. controller: SearchCtrl,
  284. bindToController: true,
  285. controllerAs: 'ctrl',
  286. scope: {},
  287. };
  288. }
  289. coreModule.directive('dashboardSearch', searchDirective);