diff --git a/public/app/core/components/search/search.html b/public/app/core/components/search/search.html index ba49fa6851f..00ee6cc9b20 100644 --- a/public/app/core/components/search/search.html +++ b/public/app/core/components/search/search.html @@ -56,7 +56,7 @@
No dashboards matching your query were found.
- + {{::section.title}} diff --git a/public/app/core/components/search/search.ts b/public/app/core/components/search/search.ts index 443a3b1aa3a..2bf19b75108 100644 --- a/public/app/core/components/search/search.ts +++ b/public/app/core/components/search/search.ts @@ -150,6 +150,10 @@ export class SearchCtrl { this.selectedIndex = 0; this.searchDashboards(); } + + // toggleFolder(section) { + // this.searchSrv.toggleFolder(section); + // } } export function searchDirective() { diff --git a/public/app/core/services/search_srv.ts b/public/app/core/services/search_srv.ts index a120dac84f9..f282ee57f34 100644 --- a/public/app/core/services/search_srv.ts +++ b/public/app/core/services/search_srv.ts @@ -7,39 +7,15 @@ export class SearchSrv { constructor(private backendSrv) { } - search(options) { - if (!options.query) { - options.folderIds = [0]; - } else { - options.folderIds = []; - options.type = 'dash-db'; - } + browse() { + let query = { + folderIds: [0] + }; - return this.backendSrv.search(options).then(results => { + return this.backendSrv.search(query).then(results => { let sections: any = {}; - // sections["starred"] = { - // score: 0, - // icon: 'fa fa-star-o', - // title: "Starred dashboards", - // items: [ - // {title: 'Frontend Nginx'}, - // {title: 'Cassandra overview'} - // ] - // }; - // - // sections["recent"] = { - // score: 1, - // icon: 'fa fa-clock-o', - // title: "Recent dashboards", - // items: [ - // {title: 'Frontend Nginx'}, - // {title: 'Cassandra overview'} - // ] - // }; - - // create folder index for (let hit of results) { if (hit.type === 'dash-folder') { sections[hit.id] = { @@ -73,6 +49,33 @@ export class SearchSrv { }); } + search(options) { + if (!options.query) { + return this.browse(); + } + + options.folderIds = []; + options.type = 'dash-db'; + + return this.backendSrv.search(options).then(results => { + + let section = { + hideHeader: true, + items: [], + }; + + for (let hit of results) { + if (hit.type === 'dash-folder') { + continue; + } + hit.url = 'dashboard/' + hit.uri; + section.items.push(hit); + } + + return [section]; + }); + } + getDashboardTags() { return this.backendSrv.get('/api/dashboards/tags'); } diff --git a/public/app/core/specs/search_srv.jest.ts b/public/app/core/specs/search_srv.jest.ts index 44561a7fbc1..0e14aad8dae 100644 --- a/public/app/core/specs/search_srv.jest.ts +++ b/public/app/core/specs/search_srv.jest.ts @@ -14,24 +14,28 @@ describe('SearchSrv', () => { beforeEach(() => { backendSrvMock.search = jest.fn().mockReturnValue(Promise.resolve([ + { + title: 'folder1', + type: 'dash-folder', + id: 1, + }, { title: 'dash with no folder', + type: 'dash-db', + id: 2, }, { title: 'dash in folder1 1', - folderId: 1, - folderTitle: 'folder1' + type: 'dash-db', + id: 3, + folderId: 1 }, { title: 'dash in folder1 2', - folderId: 1, - folderTitle: 'folder1' + type: 'dash-db', + id: 4, + folderId: 1 }, - { - title: 'dahs in folder2 1', - folderId: 2, - folderTitle: 'folder2' - } ])); return searchSrv.search({query: ''}).then(res => { @@ -40,7 +44,48 @@ describe('SearchSrv', () => { }); it("should create sections for each folder and root", () => { - expect(results).toHaveLength(3); + expect(results).toHaveLength(2); + }); + + it('should place folders first', () => { + expect(results[0].title).toBe('folder1'); + }); + + }); + + describe("with query string and dashboards with folders returned", () => { + let results; + + beforeEach(() => { + backendSrvMock.search = jest.fn(); + + backendSrvMock.search.mockReturnValue(Promise.resolve([ + { + id: 2, + title: 'dash with no folder', + type: 'dash-db', + }, + { + id: 3, + title: 'dash in folder1 1', + type: 'dash-db', + folderId: 1, + folderTitle: 'folder1', + }, + ])); + + return searchSrv.search({query: 'search'}).then(res => { + results = res; + }); + }); + + it("should not specify folder ids", () => { + expect(backendSrvMock.search.mock.calls[0][0].folderIds).toHaveLength(0); + }); + + it('should place all results in a single section', () => { + expect(results).toHaveLength(1); + expect(results[0].hideHeader).toBe(true); }); });