search.ts 6.3 KB

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