FolderPickerCtrl.ts 5.0 KB

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