diff --git a/public/app/core/components/search/search.ts b/public/app/core/components/search/search.ts index 020a7fe901a..9df363e301b 100644 --- a/public/app/core/components/search/search.ts +++ b/public/app/core/components/search/search.ts @@ -1,6 +1,6 @@ import _ from 'lodash'; import coreModule from '../../core_module'; -import {impressions} from 'app/features/dashboard/impression_store'; +import { SearchSrv } from 'app/core/services/search_srv'; export class SearchCtrl { isOpen: boolean; @@ -17,7 +17,7 @@ export class SearchCtrl { openCompleted: boolean; /** @ngInject */ - constructor($scope, private $location, private $timeout, private backendSrv, public contextSrv, $rootScope) { + 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); } @@ -25,7 +25,6 @@ export class SearchCtrl { closeSearch() { this.isOpen = this.ignoreClose; this.openCompleted = false; - this.contextSrv.isSearching = this.isOpen; } openSearch(evt, payload) { @@ -35,7 +34,6 @@ export class SearchCtrl { } this.isOpen = true; - this.contextSrv.isSearching = true; this.giveSearchFocus = 0; this.selectedIndex = -1; this.results = []; @@ -101,54 +99,9 @@ export class SearchCtrl { this.currentSearchId = this.currentSearchId + 1; var localSearchId = this.currentSearchId; - return this.backendSrv.search(this.query).then(results => { + return this.searchSrv.search(this.query).then(results => { if (localSearchId < this.currentSearchId) { return; } - - 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) { - let section = sections[hit.folderId]; - if (!section) { - section = { - id: hit.folderId, - title: hit.folderTitle, - items: [], - icon: 'fa fa-folder-open' - }; - // handle root - if (!hit.folderId) { - section.title = "Dashboards"; - section.icon = "fa fa-circle-o"; - } - sections[hit.folderId] = section; - } - - hit.url = 'dashboard/' + hit.uri; - section.items.push(hit); - } - - this.results = _.sortBy(_.values(sections), 'score'); + this.results = results; }); } @@ -176,7 +129,7 @@ export class SearchCtrl { } getTags() { - return this.backendSrv.get('/api/dashboards/tags').then((results) => { + return this.searchSrv.getDashboardTags().then((results) => { this.tagsMode = !this.tagsMode; this.results = results; this.giveSearchFocus = this.giveSearchFocus + 1; diff --git a/public/app/core/core.ts b/public/app/core/core.ts index 66d8b8b656d..602cb49cb68 100644 --- a/public/app/core/core.ts +++ b/public/app/core/core.ts @@ -17,6 +17,7 @@ import './utils/outline'; import './components/colorpicker/ColorPicker'; import './components/colorpicker/SeriesColorPicker'; import './components/colorpicker/spectrum_picker'; +import './services/search_srv'; import {grafanaAppDirective} from './components/grafana_app'; import {sideMenuDirective} from './components/sidemenu/sidemenu'; diff --git a/public/app/core/services/search_srv.ts b/public/app/core/services/search_srv.ts new file mode 100644 index 00000000000..ecd5268c6cb --- /dev/null +++ b/public/app/core/services/search_srv.ts @@ -0,0 +1,66 @@ +import _ from 'lodash'; +import coreModule from 'app/core/core_module'; + +export class SearchSrv { + + /** @ngInject */ + constructor(private backendSrv) { + } + + search(query) { + 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) { + let section = sections[hit.folderId]; + if (!section) { + section = { + id: hit.folderId, + title: hit.folderTitle, + items: [], + icon: 'fa fa-folder-open' + }; + // handle root + if (!hit.folderId) { + section.title = "Dashboards"; + section.icon = "fa fa-circle-o"; + } + sections[hit.folderId] = section; + } + + hit.url = 'dashboard/' + hit.uri; + section.items.push(hit); + } + + return _.sortBy(_.values(sections), 'score'); + }); + } + + getDashboardTags() { + return this.backendSrv.get('/api/dashboards/tags'); + } +} + +coreModule.service('searchSrv', SearchSrv); diff --git a/public/app/core/specs/search_srv.jest.ts b/public/app/core/specs/search_srv.jest.ts new file mode 100644 index 00000000000..44561a7fbc1 --- /dev/null +++ b/public/app/core/specs/search_srv.jest.ts @@ -0,0 +1,48 @@ +import { SearchSrv } from 'app/core/services/search_srv'; +import { BackendSrvMock } from 'test/mocks/backend_srv'; + +describe('SearchSrv', () => { + let searchSrv, backendSrvMock; + + beforeEach(() => { + backendSrvMock = new BackendSrvMock(); + searchSrv = new SearchSrv(backendSrvMock); + }); + + describe("with no query string and dashboards with folders returned", () => { + let results; + + beforeEach(() => { + backendSrvMock.search = jest.fn().mockReturnValue(Promise.resolve([ + { + title: 'dash with no folder', + }, + { + title: 'dash in folder1 1', + folderId: 1, + folderTitle: 'folder1' + }, + { + title: 'dash in folder1 2', + folderId: 1, + folderTitle: 'folder1' + }, + { + title: 'dahs in folder2 1', + folderId: 2, + folderTitle: 'folder2' + } + ])); + + return searchSrv.search({query: ''}).then(res => { + results = res; + }); + }); + + it("should create sections for each folder and root", () => { + expect(results).toHaveLength(3); + }); + + }); + +}); diff --git a/public/test/mocks/backend_srv.ts b/public/test/mocks/backend_srv.ts new file mode 100644 index 00000000000..666de593722 --- /dev/null +++ b/public/test/mocks/backend_srv.ts @@ -0,0 +1,8 @@ +export class BackendSrvMock { + search: any; + + constructor() { + } + +} +