projects/storefrontlib/shared/components/media/media.component.ts
| changeDetection | ChangeDetectionStrategy.OnPush |
| selector | cx-media |
| templateUrl | ./media.component.html |
Properties |
Methods |
|
Inputs |
Outputs |
HostBindings |
Accessors |
constructor(mediaService: MediaService)
|
||||||
|
Parameters :
|
| alt | |
Type : string
|
|
|
A specific alt text for an image, which overrules the alt text from the container data. |
|
| container | |
Type : MediaContainer | Image | ImageGroup | ImageGroup[] | undefined
|
|
|
The media container can hold multiple media items, so that
a specific media (by format) can be used or multiple media
can be provided in a |
|
| format | |
Type : string
|
|
|
if a media format is given, a media for the given format will be rendered |
|
| loading | |
Type : ImageLoadingStrategy | null
|
|
Default value : <ImageLoadingStrategy | null>(
this.loadingStrategy
)
|
|
|
Set the loading strategy of the media. Defaults to global loading strategy. Use 'lazy' or 'eager' strategies. |
|
| role | |
Type : string
|
|
|
set role of the image if different than what is intended (eg, role="presentation") |
|
| loaded | |
Type : EventEmitter<Boolean>
|
|
|
Once the media is loaded, we emit an event. |
|
| class.is-initialized |
Type : boolean
|
Default value : false
|
|
The |
| class.is-loading |
Type : boolean
|
Default value : true
|
|
The |
| class.is-missing |
Type : boolean
|
Default value : false
|
|
When there's no media provided for the content, or in case an error
happened during loading, we add the |
| Protected create |
create()
|
|
Creates the
Returns :
void
|
| errorHandler |
errorHandler()
|
|
Whenever an error happens during load, we mark the component with css classes to have a missing media.
Returns :
void
|
| Protected handleMissing |
handleMissing()
|
|
Returns :
void
|
| loadHandler |
loadHandler()
|
|
This handler is called from the UI when the image is loaded.
Returns :
void
|
| ngOnChanges |
ngOnChanges()
|
|
Returns :
void
|
| alt |
Type : string
|
Decorators :
@Input()
|
|
A specific alt text for an image, which overrules the alt text from the container data. |
| container |
Type : MediaContainer | Image | ImageGroup | ImageGroup[] | undefined
|
Decorators :
@Input()
|
|
The media container can hold multiple media items, so that
a specific media (by format) can be used or multiple media
can be provided in a |
| format |
Type : string
|
Decorators :
@Input()
|
|
if a media format is given, a media for the given format will be rendered |
| isInitialized |
Default value : false
|
Decorators :
@HostBinding('class.is-initialized')
|
|
The |
| loaded |
Type : EventEmitter<Boolean>
|
Default value : new EventEmitter<Boolean>()
|
Decorators :
@Output()
|
|
Once the media is loaded, we emit an event. |
| loading |
Type : ImageLoadingStrategy | null
|
Default value : <ImageLoadingStrategy | null>(
this.loadingStrategy
)
|
Decorators :
@Input()
|
|
Set the loading strategy of the media. Defaults to global loading strategy. Use 'lazy' or 'eager' strategies. |
| media |
Type : Media
|
|
The media contains the info for the UI to create the image. This media object might contain more info once other media types (i.e. video) is supported. |
| role |
Type : string
|
Decorators :
@Input()
|
|
set role of the image if different than what is intended (eg, role="presentation") |
| loadingStrategy |
getloadingStrategy()
|
|
Indicates whether the browser should lazy load the image.
Returns :
string | ImageLoadingStrategy | null
|
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
HostBinding,
Input,
OnChanges,
Output,
} from '@angular/core';
import { Image, ImageGroup } from '@spartacus/core';
import { ImageLoadingStrategy, Media, MediaContainer } from './media.model';
import { MediaService } from './media.service';
@Component({
selector: 'cx-media',
templateUrl: './media.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MediaComponent implements OnChanges {
/**
* The media container can hold multiple media items, so that
* a specific media (by format) can be used or multiple media
* can be provided in a `srcset` so the browser will figure out
* the best media for the device.
*/
@Input() container:
| MediaContainer
| Image
| ImageGroup
| ImageGroup[]
| undefined;
/**
* if a media format is given, a media for the given format will be rendered
*/
@Input() format: string;
/**
* A specific alt text for an image, which overrules the alt text
* from the container data.
*/
@Input() alt: string;
/**
* set role of the image if different than what is intended (eg, role="presentation")
*/
@Input() role: string;
// TODO: Remove type forcing of `this.loadingStrategy` (ie. <ImageLoadingStrategy | null>) in 5.0 (#14236)
/**
* Set the loading strategy of the media. Defaults to global loading strategy.
* Use 'lazy' or 'eager' strategies.
*/
@Input() loading: ImageLoadingStrategy | null = <ImageLoadingStrategy | null>(
this.loadingStrategy
);
/**
* Once the media is loaded, we emit an event.
*/
@Output() loaded: EventEmitter<Boolean> = new EventEmitter<Boolean>();
/**
* The media contains the info for the UI to create the image. This media
* object might contain more info once other media types (i.e. video) is supported.
*/
media: Media;
/**
* The `cx-media` component has an `is-initialized` class as long as the
* media is being initialized.
*/
@HostBinding('class.is-initialized') isInitialized = false;
/**
* The `cx-media` component has a `is-loading` class as long as the
* media is loaded. Wehn the media is loaded, the `is-initialized` class
* is added.
*/
@HostBinding('class.is-loading') isLoading = true;
/**
* When there's no media provided for the content, or in case an error
* happened during loading, we add the `is-missing` class. Visual effects
* can be controlled by CSS.
*/
@HostBinding('class.is-missing') isMissing = false;
constructor(protected mediaService: MediaService) {}
ngOnChanges(): void {
this.create();
}
/**
* Creates the `Media` object
*/
protected create(): void {
this.media = this.mediaService.getMedia(
this.container instanceof Array ? this.container[0] : this.container,
this.format,
this.alt,
this.role
);
if (!this.media?.src) {
this.handleMissing();
}
}
/**
* This handler is called from the UI when the image is loaded.
*/
loadHandler(): void {
this.isLoading = false;
this.isInitialized = true;
this.isMissing = false;
this.loaded.emit(true);
}
// TODO: Remove string return type (#14236)
/**
* Indicates whether the browser should lazy load the image.
* @deprecated since 4.2. use ImageLoadingStrategy or null return types only
*/
get loadingStrategy(): string | ImageLoadingStrategy | null {
return this.mediaService.loadingStrategy === ImageLoadingStrategy.LAZY
? ImageLoadingStrategy.LAZY
: null;
}
/**
* Whenever an error happens during load, we mark the component
* with css classes to have a missing media.
*/
errorHandler(): void {
this.handleMissing();
}
protected handleMissing() {
this.isLoading = false;
this.isInitialized = true;
this.isMissing = true;
this.loaded.emit(false);
}
}
<img
*ngIf="media?.src"
[attr.src]="media.src"
[attr.srcset]="media.srcset"
[attr.alt]="media.alt"
[attr.role]="media.role"
[attr.loading]="loading"
(load)="loadHandler()"
(error)="errorHandler()"
/>