File

app/core/services/label-service/label-service.service.ts

Index

Properties
Methods

Constructor

constructor(cacheService: ClientSideCacheService, publicationService: PublicationService, http: HttpClient)

Constructor populate labels.

Parameters :
Name Type Optional
cacheService ClientSideCacheService No
publicationService PublicationService No
http HttpClient No

Methods

Public getLabel
getLabel(label: string)
Parameters :
Name Type Optional
label string No
Returns : any
Public getValue
getValue(key: string)

Fetch label value against the label key.

Parameters :
Name Type Optional Description
key string No
  • label key
Returns : Observable<string>

Properties

Private labelCache
Type : object
Default value : {}
Private Readonly labelJsonResourceURL
Type : string
Default value : '/content{{pubPath}}/labels'

Module URL to fetch the labels key and values from Tridion

import { Injectable } from '@angular/core';
import { PublicationService } from '../publication-service/publication.service';
import { HttpClient } from '@angular/common/http';
import { ClientSideCacheService } from '../../client-side-cache/client-side-cache.service';
import { Observable, ReplaySubject } from 'rxjs';

@Injectable()
export class LabelService {

	/** Module URL to fetch the labels key and values from Tridion */
	private readonly labelJsonResourceURL = '/content{{pubPath}}/labels';
	private labelCache = {};

	/**
	 * Constructor populate labels.
	 *
	 * @param cacheService
	 * @param publicationService
	 */
	constructor(
		private cacheService: ClientSideCacheService,
		private publicationService: PublicationService,
		private http: HttpClient
	) {}

	/**
	 * Fetch label value against the label key.
	 *
	 * @param key - label key
	 */
	public getValue(key: string): Observable<string> {
		const pubPath = this.publicationService.getPublicationPath();
		const cacheKey: string = pubPath + '-labels';
		const labelSubject = new ReplaySubject<string>(1);
		const labelObs = labelSubject.asObservable();
		const url = this.labelJsonResourceURL.replace('{{pubPath}}', pubPath);

		this.cacheService.getValue(cacheKey, url, 10).toPromise().then(data => {
			const requestedLabel = data[key];
			labelSubject.next(requestedLabel);
		});

		return labelObs;
	}

	public getLabel(label: string) {
		const substituteLabel = `[${label}]`;
		const pubPath = this.publicationService.getPublicationPath();

		const promise = new Promise<string>((resolve, reject) => {
			if (pubPath in this.labelCache) {
				if (label in this.labelCache[pubPath]) {
					resolve(this.labelCache[pubPath][label]);
				} else {
					resolve(label);
				}
			}
			else {
				const cacheKey: string = pubPath + '-labels';
				const url = this.labelJsonResourceURL.replace('{{pubPath}}', pubPath);
				this.cacheService.getValue(cacheKey, url, 10).toPromise().then(res => {
					this.labelCache[pubPath] = res;
					if (label in res) {
						resolve(res[label]);
					} else {
						resolve(label);
					}
				},
				() => {	// Error
					resolve(substituteLabel);
				});
			}
		});

		return promise;
	}
}

result-matching ""

    No results matching ""