search.ts 6.4 KB

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