File

packages/data/src/lib/files/image-select-pop/image-select-pop.component.ts

Description

This component is a pop with a form to add images. You can set an the alternative Text and the size. The size inputs will keep the image ratio by default.

Extends

PopComponent

Implements

OnInit

Metadata

selector ec-image-select-pop
templateUrl ./image-select-pop.component.html

Index

Properties
Methods
Inputs
Outputs
HostBindings
HostListeners

Constructor

constructor(popService: PopService, sdk: SdkService, symbol: SymbolService, fileService: FileService, elementRef: ElementRef, cdr: ChangeDetectorRef)
Parameters :
Name Type Optional
popService PopService No
sdk SdkService No
symbol SymbolService No
fileService FileService No
elementRef ElementRef No
cdr ChangeDetectorRef No

Inputs

assetGroupID

The assetGroupID to pick from

defaultSize
Default value : 400

The default size used

active
Type : boolean
Inherited from PopComponent
Defined in PopComponent:28

If true, .ec-pop is part of the DOM (*ngIf) + .active is set on .ec-pop-container.

hideOnClickOutside
Default value : false
Inherited from PopComponent
Defined in PopComponent:34

If set to true, the pop will hide when a click happens outside the pop.

hideOnEscape
Default value : true
Inherited from PopComponent
Defined in PopComponent:36

If set to false, esc will not close the pop

type
Type : string
Inherited from PopComponent
Defined in PopComponent:32

The used type on the host element

Outputs

changed
Type : EventEmitter<any>

Ouput that emits when image changes

toggle
Type : EventEmitter<boolean>
Inherited from PopComponent
Defined in PopComponent:38

HostBindings

class
Default value : 'ec-image-select-pop modal-wrapper'

Set host class to make sure the type is used

HostListeners

document:click
Arguments : '$event'
document:click($event)
Inherited from PopComponent
Defined in PopComponent:43

Listens for document:click and hides

Methods

ngOnInit
ngOnInit()

Inits the form

Returns : void
Public hide
hide()
Inherited from PopComponent
Defined in PopComponent:88

Hides the pop. Sets active false and removes pop from popService.stack

Returns : void
isOutside
isOutside(element)
Inherited from PopComponent
Defined in PopComponent:58

yields true if the given element is outside the pop / or is the wrapper element itself (the backdrop)

Parameters :
Name Optional
element No
Returns : boolean
Public show
show(e?)
Inherited from PopComponent
Defined in PopComponent:72

Shows the pop. Sets active true and adds pop to popService.stack

Parameters :
Name Optional
e Yes
Returns : void
Public toggle
toggle(e?)
Inherited from PopComponent
Defined in PopComponent:63

Shows if not active, hides if active.

Parameters :
Name Optional
e Yes
Returns : void

Properties

Public cdr
Type : ChangeDetectorRef
Public elementRef
Type : ElementRef
Public fileService
Type : FileService
imageForm
Type : FormConfig<any>

The instance of the imageForm

imageLoader
Type : LoaderComponent
Decorators :
@ViewChild('imageLoader', {static: true})

The loader that is shown after an image has been selected

Public popService
Type : PopService
Public sdk
Type : SdkService
Public symbol
Type : SymbolService
activated
Default value : false
Inherited from PopComponent
Defined in PopComponent:30

Flip and stays true after first show

clickEvent
Inherited from PopComponent
Defined in PopComponent:40
Public elementRef
Type : ElementRef
Inherited from PopComponent
Defined in PopComponent:55
import {
  ChangeDetectorRef,
  Component,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  OnInit,
  Output,
  ViewChild
} from '@angular/core';
import { FormConfig } from '@ec.components/core';
import { FormComponent, LoaderComponent, PopComponent, PopService, SymbolService } from '@ec.components/ui';
import { DefaultEntryInputComponent } from '../../entry-form/default-entry-input.component';
import { SdkService } from '../../sdk/sdk.service';
import { FileService } from '../file.service';

/** This component is a pop with a form to add images. You can set an the alternative Text and the size.
 * The size inputs will keep the image ratio by default.  */
