File

projects/storefrontlib/shared/components/media/media.component.ts

Implements

OnChanges

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector cx-media
templateUrl ./media.component.html

Index

Properties
Methods
Inputs
Outputs
HostBindings
Accessors

Constructor

constructor(mediaService: MediaService)
Parameters :
Name Type Optional
mediaService MediaService No

Inputs

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 srcset so the browser will figure out the best media for the device.

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")

Outputs

loaded
Type : EventEmitter<Boolean>

Once the media is loaded, we emit an event.

HostBindings

class.is-initialized
Type : boolean
Default value : false

The cx-media component has an is-initialized class as long as the media is being initialized.

class.is-loading
Type : boolean
Default value : true

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.

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 is-missing class. Visual effects can be controlled by CSS.

Methods

Protected create
create()

Creates the Media object

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

Properties

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 srcset so the browser will figure out the best media for the device.

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 cx-media component has an is-initialized class as long as the media is being initialized.

isLoading
Default value : true
Decorators :
@HostBinding('class.is-loading')

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.

isMissing
Default value : false
Decorators :
@HostBinding('class.is-missing')

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.

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")

Accessors

loadingStrategy
getloadingStrategy()

Indicates whether the browser should lazy load the image.

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()"
/>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""