CreateFolderCtrl.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import appEvents from 'app/core/app_events';
  2. import locationUtil from 'app/core/utils/location_util';
  3. import { BackendSrv } from 'app/core/services/backend_srv';
  4. import { ILocationService } from 'angular';
  5. import { ValidationSrv } from 'app/features/manage-dashboards';
  6. import { NavModelSrv } from 'app/core/nav_model_srv';
  7. export default class CreateFolderCtrl {
  8. title = '';
  9. navModel: any;
  10. titleTouched = false;
  11. hasValidationError: boolean;
  12. validationError: any;
  13. /** @ngInject */
  14. constructor(
  15. private backendSrv: BackendSrv,
  16. private $location: ILocationService,
  17. private validationSrv: ValidationSrv,
  18. navModelSrv: NavModelSrv
  19. ) {
  20. this.navModel = navModelSrv.getNav('dashboards', 'manage-dashboards', 0);
  21. }
  22. create() {
  23. if (this.hasValidationError) {
  24. return;
  25. }
  26. return this.backendSrv.createFolder({ title: this.title }).then((result: any) => {
  27. appEvents.emit('alert-success', ['Folder Created', 'OK']);
  28. this.$location.url(locationUtil.stripBaseFromUrl(result.url));
  29. });
  30. }
  31. titleChanged() {
  32. this.titleTouched = true;
  33. this.validationSrv
  34. .validateNewFolderName(this.title)
  35. .then(() => {
  36. this.hasValidationError = false;
  37. })
  38. .catch(err => {
  39. this.hasValidationError = true;
  40. this.validationError = err.message;
  41. });
  42. }
  43. }