feature-libs/product-configurator/rulebased/components/overview-form/configurator-overview-form.component.ts
| changeDetection | ChangeDetectionStrategy.OnPush |
| selector | cx-configurator-overview-form |
| templateUrl | ./configurator-overview-form.component.html |
Properties |
Methods |
constructor(configuratorCommonsService: ConfiguratorCommonsService, configRouterExtractorService: ConfiguratorRouterExtractorService)
|
|||||||||
|
Parameters :
|
| getStyleClasses | ||||||||||||
getStyleClasses(attributes: Configurator.AttributeOverview[], index: number)
|
||||||||||||
|
Retrieves the styling for the corresponding element.
Parameters :
Returns :
string
|
| hasAttributes | ||||||||
hasAttributes(configuration: Configurator.Configuration)
|
||||||||
|
Does the configuration contain any selected attribute values?
Parameters :
Returns :
boolean
|
| isSameAttribute | ||||||||||||
isSameAttribute(attributes: Configurator.AttributeOverview[], index: number)
|
||||||||||||
|
Verifies whether the next or the previous attributes are same.
Parameters :
Returns :
boolean
|
| attributeOverviewType |
Default value : Configurator.AttributeOverviewType
|
| configuration$ |
Type : Observable<Configurator.Configuration>
|
Default value : this.configRouterExtractorService.extractRouterData().pipe(
switchMap((routerData) =>
this.configuratorCommonsService.getOrCreateConfiguration(
routerData.owner
)
),
distinctUntilKeyChanged('configId'),
switchMap((configuration) =>
this.configuratorCommonsService.getConfigurationWithOverview(
configuration
)
),
filter((configuration) => configuration.overview != null)
)
|
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ConfiguratorRouterExtractorService } from '@spartacus/product-configurator/common';
import { Observable } from 'rxjs';
import {
distinctUntilKeyChanged,
filter,
switchMap,
take,
} from 'rxjs/operators';
import { ConfiguratorCommonsService } from '../../core/facade/configurator-commons.service';
import { Configurator } from '../../core/model/configurator.model';
@Component({
selector: 'cx-configurator-overview-form',
templateUrl: './configurator-overview-form.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ConfiguratorOverviewFormComponent {
attributeOverviewType = Configurator.AttributeOverviewType;
configuration$: Observable<Configurator.Configuration> =
this.configRouterExtractorService.extractRouterData().pipe(
switchMap((routerData) =>
this.configuratorCommonsService.getOrCreateConfiguration(
routerData.owner
)
),
distinctUntilKeyChanged('configId'),
switchMap((configuration) =>
this.configuratorCommonsService.getConfigurationWithOverview(
configuration
)
),
filter((configuration) => configuration.overview != null)
);
constructor(
protected configuratorCommonsService: ConfiguratorCommonsService,
protected configRouterExtractorService: ConfiguratorRouterExtractorService
) {
//In case the 'forceReload' is set (means the page is launched from the checkout in display only mode),
//we need to initialise the cart configuration
this.configRouterExtractorService
.extractRouterData()
.pipe(take(1))
.subscribe((routingData) => {
if (routingData.forceReload) {
this.configuratorCommonsService.removeConfiguration(
routingData.owner
);
}
});
}
/**
* Does the configuration contain any selected attribute values?
* @param {Configurator.Configuration} configuration - Current configuration
* @returns {boolean} - Any attributes available
*/
hasAttributes(configuration: Configurator.Configuration): boolean {
return (
configuration.overview?.groups?.find((group) =>
group.attributes ? group.attributes.length : 0 > 0
) !== undefined
);
}
/**
* Verifies whether the next or the previous attributes are same.
*
* @param {Configurator.AttributeOverview[]} attributes - Attribute array
* @param {number} index - Index of the attribute in the array
* @return {boolean} - 'True' if it is the same attribute, otherwise 'false'
*/
isSameAttribute(
attributes: Configurator.AttributeOverview[],
index: number
): boolean {
if (attributes.length > 1) {
if (index === 0) {
return (
attributes[index]?.attribute === attributes[index + 1]?.attribute
);
} else {
return (
attributes[index]?.attribute === attributes[index - 1]?.attribute
);
}
}
return false;
}
/**
* Retrieves the styling for the corresponding element.
*
* @param {Configurator.AttributeOverview[]} attributes - Attribute array
* @param {number} index - Index of the attribute in the array
* @return {string} - corresponding style class
*/
getStyleClasses(
attributes: Configurator.AttributeOverview[],
index: number
): string {
let styleClass = '';
switch (attributes[index]?.type) {
case this.attributeOverviewType.BUNDLE:
styleClass += 'bundle';
break;
case this.attributeOverviewType.GENERAL:
styleClass += 'general';
break;
}
if (index === 0 || !this.isSameAttribute(attributes, index)) {
styleClass += ' margin';
}
if (
!this.isSameAttribute(attributes, index + 1) &&
!styleClass.includes('bundle')
) {
styleClass += ' last-value-pair';
}
return styleClass;
}
}
<ng-container *ngIf="configuration$ | async as configuration">
<ng-container *ngIf="hasAttributes(configuration); else noAttributes">
<ng-container
*ngTemplateOutlet="
groups;
context: {
overviewGroups: configuration.overview.groups,
level: 1
}
"
></ng-container>
</ng-container>
</ng-container>
<ng-template #noAttributes>
<div class="cx-no-attribute-value-pairs">
<!-- We currently do not support filtering on overview page so this should never be displayed -->
<h2>{{ 'configurator.overviewForm.noAttributeHeader' | cxTranslate }}</h2>
<p>{{ 'configurator.overviewForm.noAttributeText' | cxTranslate }}</p>
</div>
</ng-template>
<ng-template #groups let-overviewGroups="overviewGroups" let-level="level">
<ng-container *ngFor="let group of overviewGroups">
<div
class="cx-group"
[class.topLevel]="level === 1"
[class.subgroupTopLevel]="level === 1 && group.subGroups?.length > 0"
>
<h2>
<span>{{ group.groupDescription }}</span>
</h2>
<div
*ngFor="let attributeOverview of group.attributes; let i = index"
class="cx-attribute-value-pair"
[ngClass]="getStyleClasses(group.attributes, i)"
>
<ng-container [ngSwitch]="attributeOverview?.type">
<ng-container *ngSwitchCase="attributeOverviewType.GENERAL">
<cx-configurator-overview-attribute
[attributeOverview]="attributeOverview"
>
</cx-configurator-overview-attribute>
</ng-container>
<ng-container *ngSwitchCase="attributeOverviewType.BUNDLE">
<cx-configurator-cpq-overview-attribute
[attributeOverview]="attributeOverview"
>
</cx-configurator-cpq-overview-attribute>
</ng-container>
<ng-container *ngSwitchDefault>
<cx-configurator-overview-attribute
[attributeOverview]="attributeOverview"
>
</cx-configurator-overview-attribute>
</ng-container>
</ng-container>
</div>
</div>
<ng-container *ngIf="group.subGroups?.length > 0">
<ng-container
*ngTemplateOutlet="
groups;
context: { overviewGroups: group.subGroups, level: level + 1 }
"
></ng-container>
</ng-container>
</ng-container>
</ng-template>