Torkel Ödegaard 8 лет назад
Родитель
Сommit
9ace21b689

+ 1 - 1
public/app/core/components/search/search.html

@@ -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>

+ 4 - 0
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() {

+ 32 - 29
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');
   }

+ 55 - 10
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);
     });
 
   });