]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
7666fc85eccfd37239276b3e644614b0cca3c5c4
[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 { I18n } from '@ngx-translate/i18n-polyfill';
8 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
9 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
10 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
11 import { RgwUserCapabilities } from '../models/rgw-user-capabilities';
12 import { RgwUserCapability } from '../models/rgw-user-capability';
13
14 @Component({
15   selector: 'cd-rgw-user-capability-modal',
16   templateUrl: './rgw-user-capability-modal.component.html',
17   styleUrls: ['./rgw-user-capability-modal.component.scss']
18 })
19 export class RgwUserCapabilityModalComponent {
20   /**
21    * The event that is triggered when the 'Add' or 'Update' button
22    * has been pressed.
23    */
24   @Output()
25   submitAction = new EventEmitter();
26
27   formGroup: CdFormGroup;
28   editing = true;
29   types: string[] = [];
30   resource: string;
31   action: string;
32
33   constructor(
34     private formBuilder: CdFormBuilder,
35     public bsModalRef: BsModalRef,
36     private i18n: I18n,
37     public actionLabels: ActionLabelsI18n
38   ) {
39     this.resource = this.i18n('capability');
40     this.createForm();
41   }
42
43   createForm() {
44     this.formGroup = this.formBuilder.group({
45       type: [null, [Validators.required]],
46       perm: [null, [Validators.required]]
47     });
48   }
49
50   /**
51    * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
52    * otherwise in 'Add' mode. According to the mode the dialog and its controls
53    * behave different.
54    * @param {boolean} viewing
55    */
56   setEditing(editing: boolean = true) {
57     this.editing = editing;
58     this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.ADD;
59   }
60
61   /**
62    * Set the values displayed in the dialog.
63    */
64   setValues(type: string, perm: string) {
65     this.formGroup.setValue({
66       type: type,
67       perm: perm
68     });
69   }
70
71   /**
72    * Set the current capabilities of the user.
73    */
74   setCapabilities(capabilities: RgwUserCapability[]) {
75     // Parse the configured capabilities to get a list of types that
76     // should be displayed.
77     const usedTypes: string[] = [];
78     capabilities.forEach((capability) => {
79       usedTypes.push(capability.type);
80     });
81     this.types = [];
82     RgwUserCapabilities.getAll().forEach((type) => {
83       if (_.indexOf(usedTypes, type) === -1) {
84         this.types.push(type);
85       }
86     });
87   }
88
89   onSubmit() {
90     const capability: RgwUserCapability = this.formGroup.value;
91     this.submitAction.emit(capability);
92     this.bsModalRef.hide();
93   }
94 }