dashboard_list_ctrl.ts 5.1 KB

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