module.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import _ from 'lodash';
  2. import { PanelCtrl } from 'app/plugins/sdk';
  3. import impressionSrv from 'app/core/services/impression_srv';
  4. import { auto } from 'angular';
  5. import { BackendSrv } from 'app/core/services/backend_srv';
  6. import { DashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
  7. class DashListCtrl extends PanelCtrl {
  8. static templateUrl = 'module.html';
  9. static scrollable = true;
  10. groups: any[];
  11. modes: any[];
  12. panelDefaults: any = {
  13. query: '',
  14. limit: 10,
  15. tags: [],
  16. recent: false,
  17. search: false,
  18. starred: true,
  19. headings: true,
  20. folderId: null,
  21. };
  22. /** @ngInject */
  23. constructor(
  24. $scope: any,
  25. $injector: auto.IInjectorService,
  26. private backendSrv: BackendSrv,
  27. private dashboardSrv: DashboardSrv
  28. ) {
  29. super($scope, $injector);
  30. _.defaults(this.panel, this.panelDefaults);
  31. if (this.panel.tag) {
  32. this.panel.tags = [this.panel.tag];
  33. delete this.panel.tag;
  34. }
  35. this.events.on('refresh', this.onRefresh.bind(this));
  36. this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
  37. this.groups = [
  38. { list: [], show: false, header: 'Starred dashboards' },
  39. { list: [], show: false, header: 'Recently viewed dashboards' },
  40. { list: [], show: false, header: 'Search' },
  41. ];
  42. // update capability
  43. if (this.panel.mode) {
  44. if (this.panel.mode === 'starred') {
  45. this.panel.starred = true;
  46. this.panel.headings = false;
  47. }
  48. if (this.panel.mode === 'recently viewed') {
  49. this.panel.recent = true;
  50. this.panel.starred = false;
  51. this.panel.headings = false;
  52. }
  53. if (this.panel.mode === 'search') {
  54. this.panel.search = true;
  55. this.panel.starred = false;
  56. this.panel.headings = false;
  57. }
  58. delete this.panel.mode;
  59. }
  60. }
  61. onInitEditMode() {
  62. this.modes = ['starred', 'search', 'recently viewed'];
  63. this.addEditorTab('Options', 'public/app/plugins/panel/dashlist/editor.html');
  64. }
  65. onRefresh() {
  66. const promises = [];
  67. promises.push(this.getRecentDashboards());
  68. promises.push(this.getStarred());
  69. promises.push(this.getSearch());
  70. return Promise.all(promises).then(this.renderingCompleted.bind(this));
  71. }
  72. getSearch() {
  73. this.groups[2].show = this.panel.search;
  74. if (!this.panel.search) {
  75. return Promise.resolve();
  76. }
  77. const params = {
  78. limit: this.panel.limit,
  79. query: this.panel.query,
  80. tag: this.panel.tags,
  81. folderIds: this.panel.folderId,
  82. type: 'dash-db',
  83. };
  84. return this.backendSrv.search(params).then(result => {
  85. this.groups[2].list = result;
  86. });
  87. }
  88. getStarred() {
  89. this.groups[0].show = this.panel.starred;
  90. if (!this.panel.starred) {
  91. return Promise.resolve();
  92. }
  93. const params = { limit: this.panel.limit, starred: 'true' };
  94. return this.backendSrv.search(params).then(result => {
  95. this.groups[0].list = result;
  96. });
  97. }
  98. starDashboard(dash: any, evt: any) {
  99. this.dashboardSrv.starDashboard(dash.id, dash.isStarred).then((newState: any) => {
  100. dash.isStarred = newState;
  101. });
  102. if (evt) {
  103. evt.stopPropagation();
  104. evt.preventDefault();
  105. }
  106. }
  107. getRecentDashboards() {
  108. this.groups[1].show = this.panel.recent;
  109. if (!this.panel.recent) {
  110. return Promise.resolve();
  111. }
  112. const dashIds = _.take(impressionSrv.getDashboardOpened(), this.panel.limit);
  113. return this.backendSrv.search({ dashboardIds: dashIds, limit: this.panel.limit }).then(result => {
  114. this.groups[1].list = dashIds
  115. .map(orderId => {
  116. return _.find(result, dashboard => {
  117. return dashboard.id === orderId;
  118. });
  119. })
  120. .filter(el => {
  121. return el !== undefined;
  122. });
  123. });
  124. }
  125. onFolderChange(folder: any) {
  126. this.panel.folderId = folder.id;
  127. this.refresh();
  128. }
  129. }
  130. export { DashListCtrl, DashListCtrl as PanelCtrl };