search.ts 3.6 KB

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