search_srv.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import _ from 'lodash';
  2. import coreModule from 'app/core/core_module';
  3. export class SearchSrv {
  4. /** @ngInject */
  5. constructor(private backendSrv) {
  6. }
  7. browse() {
  8. let query = {
  9. folderIds: [0]
  10. };
  11. return this.backendSrv.search(query).then(results => {
  12. let sections: any = {};
  13. for (let hit of results) {
  14. if (hit.type === 'dash-folder') {
  15. sections[hit.id] = {
  16. id: hit.id,
  17. title: hit.title,
  18. items: [],
  19. icon: 'fa fa-folder',
  20. score: _.keys(sections).length,
  21. };
  22. }
  23. }
  24. sections[0] = {
  25. id: 0,
  26. title: 'Root',
  27. items: [],
  28. icon: 'fa fa-folder-open',
  29. score: _.keys(sections).length,
  30. expanded: true,
  31. };
  32. for (let hit of results) {
  33. if (hit.type === 'dash-folder') {
  34. continue;
  35. }
  36. let section = sections[hit.folderId || 0];
  37. hit.url = 'dashboard/' + hit.uri;
  38. section.items.push(hit);
  39. }
  40. return _.sortBy(_.values(sections), 'score');
  41. });
  42. }
  43. search(options) {
  44. if (!options.query) {
  45. return this.browse();
  46. }
  47. let query = _.clone(options);
  48. query.folderIds = [];
  49. query.type = 'dash-db';
  50. return this.backendSrv.search(query).then(results => {
  51. let section = {
  52. hideHeader: true,
  53. items: [],
  54. expanded: true,
  55. };
  56. for (let hit of results) {
  57. if (hit.type === 'dash-folder') {
  58. continue;
  59. }
  60. hit.url = 'dashboard/' + hit.uri;
  61. section.items.push(hit);
  62. }
  63. return [section];
  64. });
  65. }
  66. toggleFolder(section) {
  67. section.expanded = !section.expanded;
  68. section.icon = section.expanded ? 'fa fa-folder-open' : 'fa fa-folder';
  69. if (section.items.length) {
  70. return;
  71. }
  72. let query = {
  73. folderIds: [section.id]
  74. };
  75. return this.backendSrv.search(query).then(results => {
  76. for (let hit of results) {
  77. hit.url = 'dashboard/' + hit.uri;
  78. section.items.push(hit);
  79. }
  80. });
  81. }
  82. getDashboardTags() {
  83. return this.backendSrv.get('/api/dashboards/tags');
  84. }
  85. }
  86. coreModule.service('searchSrv', SearchSrv);