File

app/dxa/dxa-entity/views/search-based-content/search-based-content-main/search-based-content-main.component.ts

Implements

OnInit OnDestroy

Metadata

selector div[search-based-content-main]
styleUrls ./search-based-content-main.component.scss
templateUrl ./search-based-content-main.component.html

Index

Properties
Methods
Inputs
HostBindings

Constructor

constructor(searchFactory: SearchFactory, util: UtilService, publicationService: PublicationService, idGenerator: IdGeneratorService)
Parameters :
Name Type Optional
searchFactory SearchFactory No
util UtilService No
publicationService PublicationService No
idGenerator IdGeneratorService No

Inputs

entity

HostBindings

class

Methods

getCategoryTcmId
getCategoryTcmId(id)

Builds a category tcm-id based on a given id

Parameters :
Name Optional
id No
Returns : string
getColClass
getColClass()

Get different boostrap column classes depending on the search type and state of view.

Returns : "col-12 col-sm-6 col-md-4 mb-4" | "col-md-12 mb-4"
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
setView
setView(view)

Set view between Grid and List, used for news search

Parameters :
Name Optional
view No
Returns : void

Properties

newsView
Type : string
Default value : 'GRID'
noResults
Type : boolean
result
search
Type : SearchService
searchId
Type : string
searchSubscription
Type : Subscription
searchType
Type : string
sortData
Type : SortData
import { Component, OnInit, Input, OnDestroy, HostBinding } from '@angular/core';
import { SearchService, SearchFactory, HttpParams } from 'skf-search-angular-service';
import { UtilService } from 'src/app/core/services/util-service/util.service';
import { PublicationService } from 'src/app/core/services/publication-service/publication.service';
import { IdGeneratorService } from 'src/app/core/services/id-generator.service';
import { Subscription } from 'rxjs';
import { SortData } from 'src/app/sitesearch/shared/sort-options/sort-options.component';
import { AppConfig } from '../../../../../app.config';

@Component({
	selector: 'div[search-based-content-main]',
	templateUrl: './search-based-content-main.component.html',
	styleUrls: ['./search-based-content-main.component.scss']
})
export class SearchBasedContentMainComponent implements OnInit, OnDestroy {

	@Input() entity;
	search: SearchService;
	result;
	searchType: string;
	searchSubscription: Subscription;
	sortData: SortData;
	noResults: boolean;
	searchId: string;
	newsView = 'GRID';

	@HostBinding('class') get class() { return 'col-md-12 space-between'; }

	constructor(
		private searchFactory: SearchFactory,
		private util: UtilService,
		private publicationService: PublicationService,
		private idGenerator: IdGeneratorService,
	) {
		this.searchId = this.idGenerator.getId();	// ID for search service instance
	}

