app/dxa/dxa-entity/views/tools/tools-document/tools-document.component.ts
selector | [tools-document] |
styleUrls | tools-document.component.scss |
templateUrl | ./tools-document.component.html |
Properties |
|
Methods |
Inputs |
HostBindings |
constructor(util: UtilService, keycloakService: KeycloakService, labels: LabelService)
|
||||||||||||
Parameters :
|
entity | |
class |
getLinkType | ||||
getLinkType(json)
|
||||
Parameters :
Returns :
"external" | "internal"
|
isRestricted |
isRestricted(json: any, openOrRestricted: string)
|
Returns :
boolean
|
isRoleBased |
isRoleBased(json: any, openOrRestricted: string)
|
Returns :
boolean
|
Public login |
login()
|
Returns :
boolean
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
parseRole | ||||||
parseRole(role: string)
|
||||||
Parameters :
Returns :
String
|
parseTool | ||||||
parseTool(json: any)
|
||||||
Parameters :
Returns :
Tool
|
isLoggedIn |
Type : boolean
|
loginLabel |
Type : String
|
Readonly loginLabelKey |
Type : string
|
Default value : 'loginRequired'
|
Readonly restrictedAccessKey |
Type : string
|
Default value : 'restrictedAccess'
|
restrictedAccessLabel |
Type : String
|
roles |
Type : []
|
Default value : []
|
tool |
Type : any
|
import { Component, OnInit, Input, HostBinding, OnChanges } from '@angular/core';
import { UtilService } from 'src/app/core/services/util-service/util.service';
import { Tool } from 'src/app/core/interfaces/interfaces.component';
import { KeycloakService } from 'keycloak-angular';
import { LabelService } from 'src/app/core/services/label-service/label-service.service';
@Component({
selector: '[tools-document]',
templateUrl: './tools-document.component.html',
styleUrls: ['tools-document.component.scss']
})
export class ToolsDocumentComponent implements OnInit {
@Input() entity;
roles = [];
isLoggedIn: boolean;
tool: any;
readonly loginLabelKey = 'loginRequired';
readonly restrictedAccessKey = 'restrictedAccess';
loginLabel: String;
restrictedAccessLabel: String;
@HostBinding('class') get class() { return 'card'; }
constructor(
private util: UtilService,
private keycloakService: KeycloakService,
private labels: LabelService
) {}
ngOnInit(): void {
this.labels.getLabel(this.loginLabelKey).then(label => this.loginLabel = label.toUpperCase());
this.labels.getLabel(this.restrictedAccessKey).then(label => this.restrictedAccessLabel = label.toUpperCase());
this.keycloakService.isLoggedIn().then(res => {
this.isLoggedIn = res;
if (this.isLoggedIn) {
this.keycloakService.getUserRoles(true).map(role => this.roles.push(role));
this.roles = this.roles.map(role => this.parseRole(role.toString()));
}
this.tool = this.parseTool(this.entity);
});
}
parseRole(role: string): String {
if (role.includes('[')) {
role = role.slice(0, role.indexOf('['));
}
return role.trim();
}
parseTool(json: any): Tool {
return {
id: this.util.extract(json, 'Id'),
imageUrl: this.util.extract(json, 'Image', 'Url') || this.util.extract(json, 'image_url'),
altText: this.util.extract('Image', 'Assetmetadata', 'Titlemm') || '',
title: this.util.extract(json, 'ButtonTitle') || this.util.extract(json, 'title') || '',
description: this.util.extract(json, 'Description') || this.util.extract(json, 'description') || '',
toolUrl: this.util.extract(json, 'UrlToTool', 'ExternalLink') || this.util.extract(json, 'ToolFile', 'Url') || this.util.extract(json, 'url'),
restricted: this.isRestricted(json, this.util.extract(json, 'OpenOrRestrictedTool') || this.util.extract(json, 'security_level')),
linkType: this.getLinkType(json),
windowTarget: '_blank',
userLoggedIn: this.isLoggedIn,
roleRequired: this.isRoleBased(json, this.util.extract(json, 'OpenOrRestrictedTool') || this.util.extract(json, 'security_level'))
};
}
getLinkType(json) {
const external = this.util.extract(json, 'UrlToTool', 'ExternalLink');
const file = this.util.extract(json, 'ToolFile', 'Url');
if(external || file) {
return 'external';
} else {
return 'internal';
}
}
isRestricted(json: any, openOrRestricted: string): boolean {
let restricted = false;
if (openOrRestricted && openOrRestricted.startsWith('Role based tool')) {
restricted = true;
const requiredRoles = this.util.extract(json, 'Roles') || this.util.extract(json, 'security_role') || [];
requiredRoles.forEach(requiredRole => {
if (this.roles.includes(requiredRole)) {
restricted = false;
}
});
}
else if (openOrRestricted && openOrRestricted.startsWith('Login required tool')) {
restricted = true;
if (this.isLoggedIn) {
restricted = false;
}
}
return restricted;
}
isRoleBased(json: any, openOrRestricted: string): boolean {
let rolebased = false;
if (openOrRestricted && openOrRestricted.startsWith('Role based tool')) {
rolebased = true;
}
else if (openOrRestricted && openOrRestricted.startsWith('Login required tool')) {
rolebased = false;
}
return rolebased;
}
public login() {
this.keycloakService.login();
return false;
}
}
<ng-container *ngIf="tool">
<div *ngIf="tool.restricted" (click)="!tool.roleRequired && login()"
[ngClass]="{'clickable': !tool.roleRequired}">
<ng-container *ngTemplateOutlet="cardHolder"></ng-container>
</div>
<a [href]="tool.toolUrl" target='_blank' *ngIf="!tool.restricted && tool.linkType === 'external'">
<ng-container *ngTemplateOutlet="cardHolder"></ng-container>
</a>
<a [routerLink]="tool.toolUrl" *ngIf="!tool.restricted && tool.linkType === 'internal'">
<ng-container *ngTemplateOutlet="cardHolder"></ng-container>
</a>
<ng-template #cardHolder>
<div class="card-img-container">
<img class="card-img-top" [src]="tool.imageUrl" [alt]="tool.altText" />
<div class="card-item-lock" *ngIf="tool.restricted">
<div class="lock-overlay">
<i class="icon-card-lock"></i>
<span class="textmargin" *ngIf="!tool.roleRequired && !isLoggedIn"> {{loginLabel}} </span>
<span class="textmargin" *ngIf="tool.roleRequired"> {{restrictedAccessLabel}} </span>
</div>
</div>
</div>
<div class="card-body">
<div class="card-title" *ngIf="tool.title">
{{tool.title}}
</div>
<div class="card-text">
{{tool.description}}
</div>
</div>
</ng-template>
</ng-container>
tools-document.component.scss
@import "src/app/styles/helpers";
:host {
margin-bottom: 0 !important;
border: none;
@include media-breakpoint-up(sm) {
align-self: flex-end;
flex: 0 0 calc(50% - 30px);
margin-bottom: calc-rem(50);
}
@include media-breakpoint-up(lg) {
flex: 0 0 calc(25% - 30px);
}
a {
&:hover {
text-decoration: none;
.card-title {
text-decoration: underline;
}
}
&[target="_blank"]:after {
content: none;
}
}
}
.clickable {
cursor: pointer;
&:hover {
.card-title {
text-decoration: underline;
}
}
}
.card-img-container {
@include aspect-ratio(16 9);
max-height: 180px;
.card-img-top {
object-fit: cover;
}
.card-item-lock {
position: absolute;
top: 0;
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
padding: 15%;
background-color: rgba($white, 0.8);
.lock-overlay {
display: flex;
background-color: white;
.textmargin {
margin-left: .5em;
word-break: break-all;
user-select: none;
}
}
}
}
.card-body {
background-color: $off-white;
justify-content: space-between;
min-height: calc-rem(224);
::ng-deep .bg-off-white & {
background-color: white !important;
border-top: 1px solid $light-green;
}
.card-title {
@include font-scale(20, light);
color: $blue;
margin-bottom: calc-rem(5);
}
.card-text {
color: $cool-grey;
}
}