folder_picker.ts 3.9 KB

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