module.ts 3.3 KB

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