search.ts 4.5 KB

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