folder_picker.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = 'General';
  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. const params = {
  30. query: query,
  31. type: 'dash-folder',
  32. permission: 'Edit',
  33. };
  34. return this.backendSrv.get('api/search', params).then(result => {
  35. if (
  36. query === '' ||
  37. query.toLowerCase() === 'g' ||
  38. query.toLowerCase() === 'ge' ||
  39. query.toLowerCase() === 'gen' ||
  40. query.toLowerCase() === 'gene' ||
  41. query.toLowerCase() === 'gener' ||
  42. query.toLowerCase() === 'genera' ||
  43. query.toLowerCase() === 'general'
  44. ) {
  45. result.unshift({ title: this.rootName, id: 0 });
  46. }
  47. if (this.enableCreateNew && query === '') {
  48. result.unshift({ title: '-- New Folder --', id: -1 });
  49. }
  50. return _.map(result, item => {
  51. return { text: item.title, value: item.id };
  52. });
  53. });
  54. }
  55. onFolderChange(option) {
  56. if (option.value === -1) {
  57. this.createNewFolder = true;
  58. this.enterFolderCreation();
  59. return;
  60. }
  61. this.onChange({ $folder: { id: option.value, title: option.text } });
  62. }
  63. newFolderNameChanged() {
  64. this.newFolderNameTouched = true;
  65. this.validationSrv
  66. .validateNewFolderName(this.newFolderName)
  67. .then(() => {
  68. this.hasValidationError = false;
  69. })
  70. .catch(err => {
  71. this.hasValidationError = true;
  72. this.validationError = err.message;
  73. });
  74. }
  75. createFolder(evt) {
  76. if (evt) {
  77. evt.stopPropagation();
  78. evt.preventDefault();
  79. }
  80. return this.backendSrv.createDashboardFolder(this.newFolderName).then(result => {
  81. appEvents.emit('alert-success', ['Folder Created', 'OK']);
  82. this.closeCreateFolder();
  83. this.folder = {
  84. text: result.dashboard.title,
  85. value: result.dashboard.id,
  86. };
  87. this.onFolderChange(this.folder);
  88. });
  89. }
  90. cancelCreateFolder(evt) {
  91. if (evt) {
  92. evt.stopPropagation();
  93. evt.preventDefault();
  94. }
  95. this.closeCreateFolder();
  96. this.loadInitialValue();
  97. }
  98. private closeCreateFolder() {
  99. this.exitFolderCreation();
  100. this.createNewFolder = false;
  101. this.hasValidationError = false;
  102. this.validationError = null;
  103. this.newFolderName = '';
  104. this.newFolderNameTouched = false;
  105. }
  106. private loadInitialValue() {
  107. if (this.initialFolderId && this.initialFolderId > 0) {
  108. this.getOptions('').then(result => {
  109. this.folder = _.find(result, { value: this.initialFolderId });
  110. if (!this.folder) {
  111. this.folder = { text: this.initialTitle, value: this.initialFolderId };
  112. }
  113. this.onFolderLoad();
  114. });
  115. } else {
  116. if (this.initialTitle) {
  117. this.folder = { text: this.initialTitle, value: null };
  118. } else {
  119. this.folder = { text: this.rootName, value: 0 };
  120. }
  121. this.onFolderLoad();
  122. }
  123. }
  124. private onFolderLoad() {
  125. if (this.onLoad) {
  126. this.onLoad({
  127. $folder: { id: this.folder.value, title: this.folder.text },
  128. });
  129. }
  130. }
  131. }
  132. export function folderPicker() {
  133. return {
  134. restrict: 'E',
  135. templateUrl: 'public/app/features/dashboard/folder_picker/folder_picker.html',
  136. controller: FolderPickerCtrl,
  137. bindToController: true,
  138. controllerAs: 'ctrl',
  139. scope: {
  140. initialTitle: '<',
  141. initialFolderId: '<',
  142. labelClass: '@',
  143. rootName: '@',
  144. onChange: '&',
  145. onLoad: '&',
  146. onCreateFolder: '&',
  147. enterFolderCreation: '&',
  148. exitFolderCreation: '&',
  149. enableCreateNew: '@',
  150. },
  151. };
  152. }
  153. coreModule.directive('folderPicker', folderPicker);