dashboard_list_ctrl.ts 5.5 KB

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