search.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /** @ngInject */
  20. constructor(private $scope, private $location, private $timeout, private backendSrv, private contextSrv, private $rootScope) {
  21. $rootScope.onAppEvent('show-dash-search', this.openSearch.bind(this), $scope);
  22. $rootScope.onAppEvent('hide-dash-search', this.closeSearch.bind(this), $scope);
  23. }
  24. closeSearch() {
  25. this.isOpen = this.ignoreClose;
  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.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. if (this.$scope.tagMode) {
  67. var tag = this.results[this.selectedIndex];
  68. if (tag) {
  69. this.filterByTag(tag.term, null);
  70. }
  71. return;
  72. }
  73. var selectedDash = this.results[this.selectedIndex];
  74. if (selectedDash) {
  75. this.$location.search({});
  76. this.$location.path(selectedDash.url);
  77. }
  78. }
  79. }
  80. moveSelection(direction) {
  81. var max = (this.results || []).length;
  82. var newIndex = this.selectedIndex + direction;
  83. this.selectedIndex = ((newIndex %= max) < 0) ? newIndex + max : newIndex;
  84. }
  85. searchDashboards() {
  86. this.tagsMode = false;
  87. this.currentSearchId = this.currentSearchId + 1;
  88. var localSearchId = this.currentSearchId;
  89. return this.backendSrv.search(this.query).then((results) => {
  90. if (localSearchId < this.currentSearchId) { return; }
  91. this.results = _.map(results, function(dash) {
  92. dash.url = 'dashboard/' + dash.uri;
  93. return dash;
  94. });
  95. if (this.queryHasNoFilters()) {
  96. this.results.unshift({ title: 'Home', url: config.appSubUrl + '/', type: 'dash-home' });
  97. }
  98. });
  99. }
  100. queryHasNoFilters() {
  101. var query = this.query;
  102. return query.query === '' && query.starred === false && query.tag.length === 0;
  103. };
  104. filterByTag(tag, evt) {
  105. this.query.tag.push(tag);
  106. this.search();
  107. this.giveSearchFocus = this.giveSearchFocus + 1;
  108. if (evt) {
  109. evt.stopPropagation();
  110. evt.preventDefault();
  111. }
  112. };
  113. removeTag(tag, evt) {
  114. this.query.tag = _.without(this.query.tag, tag);
  115. this.search();
  116. this.giveSearchFocus = this.giveSearchFocus + 1;
  117. evt.stopPropagation();
  118. evt.preventDefault();
  119. };
  120. getTags() {
  121. return this.backendSrv.get('/api/dashboards/tags').then((results) => {
  122. this.tagsMode = !this.tagsMode;
  123. this.results = results;
  124. this.giveSearchFocus = this.giveSearchFocus + 1;
  125. if ( !this.tagsMode ) {
  126. this.search();
  127. }
  128. });
  129. };
  130. showStarred() {
  131. this.query.starred = !this.query.starred;
  132. this.giveSearchFocus = this.giveSearchFocus + 1;
  133. this.search();
  134. };
  135. search() {
  136. this.showImport = false;
  137. this.selectedIndex = 0;
  138. this.searchDashboards();
  139. };
  140. }
  141. export function searchDirective() {
  142. return {
  143. restrict: 'E',
  144. templateUrl: 'public/app/core/components/search/search.html',
  145. controller: SearchCtrl,
  146. bindToController: true,
  147. controllerAs: 'ctrl',
  148. };
  149. }
  150. coreModule.directive('dashboardSearch', searchDirective);