]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
2a4a282fddbe5f9d0de271fd725f14a3a174cea0
[ceph.git] /
1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
3
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
6
7 import { RgwUserCapability } from '../models/rgw-user-capability';
8
9 @Component({
10   selector: 'cd-rgw-user-capability-modal',
11   templateUrl: './rgw-user-capability-modal.component.html',
12   styleUrls: ['./rgw-user-capability-modal.component.scss']
13 })
14 export class RgwUserCapabilityModalComponent {
15   /**
16    * The event that is triggered when the 'Add' or 'Update' button
17    * has been pressed.
18    */
19   @Output() submitAction = new EventEmitter();
20
21   formGroup: FormGroup;
22   editing = true;
23   types: string[] = [];
24
25   constructor(private formBuilder: FormBuilder, public bsModalRef: BsModalRef) {
26     this.createForm();
27   }
28
29   createForm() {
30     this.formGroup = this.formBuilder.group({
31       type: [null, [Validators.required]],
32       perm: [null, [Validators.required]]
33     });
34   }
35
36   /**
37    * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
38    * otherwise in 'Add' mode. According to the mode the dialog and its controls
39    * behave different.
40    * @param {boolean} viewing
41    */
42   setEditing(editing: boolean = true) {
43     this.editing = editing;
44   }
45
46   /**
47    * Set the values displayed in the dialog.
48    */
49   setValues(type: string, perm: string) {
50     this.formGroup.setValue({
51       type: type,
52       perm: perm
53     });
54   }
55
56   /**
57    * Set the current capabilities of the user.
58    */
59   setCapabilities(capabilities: RgwUserCapability[]) {
60     // Parse the configured capabilities to get a list of types that
61     // should be displayed.
62     const usedTypes = [];
63     capabilities.forEach((capability) => {
64       usedTypes.push(capability.type);
65     });
66     this.types = [];
67     ['users', 'buckets', 'metadata', 'usage', 'zone'].forEach((type) => {
68       if (_.indexOf(usedTypes, type) === -1) {
69         this.types.push(type);
70       }
71     });
72   }
73
74   onSubmit() {
75     const capability: RgwUserCapability = this.formGroup.value;
76     this.submitAction.emit(capability);
77     this.bsModalRef.hide();
78   }
79 }