module.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. folderId: 0,
  19. };
  20. /** @ngInject */
  21. constructor($scope, $injector, private backendSrv) {
  22. super($scope, $injector);
  23. _.defaults(this.panel, this.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. folderId: this.panel.folderId
  77. };
  78. return this.backendSrv.search(params).then(result => {
  79. this.groups[2].list = result;
  80. });
  81. }
  82. getStarred() {
  83. this.groups[0].show = this.panel.starred;
  84. if (!this.panel.starred) {
  85. return Promise.resolve();
  86. }
  87. var params = {limit: this.panel.limit, starred: "true"};
  88. return this.backendSrv.search(params).then(result => {
  89. this.groups[0].list = result;
  90. });
  91. }
  92. getRecentDashboards() {
  93. this.groups[1].show = this.panel.recent;
  94. if (!this.panel.recent) {
  95. return Promise.resolve();
  96. }
  97. var dashIds = _.take(impressions.getDashboardOpened(), this.panel.limit);
  98. return this.backendSrv.search({dashboardIds: dashIds, limit: this.panel.limit}).then(result => {
  99. this.groups[1].list = dashIds.map(orderId => {
  100. return _.find(result, dashboard => {
  101. return dashboard.id === orderId;
  102. });
  103. }).filter(el => {
  104. return el !== undefined;
  105. });
  106. });
  107. }
  108. onFolderChange(folder: any) {
  109. this.panel.folderId = folder.id;
  110. this.refresh();
  111. }
  112. }
  113. export {DashListCtrl, DashListCtrl as PanelCtrl};