create_folder_ctrl.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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}/${result.meta.slug}`;
  19. this.$location.url(folderUrl);
  20. });
  21. }
  22. titleChanged() {
  23. this.titleTouched = true;
  24. this.backendSrv.search({query: this.title}).then(res => {
  25. this.nameExists = false;
  26. for (let hit of res) {
  27. if (this.title === hit.title) {
  28. this.nameExists = true;
  29. break;
  30. }
  31. }
  32. });
  33. }
  34. }