folder_picker.ts 4.3 KB

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