search.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import config from 'app/core/config';
  4. import _ from 'lodash';
  5. import $ from 'jquery';
  6. import coreModule from '../../core_module';
  7. import appEvents from 'app/core/app_events';
  8. export class SearchCtrl {
  9. isOpen: boolean;
  10. query: any;
  11. giveSearchFocus: number;
  12. selectedIndex: number;
  13. results: any;
  14. currentSearchId: number;
  15. tagsMode: boolean;
  16. showImport: boolean;
  17. dismiss: any;
  18. ignoreClose: any;
  19. // triggers fade animation class
  20. openCompleted: boolean;
  21. /** @ngInject */
  22. constructor(private $scope, private $location, private $timeout, private backendSrv, private contextSrv, private $rootScope) {
  23. $rootScope.onAppEvent('show-dash-search', this.openSearch.bind(this), $scope);
  24. $rootScope.onAppEvent('hide-dash-search', this.closeSearch.bind(this), $scope);
  25. }
  26. closeSearch() {
  27. this.isOpen = this.ignoreClose;
  28. this.openCompleted = false;
  29. }
  30. openSearch(evt, payload) {
  31. if (this.isOpen) {
  32. this.isOpen = false;
  33. return;
  34. }
  35. this.isOpen = true;
  36. this.giveSearchFocus = 0;
  37. this.selectedIndex = -1;
  38. this.results = [];
  39. this.query = { query: '', tag: [], starred: false, mode: 'tree' };
  40. this.currentSearchId = 0;
  41. this.ignoreClose = true;
  42. if (payload && payload.starred) {
  43. this.query.starred = true;
  44. }
  45. if (payload && payload.tagsMode) {
  46. return this.$timeout(() => {
  47. this.ignoreClose = false;
  48. this.giveSearchFocus = this.giveSearchFocus + 1;
  49. this.getTags();
  50. }, 100);
  51. }
  52. this.$timeout(() => {
  53. this.openCompleted = true;
  54. this.ignoreClose = false;
  55. this.giveSearchFocus = this.giveSearchFocus + 1;
  56. this.search();
  57. }, 100);
  58. }
  59. keyDown(evt) {
  60. if (evt.keyCode === 27) {
  61. this.closeSearch();
  62. }
  63. if (evt.keyCode === 40) {
  64. this.moveSelection(1);
  65. }
  66. if (evt.keyCode === 38) {
  67. this.moveSelection(-1);
  68. }
  69. if (evt.keyCode === 13) {
  70. if (this.$scope.tagMode) {
  71. var tag = this.results[this.selectedIndex];
  72. if (tag) {
  73. this.filterByTag(tag.term, null);
  74. }
  75. return;
  76. }
  77. var selectedDash = this.results[this.selectedIndex];
  78. if (selectedDash) {
  79. this.$location.search({});
  80. this.$location.path(selectedDash.url);
  81. }
  82. }
  83. }
  84. moveSelection(direction) {
  85. var max = (this.results || []).length;
  86. var newIndex = this.selectedIndex + direction;
  87. this.selectedIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  88. }
  89. searchDashboards() {
  90. this.tagsMode = false;
  91. this.currentSearchId = this.currentSearchId + 1;
  92. var localSearchId = this.currentSearchId;
  93. return this.backendSrv.search(this.query).then((results) => {
  94. if (localSearchId < this.currentSearchId) { return; }
  95. this.results = _.map(results, function(dash) {
  96. dash.url = 'dashboard/' + dash.uri;
  97. return dash;
  98. });
  99. if (this.queryHasNoFilters()) {
  100. this.results.unshift({ title: 'Home', url: config.appSubUrl + '/', type: 'dash-home' });
  101. }
  102. });
  103. }
  104. queryHasNoFilters() {
  105. var query = this.query;
  106. return query.query === '' && query.starred === false && query.tag.length === 0;
  107. }
  108. filterByTag(tag, evt) {
  109. this.query.tag.push(tag);
  110. this.search();
  111. this.giveSearchFocus = this.giveSearchFocus + 1;
  112. if (evt) {
  113. evt.stopPropagation();
  114. evt.preventDefault();
  115. }
  116. }
  117. removeTag(tag, evt) {
  118. this.query.tag = _.without(this.query.tag, tag);
  119. this.search();
  120. this.giveSearchFocus = this.giveSearchFocus + 1;
  121. evt.stopPropagation();
  122. evt.preventDefault();
  123. }
  124. getTags() {
  125. return this.backendSrv.get('/api/dashboards/tags').then((results) => {
  126. this.tagsMode = !this.tagsMode;
  127. this.results = results;
  128. this.giveSearchFocus = this.giveSearchFocus + 1;
  129. if ( !this.tagsMode ) {
  130. this.search();
  131. }
  132. });
  133. }
  134. showStarred() {
  135. this.query.starred = !this.query.starred;
  136. this.giveSearchFocus = this.giveSearchFocus + 1;
  137. this.search();
  138. }
  139. showNewFolderModal() {
  140. this.$scope.appEvent('show-modal', {
  141. templateHtml: '<folder-modal></folder-modal>',
  142. modalClass: 'modal--narrow'
  143. });
  144. }
  145. search() {
  146. this.showImport = false;
  147. this.selectedIndex = 0;
  148. this.searchDashboards();
  149. }
  150. }
  151. export function searchDirective() {
  152. return {
  153. restrict: 'E',
  154. templateUrl: 'public/app/core/components/search/search.html',
  155. controller: SearchCtrl,
  156. bindToController: true,
  157. controllerAs: 'ctrl',
  158. scope: {},
  159. };
  160. }
  161. coreModule.directive('dashboardSearch', searchDirective);