File
Implements
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
|
|
Methods
getCategoryTcmId
|
getCategoryTcmId(id)
|
|
Builds a category tcm-id based on a given id
|
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()
|
|
|
setView
|
setView(view)
|
|
Set view between Grid and List, used for news search
|
newsView
|
Type : string
|
Default value : 'GRID'
|
|
search
|
Type : SearchService
|
|
searchSubscription
|
Type : Subscription
|
|
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>
@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 with directive