]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
e9e45a831d481776f4edb4da0abe265fa73706ca
[ceph.git] /
1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
3
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
6
7 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
9 import { CdValidators } from '../../../shared/forms/cd-validators';
10 import { RgwUserS3Key } from '../models/rgw-user-s3-key';
11
12 @Component({
13   selector: 'cd-rgw-user-s3-key-modal',
14   templateUrl: './rgw-user-s3-key-modal.component.html',
15   styleUrls: ['./rgw-user-s3-key-modal.component.scss']
16 })
17 export class RgwUserS3KeyModalComponent {
18   /**
19    * The event that is triggered when the 'Add' button as been pressed.
20    */
21   @Output()
22   submitAction = new EventEmitter();
23
24   formGroup: CdFormGroup;
25   viewing = true;
26   userCandidates: string[] = [];
27
28   constructor(private formBuilder: CdFormBuilder, public bsModalRef: BsModalRef) {
29     this.createForm();
30     this.listenToChanges();
31   }
32
33   createForm() {
34     this.formGroup = this.formBuilder.group({
35       user: [null, [Validators.required]],
36       generate_key: [true],
37       access_key: [null, [CdValidators.requiredIf({ generate_key: false })]],
38       secret_key: [null, [CdValidators.requiredIf({ generate_key: false })]]
39     });
40   }
41
42   listenToChanges() {
43     // Reset the validation status of various controls, especially those that are using
44     // the 'requiredIf' validator. This is necessary because the controls itself are not
45     // validated again if the status of their prerequisites have been changed.
46     this.formGroup.get('generate_key').valueChanges.subscribe(() => {
47       ['access_key', 'secret_key'].forEach((path) => {
48         this.formGroup.get(path).updateValueAndValidity({ onlySelf: true });
49       });
50     });
51   }
52
53   /**
54    * Set the 'viewing' flag. If set to TRUE, the modal dialog is in 'View' mode,
55    * otherwise in 'Add' mode. According to the mode the dialog and its controls
56    * behave different.
57    * @param {boolean} viewing
58    */
59   setViewing(viewing: boolean = true) {
60     this.viewing = viewing;
61   }
62
63   /**
64    * Set the values displayed in the dialog.
65    */
66   setValues(user: string, access_key: string, secret_key: string) {
67     this.formGroup.setValue({
68       user: user,
69       generate_key: _.isEmpty(access_key),
70       access_key: access_key,
71       secret_key: secret_key
72     });
73   }
74
75   /**
76    * Set the user candidates displayed in the 'Username' dropdown box.
77    */
78   setUserCandidates(candidates: string[]) {
79     this.userCandidates = candidates;
80   }
81
82   onSubmit() {
83     const key: RgwUserS3Key = this.formGroup.value;
84     this.submitAction.emit(key);
85     this.bsModalRef.hide();
86   }
87 }