Commit 6ad06364 by Daniel Lee Committed by GitHub

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

Fixes #10231.
parent 0bc6f4e2
import coreModule from '../../core_module'; import coreModule from '../../core_module';
import {NavModel} from '../../nav_model_srv'; import {NavModel} from '../../nav_model_srv';
import appEvents from 'app/core/app_events';
export class NavbarCtrl { export class NavbarCtrl {
model: NavModel; model: NavModel;
/** @ngInject */ /** @ngInject */
constructor(private $rootScope) { constructor() {}
}
showSearch() { showSearch() {
this.$rootScope.appEvent('show-dash-search'); appEvents.emit('show-dash-search');
} }
navItemClicked(navItem, evt) { navItemClicked(navItem, evt) {
......
import _ from 'lodash'; import _ from 'lodash';
import coreModule from '../../core_module'; import coreModule from '../../core_module';
import { SearchSrv } from 'app/core/services/search_srv'; import { SearchSrv } from 'app/core/services/search_srv';
import appEvents from 'app/core/app_events';
export class SearchCtrl { export class SearchCtrl {
isOpen: boolean; isOpen: boolean;
...@@ -16,9 +17,9 @@ export class SearchCtrl { ...@@ -16,9 +17,9 @@ export class SearchCtrl {
initialFolderFilterTitle: string; initialFolderFilterTitle: string;
/** @ngInject */ /** @ngInject */
constructor($scope, private $location, private $timeout, private searchSrv: SearchSrv, $rootScope) { constructor($scope, private $location, private $timeout, private searchSrv: SearchSrv) {
$rootScope.onAppEvent('show-dash-search', this.openSearch.bind(this), $scope); appEvents.on('show-dash-search', this.openSearch.bind(this), $scope);
$rootScope.onAppEvent('hide-dash-search', this.closeSearch.bind(this), $scope); appEvents.on('hide-dash-search', this.closeSearch.bind(this), $scope);
this.initialFolderFilterTitle = "All"; this.initialFolderFilterTitle = "All";
} }
...@@ -74,6 +75,7 @@ export class SearchCtrl { ...@@ -74,6 +75,7 @@ export class SearchCtrl {
if (selectedDash) { if (selectedDash) {
this.$location.search({}); this.$location.search({});
this.$location.path(selectedDash.url); this.$location.path(selectedDash.url);
this.closeSearch();
} }
} else { } else {
const selectedFolder = this.results[currentItem.folderIndex]; const selectedFolder = this.results[currentItem.folderIndex];
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
<span class="search-item__icon"> <span class="search-item__icon">
<i class="gicon mini gicon-dashboard-list"></i> <i class="gicon mini gicon-dashboard-list"></i>
</span> </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> <div class="search-item__body-title">{{::item.title}}</div>
</span> </span>
<span class="search-item__tags"> <span class="search-item__tags">
......
import _ from 'lodash'; import _ from 'lodash';
import coreModule from '../../core_module'; import coreModule from '../../core_module';
import appEvents from 'app/core/app_events';
export class SearchResultsCtrl { export class SearchResultsCtrl {
results: any; results: any;
...@@ -61,6 +62,12 @@ export class SearchResultsCtrl { ...@@ -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) { selectTag(tag, evt) {
if (this.onTagSelected) { if (this.onTagSelected) {
this.onTagSelected({$tag: tag}); this.onTagSelected({$tag: tag});
......
...@@ -36,15 +36,15 @@ export class KeybindingSrv { ...@@ -36,15 +36,15 @@ export class KeybindingSrv {
} }
openSearchStarred() { openSearchStarred() {
this.$rootScope.appEvent('show-dash-search', {starred: true}); appEvents.emit('show-dash-search', {starred: true});
} }
openSearchTags() { openSearchTags() {
this.$rootScope.appEvent('show-dash-search', {tagsMode: true}); appEvents.emit('show-dash-search', {tagsMode: true});
} }
openSearch() { openSearch() {
this.$rootScope.appEvent('show-dash-search'); appEvents.emit('show-dash-search');
} }
openAlerting() { openAlerting() {
......
...@@ -6,7 +6,7 @@ describe('SearchCtrl', () => { ...@@ -6,7 +6,7 @@ describe('SearchCtrl', () => {
search: (options: any) => {}, search: (options: any) => {},
getDashboardTags: () => {} getDashboardTags: () => {}
}; };
let ctrl = new SearchCtrl({}, {}, {}, <SearchSrv>searchSrvStub, { onAppEvent: () => { } }); let ctrl = new SearchCtrl({$on: () => {}}, {}, {}, <SearchSrv>searchSrvStub);
describe('Given an empty result', () => { describe('Given an empty result', () => {
beforeEach(() => { beforeEach(() => {
......
import { SearchResultsCtrl } from '../components/search/search_results'; 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', () => { describe('SearchResultsCtrl', () => {
let ctrl; let ctrl;
...@@ -94,4 +102,39 @@ describe('SearchResultsCtrl', () => { ...@@ -94,4 +102,39 @@ describe('SearchResultsCtrl', () => {
expect(folderExpanded).toBeFalsy(); 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();
});
});
}); });
...@@ -11,7 +11,6 @@ export class DashNavCtrl { ...@@ -11,7 +11,6 @@ export class DashNavCtrl {
/** @ngInject */ /** @ngInject */
constructor( constructor(
private $scope, private $scope,
private $rootScope,
private dashboardSrv, private dashboardSrv,
private $location, private $location,
public playlistSrv) { public playlistSrv) {
...@@ -75,7 +74,7 @@ export class DashNavCtrl { ...@@ -75,7 +74,7 @@ export class DashNavCtrl {
} }
showSearch() { showSearch() {
this.$rootScope.appEvent('show-dash-search'); appEvents.emit('show-dash-search');
} }
addPanel() { addPanel() {
......
///<reference path="../../headers/common.d.ts" /> ///<reference path="../../headers/common.d.ts" />
import coreModule from "app/core/core_module"; import coreModule from 'app/core/core_module';
import { appEvents } from "app/core/core"; import appEvents from 'app/core/app_events';
export class TeamsCtrl { export class TeamsCtrl {
teams: any; teams: any;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment