folder_picker.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 = {
  93. text: result.title,
  94. value: result.id,
  95. };
  96. this.onFolderChange(this.folder);
  97. });
  98. }
  99. cancelCreateFolder(evt) {
  100. if (evt) {
  101. evt.stopPropagation();
  102. evt.preventDefault();
  103. }
  104. this.closeCreateFolder();
  105. this.loadInitialValue();
  106. }
  107. private closeCreateFolder() {
  108. this.exitFolderCreation();
  109. this.createNewFolder = false;
  110. this.hasValidationError = false;
  111. this.validationError = null;
  112. this.newFolderName = '';
  113. this.newFolderNameTouched = false;
  114. }
  115. private loadInitialValue() {
  116. const resetFolder = { text: this.initialTitle, value: null };
  117. const rootFolder = { text: this.rootName, value: 0 };
  118. this.getOptions('').then(result => {
  119. let folder;
  120. if (this.initialFolderId) {
  121. folder = _.find(result, { value: this.initialFolderId });
  122. } else if (this.enableReset && this.initialTitle && this.initialFolderId === null) {
  123. folder = resetFolder;
  124. }
  125. if (!folder) {
  126. if (this.isEditor) {
  127. folder = rootFolder;
  128. } else {
  129. folder = result.length > 0 ? result[0] : resetFolder;
  130. }
  131. }
  132. this.folder = folder;
  133. this.onFolderLoad();
  134. });
  135. }
  136. private onFolderLoad() {
  137. if (this.onLoad) {
  138. this.onLoad({
  139. $folder: { id: this.folder.value, title: this.folder.text },
  140. });
  141. }
  142. }
  143. }
  144. export function folderPicker() {
  145. return {
  146. restrict: 'E',
  147. templateUrl: 'public/app/features/dashboard/folder_picker/folder_picker.html',
  148. controller: FolderPickerCtrl,
  149. bindToController: true,
  150. controllerAs: 'ctrl',
  151. scope: {
  152. initialTitle: '<',
  153. initialFolderId: '<',
  154. labelClass: '@',
  155. rootName: '@',
  156. onChange: '&',
  157. onLoad: '&',
  158. onCreateFolder: '&',
  159. enterFolderCreation: '&',
  160. exitFolderCreation: '&',
  161. enableCreateNew: '@',
  162. enableReset: '@',
  163. },
  164. };
  165. }
  166. coreModule.directive('folderPicker', folderPicker);