mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
improved search srv
This commit is contained in:
parent
6c5e19c1e0
commit
9ace21b689
@ -56,7 +56,7 @@
|
||||
<h6 ng-hide="ctrl.results.length">No dashboards matching your query were found.</h6>
|
||||
|
||||
<div ng-repeat="section in ctrl.results" class="search-section">
|
||||
<a class="search-section__header pointer" ng-show="::section.title" ng-click="section.collapsed = !section.collapsed">
|
||||
<a class="search-section__header pointer" ng-show="::section.title" ng-click="ctrl.toggleFolder(section)">
|
||||
<i class="search-section__header__icon" ng-class="section.icon"></i>
|
||||
<span class="search-section__header__text">{{::section.title}}</span>
|
||||
<i class="fa fa-minus search-section__header__toggle" ng-hide="section.collapsed"></i>
|
||||
|
@ -150,6 +150,10 @@ export class SearchCtrl {
|
||||
this.selectedIndex = 0;
|
||||
this.searchDashboards();
|
||||
}
|
||||
|
||||
// toggleFolder(section) {
|
||||
// this.searchSrv.toggleFolder(section);
|
||||
// }
|
||||
}
|
||||
|
||||
export function searchDirective() {
|
||||
|
@ -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');
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user