	ngOnInit(): void {
		
		const lang = this.publicationService.getLanguage();
		const pubId = this.publicationService.getPublicationId();

		this.searchType = this.util.extract(this.entity, 'InformationToPresent').toLowerCase();

		const products = this.util.extract(this.entity, 'SearchPhraseProducts') || [];
		const services = this.util.extract(this.entity, 'SearchPhraseServices') || [];
		const industries = this.util.extract(this.entity, 'SearchPhraseIndustries') || [];

		const categories = [industries, services].filter(category => category.length > 0);

		let searchString = `site=${pubId}&language=${lang}`;

		/**
		 * Different searchtypes can be 'news', 'tools', 'category', 'products' from Findwise.
		  */
		if (this.searchType === 'news'){ 
			searchString += '&sort=date';
			this.search = this.searchFactory.get(`search-based-${this.searchId}`, `sbcc-news-${AppConfig.settings.searchEnvironment.name}`);			
		} else if(this.searchType === 'tools') {
			this.search = this.searchFactory.get(`search-based-${this.searchId}`, `sbcc-tools-${AppConfig.settings.searchEnvironment.name}`);
		} else if (this.searchType === 'products') {
			/**	Loop through categories and build query.
			*	Format: [category tcm-id]=[keyword name]&[category tcm-id]=[keyword name]&...
			*/
			categories.forEach(category => {
				const catId = this.getCategoryTcmId(this.util.extract(category[0], 'TaxonomyId'));
				if (catId) {
					category.forEach(keyword => {
						const title = encodeURIComponent(keyword.Title);
						searchString += `&${catId}=${title}`;
					});
				}
			});

			/** Product taxonomy uses taxonomy as parameter. (Findwise requirement)
			 * 	This is assumed to be the second lowest level in the taxonomy. The lowest (leaf) level is
			 *  used as a checkbox filter, so it should not be included in the taxonomy.
			 * */
			products.forEach(product => {
				let path = product.Path.replace(/\\/g, '/');

				if (path.substring(0, 1) === '/') {
					path = path.slice(1);
				}

				searchString += `&taxonomy=${path}`;
			});
			
			this.search = this.searchFactory.get(`search-based-${this.searchId}`, `sbcc-products-${AppConfig.settings.searchEnvironment.name}`);			
		}
		
		this.searchSubscription = this.search.result.subscribe(res => {
			this.result = res;

			this.sortData = {
				systems: null,
				sortOptions: this.result.documentList.sortOptions,
			};

			this.noResults = this.result.documentList.documents.length === 0;
		});

		const httpParams = new HttpParams({ fromString: searchString });
		this.search.doSearchWithParameters({ params: httpParams }); 
		
	}

	ngOnDestroy(): void {
		if (this.searchSubscription) { this.searchSubscription.unsubscribe(); }
	}

	/** Builds a category tcm-id based on a given id */
	getCategoryTcmId(id): string {
		let taxonomyId;

		if (id) {
			const pubId = this.publicationService.getPublicationId();
			taxonomyId = `tcm:${pubId}-${id}-512`;	// 512 is id for category type
		}
		return taxonomyId;
	}

	/** Set view between Grid and List, used for news search */
	setView(view) {
		this.newsView = view;
	}

	/** Get different boostrap column classes depending on the search type and state of view. */
	getColClass() {
		if ((this.newsView === 'GRID' && this.searchType === 'news') || this.searchType !== 'news') {
			return 'col-12 col-sm-6 col-md-4 mb-4';
		} else if (this.newsView === 'LIST' && this.searchType === 'news') {
			return 'col-md-12 mb-4';
		}
	}
}
<div [id]="'result-' + searchId" *ngIf="result">
    <div class="row">
        <div class="col-lg-9 offset-lg-3 toolbelt">
            <sort-options *ngIf="result?.documentList" [data]="sortData"
                [viewOptions]="searchType === 'news' && ['GRID', 'LIST']" (view)="setView($event)" [id]="searchId"></sort-options>
        </div>
        <div class="col-lg-3">
            <app-facet-list [facets]="result.facets" [stats]="result.stats" [id]="searchId"></app-facet-list>
        </div>
        <div class="col-md-12 col-lg-9" *ngIf="!noResults">
            <div class="row">
                <ng-container *ngFor="let document of result.documentList.documents">
                    <div [ngClass]="[getColClass()]">
                        <tridion-document *ngIf="document && searchType === 'products'" [document]="document"></tridion-document>
                        <div class="news-doc" news-document *ngIf="document && searchType === 'news'" [newsDocument]="document" [type]="newsView"></div>
                        <div tools-document *ngIf="document && searchType === 'tools'" [entity]="document"></div>
                    </div>
                </ng-container>
            </div>
            <app-pagination [pagination]="result.documentList.pagination" *ngIf="result?.documentList?.pagination" [id]="searchId"></app-pagination>
        </div>
        <div class="col-md-12 col-lg-9" *ngIf="noResults">
            <no-search-result></no-search-result>
        </div>
    </div>
</div>

./search-based-content-main.component.scss

@import "src/app/styles/helpers";

.toolbelt {
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    margin-bottom: calc-rem(20);
    align-items: center;
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""