|
| 1 | +import { Component, Injector, Inject, OnInit, Optional } from '@angular/core'; |
| 2 | +import { |
| 3 | + MatDialogRef, |
| 4 | + MAT_DIALOG_DATA, |
| 5 | + MatCheckboxChange |
| 6 | +} from '@angular/material'; |
| 7 | +import * as _ from 'lodash'; |
| 8 | +import { |
| 9 | + RoleServiceProxy, |
| 10 | + GetRoleForEditOutput, |
| 11 | + RoleDto, |
| 12 | + ListResultDtoOfPermissionDto, |
| 13 | + PermissionDto, |
| 14 | + CreateRoleDto |
| 15 | +} from '@shared/service-proxies/service-proxies'; |
| 16 | +import { AppComponentBase } from '@shared/app-component-base'; |
| 17 | +import { finalize } from 'rxjs/operators'; |
| 18 | + |
| 19 | +@Component({ |
| 20 | + templateUrl: 'create-or-edit-role-dialog.component.html', |
| 21 | + styles: [ |
| 22 | + ` |
| 23 | + mat-form-field { |
| 24 | + width: 100%; |
| 25 | + } |
| 26 | + mat-checkbox { |
| 27 | + padding-bottom: 5px; |
| 28 | + } |
| 29 | + ` |
| 30 | + ] |
| 31 | +}) |
| 32 | +export class CreateOrEditRoleDialogComponent extends AppComponentBase |
| 33 | + implements OnInit { |
| 34 | + role: RoleDto = new RoleDto(); |
| 35 | + permissions: PermissionDto[] = []; |
| 36 | + grantedPermissionNames: string[] = []; |
| 37 | + checkedPermissionsMap: { [key: string]: boolean } = {}; |
| 38 | + defaultPermissionCheckedStatus = true; |
| 39 | + permissionName = ''; |
| 40 | + saving = false; |
| 41 | + |
| 42 | + constructor( |
| 43 | + injector: Injector, |
| 44 | + private _roleService: RoleServiceProxy, |
| 45 | + private _dialogRef: MatDialogRef<CreateOrEditRoleDialogComponent>, |
| 46 | + @Optional() @Inject(MAT_DIALOG_DATA) private _id: number |
| 47 | + ) { |
| 48 | + super(injector); |
| 49 | + } |
| 50 | + |
| 51 | + ngOnInit(): void { |
| 52 | + if (this.isCreateDialog()) { |
| 53 | + this._roleService |
| 54 | + .getAllPermissions() |
| 55 | + .subscribe((result: ListResultDtoOfPermissionDto) => { |
| 56 | + this.permissions = result.items; |
| 57 | + this.setInitialPermissionsStatus(); |
| 58 | + }); |
| 59 | + } else { |
| 60 | + this._roleService |
| 61 | + .getRoleForEdit(this._id) |
| 62 | + .subscribe((result: GetRoleForEditOutput) => { |
| 63 | + this.role.init(result.role); |
| 64 | + _.map(result.permissions, item => { |
| 65 | + const permission = new PermissionDto(); |
| 66 | + permission.init(item); |
| 67 | + this.permissions.push(permission); |
| 68 | + }); |
| 69 | + this.grantedPermissionNames = result.grantedPermissionNames; |
| 70 | + this.permissionName = result.role.name; |
| 71 | + this.setInitialPermissionsStatus(); |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + isCreateDialog(): boolean { |
| 77 | + return this._id <= 0; |
| 78 | + } |
| 79 | + |
| 80 | + setInitialPermissionsStatus(): void { |
| 81 | + _.map(this.permissions, item => { |
| 82 | + this.checkedPermissionsMap[item.name] = this.isCreateDialog() |
| 83 | + ? this.defaultPermissionCheckedStatus |
| 84 | + : _.includes(this.grantedPermissionNames, item.name); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + isPermissionChecked(permissionName: string): boolean { |
| 89 | + if (this.isCreateDialog()) { |
| 90 | + return this.defaultPermissionCheckedStatus; |
| 91 | + } |
| 92 | + return _.includes(this.grantedPermissionNames, permissionName); |
| 93 | + } |
| 94 | + |
| 95 | + onPermissionChange(permission: PermissionDto, $event: MatCheckboxChange) { |
| 96 | + this.checkedPermissionsMap[permission.name] = $event.checked; |
| 97 | + } |
| 98 | + |
| 99 | + getCheckedPermissions(): string[] { |
| 100 | + const permissions: string[] = []; |
| 101 | + _.forEach(this.checkedPermissionsMap, function (value, key) { |
| 102 | + if (value) { |
| 103 | + permissions.push(key); |
| 104 | + } |
| 105 | + }); |
| 106 | + return permissions; |
| 107 | + } |
| 108 | + |
| 109 | + save(): void { |
| 110 | + this.saving = true; |
| 111 | + |
| 112 | + const role = new RoleDto(); |
| 113 | + role.permissions = this.getCheckedPermissions(); |
| 114 | + role.init(this.role); |
| 115 | + |
| 116 | + if (this.isCreateDialog()) { |
| 117 | + const createRole = new CreateRoleDto(); |
| 118 | + createRole.init(role); |
| 119 | + this._roleService.create(createRole) |
| 120 | + .pipe( |
| 121 | + finalize(() => { |
| 122 | + this.saving = false; |
| 123 | + }) |
| 124 | + ).subscribe(() => { |
| 125 | + this.notify.info(this.l('SavedSuccessfully')); |
| 126 | + this.close(true); |
| 127 | + }); |
| 128 | + } else { |
| 129 | + this._roleService.update(role) |
| 130 | + .pipe( |
| 131 | + finalize(() => { |
| 132 | + this.saving = false; |
| 133 | + }) |
| 134 | + ).subscribe(() => { |
| 135 | + this.notify.info(this.l('SavedSuccessfully')); |
| 136 | + this.close(true); |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + close(result: any): void { |
| 142 | + this._dialogRef.close(result); |
| 143 | + } |
| 144 | +} |
0 commit comments