folder_picker.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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
  77. .createDashboardFolder(this.newFolderName)
  78. .then(result => {
  79. appEvents.emit("alert-success", ["Folder Created", "OK"]);
  80. this.closeCreateFolder();
  81. this.folder = {
  82. text: result.dashboard.title,
  83. value: result.dashboard.id
  84. };
  85. this.onFolderChange(this.folder);
  86. });
  87. }
  88. cancelCreateFolder(evt) {
  89. if (evt) {
  90. evt.stopPropagation();
  91. evt.preventDefault();
  92. }
  93. this.closeCreateFolder();
  94. this.loadInitialValue();
  95. }
  96. private closeCreateFolder() {
  97. this.exitFolderCreation();
  98. this.createNewFolder = false;
  99. this.hasValidationError = false;
  100. this.validationError = null;
  101. this.newFolderName = "";
  102. this.newFolderNameTouched = false;
  103. }
  104. private loadInitialValue() {
  105. if (this.initialFolderId && this.initialFolderId > 0) {
  106. this.getOptions("").then(result => {
  107. this.folder = _.find(result, { value: this.initialFolderId });
  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:
  131. "public/app/features/dashboard/folder_picker/folder_picker.html",
  132. controller: FolderPickerCtrl,
  133. bindToController: true,
  134. controllerAs: "ctrl",
  135. scope: {
  136. initialTitle: "<",
  137. initialFolderId: "<",
  138. labelClass: "@",
  139. rootName: "@",
  140. onChange: "&",
  141. onLoad: "&",
  142. onCreateFolder: "&",
  143. enterFolderCreation: "&",
  144. exitFolderCreation: "&",
  145. enableCreateNew: "@"
  146. }
  147. };
  148. }
  149. coreModule.directive("folderPicker", folderPicker);