[MNT-22236] Permission i18n support (#6843)

* localized role pipe

* update tests

* additional tests

* update module

* fix package lock

* update module settings

* try update code

* update import

* test fixes
This commit is contained in:
Denys Vuika
2021-03-23 16:22:32 +00:00
committed by GitHub
parent c0a3f9d99d
commit b97aa7dfee
11 changed files with 217 additions and 64 deletions

View File

@@ -5,9 +5,9 @@
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@alfresco/js-api": { "@alfresco/js-api": {
"version": "4.3.0-3278", "version": "4.3.0",
"resolved": "https://registry.npmjs.org/@alfresco/js-api/-/js-api-4.3.0-3278.tgz", "resolved": "https://registry.npmjs.org/@alfresco/js-api/-/js-api-4.3.0.tgz",
"integrity": "sha512-rSNfbztQkiiNZ/B3mTazeNUpIjJuxhlzKLfJKa2eEyQNOr/uazNg/LiYQ0pPoTDWexurYfCetlHhd0IW//Qbwg==", "integrity": "sha512-Q/OeFqQ5uQWG2mGsXBbiG6CKh5MJvdL5h3mkcDqyFHMzZwM+URB082KOlL+6tQN5JgWegHgA3jGZE8EOPviQxw==",
"requires": { "requires": {
"event-emitter": "^0.3.5", "event-emitter": "^0.3.5",
"minimatch": "3.0.4", "minimatch": "3.0.4",
@@ -212,6 +212,15 @@
} }
} }
}, },
"call-bind": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
"integrity": "sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==",
"requires": {
"function-bind": "^1.1.1",
"get-intrinsic": "^1.0.2"
}
},
"caseless": { "caseless": {
"version": "0.12.0", "version": "0.12.0",
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
@@ -520,9 +529,9 @@
}, },
"dependencies": { "dependencies": {
"type": { "type": {
"version": "2.3.0", "version": "2.5.0",
"resolved": "https://registry.npmjs.org/type/-/type-2.3.0.tgz", "resolved": "https://registry.npmjs.org/type/-/type-2.5.0.tgz",
"integrity": "sha512-rgPIqOdfK/4J9FhiVrZ3cveAjRRo5rsQBAIhnylX874y1DX/kEKSVdLsnuHB6l1KTjHyU01VjiMBHgU2adejyg==" "integrity": "sha512-180WMDQaIMm3+7hGXWf12GtdniDEy7nYcyFMKJn/eZz/6tSLXrUN9V0wKSbMjej0I1WHWbpREDEKHtqPQa9NNw=="
} }
} }
}, },
@@ -702,6 +711,16 @@
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
}, },
"get-intrinsic": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.1.1.tgz",
"integrity": "sha512-kWZrnVM42QCiEA2Ig1bG8zjoIMOgxWwYCEeNdwY6Tv/cOSeGpcoX4pXHfKUxNKVoArnrEr2e9srnAxxGIraS9Q==",
"requires": {
"function-bind": "^1.1.1",
"has": "^1.0.3",
"has-symbols": "^1.0.1"
}
},
"getpass": { "getpass": {
"version": "0.1.7", "version": "0.1.7",
"resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz",
@@ -755,6 +774,11 @@
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
"integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0="
}, },
"has-symbols": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.2.tgz",
"integrity": "sha512-chXa79rL/UC2KlX17jo3vRGz0azaWEx5tGqZg5pO3NUyEJVB17dMruQlzCCOfUvElghKcm5194+BCRvi2Rv/Gw=="
},
"hosted-git-info": { "hosted-git-info": {
"version": "2.8.8", "version": "2.8.8",
"resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz",
@@ -1193,6 +1217,11 @@
"resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz",
"integrity": "sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==" "integrity": "sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ=="
}, },
"object-inspect": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.9.0.tgz",
"integrity": "sha512-i3Bp9iTqwhaLZBxGkRfo5ZbE07BQRT7MGu8+nNgwW9ItGp1TzCTw2DLEoWwjClxBjOFI/hWljTAmYGCEwmtnOw=="
},
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
@@ -1334,9 +1363,12 @@
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
}, },
"qs": { "qs": {
"version": "6.9.6", "version": "6.10.1",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.6.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.10.1.tgz",
"integrity": "sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ==" "integrity": "sha512-M528Hph6wsSVOBiYUnGf+K/7w0hNshs/duGsNXPUCLH5XAqjEtiPGwNONLV0tBH8NoGb0mvD5JubnUTrujKDTg==",
"requires": {
"side-channel": "^1.0.4"
}
}, },
"read-installed": { "read-installed": {
"version": "4.0.3", "version": "4.0.3",
@@ -1492,9 +1524,9 @@
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
}, },
"semver": { "semver": {
"version": "7.3.4", "version": "7.3.5",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.4.tgz", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz",
"integrity": "sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==", "integrity": "sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==",
"requires": { "requires": {
"lru-cache": "^6.0.0" "lru-cache": "^6.0.0"
} }
@@ -1509,6 +1541,16 @@
"rechoir": "^0.6.2" "rechoir": "^0.6.2"
} }
}, },
"side-channel": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
"integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==",
"requires": {
"call-bind": "^1.0.0",
"get-intrinsic": "^1.0.2",
"object-inspect": "^1.9.0"
}
},
"slide": { "slide": {
"version": "1.1.6", "version": "1.1.6",
"resolved": "https://registry.npmjs.org/slide/-/slide-1.1.6.tgz", "resolved": "https://registry.npmjs.org/slide/-/slide-1.1.6.tgz",

View File

@@ -33,11 +33,11 @@
[placeholder]="entry.data.getValue(entry.row, entry.col)" [placeholder]="entry.data.getValue(entry.row, entry.col)"
value="{{entry.data.getValue(entry.row, entry.col)}}" value="{{entry.data.getValue(entry.row, entry.col)}}"
(selectionChange)="saveNewRole($event, entry.row.obj)"> (selectionChange)="saveNewRole($event, entry.row.obj)">
<mat-option *ngFor="let role of settableRoles" [value]="role">{{ role }}</mat-option> <mat-option *ngFor="let role of settableRoles" [value]="role">{{ role | adfLocalizedRole }}</mat-option>
</mat-select> </mat-select>
</mat-form-field> </mat-form-field>
<ng-template #show_only_label> <ng-template #show_only_label>
<span>{{entry.data.getValue(entry.row, entry.col)}}</span> <span>{{ entry.data.getValue(entry.row, entry.col) | adfLocalizedRole }}</span>
</ng-template> </ng-template>
</ng-template> </ng-template>
</data-column> </data-column>

View File

@@ -31,7 +31,7 @@ import { fakeNodeWithPermissions,
import { ContentTestingModule } from '../../../testing/content.testing.module'; import { ContentTestingModule } from '../../../testing/content.testing.module';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
describe('PermissionDisplayComponent', () => { describe('PermissionListComponent', () => {
let fixture: ComponentFixture<PermissionListComponent>; let fixture: ComponentFixture<PermissionListComponent>;
let component: PermissionListComponent; let component: PermissionListComponent;
@@ -127,10 +127,10 @@ describe('PermissionDisplayComponent', () => {
const options: any = fixture.debugElement.queryAll(By.css('mat-option')); const options: any = fixture.debugElement.queryAll(By.css('mat-option'));
expect(options).not.toBeNull(); expect(options).not.toBeNull();
expect(options.length).toBe(4); expect(options.length).toBe(4);
expect(options[0].nativeElement.innerText).toContain('SiteCollaborator'); expect(options[0].nativeElement.innerText).toContain('ADF.ROLES.SITECOLLABORATOR');
expect(options[1].nativeElement.innerText).toContain('SiteConsumer'); expect(options[1].nativeElement.innerText).toContain('ADF.ROLES.SITECONSUMER');
expect(options[2].nativeElement.innerText).toContain('SiteContributor'); expect(options[2].nativeElement.innerText).toContain('ADF.ROLES.SITECONTRIBUTOR');
expect(options[3].nativeElement.innerText).toContain('SiteManager'); expect(options[3].nativeElement.innerText).toContain('ADF.ROLES.SITEMANAGER');
}); });
}); });
})); }));
@@ -152,11 +152,11 @@ describe('PermissionDisplayComponent', () => {
const options: any = fixture.debugElement.queryAll(By.css('mat-option')); const options: any = fixture.debugElement.queryAll(By.css('mat-option'));
expect(options).not.toBeNull(); expect(options).not.toBeNull();
expect(options.length).toBe(5); expect(options.length).toBe(5);
expect(options[0].nativeElement.innerText).toContain('Contributor'); expect(options[0].nativeElement.innerText).toContain('ADF.ROLES.CONTRIBUTOR');
expect(options[1].nativeElement.innerText).toContain('Collaborator'); expect(options[1].nativeElement.innerText).toContain('ADF.ROLES.COLLABORATOR');
expect(options[2].nativeElement.innerText).toContain('Coordinator'); expect(options[2].nativeElement.innerText).toContain('ADF.ROLES.COORDINATOR');
expect(options[3].nativeElement.innerText).toContain('Editor'); expect(options[3].nativeElement.innerText).toContain('ADF.ROLES.EDITOR');
expect(options[4].nativeElement.innerText).toContain('Consumer'); expect(options[4].nativeElement.innerText).toContain('ADF.ROLES.CONSUMER');
}); });
}); });
})); }));

