|
|
|
@ -5,7 +5,7 @@ |
|
|
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|
|
|
*/ |
|
|
|
|
|
|
|
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, forwardRef, Input, OnChanges, Renderer2, SimpleChanges, ViewChild } from '@angular/core'; |
|
|
|
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, forwardRef, Input, OnChanges, OnDestroy, Renderer2, SimpleChanges, ViewChild } from '@angular/core'; |
|
|
|
import { NG_VALUE_ACCESSOR } from '@angular/forms'; |
|
|
|
import { Router } from '@angular/router'; |
|
|
|
import { StatefulControlComponent, Types } from '@app/framework/internal'; |
|
|
|
@ -14,6 +14,11 @@ export const SQX_IFRAME_EDITOR_CONTROL_VALUE_ACCESSOR: any = { |
|
|
|
provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => IFrameEditorComponent), multi: true |
|
|
|
}; |
|
|
|
|
|
|
|
interface State { |
|
|
|
// True, when the editor is shown as fullscreen.
|
|
|
|
isFullscreen: boolean; |
|
|
|
} |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: 'sqx-iframe-editor', |
|
|
|
styleUrls: ['./iframe-editor.component.scss'], |
|
|
|
@ -23,7 +28,7 @@ export const SQX_IFRAME_EDITOR_CONTROL_VALUE_ACCESSOR: any = { |
|
|
|
], |
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush |
|
|
|
}) |
|
|
|
export class IFrameEditorComponent extends StatefulControlComponent<any, any> implements OnChanges, AfterViewInit { |
|
|
|
export class IFrameEditorComponent extends StatefulControlComponent<State, any> implements OnChanges, OnDestroy, AfterViewInit { |
|
|
|
private value: any; |
|
|
|
private isDisabled = false; |
|
|
|
private isInitialized = false; |
|
|
|
@ -31,6 +36,12 @@ export class IFrameEditorComponent extends StatefulControlComponent<any, any> im |
|
|
|
@ViewChild('iframe', { static: false }) |
|
|
|
public iframe: ElementRef<HTMLIFrameElement>; |
|
|
|
|
|
|
|
@ViewChild('container', { static: false }) |
|
|
|
public container: ElementRef<HTMLElement>; |
|
|
|
|
|
|
|
@ViewChild('inner', { static: false }) |
|
|
|
public inner: ElementRef<HTMLElement>; |
|
|
|
|
|
|
|
@Input() |
|
|
|
public context: any = {}; |
|
|
|
|
|
|
|
@ -40,11 +51,19 @@ export class IFrameEditorComponent extends StatefulControlComponent<any, any> im |
|
|
|
@Input() |
|
|
|
public url: string; |
|
|
|
|
|
|
|
public fullscreen: boolean; |
|
|
|
|
|
|
|
constructor(changeDetector: ChangeDetectorRef, |
|
|
|
private readonly renderer: Renderer2, |
|
|
|
private readonly router: Router |
|
|
|
) { |
|
|
|
super(changeDetector, {}); |
|
|
|
super(changeDetector, { |
|
|
|
isFullscreen: false |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
public ngOnDestroy() { |
|
|
|
this.toggleFullscreen(false); |
|
|
|
} |
|
|
|
|
|
|
|
public ngOnChanges(changes: SimpleChanges) { |
|
|
|
@ -75,17 +94,24 @@ export class IFrameEditorComponent extends StatefulControlComponent<any, any> im |
|
|
|
this.isInitialized = true; |
|
|
|
|
|
|
|
this.sendInit(); |
|
|
|
this.sendFullscreen(); |
|
|
|
this.sendFormValue(); |
|
|
|
this.sendDisabled(); |
|
|
|
this.sendValue(); |
|
|
|
} else if (type === 'resize') { |
|
|
|
const { height } = event.data; |
|
|
|
|
|
|
|
this.iframe.nativeElement.height = height + 'px'; |
|
|
|
this.renderer.setStyle(this.iframe.nativeElement, 'height', height + 'px'); |
|
|
|
} else if (type === 'navigate') { |
|
|
|
const { url } = event.data; |
|
|
|
|
|
|
|
this.router.navigateByUrl(url); |
|
|
|
} else if (type === 'fullscreen') { |
|
|
|
const { mode } = event.data; |
|
|
|
|
|
|
|
if (mode !== this.snapshot.isFullscreen) { |
|
|
|
this.toggleFullscreen(mode); |
|
|
|
} |
|
|
|
} else if (type === 'valueChanged') { |
|
|
|
const { value } = event.data; |
|
|
|
|
|
|
|
@ -97,6 +123,8 @@ export class IFrameEditorComponent extends StatefulControlComponent<any, any> im |
|
|
|
} else if (type === 'touched') { |
|
|
|
this.callTouched(); |
|
|
|
} |
|
|
|
|
|
|
|
this.detectChanges(); |
|
|
|
} |
|
|
|
})); |
|
|
|
} |
|
|
|
@ -121,6 +149,10 @@ export class IFrameEditorComponent extends StatefulControlComponent<any, any> im |
|
|
|
this.sendMessage('valueChanged', { value: this.value }); |
|
|
|
} |
|
|
|
|
|
|
|
private sendFullscreen() { |
|
|
|
this.sendMessage('fullscreenChanged', { fullscreen: this.snapshot.isFullscreen }); |
|
|
|
} |
|
|
|
|
|
|
|
private sendDisabled() { |
|
|
|
this.sendMessage('disabled', { isDisabled: this.isDisabled }); |
|
|
|
} |
|
|
|
@ -129,6 +161,20 @@ export class IFrameEditorComponent extends StatefulControlComponent<any, any> im |
|
|
|
this.sendMessage('formValueChanged', { formValue: this.formValue }); |
|
|
|
} |
|
|
|
|
|
|
|
private toggleFullscreen(isFullscreen: boolean) { |
|
|
|
this.next(s => ({ ...s, isFullscreen })); |
|
|
|
|
|
|
|
let target = this.container.nativeElement; |
|
|
|
|
|
|
|
if (isFullscreen) { |
|
|
|
target = document.body; |
|
|
|
} |
|
|
|
|
|
|
|
this.renderer.appendChild(target, this.inner.nativeElement); |
|
|
|
|
|
|
|
this.sendFullscreen(); |
|
|
|
} |
|
|
|
|
|
|
|
private sendMessage(type: string, payload: any) { |
|
|
|
if (!this.iframe) { |
|
|
|
return; |
|
|
|
|