create_folder_ctrl.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import appEvents from "app/core/app_events";
  2. export class CreateFolderCtrl {
  3. title = "";
  4. navModel: any;
  5. nameExists = false;
  6. titleTouched = false;
  7. /** @ngInject **/
  8. constructor(private backendSrv, private $location, navModelSrv) {
  9. this.navModel = navModelSrv.getNav("dashboards", "manage-dashboards", 0);
  10. }
  11. create() {
  12. if (!this.title || this.title.trim().length === 0) {
  13. return;
  14. }
  15. const title = this.title.trim();
  16. return this.backendSrv.createDashboardFolder(title).then(result => {
  17. appEvents.emit("alert-success", ["Folder Created", "OK"]);
  18. var folderUrl = `/dashboards/folder/${result.dashboard.id}/${
  19. result.meta.slug
  20. }`;
  21. this.$location.url(folderUrl);
  22. });
  23. }
  24. titleChanged() {
  25. this.titleTouched = true;
  26. this.backendSrv.search({ query: this.title }).then(res => {
  27. this.nameExists = false;
  28. for (let hit of res) {
  29. if (this.title === hit.title) {
  30. this.nameExists = true;
  31. break;
  32. }
  33. }
  34. });
  35. }
  36. }