dashboard_list_ctrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import _ from 'lodash';
  2. import appEvents from 'app/core/app_events';
  3. import { SearchSrv } from 'app/core/services/search_srv';
  4. export class DashboardListCtrl {
  5. public sections: any [];
  6. tags: any [];
  7. query: any;
  8. navModel: any;
  9. canDelete = false;
  10. canMove = false;
  11. /** @ngInject */
  12. constructor(private backendSrv, navModelSrv, private $q, private searchSrv: SearchSrv) {
  13. this.navModel = navModelSrv.getNav('dashboards', 'dashboards');
  14. this.query = {query: '', mode: 'tree', tag: []};
  15. this.getDashboards();
  16. // this.getDashboards().then(() => {
  17. // this.getTags();
  18. // });
  19. }
  20. getDashboards() {
  21. return this.searchSrv.browse().then((result) => {
  22. this.sections = result;
  23. for (let section of this.sections) {
  24. section.checked = false;
  25. for (let dashboard of section.items) {
  26. dashboard.checked = false;
  27. }
  28. }
  29. });
  30. }
  31. selectionChanged() {
  32. let selectedDashboards = 0;
  33. for (let section of this.sections) {
  34. selectedDashboards += _.filter(section.items, {checked: true}).length;
  35. }
  36. const selectedFolders = _.filter(this.sections, {checked: true}).length;
  37. this.canMove = selectedDashboards > 0 && selectedFolders === 0;
  38. this.canDelete = selectedDashboards > 0 || selectedFolders > 0;
  39. }
  40. getDashboardsToDelete() {
  41. let selectedDashboards = [];
  42. for (const section of this.sections) {
  43. if (section.checked) {
  44. selectedDashboards.push(section.uri);
  45. } else {
  46. const selected = _.filter(section.items, {checked: true});
  47. selectedDashboards.push(... _.map(selected, 'uri'));
  48. }
  49. }
  50. return selectedDashboards;
  51. }
  52. getFolderIds(sections) {
  53. const ids = [];
  54. for (let s of sections) {
  55. if (s.checked) {
  56. ids.push(s.id);
  57. }
  58. }
  59. return ids;
  60. }
  61. delete() {
  62. const selectedDashboards = this.getDashboardsToDelete();
  63. appEvents.emit('confirm-modal', {
  64. title: 'Delete',
  65. text: `Do you want to delete the ${selectedDashboards.length} selected dashboards?`,
  66. icon: 'fa-trash',
  67. yesText: 'Delete',
  68. onConfirm: () => {
  69. const promises = [];
  70. for (let dash of selectedDashboards) {
  71. promises.push(this.backendSrv.delete(`/api/dashboards/${dash}`));
  72. }
  73. this.$q.all(promises).then(() => {
  74. this.getDashboards();
  75. });
  76. }
  77. });
  78. }
  79. getDashboardsToMove() {
  80. let selectedDashboards = [];
  81. for (const section of this.sections) {
  82. const selected = _.filter(section.items, {checked: true});
  83. selectedDashboards.push(... _.map(selected, 'uri'));
  84. }
  85. return selectedDashboards;
  86. }
  87. moveTo() {
  88. const selectedDashboards = this.getDashboardsToMove();
  89. const template = '<move-to-folder-modal dismiss="dismiss()" ' +
  90. 'dashboards="model.dashboards" after-save="model.afterSave()">' +
  91. '</move-to-folder-modal>`';
  92. appEvents.emit('show-modal', {
  93. templateHtml: template,
  94. modalClass: 'modal--narrow',
  95. model: {dashboards: selectedDashboards, afterSave: this.getDashboards.bind(this)}
  96. });
  97. }
  98. // getTags() {
  99. // return this.backendSrv.get('/api/dashboards/tags').then((results) => {
  100. // this.tags = results;
  101. // });
  102. // }
  103. filterByTag(tag, evt) {
  104. this.query.tag.push(tag);
  105. this.getDashboards();
  106. if (evt) {
  107. evt.stopPropagation();
  108. evt.preventDefault();
  109. }
  110. }
  111. removeTag(tag, evt) {
  112. this.query.tag = _.without(this.query.tag, tag);
  113. this.getDashboards();
  114. if (evt) {
  115. evt.stopPropagation();
  116. evt.preventDefault();
  117. }
  118. }
  119. }