dashboard_list_ctrl.ts 5.1 KB

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