dashboard_list_ctrl.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import _ from 'lodash';
  2. import appEvents from 'app/core/app_events';
  3. export class DashboardListCtrl {
  4. public dashboards: any [];
  5. query: any;
  6. navModel: any;
  7. canDelete = false;
  8. canMove = false;
  9. /** @ngInject */
  10. constructor(private backendSrv, navModelSrv, private $q) {
  11. this.navModel = navModelSrv.getNav('cfg', 'dashboards');
  12. this.query = {query: '', mode: 'tree', tag: []};
  13. this.getDashboards();
  14. }
  15. getDashboards() {
  16. return this.backendSrv.search(this.query).then((result) => {
  17. this.dashboards = this.groupDashboardsInFolders(result);
  18. for (let dash of this.dashboards) {
  19. dash.checked = false;
  20. }
  21. });
  22. }
  23. groupDashboardsInFolders(results) {
  24. let byId = _.groupBy(results, 'id');
  25. let byFolderId = _.groupBy(results, 'folderId');
  26. let finalList = [];
  27. // add missing parent folders
  28. _.each(results, (hit, index) => {
  29. if (hit.folderId && !byId[hit.folderId]) {
  30. const folder = {
  31. id: hit.folderId,
  32. uri: `db/${hit.folderSlug}`,
  33. title: hit.folderTitle,
  34. type: 'dash-folder'
  35. };
  36. byId[hit.folderId] = folder;
  37. results.splice(index, 0, folder);
  38. }
  39. });
  40. // group by folder
  41. for (let hit of results) {
  42. if (hit.folderId) {
  43. hit.type = "dash-child";
  44. } else {
  45. finalList.push(hit);
  46. }
  47. hit.url = 'dashboard/' + hit.uri;
  48. if (hit.type === 'dash-folder') {
  49. if (!byFolderId[hit.id]) {
  50. continue;
  51. }
  52. for (let child of byFolderId[hit.id]) {
  53. finalList.push(child);
  54. }
  55. }
  56. }
  57. return finalList;
  58. }
  59. selectionChanged() {
  60. const selected = _.filter(this.dashboards, {checked: true}).length;
  61. this.canDelete = selected > 0;
  62. const selectedDashboards = _.filter(this.dashboards, (o) => {
  63. return o.checked && (o.type === 'dash-db' || o.type === 'dash-child');
  64. }).length;
  65. const selectedFolders = _.filter(this.dashboards, {checked: true, type: 'dash-folder'}).length;
  66. this.canMove = selectedDashboards > 0 && selectedFolders === 0;
  67. }
  68. getDashboardsToDelete() {
  69. const selectedFolderIds = this.getFolderIds(this.dashboards);
  70. return _.filter(this.dashboards, o => {
  71. return o.checked && (
  72. o.type !== 'dash-child' ||
  73. (o.type === 'dash-child' && !_.includes(selectedFolderIds, o.folderId))
  74. );
  75. });
  76. }
  77. getFolderIds(dashboards) {
  78. const ids = [];
  79. for (let dash of dashboards) {
  80. if (dash.type === 'dash-folder') {
  81. ids.push(dash.id);
  82. }
  83. }
  84. return ids;
  85. }
  86. delete() {
  87. const selectedDashboards = this.getDashboardsToDelete();
  88. appEvents.emit('confirm-modal', {
  89. title: 'Delete',
  90. text: `Do you want to delete the ${selectedDashboards.length} selected dashboards?`,
  91. icon: 'fa-trash',
  92. yesText: 'Delete',
  93. onConfirm: () => {
  94. const promises = [];
  95. for (let dash of selectedDashboards) {
  96. promises.push(this.backendSrv.delete(`/api/dashboards/${dash.uri}`));
  97. }
  98. this.$q.all(promises).then(() => {
  99. this.getDashboards();
  100. });
  101. }
  102. });
  103. }
  104. moveTo() {
  105. const selectedDashboards = _.filter(this.dashboards, {checked: true});
  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. filterByTag(tag, evt) {
  116. this.query.tag.push(tag);
  117. this.getDashboards();
  118. if (evt) {
  119. evt.stopPropagation();
  120. evt.preventDefault();
  121. }
  122. }
  123. removeTag(tag, evt) {
  124. this.query.tag = _.without(this.query.tag, tag);
  125. this.getDashboards();
  126. if (evt) {
  127. evt.stopPropagation();
  128. evt.preventDefault();
  129. }
  130. }
  131. }