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';
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';
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.
22 submitAction = new EventEmitter();
24 formGroup: CdFormGroup;
26 userCandidates: string[] = [];
28 constructor(private formBuilder: CdFormBuilder, public bsModalRef: BsModalRef) {
30 this.listenToChanges();
34 this.formGroup = this.formBuilder.group({
35 user: [null, [Validators.required]],
37 access_key: [null, [CdValidators.requiredIf({ generate_key: false })]],
38 secret_key: [null, [CdValidators.requiredIf({ generate_key: false })]]
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 });
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
57 * @param {boolean} viewing
59 setViewing(viewing: boolean = true) {
60 this.viewing = viewing;
64 * Set the values displayed in the dialog.
66 setValues(user: string, access_key: string, secret_key: string) {
67 this.formGroup.setValue({
69 generate_key: _.isEmpty(access_key),
70 access_key: access_key,
71 secret_key: secret_key
76 * Set the user candidates displayed in the 'Username' dropdown box.
78 setUserCandidates(candidates: string[]) {
79 this.userCandidates = candidates;
83 const key: RgwUserS3Key = this.formGroup.value;
84 this.submitAction.emit(key);
85 this.bsModalRef.hide();