module.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import {PanelCtrl} from 'app/plugins/sdk';
  5. import {impressions} from 'app/features/dashboard/impression_store';
  6. class DashListCtrl extends PanelCtrl {
  7. static templateUrl = 'module.html';
  8. groups: any[];
  9. modes: any[];
  10. panelDefaults = {
  11. query: '',
  12. limit: 10,
  13. tags: [],
  14. recent: false,
  15. search: false,
  16. starred: true,
  17. headings: true,
  18. };
  19. /** @ngInject */
  20. constructor($scope, $injector, private backendSrv) {
  21. super($scope, $injector);
  22. _.defaults(this.panel, this.panelDefaults);
  23. if (this.panel.tag) {
  24. this.panel.tags = [this.panel.tag];
  25. delete this.panel.tag;
  26. }
  27. this.events.on('refresh', this.onRefresh.bind(this));
  28. this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
  29. this.groups = [
  30. {list: [], show: false, header: "Starred dashboards",},
  31. {list: [], show: false, header: "Recently viewed dashboards"},
  32. {list: [], show: false, header: "Search"},
  33. ];
  34. // update capability
  35. if (this.panel.mode) {
  36. if (this.panel.mode === 'starred') {
  37. this.panel.starred = true;
  38. this.panel.headings = false;
  39. }
  40. if (this.panel.mode === 'recently viewed') {
  41. this.panel.recent = true;
  42. this.panel.starred = false;
  43. this.panel.headings = false;
  44. }
  45. if (this.panel.mode === 'search') {
  46. this.panel.search = true;
  47. this.panel.starred = false;
  48. this.panel.headings = false;
  49. }
  50. delete this.panel.mode;
  51. }
  52. }
  53. onInitEditMode() {
  54. this.editorTabIndex = 1;
  55. this.modes = ['starred', 'search', 'recently viewed'];
  56. this.addEditorTab('Options', 'public/app/plugins/panel/dashlist/editor.html');
  57. }
  58. onRefresh() {
  59. var promises = [];
  60. promises.push(this.getRecentDashboards());
  61. promises.push(this.getStarred());
  62. promises.push(this.getSearch());
  63. return Promise.all(promises)
  64. .then(this.renderingCompleted.bind(this));
  65. }
  66. getSearch() {
  67. this.groups[2].show = this.panel.search;
  68. if (!this.panel.search) {
  69. return Promise.resolve();
  70. }
  71. var params = {
  72. limit: this.panel.limit,
  73. query: this.panel.query,
  74. tag: this.panel.tags,
  75. };
  76. return this.backendSrv.search(params).then(result => {
  77. this.groups[2].list = result;
  78. });
  79. }
  80. getStarred() {
  81. this.groups[0].show = this.panel.starred;
  82. if (!this.panel.starred) {
  83. return Promise.resolve();
  84. }
  85. var params = {limit: this.panel.limit, starred: "true"};
  86. return this.backendSrv.search(params).then(result => {
  87. this.groups[0].list = result;
  88. });
  89. }
  90. getRecentDashboards() {
  91. this.groups[1].show = this.panel.recent;
  92. if (!this.panel.recent) {
  93. return Promise.resolve();
  94. }
  95. var dashIds = _.first(impressions.getDashboardOpened(), this.panel.limit);
  96. return this.backendSrv.search({dashboardIds: dashIds, limit: this.panel.limit}).then(result => {
  97. this.groups[1].list = dashIds.map(orderId => {
  98. return _.find(result, dashboard => {
  99. return dashboard.id === orderId;
  100. });
  101. }).filter(el => {
  102. return el !== undefined;
  103. });
  104. });
  105. }
  106. }
  107. export {DashListCtrl, DashListCtrl as PanelCtrl}