File

app/dxa/dxa-entity/views/hierarchy-cards/hierarchy-cards-vertical/hierarchy-cards-vertical.component.ts

Index

Properties

Properties

linkObject
linkObject: any
Type : any
linkTitle
linkTitle: string
Type : string
newWindow
newWindow: boolean
Type : boolean
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;
	}
}

result-matching ""

    No results matching ""