File

app/core/services/data-resolver/data-resolver.service.ts

Index

Methods

Constructor

constructor(http: HttpClient, cacheService: ClientSideCacheService, util: UtilService, publicationService: PublicationService)
Parameters :
Name Type Optional
http HttpClient No
cacheService ClientSideCacheService No
util UtilService No
publicationService PublicationService No

Methods

Public getDynamicComponent
getDynamicComponent(componentId: string, templateId: string)

Fetch the dynamic component associated to @param componentId and @param templateId

Parameters :
Name Type Optional Description
componentId string No

Component Id

templateId string No

Template Id

Returns : Promise<any>
Public getKeywords
getKeywords(category: string)

Get all keywords from given tridion category. Returns a promise that gives a list of keyword objects.

Parameters :
Name Type Optional
category string No
Returns : Promise<any>
Public getNavigationData
getNavigationData()

Fetch navigation data from current publication

Returns : Promise<any>
Public getPageData
getPageData(url)

Fetch tridion page data from given URL

Parameters :
Name Optional
url No
Returns : Promise<any>
Private updateLanguage
updateLanguage(page)

Extracts language from a page and set it in the publication service

Parameters :
Name Optional
page No
Returns : void
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ClientSideCacheService } from '../../client-side-cache/client-side-cache.service';
import { UtilService } from '../util-service/util.service';
import { PublicationService } from '../publication-service/publication.service';

@Injectable()
export class DataResolverService {

	constructor(
		private http: HttpClient,
		private cacheService: ClientSideCacheService,
		private util: UtilService,
		private publicationService: PublicationService,
	) { }

	/** Fetch navigation data from current publication */
	public getNavigationData(): Promise<any> {
		const pubPath = this.publicationService.getPublicationPath();
		const result = new Promise((resolve, reject) => {
			this.cacheService.getValue(`content${pubPath}-navigation`, `content${pubPath}/navigation`, 5).toPromise().then(res => {
				resolve(res);
			},
			err => {
				resolve({ error: `Navigation error: ${err}` });
			});
		});

		return result;
	}

	/**
	 * Fetch the dynamic component associated to @param componentId and @param templateId 
	 * @param componentId Component Id
	 * @param templateId  Template Id
	 */
	public getDynamicComponent(componentId: string, templateId: string): Promise<any> {
		const pubPath = this.publicationService.getPublicationPath();

		const result = new Promise((resolve, reject) => {
			this.http.get(`content${pubPath}/dynamicComponent?itemId=${componentId}&dynamicTemplateId=${templateId}`).toPromise().then(content => {
				resolve(content);
			}
			).catch(err => {
				reject(err);
			});
		});
		return result;

	}

	/** Fetch tridion page data from given URL */
	public getPageData(url): Promise<any> {
		const result = new Promise((resolve, reject) => {
			this.http.get(`content${url}?format=json`, {
				// headers: {'Authorization': 'Bearer ' + this.keycloak.token }
			}).toPromise().then(content => {
				this.updateLanguage(content);
				resolve(content);
			},
				err => {
					if (err.status === 404) {
						window.location.replace('/error');
					} else {
						resolve({ error: `Page error: ${err}` });
					}
				});
		});

		return result;
	}

	/**
	 *  Get all keywords from given tridion category.
	 *  Returns a promise that gives a list of keyword objects.
	 */
	public getKeywords(category: string): Promise<any> {
		const pubPath = this.publicationService.getPublicationPath();
		const cat = encodeURIComponent(category);

		const result = new Promise((resolve, reject) => {
			this.cacheService.getValue(`content${pubPath}-category-${category}`, `content${pubPath}/api/category/${cat}`, 5)
				.toPromise().then(keywordList => {
					resolve(keywordList);
				}).catch(error => {
					reject(error);
				}
			);
		});

		return result;
	}

	/** Extracts language from a page and set it in the publication service */
	private updateLanguage(page) {
		const lang = this.util.extract(page, 'Meta', 'og:locale');
		if (lang) {
			this.publicationService.setLanguage(lang.split('-')[0]);
		}
	}
}

result-matching ""

    No results matching ""