folder_picker.ts 4.7 KB

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