dashboard_list_ctrl.ts 4.2 KB

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