# All files / src/app/ceph/rgw/rgw-user-s3-key-modal rgw-user-s3-key-modal.component.ts

76.19% Statements 32/42
64.71% Branches 11/17
33.33% Functions 3/9
75.68% Lines 28/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    993x
    3x

    3x
    3x
    3x

    3x
    3x

    3x

    3x

    3x

    1x
    1x

    1x
    1x
    1x
    1x

    1x
    1x
    1x

    3x
    1x

    3x

    1x

    3x

    3x

    3x

    3x

    3x
     import { Component, EventEmitter, Output } from '@angular/core';
    import { Validators } from '@angular/forms';

    import { I18n } from '@ngx-translate/i18n-polyfill';
    import * as _ from 'lodash';
    import { BsModalRef } from 'ngx-bootstrap/modal';

    import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
    import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
    import { CdFormGroup } from '../../../shared/forms/cd-form-group';
    import { CdValidators } from '../../../shared/forms/cd-validators';
    import { RgwUserS3Key } from '../models/rgw-user-s3-key';

    @Component({
      selector: 'cd-rgw-user-s3-key-modal',
      template: require('./rgw-user-s3-key-modal.component.html'),
      styles: []
    })
    export class RgwUserS3KeyModalComponent {
      /**
       * The event that is triggered when the 'Add' button as been pressed.
       */
      @Output()
      submitAction = new EventEmitter();

      formGroup: CdFormGroup;
      viewing = true;
      userCandidates: string[] = [];
      resource: string;
      action: string;

      constructor(
        private formBuilder: CdFormBuilder,
        public bsModalRef: BsModalRef,
        private i18n: I18n,
        public actionLabels: ActionLabelsI18n
      ) {
        this.resource = this.i18n('S3 Key');
        this.createForm();
        this.listenToChanges();
      }

      createForm() {
        this.formGroup = this.formBuilder.group({
          user: [null, [Validators.required]],
          generate_key: [true],
          access_key: [null, [CdValidators.requiredIf({ generate_key: false })]],
          secret_key: [null, [CdValidators.requiredIf({ generate_key: false })]]
        });
      }

      listenToChanges() {
        // Reset the validation status of various controls, especially those that are using
        // the 'requiredIf' validator. This is necessary because the controls itself are not
        // validated again if the status of their prerequisites have been changed.
        this.formGroup.get('generate_key').valueChanges.subscribe(() => {
          ['access_key', 'secret_key'].forEach((path) => {
            this.formGroup.get(path).updateValueAndValidity({ onlySelf: true });
          });
        });
      }

      /**
       * Set the 'viewing' flag. If set to TRUE, the modal dialog is in 'View' mode,
       * otherwise in 'Add' mode. According to the mode the dialog and its controls
       * behave different.
       * @param {boolean} viewing
       */
      setViewing(viewing: boolean = true) {
        this.action = this.actionLabels.SHOW;
        this.viewing = viewing;
      }

      /**
       * Set the values displayed in the dialog.
       */
      setValues(user: string, access_key: string, secret_key: string) {
        this.formGroup.setValue({
          user: user,
          generate_key: _.isEmpty(access_key),
          access_key: access_key,
          secret_key: secret_key
        });
      }

      /**
       * Set the user candidates displayed in the 'Username' dropdown box.
       */
      setUserCandidates(candidates: string[]) {
        this.userCandidates = candidates;
      }

      onSubmit() {
        const key: RgwUserS3Key = this.formGroup.value;
        this.submitAction.emit(key);
        this.bsModalRef.hide();
      }
    }