folder_picker.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. import appEvents from 'app/core/app_events';
  5. export class FolderPickerCtrl {
  6. initialTitle: string;
  7. initialFolderId?: number;
  8. labelClass: string;
  9. onChange: any;
  10. onLoad: any;
  11. onCreateFolder: any;
  12. enterFolderCreation: any;
  13. exitFolderCreation: any;
  14. enableCreateNew: boolean;
  15. rootName = 'Root';
  16. folder: any;
  17. createNewFolder: boolean;
  18. newFolderName: string;
  19. newFolderNameTouched: boolean;
  20. hasValidationError: boolean;
  21. validationError: any;
  22. /** @ngInject */
  23. constructor(private backendSrv, private validationSrv) {
  24. if (!this.labelClass) {
  25. this.labelClass = "width-7";
  26. }
  27. this.loadInitialValue();
  28. }
  29. getOptions(query) {
  30. var params = {
  31. query: query,
  32. type: 'dash-folder',
  33. };
  34. return this.backendSrv.search(params).then(result => {
  35. if (query === '' ||
  36. query.toLowerCase() === "r" ||
  37. query.toLowerCase() === "ro" ||
  38. query.toLowerCase() === "roo" ||
  39. query.toLowerCase() === "root") {
  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.validateNewDashboardOrFolderName(this.newFolderName)
  61. .then(() => {
  62. this.hasValidationError = false;
  63. })
  64. .catch(err => {
  65. this.hasValidationError = true;
  66. this.validationError = err.message;
  67. });
  68. }
  69. createFolder(evt) {
  70. if (evt) {
  71. evt.stopPropagation();
  72. evt.preventDefault();
  73. }
  74. return this.backendSrv.createDashboardFolder(this.newFolderName).then(result => {
  75. appEvents.emit('alert-success', ['Folder Created', 'OK']);
  76. this.closeCreateFolder();
  77. this.folder = {text: result.dashboard.title, value: result.dashboard.id};
  78. this.onFolderChange(this.folder);
  79. });
  80. }
  81. cancelCreateFolder(evt) {
  82. if (evt) {
  83. evt.stopPropagation();
  84. evt.preventDefault();
  85. }
  86. this.closeCreateFolder();
  87. this.loadInitialValue();
  88. }
  89. private closeCreateFolder() {
  90. this.exitFolderCreation();
  91. this.createNewFolder = false;
  92. this.hasValidationError = false;
  93. this.validationError = null;
  94. this.newFolderName = '';
  95. this.newFolderNameTouched = false;
  96. }
  97. private loadInitialValue() {
  98. if (this.initialFolderId && this.initialFolderId > 0) {
  99. this.getOptions('').then(result => {
  100. this.folder = _.find(result, {value: this.initialFolderId});
  101. this.onFolderLoad();
  102. });
  103. } else {
  104. if (this.initialTitle) {
  105. this.folder = {text: this.initialTitle, value: null};
  106. } else {
  107. this.folder = {text: this.rootName, value: 0};
  108. }
  109. this.onFolderLoad();
  110. }
  111. }
  112. private onFolderLoad() {
  113. if (this.onLoad) {
  114. this.onLoad({$folder: {id: this.folder.value, title: this.folder.text}});
  115. }
  116. }
  117. }
  118. export function folderPicker() {
  119. return {
  120. restrict: 'E',
  121. templateUrl: 'public/app/features/dashboard/folder_picker/folder_picker.html',
  122. controller: FolderPickerCtrl,
  123. bindToController: true,
  124. controllerAs: 'ctrl',
  125. scope: {
  126. initialTitle: '<',
  127. initialFolderId: '<',
  128. labelClass: '@',
  129. rootName: '@',
  130. onChange: '&',
  131. onLoad: '&',
  132. onCreateFolder: '&',
  133. enterFolderCreation: '&',
  134. exitFolderCreation: '&',
  135. enableCreateNew: '@'
  136. }
  137. };
  138. }
  139. coreModule.directive('folderPicker', folderPicker);