search.ts 8.2 KB

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