]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
dca0efc077166472b647253f125e38e6fc647010
[ceph-ci.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   /**
17    * The event that is triggered when the 'Add' or 'Update' button
18    * has been pressed.
19    */
20   @Output() submitAction = new EventEmitter();
21
22   formGroup: FormGroup;
23   editing = true;
24   types: string[] = [];
25
26   constructor(private formBuilder: FormBuilder,
27               public bsModalRef: BsModalRef) {
28     this.createForm();
29   }
30
31   createForm() {
32     this.formGroup = this.formBuilder.group({
33       'type': [
34         null,
35         [Validators.required]
36       ],
37       'perm': [
38         null,
39         [Validators.required]
40       ]
41     });
42   }
43
44   /**
45    * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
46    * otherwise in 'Add' mode. According to the mode the dialog and its controls
47    * behave different.
48    * @param {boolean} viewing
49    */
50   setEditing(editing: boolean = true) {
51     this.editing = editing;
52   }
53
54   /**
55    * Set the values displayed in the dialog.
56    */
57   setValues(type: string, perm: string) {
58     this.formGroup.setValue({
59       'type': type,
60       'perm': perm
61     });
62   }
63
64   /**
65    * Set the current capabilities of the user.
66    */
67   setCapabilities(capabilities: RgwUserCapability[]) {
68     // Parse the configured capabilities to get a list of types that
69     // should be displayed.
70     const usedTypes = [];
71     capabilities.forEach((capability) => {
72       usedTypes.push(capability.type);
73     });
74     this.types = [];
75     ['users', 'buckets', 'metadata', 'usage', 'zone'].forEach((type) => {
76       if (_.indexOf(usedTypes, type) === -1) {
77         this.types.push(type);
78       }
79     });
80   }
81
82   onSubmit() {
83     const capability: RgwUserCapability = this.formGroup.value;
84     this.submitAction.emit(capability);
85     this.bsModalRef.hide();
86   }
87 }