folder_picker.ts 4.1 KB

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