search.ts 4.2 KB

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