import {Component, EventEmitter, forwardRef, Input, Output} from "@angular/core"; import {ControlValueAccessor, DefaultValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms"; import {Select2} from "../../../controls/select2/Select2"; @Component({ selector:'wp-image-holder', providers:[{ provide:NG_VALUE_ACCESSOR, useExisting: forwardRef(() => WpImageHolder), multi:true }], template:`

Click here to upload

`, styles:[` .deleteIcon:hover{ color:red; } `] }) export class WpImageHolder implements ControlValueAccessor{ @Input() Width:string='150px'; @Input() Height:string='150px'; private Source:WpImageHolderSource; private _onChange:any; //endregion UploadFile(){ let frame:any=wp.media({ title:'Select or upload your image', button:{ text:'Use this image' }, multiple:false }); frame.on('select',()=>{ let attachment = frame.state().get('selection').first().toJSON(); console.log(attachment); this.Source={ attachmentId:attachment.id, url:attachment.url } this._onChange(this.Source); }); frame.open(); } ClearImage(event) { event.stopImmediatePropagation(); this.Source=null; this._onChange(this.Source); } writeValue(obj: WpImageHolderSource): void { this.Source=obj; } registerOnChange(fn: any): void { this._onChange=fn; } registerOnTouched(fn: any): void { } } declare let wp:any; export interface WpImageHolderSource{ attachmentId:string; url:string; }