File

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

Description

Loads an public asset by id to the template. assets/asset https://components.entrecode.de/assets/asset?e=1

Implements

OnChanges

Metadata

Selector [ecAsset]

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor(sdk: SdkService)

Injects the sdk

Parameters :
Name Type Optional
sdk SdkService No

Inputs

api
Type : PublicAPI

The api to use. Defaults to sdk.api

assetId
Type : string

The asset id that should be loaded

autoload
Type : boolean

Should the asset be loaded immediately? Defaults to true

ecAsset
Type : string

The asset id that should be loaded

levels
Type : number

The levels to use.

Outputs

loaded
Type : EventEmitter<PublicAssetResource>

Fires as soon as the asset has been loaded.

Methods

load
load(id?: string)

Loads the asset. Can be called from template when using autoload=false

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

as soon as model and id are known, the asset will be loaded.

Returns : void

Properties

Public asset
Type : any

The current loaded asset

promise
Type : any

The loading promise

Public sdk
Type : SdkService

ecAsset

This directive can be used to load a single asset directly from the template:

<ul ecAsset="f663ba74-d33b-499f-a37c-9a5b4cf94543" #myAsset="ecAsset">
  <li>id: {{myAsset.asset?.assetID}}</li>
  <li>title: {{myAsset.asset?.title}}</li>
  <li>tags: {{myAsset.asset?.tags}}</li>
  <li>created: {{myAsset.asset?.created}}</li>
  <li>type: {{myAsset.asset?.type}}</li>
  <li>files: {{myAsset.asset?.files?.length}}</li>
</ul>

import { Directive, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import PublicAssetResource from 'ec.sdk/lib/resources/publicAPI/PublicAssetResource';
import { SdkService } from '../../sdk/sdk.service';
import PublicAPI from 'ec.sdk/lib/PublicAPI';

/** Loads an public asset by id to the template.
 * assets/asset
 * <example-url>https://components.entrecode.de/assets/asset?e=1</example-url>
 */
@Directive({
  selector: '[ecAsset]',
  exportAs: 'ecAsset',
})
export class AssetDirective implements OnChanges {
  /** The loading promise */
  promise: any;
  /** The asset id that should be loaded*/
  @Input() assetId: string;
  /** The asset id that should be loaded*/
  @Input() ecAsset: string;
  /** Should the asset be loaded immediately? Defaults to true */
  @Input() autoload: boolean;
  /** The levels to use. */
  @Input() levels: number;
  /** The api to use. Defaults to sdk.api */
  @Input() api: PublicAPI;
  /** Fires as soon as the asset has been loaded. */
  @Output() loaded: EventEmitter<PublicAssetResource> = new EventEmitter();
  /** The current loaded asset */
  public asset: any;

  /** Injects the sdk */
  constructor(public sdk: SdkService) {}

  /** as soon as model and id are known, the asset will be loaded. */
  ngOnChanges() {
    if (this.ecAsset) {
      this.assetId = this.ecAsset;
    }
    if (this.autoload === false) {
      return;
    }
    this.load();
  }

  /** Loads the asset. Can be called from template when using autoload=false */
  load(id?: string) {
    this.assetId = id || this.assetId;
    if (!this.assetId) {
      return Promise.reject('cannot load asset: no assetId is set');
    }
    const api = this.api || this.sdk.api;
    if (!api) {
      throw new Error('cannot load asset: no api was set!');
    }
    this.promise = api.asset(this.assetId).then((asset) => {
      this.asset = asset;
      this.loaded.emit(asset);
      return asset;
    });
    return this.promise;
  }
}

result-matching ""

    No results matching ""