import { Component, OnInit, Input, HostBinding, ChangeDetectorRef, ChangeDetectionStrategy } from '@angular/core';
import { ComponentLinkService } from 'src/app/core/services/component-link-service/component-link.service';
import { UtilService } from 'src/app/core/services/util-service/util.service';
import { IdGeneratorService } from 'src/app/core/services/id-generator.service';
import { PublicationService } from 'src/app/core/services/publication-service/publication.service';
import { DataResolverService } from 'src/app/core/services/data-resolver/data-resolver.service';
import { Router, ActivatedRoute } from '@angular/router';
interface HierarchyCard {
title: string;
links: Link[];
}
interface Link {
linkObject: any;
linkTitle: string;
newWindow: boolean;
}
type Level = 'parent' | 'sibling' | 'child';
@Component({
selector: 'div[hierarchy-cards-vertical]',
templateUrl: './hierarchy-cards-vertical.component.html',
styleUrls: ['./hierarchy-cards-vertical.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class HierarchyCardsVerticalComponent implements OnInit {
@Input() entity: any;
public Cards: HierarchyCard[] = [];
public id: string;
@HostBinding('class') get class() { return 'col-md-4 space-between'; }
constructor(private linkService: ComponentLinkService,
private util: UtilService,
private idService: IdGeneratorService,
private dataService: DataResolverService,
private pubService: PublicationService,
private router: Router,
private changeDetector: ChangeDetectorRef,
private activatedRoute: ActivatedRoute
) { }
ngOnInit() {
this.id = this.idService.getId();
this.setup();
}
async setup() {
await this.parseHierarchyCard(this.util.extract(this.entity, 'HierarchyCardBoxOne'), 'parent').then(card => this.Cards[0] = card);
await this.parseHierarchyCard(this.util.extract(this.entity, 'HierarchyCardBoxTwo'), 'sibling').then(card => this.Cards[1] = card);
await this.parseHierarchyCard(this.util.extract(this.entity, 'HierarchyCardBoxThree'), 'child').then(card => this.Cards[2] = card);
this.changeDetector.detectChanges();
}
public parseHierarchyCard(JsonCard: any, level: Level): Promise<HierarchyCard> {
// remove elements where there is no component linked to
const linkTargets = (this.util.extract(JsonCard, 'LinkTarget') || []).filter(linkObj => this.linkService.getUrl(linkObj));
const hierarchyCard: HierarchyCard = {
title: this.util.extract(JsonCard, 'TitleContentBox'),
links: linkTargets.map(link => this.linkService.parseLink(link)),
};
const fetchSiteStructure = this.util.extract(JsonCard, 'FetchSiteStructureItems') === 'Yes';
return new Promise(resolve => {
if (fetchSiteStructure) {
this.dataService.getNavigationData().then(navigation => {
const url = `/${this.activatedRoute.snapshot.url.join('/')}`;
const automaticLinks = this.getAutomaticLinks(navigation, level, url);
// Unshift since we want automaticlinks to be first in the list
hierarchyCard.links.unshift(...automaticLinks);
}
);
}
resolve(hierarchyCard);
});
}
public getAutomaticLinks(navigation: any, level: Level, url: string): Link[] {
let SGs = [];
switch (level) {
case 'parent': {
SGs = this.pubService.directParent(navigation, url);
break;
}
case 'sibling': {
SGs = this.pubService.allSiblings(navigation, url);
break;
}
case 'child': {
SGs = this.pubService.allChildren(navigation, url);
break;
}
}
return (SGs || []).map(e => this.SGtoLink(e));
}
public SGtoLink(SG: any): Link {
// This is needed since the link is using the component link component and it expects a link object
const linkObjectMock = {
LinkTarget: this.util.extract(SG, 'Url')
};
const LinkTemplate = {
linkObject: linkObjectMock,
linkTitle: this.util.extract(SG, 'TitleOverride') || this.util.extract(SG, 'Title'),
newWindow: false
};
return LinkTemplate;
}
}
<div class="row">
<ng-container *ngFor="let card of Cards; let i = index">
<div class="col-12" *ngIf="card.links.length !== 0" [ngClass]="{'mb-2': i !== 2 }" >
<div class="card">
<div class="card-body">
<div class="card-title">
{{ card.title }}
</div>
<ul class="list-group list-unstyled">
<li *ngFor="let link of card.links">
<a component-link [linkObject]="link.linkObject">
<i class="icon-chevronLarge-next"></i>
<span>
{{ link.title || link.linkTitle }}
<i *ngIf="link.newWindow" class="icon-link-openNewWindow"> </i>
</span>
</a>
</li>
</ul>
</div>
</div>
</div>
</ng-container>
</div>
@import "src/app/styles/helpers";
@import "../hierarchy-cards-horizontal/hierarchy-cards-horizontal.component.scss";
.card {
//extends card from the other hierarchy-card
@extend .card;
padding: calc-rem(10);
@include media-breakpoint-up(md) {
padding: calc-rem(10);
}
&-title {
@include font-scale(18, light);
}
}