diff --git a/public/app/core/components/navbar/navbar.ts b/public/app/core/components/navbar/navbar.ts index 54f39eaa889..3fc266dccb3 100644 --- a/public/app/core/components/navbar/navbar.ts +++ b/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) { diff --git a/public/app/core/components/search/search.ts b/public/app/core/components/search/search.ts index ffbec30cc0a..e06380a688b 100644 --- a/public/app/core/components/search/search.ts +++ b/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]; diff --git a/public/app/core/components/search/search_results.html b/public/app/core/components/search/search_results.html index a776dabe0dc..489e60ac816 100644 --- a/public/app/core/components/search/search_results.html +++ b/public/app/core/components/search/search_results.html @@ -32,7 +32,7 @@ - +
{{::item.title}}
diff --git a/public/app/core/components/search/search_results.ts b/public/app/core/components/search/search_results.ts index 3e7e8e6147b..8001ac8c644 100644 --- a/public/app/core/components/search/search_results.ts +++ b/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}); diff --git a/public/app/core/services/keybindingSrv.ts b/public/app/core/services/keybindingSrv.ts index 4e819abd1bc..67b766d497a 100644 --- a/public/app/core/services/keybindingSrv.ts +++ b/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() { diff --git a/public/app/core/specs/search.jest.ts b/public/app/core/specs/search.jest.ts index d3058e7550e..b382cccb63e 100644 --- a/public/app/core/specs/search.jest.ts +++ b/public/app/core/specs/search.jest.ts @@ -6,7 +6,7 @@ describe('SearchCtrl', () => { search: (options: any) => {}, getDashboardTags: () => {} }; - let ctrl = new SearchCtrl({}, {}, {}, searchSrvStub, { onAppEvent: () => { } }); + let ctrl = new SearchCtrl({$on: () => {}}, {}, {}, searchSrvStub); describe('Given an empty result', () => { beforeEach(() => { diff --git a/public/app/core/specs/search_results.jest.ts b/public/app/core/specs/search_results.jest.ts index 7084f5e7d8f..e18aa89162d 100644 --- a/public/app/core/specs/search_results.jest.ts +++ b/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() + }; +}); 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(); + }); + }); }); diff --git a/public/app/features/dashboard/dashnav/dashnav.ts b/public/app/features/dashboard/dashnav/dashnav.ts index afb7155bb55..b9523efcbdc 100644 --- a/public/app/features/dashboard/dashnav/dashnav.ts +++ b/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() { diff --git a/public/app/features/org/teams_ctrl.ts b/public/app/features/org/teams_ctrl.ts index d02f3663949..7a62c80ce0b 100644 --- a/public/app/features/org/teams_ctrl.ts +++ b/public/app/features/org/teams_ctrl.ts @@ -1,7 +1,7 @@ /// -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;