View File

@@ -35,11 +35,11 @@ export class PermissionListComponent implements OnInit {
/** Emitted when the permission is updated. */ /** Emitted when the permission is updated. */
@Output() @Output()
update: EventEmitter<PermissionElement> = new EventEmitter(); update = new EventEmitter<PermissionElement>();
/** Emitted when an error occurs. */ /** Emitted when an error occurs. */
@Output() @Output()
error: EventEmitter<any> = new EventEmitter(); error = new EventEmitter<any>();
permissionList: PermissionDisplayModel[]; permissionList: PermissionDisplayModel[];
settableRoles: any[]; settableRoles: any[];
@@ -47,7 +47,6 @@ export class PermissionListComponent implements OnInit {
constructor(private nodeService: NodesApiService, constructor(private nodeService: NodesApiService,
private nodePermissionService: NodePermissionService) { private nodePermissionService: NodePermissionService) {
} }
ngOnInit() { ngOnInit() {
@@ -61,7 +60,7 @@ export class PermissionListComponent implements OnInit {
private fetchNodePermissions() { private fetchNodePermissions() {
this.nodeService.getNode(this.nodeId).subscribe((node: Node) => { this.nodeService.getNode(this.nodeId).subscribe((node: Node) => {
this.actualNode = node; this.actualNode = node;
this.permissionList = this.getPermissionList(node); this.permissionList = this.nodePermissionService.getNodePermissions(node);
this.nodePermissionService.getNodeRoles(node).subscribe((settableList: string[]) => { this.nodePermissionService.getNodeRoles(node).subscribe((settableList: string[]) => {
this.settableRoles = settableList; this.settableRoles = settableList;
@@ -69,26 +68,9 @@ export class PermissionListComponent implements OnInit {
}); });
} }
private getPermissionList(node: Node): PermissionDisplayModel[] {
const allPermissions: PermissionDisplayModel[] = [];
if (node?.permissions?.locallySet) {
node.permissions.locallySet.map((permissionElement: PermissionElement) => {
const permission = new PermissionDisplayModel(permissionElement);
allPermissions.push(permission);
});
}
if (node?.permissions?.inherited) {
node.permissions.inherited.map((permissionElement: PermissionElement) => {
const permissionInherited = new PermissionDisplayModel(permissionElement);
permissionInherited.isInherited = true;
allPermissions.push(permissionInherited);
});
}
return allPermissions;
}
saveNewRole(event: any, permissionRow: PermissionDisplayModel) { saveNewRole(event: any, permissionRow: PermissionDisplayModel) {
const updatedPermissionRole: PermissionElement = this.buildUpdatedPermission(event.value, permissionRow); const updatedPermissionRole = this.buildUpdatedPermission(event.value, permissionRow);
this.nodePermissionService.updatePermissionRole(this.actualNode, updatedPermissionRole) this.nodePermissionService.updatePermissionRole(this.actualNode, updatedPermissionRole)
.subscribe(() => { .subscribe(() => {
this.update.emit(updatedPermissionRole); this.update.emit(updatedPermissionRole);
@@ -96,11 +78,11 @@ export class PermissionListComponent implements OnInit {
} }
private buildUpdatedPermission(newRole: string, permissionRow: PermissionDisplayModel): PermissionElement { private buildUpdatedPermission(newRole: string, permissionRow: PermissionDisplayModel): PermissionElement {
const permissionRole: PermissionElement = {}; return {
permissionRole.accessStatus = permissionRow.accessStatus; accessStatus: permissionRow.accessStatus,
permissionRole.name = newRole; name: newRole,
permissionRole.authorityId = permissionRow.authorityId; authorityId: permissionRow.authorityId
return permissionRole; };
} }
removePermission(permissionRow: PermissionDisplayModel) { removePermission(permissionRow: PermissionDisplayModel) {
@@ -111,5 +93,4 @@ export class PermissionListComponent implements OnInit {
error => this.error.emit(error) error => this.error.emit(error)
); );
} }
} }

View File

@@ -22,7 +22,7 @@ import { MaterialModule } from '../material.module';
import { PermissionListComponent } from './components/permission-list/permission-list.component'; import { PermissionListComponent } from './components/permission-list/permission-list.component';
import { AddPermissionComponent } from './components/add-permission/add-permission.component'; import { AddPermissionComponent } from './components/add-permission/add-permission.component';
import { AddPermissionDialogComponent } from './components/add-permission/add-permission-dialog.component'; import { AddPermissionDialogComponent } from './components/add-permission/add-permission-dialog.component';
import { CoreModule } from '@alfresco/adf-core'; import { CoreModule, PipeModule } from '@alfresco/adf-core';
import { InheritPermissionDirective } from './components/inherited-button.directive'; import { InheritPermissionDirective } from './components/inherited-button.directive';
import { NoPermissionTemplateComponent } from './components/permission-list/no-permission.component'; import { NoPermissionTemplateComponent } from './components/permission-list/no-permission.component';
import { AddPermissionPanelComponent } from './components/add-permission/add-permission-panel.component'; import { AddPermissionPanelComponent } from './components/add-permission/add-permission-panel.component';
@@ -35,7 +35,8 @@ import { SearchModule } from '../search/search.module';
FormsModule, FormsModule,
ReactiveFormsModule, ReactiveFormsModule,
MaterialModule, MaterialModule,
SearchModule SearchModule,
PipeModule
], ],
declarations: [ declarations: [
PermissionListComponent, PermissionListComponent,

View File

@@ -20,6 +20,7 @@ import { Observable, of, from, throwError } from 'rxjs';
import { AlfrescoApiService, SearchService, NodesApiService, TranslationService } from '@alfresco/adf-core'; import { AlfrescoApiService, SearchService, NodesApiService, TranslationService } from '@alfresco/adf-core';
import { QueryBody, Node, NodeEntry, PathElement, GroupMemberEntry, GroupMemberPaging, PermissionElement } from '@alfresco/js-api'; import { QueryBody, Node, NodeEntry, PathElement, GroupMemberEntry, GroupMemberPaging, PermissionElement } from '@alfresco/js-api';
import { switchMap, map } from 'rxjs/operators'; import { switchMap, map } from 'rxjs/operators';
import { PermissionDisplayModel } from '../models/permission.model';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@@ -52,6 +53,25 @@ export class NodePermissionService {
); );
} }
getNodePermissions(node: Node): PermissionDisplayModel[] {
const result: PermissionDisplayModel[] = [];
if (node?.permissions?.locallySet) {
node.permissions.locallySet.map((permissionElement) => {
result.push(new PermissionDisplayModel(permissionElement));
});
}
if (node?.permissions?.inherited) {
node.permissions.inherited.map((permissionElement) => {
const permissionInherited = new PermissionDisplayModel(permissionElement);
permissionInherited.isInherited = true;
result.push(permissionInherited);
});
}
return result;
}
/** /**
* Updates the permission role for a node. * Updates the permission role for a node.
* @param node Target node * @param node Target node
@@ -128,14 +148,13 @@ export class NodePermissionService {
private transformNodeToPermissionElement(nodes: NodeEntry[], nodeRole: any): PermissionElement[] { private transformNodeToPermissionElement(nodes: NodeEntry[], nodeRole: any): PermissionElement[] {
return nodes.map((node) => { return nodes.map((node) => {
const newPermissionElement: PermissionElement = <PermissionElement> { return {
'authorityId': node.entry.properties['cm:authorityName'] ? 'authorityId': node.entry.properties['cm:authorityName'] ?
node.entry.properties['cm:authorityName'] : node.entry.properties['cm:authorityName'] :
node.entry.properties['cm:userName'], node.entry.properties['cm:userName'],
'name': nodeRole, 'name': nodeRole,
'accessStatus': 'ALLOWED' 'accessStatus': 'ALLOWED'
}; };
return newPermissionElement;
}); });
} }
@@ -188,10 +207,11 @@ export class NodePermissionService {
private buildRetrieveSiteQueryBody(nodePath: PathElement[]): QueryBody { private buildRetrieveSiteQueryBody(nodePath: PathElement[]): QueryBody {
const pathNames = nodePath.map((node: PathElement) => 'name: "' + node.name + '"'); const pathNames = nodePath.map((node: PathElement) => 'name: "' + node.name + '"');
const buildedPathNames = pathNames.join(' OR '); const builtPathNames = pathNames.join(' OR ');
return { return {
'query': { 'query': {
'query': buildedPathNames 'query': builtPathNames
}, },
'paging': { 'paging': {
'maxItems': 100, 'maxItems': 100,

View File

@@ -1,4 +1,18 @@
{ {
"ADF": {
"ROLES": {
"ALL": "All",
"CONTRIBUTOR": "Contributor",
"COLLABORATOR": "Collaborator",
"COORDINATOR": "Coordinator",
"EDITOR": "Editor",
"CONSUMER": "Consumer",
"SITECOLLABORATOR": "Site Collaborator",
"SITECONSUMER": "Site Consumer",
"SITECONTRIBUTOR": "Site Contributor",
"SITEMANAGER": "Site Manager"
}
},
"SAVE": "SAVE", "SAVE": "SAVE",
"COMPLETE": "COMPLETE", "COMPLETE": "COMPLETE",
"CANCEL": "CANCEL", "CANCEL": "CANCEL",

View File

@@ -0,0 +1,50 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LocalizedRolePipe } from './localized-role.pipe';
describe('LocalizedRolePipe', () => {
let translationService: any;
let pipe: LocalizedRolePipe;
beforeEach(() => {
translationService = jasmine.createSpyObj('TranslationService', ['instant']);
pipe = new LocalizedRolePipe(translationService);
});
it('should return null', () => {
expect(pipe.transform(null)).toBeNull();
});
it('should translate value', () => {
translationService.instant.and.returnValue('Consumer');
expect(pipe.transform('ADF.ROLES.CONSUMER')).toEqual('Consumer');
});
it('should return the key when translation not present', () => {
translationService.instant.and.callFake((value) => {
if (value === 'ADF.ROLES.CONSUMER') {
return 'Consumer';
} else {
return value;
}
});
expect(pipe.transform('Contributor')).toBe('ADF.ROLES.CONTRIBUTOR');
expect(pipe.transform('Consumer')).toEqual('Consumer');
});
});

View File

@@ -0,0 +1,39 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Pipe, PipeTransform } from '@angular/core';
import { TranslationService } from '../services/translation.service';
@Pipe({
name: 'adfLocalizedRole'
})
export class LocalizedRolePipe implements PipeTransform {
constructor(private translationService: TranslationService) {
}
transform(value: string): any {
if (value) {
const key = `ADF.ROLES.${value.toUpperCase()}`;
const translation = this.translationService.instant(key);
if (translation) {
return translation;
}
}
return value;
}
}

View File

@@ -30,10 +30,13 @@ import { FileTypePipe } from './file-type.pipe';
import { MultiValuePipe } from './multi-value.pipe'; import { MultiValuePipe } from './multi-value.pipe';
import { LocalizedDatePipe } from './localized-date.pipe'; import { LocalizedDatePipe } from './localized-date.pipe';
import { DecimalNumberPipe } from './decimal-number.pipe'; import { DecimalNumberPipe } from './decimal-number.pipe';
import { LocalizedRolePipe } from './localized-role.pipe';
import { TranslateModule } from '@ngx-translate/core';
@NgModule({ @NgModule({
imports: [ imports: [
CommonModule CommonModule,
TranslateModule
], ],
declarations: [ declarations: [
FileSizePipe, FileSizePipe,
@@ -47,7 +50,8 @@ import { DecimalNumberPipe } from './decimal-number.pipe';
FileTypePipe, FileTypePipe,
MultiValuePipe, MultiValuePipe,
LocalizedDatePipe, LocalizedDatePipe,
DecimalNumberPipe DecimalNumberPipe,
LocalizedRolePipe
], ],
providers: [ providers: [
FileSizePipe, FileSizePipe,
@@ -60,7 +64,8 @@ import { DecimalNumberPipe } from './decimal-number.pipe';
FileTypePipe, FileTypePipe,
MultiValuePipe, MultiValuePipe,
LocalizedDatePipe, LocalizedDatePipe,
DecimalNumberPipe DecimalNumberPipe,
LocalizedRolePipe
], ],
exports: [ exports: [
FileSizePipe, FileSizePipe,
@@ -74,7 +79,8 @@ import { DecimalNumberPipe } from './decimal-number.pipe';
FileTypePipe, FileTypePipe,
MultiValuePipe, MultiValuePipe,
LocalizedDatePipe, LocalizedDatePipe,
DecimalNumberPipe DecimalNumberPipe,
LocalizedRolePipe
] ]
}) })
export class PipeModule { export class PipeModule {

View File

@@ -27,5 +27,5 @@ export * from './node-name-tooltip.pipe';
export * from './text-highlight.pipe'; export * from './text-highlight.pipe';
export * from './time-ago.pipe'; export * from './time-ago.pipe';
export * from './user-initial.pipe'; export * from './user-initial.pipe';
export * from './localized-role.pipe';
export * from './pipe.module'; export * from './pipe.module';