folder_picker.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. if (this.initialFolderId && this.initialFolderId > 0) {
  117. this.getOptions('').then(result => {
  118. this.folder = _.find(result, { value: this.initialFolderId });
  119. if (!this.folder) {
  120. this.folder = { text: this.initialTitle, value: this.initialFolderId };
  121. }
  122. this.onFolderLoad();
  123. });
  124. } else {
  125. if (this.initialTitle && this.initialFolderId === null) {
  126. this.folder = { text: this.initialTitle, value: null };
  127. } else {
  128. this.folder = { text: this.rootName, value: 0 };
  129. }
  130. this.onFolderLoad();
  131. }
  132. }
  133. private onFolderLoad() {
  134. if (this.onLoad) {
  135. this.onLoad({
  136. $folder: { id: this.folder.value, title: this.folder.text },
  137. });
  138. }
  139. }
  140. }
  141. export function folderPicker() {
  142. return {
  143. restrict: 'E',
  144. templateUrl: 'public/app/features/dashboard/folder_picker/folder_picker.html',
  145. controller: FolderPickerCtrl,
  146. bindToController: true,
  147. controllerAs: 'ctrl',
  148. scope: {
  149. initialTitle: '<',
  150. initialFolderId: '<',
  151. labelClass: '@',
  152. rootName: '@',
  153. onChange: '&',
  154. onLoad: '&',
  155. onCreateFolder: '&',
  156. enterFolderCreation: '&',
  157. exitFolderCreation: '&',
  158. enableCreateNew: '@',
  159. enableReset: '@',
  160. },
  161. };
  162. }
  163. coreModule.directive('folderPicker', folderPicker);