search.ts 4.0 KB

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