Selaa lähdekoodia

search: closes dash search when selecting current dashboard (#10285)

Fixes #10231.
Daniel Lee 8 vuotta sitten
vanhempi
commit
6ad06364c7

+ 3 - 3
public/app/core/components/navbar/navbar.ts

@@ -1,15 +1,15 @@
 import coreModule from '../../core_module';
 import {NavModel}  from '../../nav_model_srv';
+import appEvents from 'app/core/app_events';
 
 export class NavbarCtrl {
   model: NavModel;
 
   /** @ngInject */
-  constructor(private $rootScope) {
-  }
+  constructor() {}
 
   showSearch() {
-    this.$rootScope.appEvent('show-dash-search');
+    appEvents.emit('show-dash-search');
   }
 
   navItemClicked(navItem, evt) {

+ 5 - 3
public/app/core/components/search/search.ts

@@ -1,6 +1,7 @@
 import _ from 'lodash';
 import coreModule from '../../core_module';
 import { SearchSrv } from 'app/core/services/search_srv';
+import appEvents from 'app/core/app_events';
 
 export class SearchCtrl {
   isOpen: boolean;
@@ -16,9 +17,9 @@ export class SearchCtrl {
   initialFolderFilterTitle: string;
 
   /** @ngInject */
-  constructor($scope, private $location, private $timeout, private searchSrv: SearchSrv, $rootScope) {
-    $rootScope.onAppEvent('show-dash-search', this.openSearch.bind(this), $scope);
-    $rootScope.onAppEvent('hide-dash-search', this.closeSearch.bind(this), $scope);
+  constructor($scope, private $location, private $timeout, private searchSrv: SearchSrv) {
+    appEvents.on('show-dash-search', this.openSearch.bind(this), $scope);
+    appEvents.on('hide-dash-search', this.closeSearch.bind(this), $scope);
 
     this.initialFolderFilterTitle = "All";
   }
@@ -74,6 +75,7 @@ export class SearchCtrl {
           if (selectedDash) {
             this.$location.search({});
             this.$location.path(selectedDash.url);
+            this.closeSearch();
           }
         } else {
           const selectedFolder = this.results[currentItem.folderIndex];

+ 1 - 1
public/app/core/components/search/search_results.html

@@ -32,7 +32,7 @@
       <span class="search-item__icon">
         <i class="gicon mini gicon-dashboard-list"></i>
       </span>
-      <span class="search-item__body">
+      <span class="search-item__body" ng-click="ctrl.onItemClick(item)">
         <div class="search-item__body-title">{{::item.title}}</div>
       </span>
       <span class="search-item__tags">

+ 7 - 0
public/app/core/components/search/search_results.ts

@@ -1,5 +1,6 @@
 import _ from 'lodash';
 import coreModule from '../../core_module';
+import appEvents from 'app/core/app_events';
 
 export class SearchResultsCtrl {
   results: any;
@@ -61,6 +62,12 @@ export class SearchResultsCtrl {
     }
   }
 
+  onItemClick(item) {
+    if (this.$location.path().indexOf(item.url) > -1) {
+      appEvents.emit('hide-dash-search');
+    }
+  }
+
   selectTag(tag, evt) {
     if (this.onTagSelected) {
       this.onTagSelected({$tag: tag});

+ 3 - 3
public/app/core/services/keybindingSrv.ts

@@ -36,15 +36,15 @@ export class KeybindingSrv {
   }
 
   openSearchStarred() {
-    this.$rootScope.appEvent('show-dash-search', {starred: true});
+    appEvents.emit('show-dash-search', {starred: true});
   }
 
   openSearchTags() {
-    this.$rootScope.appEvent('show-dash-search', {tagsMode: true});
+    appEvents.emit('show-dash-search', {tagsMode: true});
   }
 
   openSearch() {
-    this.$rootScope.appEvent('show-dash-search');
+    appEvents.emit('show-dash-search');
   }
 
   openAlerting() {

+ 1 - 1
public/app/core/specs/search.jest.ts

@@ -6,7 +6,7 @@ describe('SearchCtrl', () => {
     search: (options: any) => {},
     getDashboardTags: () => {}
   };
-  let ctrl = new SearchCtrl({}, {}, {}, <SearchSrv>searchSrvStub, { onAppEvent: () => { } });
+  let ctrl = new SearchCtrl({$on: () => {}}, {}, {}, <SearchSrv>searchSrvStub);
 
   describe('Given an empty result', () => {
     beforeEach(() => {

+ 43 - 0
public/app/core/specs/search_results.jest.ts

@@ -1,4 +1,12 @@
 import { SearchResultsCtrl } from '../components/search/search_results';
+import { beforeEach, afterEach } from 'test/lib/common';
+import appEvents from 'app/core/app_events';
+
+jest.mock('app/core/app_events', () => {
+  return {
+    emit: jest.fn<any>()
+  };
+});
 
 describe('SearchResultsCtrl', () => {
   let ctrl;
@@ -94,4 +102,39 @@ describe('SearchResultsCtrl', () => {
       expect(folderExpanded).toBeFalsy();
     });
   });
+
+  describe('when clicking on a link in search result', () => {
+    const dashPath = 'dashboard/path';
+    const $location = { path: () =>  dashPath};
+    const appEventsMock = appEvents as any;
+
+    describe('with the same url as current path', () => {
+      beforeEach(() => {
+        ctrl = new SearchResultsCtrl($location);
+        const item = { url: dashPath};
+        ctrl.onItemClick(item);
+      });
+
+      it('should close the search', () => {
+        expect(appEventsMock.emit.mock.calls.length).toBe(1);
+        expect(appEventsMock.emit.mock.calls[0][0]).toBe('hide-dash-search');
+      });
+    });
+
+    describe('with a different url than current path', () => {
+      beforeEach(() => {
+        ctrl = new SearchResultsCtrl($location);
+        const item = { url: 'another/path'};
+        ctrl.onItemClick(item);
+      });
+
+      it('should do nothing', () => {
+        expect(appEventsMock.emit.mock.calls.length).toBe(0);
+      });
+    });
+
+    afterEach(() => {
+      appEventsMock.emit.mockClear();
+    });
+  });
 });

+ 1 - 2
public/app/features/dashboard/dashnav/dashnav.ts

@@ -11,7 +11,6 @@ export class DashNavCtrl {
   /** @ngInject */
   constructor(
     private $scope,
-    private $rootScope,
     private dashboardSrv,
     private $location,
     public playlistSrv) {
@@ -75,7 +74,7 @@ export class DashNavCtrl {
     }
 
     showSearch() {
-      this.$rootScope.appEvent('show-dash-search');
+      appEvents.emit('show-dash-search');
     }
 
     addPanel() {

+ 2 - 2
public/app/features/org/teams_ctrl.ts

@@ -1,7 +1,7 @@
 ///<reference path="../../headers/common.d.ts" />
 
-import coreModule from "app/core/core_module";
-import { appEvents } from "app/core/core";
+import coreModule from 'app/core/core_module';
+import appEvents from 'app/core/app_events';
 
 export class TeamsCtrl {
   teams: any;