2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
39 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
40 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
41 import { RoleFunction } from '../role-function';
42 import { HttpClient } from '@angular/common/http';
43 import { AdminService } from '../../admin.service';
44 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
47 selector: 'app-new-role-function',
48 templateUrl: './new-role-function.component.html',
49 styleUrls: ['./new-role-function.component.scss']
51 export class NewRoleFunctionComponent implements OnInit {
53 @Input() title: string;
55 @Input() dialogState: number;
56 @Input() currentRoleFunctions: any;
57 @Input() editRoleFunction: RoleFunction;
58 @Output() passBackRoleFunctionPopup: EventEmitter<any> = new EventEmitter();
59 roleFunction: RoleFunction;
60 otherTypeValue: string;
61 typeOptions: string[] = ['menu', 'url', 'other'];
67 createOrUpdate: string;
68 constructor(public adminService: AdminService, public activeModal: NgbActiveModal, public ngbModal: NgbModal, public http: HttpClient) { }
71 this.createOrUpdate = 'create';
72 this.selectedType = 'menu';
73 this.roleFunction = new RoleFunction(this.selectedType, '', '*', '');
74 this.otherTypeValue = '';
75 if (this.editRoleFunction) {
76 this.createOrUpdate = 'update';
77 this.editDisable = true;
78 if (this.editRoleFunction.type !== 'menu' && this.editRoleFunction.type !== 'url') {
79 this.selectedType = 'other';
80 this.otherTypeValue = this.editRoleFunction.type;
82 this.selectedType = this.editRoleFunction.type;
84 this.roleFunction = new RoleFunction(this.editRoleFunction.type, this.editRoleFunction.code, this.editRoleFunction.action, this.editRoleFunction.name);
89 if (/[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.type)) {
90 this.openConfirmationModal('Confirmation', 'Type can only contain alphanumeric characters, dots(.) and underscores(_)');
93 if (this.roleFunction.action !== '*' && /[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.action)) {
94 this.openConfirmationModal('Confirmation', 'Action can only contain alphanumeric characters, hyphens(-), dots(.) and underscores(_) and single asterisk character(*)');
97 if (/[^a-zA-Z0-9\-\:\_\./*]/.test(this.roleFunction.code)) {
98 this.openConfirmationModal('Confirmation', 'Instance can only contain alphanumeric characters, hyphens(-), dots(.), colons(:), forwardSlash(/) , asterisk(*) and underscores(_)');
101 const modalInfoRef = this.ngbModal.open(InformationModalComponent);
102 modalInfoRef.componentInstance.title = 'Confirmation';
103 modalInfoRef.componentInstance.message = 'You are about to ' + this.createOrUpdate + ' the role function ' + this.roleFunction.name + '. Do you want to continue?';
104 modalInfoRef.result.then((_res) => {
106 this.showSpinner = true;
107 var exists = false, x;
108 for (x in this.currentRoleFunctions) {
109 if (this.currentRoleFunctions[x].type == this.roleFunction.type
110 && this.currentRoleFunctions[x].code == this.roleFunction.code
111 && this.currentRoleFunctions[x].action == this.roleFunction.action
112 && this.currentRoleFunctions[x].name == this.roleFunction.name) {
113 this.openConfirmationModal('Confirmation', "Role Function already exist.");
115 this.showSpinner = false;
118 if (!this.editDisable) {
119 if (this.currentRoleFunctions[x].type == this.roleFunction.type
120 && this.currentRoleFunctions[x].code == this.roleFunction.code
121 && this.currentRoleFunctions[x].action == this.roleFunction.action
123 this.openConfirmationModal('Confirmation', "Please make sure code, type and action is unique. Please create a role function with a different code or type or action to proceed.");
125 this.showSpinner = false;
131 if (this.selectedType === 'other'){
132 this.roleFunction.type = this.otherTypeValue;
134 this.roleFunction.type = this.selectedType;
137 if (!exists && this.roleFunction.name.trim() != '' && this.roleFunction.code.trim() != '') {
138 var postData = this.roleFunction;
139 //console.log("saveRoleFunction post data :: ",postData);
140 this.adminService.saveRoleFunction(JSON.stringify(postData))
141 .subscribe(_data => {
142 this.showSpinner = false;
143 //console.log("saveRoleFunction response",_data);
144 if (this.editRoleFunction) {
145 this.editRoleFunction.name = this.roleFunction.name;
146 this.passBackRoleFunctionPopup.emit(this.editRoleFunction);
148 this.passBackRoleFunctionPopup.emit(this.roleFunction);
150 if (this.editRoleFunction) {
151 this.openConfirmationModal('Success', "Role function updated successfully.");
153 this.openConfirmationModal('Success', "Role function created successfully.");
156 //console.log(error);
157 this.showSpinner = false;
158 this.openConfirmationModal('Error', error.message);
168 openConfirmationModal(_title: string, _message: string) {
169 const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
170 modalInfoRef.componentInstance.title = _title;
171 modalInfoRef.componentInstance.message = _message;