1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
7 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
9 import { CdValidators } from '../../../shared/validators/cd-validators';
10 import { RgwUserS3Key } from '../models/rgw-user-s3-key';
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']
17 export class RgwUserS3KeyModalComponent {
19 * The event that is triggered when the 'Add' button as been pressed.
21 @Output() submitAction = new EventEmitter();
23 formGroup: CdFormGroup;
25 userCandidates: string[] = [];
27 constructor(private formBuilder: CdFormBuilder, public bsModalRef: BsModalRef) {
29 this.listenToChanges();
33 this.formGroup = this.formBuilder.group({
34 user: [null, [Validators.required]],
36 access_key: [null, [CdValidators.requiredIf({ generate_key: false })]],
37 secret_key: [null, [CdValidators.requiredIf({ generate_key: false })]]
42 // Reset the validation status of various controls, especially those that are using
43 // the 'requiredIf' validator. This is necessary because the controls itself are not
44 // validated again if the status of their prerequisites have been changed.
45 this.formGroup.get('generate_key').valueChanges.subscribe(() => {
46 ['access_key', 'secret_key'].forEach((path) => {
47 this.formGroup.get(path).updateValueAndValidity({ onlySelf: true });
53 * Set the 'viewing' flag. If set to TRUE, the modal dialog is in 'View' mode,
54 * otherwise in 'Add' mode. According to the mode the dialog and its controls
56 * @param {boolean} viewing
58 setViewing(viewing: boolean = true) {
59 this.viewing = viewing;
63 * Set the values displayed in the dialog.
65 setValues(user: string, access_key: string, secret_key: string) {
66 this.formGroup.setValue({
68 generate_key: _.isEmpty(access_key),
69 access_key: access_key,
70 secret_key: secret_key
75 * Set the user candidates displayed in the 'Username' dropdown box.
77 setUserCandidates(candidates: string[]) {
78 this.userCandidates = candidates;
82 const key: RgwUserS3Key = this.formGroup.value;
83 this.submitAction.emit(key);
84 this.bsModalRef.hide();