folder_picker.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import _ from 'lodash';
  2. import coreModule from 'app/core/core_module';
  3. import appEvents from 'app/core/app_events';
  4. export class FolderPickerCtrl {
  5. initialTitle: string;
  6. initialFolderId?: number;
  7. labelClass: string;
  8. onChange: any;
  9. onLoad: any;
  10. onCreateFolder: any;
  11. enterFolderCreation: any;
  12. exitFolderCreation: any;
  13. enableCreateNew: boolean;
  14. rootName = 'Root';
  15. folder: any;
  16. createNewFolder: boolean;
  17. newFolderName: string;
  18. newFolderNameTouched: boolean;
  19. hasValidationError: boolean;
  20. validationError: any;
  21. /** @ngInject */
  22. constructor(private backendSrv, private validationSrv) {
  23. if (!this.labelClass) {
  24. this.labelClass = 'width-7';
  25. }
  26. this.loadInitialValue();
  27. }
  28. getOptions(query) {
  29. return this.backendSrv.get('api/dashboards/folders', { query: query }).then(result => {
  30. if (
  31. query === '' ||
  32. query.toLowerCase() === 'r' ||
  33. query.toLowerCase() === 'ro' ||
  34. query.toLowerCase() === 'roo' ||
  35. query.toLowerCase() === 'root'
  36. ) {
  37. result.unshift({ title: this.rootName, id: 0 });
  38. }
  39. if (this.enableCreateNew && query === '') {
  40. result.unshift({ title: '-- New Folder --', id: -1 });
  41. }
  42. return _.map(result, item => {
  43. return { text: item.title, value: item.id };
  44. });
  45. });
  46. }
  47. onFolderChange(option) {
  48. if (option.value === -1) {
  49. this.createNewFolder = true;
  50. this.enterFolderCreation();
  51. return;
  52. }
  53. this.onChange({ $folder: { id: option.value, title: option.text } });
  54. }
  55. newFolderNameChanged() {
  56. this.newFolderNameTouched = true;
  57. this.validationSrv
  58. .validateNewDashboardOrFolderName(this.newFolderName)
  59. .then(() => {
  60. this.hasValidationError = false;
  61. })
  62. .catch(err => {
  63. this.hasValidationError = true;
  64. this.validationError = err.message;
  65. });
  66. }
  67. createFolder(evt) {
  68. if (evt) {
  69. evt.stopPropagation();
  70. evt.preventDefault();
  71. }
  72. return this.backendSrv.createDashboardFolder(this.newFolderName).then(result => {
  73. appEvents.emit('alert-success', ['Folder Created', 'OK']);
  74. this.closeCreateFolder();
  75. this.folder = {
  76. text: result.dashboard.title,
  77. value: result.dashboard.id,
  78. };
  79. this.onFolderChange(this.folder);
  80. });
  81. }
  82. cancelCreateFolder(evt) {
  83. if (evt) {
  84. evt.stopPropagation();
  85. evt.preventDefault();
  86. }
  87. this.closeCreateFolder();
  88. this.loadInitialValue();
  89. }
  90. private closeCreateFolder() {
  91. this.exitFolderCreation();
  92. this.createNewFolder = false;
  93. this.hasValidationError = false;
  94. this.validationError = null;
  95. this.newFolderName = '';
  96. this.newFolderNameTouched = false;
  97. }
  98. private loadInitialValue() {
  99. if (this.initialFolderId && this.initialFolderId > 0) {
  100. this.getOptions('').then(result => {
  101. this.folder = _.find(result, { value: this.initialFolderId });
  102. this.onFolderLoad();
  103. });
  104. } else {
  105. if (this.initialTitle) {
  106. this.folder = { text: this.initialTitle, value: null };
  107. } else {
  108. this.folder = { text: this.rootName, value: 0 };
  109. }
  110. this.onFolderLoad();
  111. }
  112. }
  113. private onFolderLoad() {
  114. if (this.onLoad) {
  115. this.onLoad({
  116. $folder: { id: this.folder.value, title: this.folder.text },
  117. });
  118. }
  119. }
  120. }
  121. export function folderPicker() {
  122. return {
  123. restrict: 'E',
  124. templateUrl: 'public/app/features/dashboard/folder_picker/folder_picker.html',
  125. controller: FolderPickerCtrl,
  126. bindToController: true,
  127. controllerAs: 'ctrl',
  128. scope: {
  129. initialTitle: '<',
  130. initialFolderId: '<',
  131. labelClass: '@',
  132. rootName: '@',
  133. onChange: '&',
  134. onLoad: '&',
  135. onCreateFolder: '&',
  136. enterFolderCreation: '&',
  137. exitFolderCreation: '&',
  138. enableCreateNew: '@',
  139. },
  140. };
  141. }
  142. coreModule.directive('folderPicker', folderPicker);