File

src/services/content/child-content-handler.ts

Index

Properties
Methods

Constructor

constructor(contentService: ContentService)
Parameters :
Name Type Optional
contentService ContentService No

Methods

Private getContentHierarchyInfo
getContentHierarchyInfo(identifier: string, childContentInfo: Content)

Iteratively fetches the hierarchy of the given contentId

Parameters :
Name Type Optional
identifier string No
childContentInfo Content No
Returns : HierarchyInfo[]
Public setChildContents
setChildContents(parentIdentifier: string, level: number, identifier: string)

Initiate the getChildContent call

Parameters :
Name Type Optional
parentIdentifier string No
level number No
identifier string No
Returns : void

Properties

Public contentHierarchyInfo
Type : HierarchyInfo[]
import { Injectable, Inject } from '@angular/core';
import { ContentService, ChildContentRequest, Content, HierarchyInfo } from 'sunbird-sdk';

@Injectable({
    providedIn: 'root'
})
export class ChildContentHandler {
    public contentHierarchyInfo: HierarchyInfo[];
    constructor(@Inject('CONTENT_SERVICE') private contentService: ContentService) {
    }

    /**
     * Initiate the getChildContent call
     */
    public setChildContents(parentIdentifier: string, level: number, identifier: string): void {
        const option: ChildContentRequest = {
            contentId: parentIdentifier,
            hierarchyInfo: null,
            level
        };
        this.contentService.getChildContents(option).toPromise()
            .then((data: any) => {
                this.contentHierarchyInfo = this.getContentHierarchyInfo(identifier, data);
            })
            .catch((error: string) => {
            });
    }

    /**
     * Iteratively fetches the hierarchy of the given contentId
     */
    private getContentHierarchyInfo(identifier: string, childContentInfo: Content): HierarchyInfo[] {
        let hierarchyInfo: HierarchyInfo[];
        if (childContentInfo && childContentInfo.children && childContentInfo.children.length) {
            for (const element of childContentInfo.children) {
                if (!hierarchyInfo && element.identifier === identifier) {
                    return element.hierarchyInfo;
                } else if (!hierarchyInfo) {
                    hierarchyInfo = this.getContentHierarchyInfo(identifier, element);
                    if (hierarchyInfo) {
                        return hierarchyInfo;
                    }
                }
            }
        }
    }
}

results matching ""

    No results matching ""