dashboard_list_ctrl.ts 4.6 KB

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