@Component({
  selector: 'ec-image-select-pop',
  templateUrl: './image-select-pop.component.html',
})
export class ImageSelectPopComponent extends PopComponent implements OnInit {
  /** The assetGroupID to pick from */
  @Input() assetGroupID;
  /** The default size used */
  @Input() defaultSize = 400;
  /** Ouput that emits when image changes  */
  @Output() changed: EventEmitter<any> = new EventEmitter();
  /** The instance of the imageForm  */
  imageForm: FormConfig<any>;
  /** The loader that is shown after an image has been selected  */
  @ViewChild('imageLoader', { static: true }) imageLoader: LoaderComponent;
  /** Set host class to make sure the type is used */
  @HostBinding('class') class = 'ec-image-select-pop modal-wrapper';

  constructor(
    public popService: PopService,
    public sdk: SdkService,
    public symbol: SymbolService,
    public fileService: FileService,
    public elementRef: ElementRef,
    public cdr: ChangeDetectorRef,
  ) {
    super(popService, elementRef, cdr);
  }
  /** Inits the form */
  ngOnInit() {
    const isOldAssetGroupID = this.fileService.isOldAssetGroupID(this.assetGroupID);
    this.imageForm = {
      submitButtonLabel: this.symbol.resolve('image-select-pop.submitButtonLabel'),
      onSave: (form, value: { img; height; width; alt }) => {
        const size = Math.max(value.width, value.height);
        if (isOldAssetGroupID) {
          this.sdk.api
            .asset(value.img)
            .then((asset) => asset.getImageUrl(size, ''))
            .then((url) => {
              this.hide();
              this.changed.emit({ url, alt: value.alt, size });
            });
        } else {
          const loadImage = this.sdk.api
            .dmAsset(this.assetGroupID, value.img)
            .then((asset) => asset.getImageUrl(size))
            .then((url) => {
              this.hide();
              this.changed.emit({ url, alt: value.alt, size });
            });
          this.imageLoader.wait(loadImage);
        }
      },
      fields: {
        img: {
          label: ' ',
          input: DefaultEntryInputComponent,
          inputView: isOldAssetGroupID ? 'asset-select' : 'dmAsset-select',
          relation: !isOldAssetGroupID ? this.assetGroupID : 'legacyAsset',
          required: true,
          changed: (value, form) => {
            const loadImg = Promise.resolve()
              .then(() => {
                if (isOldAssetGroupID) {
                  return this.sdk.api.asset(value).then((asset) => {
                    const original = asset.getOriginalFile();
                    const resolution = original.resolution;
                    return { resolution, title: asset.title };
                  });
                } else {
                  return this.sdk.api.dmAsset(this.assetGroupID, value).then((asset) => {
                    const resolution = asset.file.resolution;
                    return { resolution, title: asset.title };
                  });
                }
              })
              .then(({ resolution, title }) => {
                const ratio = resolution.width / resolution.height;
                const width = Math.min(this.defaultSize, resolution.width);
                form.group.controls.width.setValue(width);
                form.group.controls.alt.setValue(title);
                form.group.controls.ratio.setValue(ratio);
              });
            this.imageLoader.wait(loadImg);
          },
        },
        alt: {
          label: this.symbol.resolve('image-select-pop.alt'),
          view: 'string',
        },
        ratio: {
          hideInForm: true,
          view: 'number',
        },
        keepRatio: {
          label: this.symbol.resolve('image-select-pop.keepRatio'),
          view: 'boolean',
          prefill: true,
        },
        width: {
          label: this.symbol.resolve('image-select-pop.width'),
          view: 'number',
          columns: 6,
          changed: (value, form: FormComponent<any>) => {
            const ratio = form.getValue('ratio');
            if (ratio && form.getValue('keepRatio')) {
              form.group.controls.height.setValue(Math.round(value / ratio));
            }
          },
        },
        height: {
          label: this.symbol.resolve('image-select-pop.height'),
          view: 'number',
          columns: 6,
          changed: (value, form) => {
            const ratio = form.getValue('ratio');
            if (ratio && form.getValue('keepRatio')) {
              form.group.controls.width.setValue(Math.round(value * ratio));
            }
          },
        },
      },
    };
  }
}
<div class="modal">
  <button (click)="hide()" class="modal__dismiss" type="button">
    <ec-icon name="close"></ec-icon>
  </button>
  <div class="modal__header">
    {{ 'image-select-pop.header' | symbol }}
  </div>
  <div class="modal__body">
    <ec-loader class="ec-loader loader" #imageLoader></ec-loader>
    <ec-form *ngIf="imageForm" [config]="imageForm" #imageSettings></ec-form>
  </div>
</div>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""