FolderPickerCtrl.ts 5.4 KB

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