File

packages/data/src/lib/files/image/image.directive.ts

Description

Loads an public asset image by id to the template. It can be used with img's to auto load the url to the src.

Implements

OnChanges

Metadata

Selector img [ecImage]

Index

Properties
Methods
Inputs

Constructor

constructor(sdk: SdkService, elementRef: ElementRef)
Parameters :
Name Type Optional
sdk SdkService No
elementRef ElementRef No

Inputs

ecImage
Type : string | DMAssetResource

The assetID that should be loaded

size
Default value : 200

The size that should be requested.

thumb
Type : boolean

If true, the image will be requested as thumb (square)

Methods

load
load(id?: string)

Calls super.load, then resolves the image url and assigns it to the native element src (only if it is an img)

Parameters :
Name Type Optional
id string Yes
Returns : any
ngOnChanges
ngOnChanges()

Reads ecImage input as assetId and loads if autoload is not false

Returns : void
setUrl
setUrl(url: string)
Parameters :
Name Type Optional
url string No
Returns : void
use
use(asset: PublicAssetResource | DMAssetResource)
Parameters :
Name Type Optional
asset PublicAssetResource | DMAssetResource No
Returns : any

Properties

Public sdk
Type : SdkService
url
Type : string

Resolved asset url.

ecImage

This directive can be used to load an image:

<img ecImage="f663ba74-d33b-499f-a37c-9a5b4cf94543" #myImage="ecImage">
<p>ID: {{myImage.asset?.assetID}}</p>

import { Directive, ElementRef, Input, OnChanges } from '@angular/core';
import { AssetDirective } from '../asset/asset.directive';
import { SdkService } from '../../sdk/sdk.service';
import DMAssetResource from 'ec.sdk/lib/resources/publicAPI/DMAssetResource';
import PublicAssetResource from 'ec.sdk/lib/resources/publicAPI/PublicAssetResource';

/** Loads an public asset image by id to the template. It can be used with img's to auto load the url to the src. */
@Directive({
  selector: 'img [ecImage]',
  exportAs: 'ecImage',
})
export class ImageDirective extends AssetDirective implements OnChanges {
  /** If true, the image will be requested as thumb (square) */
  @Input() thumb: boolean;
  /** The size that should be requested. */
  @Input() size = 200;
  /** The assetID that should be loaded */
  @Input() ecImage: string | DMAssetResource;
  /** Resolved asset url. */
  url: string;

  constructor(public sdk: SdkService, private elementRef: ElementRef) {
    super(sdk);
  }

  /** Reads ecImage input as assetId and loads if autoload is not false */
  ngOnChanges() {
    if (typeof this.ecImage === 'string') {
      this.assetId = this.ecImage;
    } else if (this.ecImage && this.ecImage.assetID) {
      this.use(this.ecImage);
    }
    if (this.autoload !== false && this.assetId) {
      this.load();
    }
  }

  /** Calls super.load, then resolves the image url and assigns it to the native element src (only if it is an img) */
  load(id?: string) {
    return super.load(id).then(this.use.bind(this));
  }

  use(asset: PublicAssetResource | DMAssetResource) {
    return Promise.resolve()
      .then(() => {
        if (asset.type !== 'image') {
          return Promise.reject(`ecImage only works for assets of type image.
        Loaded id ${asset.id} is of type ${asset.type}`);
        }
        if (asset instanceof DMAssetResource) {
          // new asset
          return asset.getFileVariant(this.size, this.thumb);
        } else if (asset instanceof PublicAssetResource) {
          // old asset
          if (this.thumb) {
            return asset.getImageThumbUrl(this.size, '');
          }
          return asset.getImageUrl(this.size, '');
        }
      })
      .then(this.setUrl.bind(this));
  }

  setUrl(url: string) {
    this.url = url;
    this.elementRef.nativeElement.src = this.url;
  }
}

result-matching ""

    No results matching ""