nm-iframe
src/app/shared/widgets/iframe/iframe.component.ts
host | { |
selector | nm-iframe |
styleUrls | iframe.component.scss |
templateUrl | ./iframe.component.html |
Widget inputs |
Widget outputs |
Properties |
|
Methods |
|
constructor(localStorageService: LocalStorageService)
|
||||||
Parameters :
|
Protected configureWidget | ||||||
configureWidget(configuration: WidgetConfig
|
||||||
Decorators : WidgetConfigure
|
||||||
Parameters :
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
Public onLoad | ||||||
onLoad(event: any)
|
||||||
Parameters :
Returns :
void
|
Public onMessage | ||||||
onMessage(event: any)
|
||||||
Parameters :
Returns :
void
|
Private _domain |
_domain:
|
Type : string
|
Private _subscriptionMessagesInput |
_subscriptionMessagesInput:
|
Type : Subscription
|
Private _subscriptionReload |
_subscriptionReload:
|
Type : Subscription
|
Private _subscriptionUrl |
_subscriptionUrl:
|
Type : Subscription
|
Private _widgetConfig |
_widgetConfig:
|
Type : WidgetConfig
|
Public configuration |
configuration:
|
Type : WidgetConfig<IframeWidgetComponentConfiguration>
|
Decorators : WidgetConfiguration
|
iframe |
iframe:
|
Decorators : ViewChild
|
Public messagesInput |
messagesInput:
|
Default value : new Subject<any>()
|
Decorators : WidgetInput
|
Sets target URL parameters. |
Public messagesOutput |
messagesOutput:
|
Default value : new Subject<any>()
|
Decorators : WidgetOutput
|
Emits any received messages from target URL. |
Public reload |
reload:
|
Default value : new ReplaySubject<any>(1)
|
Decorators : WidgetInput
|
Reloads target URL. |
Private themeEntry |
themeEntry:
|
Type : LocalStorageEntry
|
Public url |
url:
|
Default value : new BehaviorSubject<any>(null)
|
Decorators : WidgetInput
|
Sets the URL of the page to embed. |
import { debounceTime, filter } from "rxjs/operators";
import { Component, ViewChild } from "@angular/core";
import { Subject, BehaviorSubject, Subscription, ReplaySubject } from "rxjs";
import { WidgetConfig } from "../widget.configuration";
import {
WidgetComponent,
WidgetConfiguration,
WidgetConfigure,
WidgetInput,
WidgetOutput,
} from "../widget.metadata";
import { Link } from "../../components/hal/hal";
import {
LocalStorageEntry,
LocalStorageService,
} from "../../components/local-storage/local-storage.service";
import { themeLocalStorageConfig } from "../portal/config/config.component";
export interface IframeWidgetComponentConfiguration {
/**
* Sets iframe width @default("100%").
*/
width?: any;
/**
* Sets iframe height @default("800px").
*/
height?: any;
/**
* Sets iframe links.
*/
links: any;
}
@WidgetComponent("nm-iframe")
@Component({
selector: "nm-iframe",
templateUrl: "./iframe.component.html",
styleUrls: ["./iframe.component.scss"],
host: { "(window:message)": "onMessage($event)" },
})
export class IframeWidgetComponent {
/**
* Sets the URL of the page to embed.
*/
@WidgetInput()
public url = new BehaviorSubject<any>(null);
/**
* Sets target URL parameters.
*/
@WidgetInput("messages-input")
public messagesInput = new Subject<any>();
/**
* Emits any received messages from target URL.
*/
@WidgetOutput("messages-output")
public messagesOutput = new Subject<any>();
/**
* Reloads target URL.
*/
@WidgetInput()
public reload = new ReplaySubject<any>(1);
@WidgetConfiguration()
public configuration: WidgetConfig<IframeWidgetComponentConfiguration>;
@ViewChild("iframe", { static: true })
iframe;
private _widgetConfig: WidgetConfig;
private _domain: string;
private _subscriptionUrl: Subscription;
private _subscriptionReload: Subscription;
private _subscriptionMessagesInput: Subscription;
private themeEntry: LocalStorageEntry;
constructor(private localStorageService: LocalStorageService) {}
@WidgetConfigure()
protected configureWidget(
configuration: WidgetConfig<IframeWidgetComponentConfiguration>
) {
this.themeEntry = this.localStorageService.getLocalStorageEntryByConfig(
themeLocalStorageConfig
);
this._widgetConfig = configuration;
let links = configuration._links;
let domain = <Link>links["domain"];
if (domain && domain.href) {
this._domain = domain.href;
}
let url = <Link>links["url"];
if (url && url.href) {
this.url.next(url.href);
}
}
public onLoad(event: any) {
let frame = this.iframe.nativeElement;
if (this._subscriptionMessagesInput) {
this._subscriptionMessagesInput.unsubscribe();
}
this._subscriptionMessagesInput = this.messagesInput
.pipe(filter((message) => !!frame.src))
.subscribe((params) => {
let json = JSON.stringify(params);
frame.contentWindow.postMessage(json, this._domain);
});
}
public onMessage(event: any) {
var msg = event.data;
try {
msg = JSON.parse(msg);
} catch (ignore) {}
let result = {
url: this.url.getValue(),
domain: this._domain,
data: msg,
};
this.messagesOutput.next(result);
}
ngAfterViewInit() {
let frame = this.iframe.nativeElement;
this._subscriptionUrl = this.url
.pipe(
debounceTime(500),
filter((value) => !!value)
)
.subscribe((url) => {
frame.src = url;
});
}
ngOnInit() {}
ngOnDestroy() {
if (this._subscriptionMessagesInput) {
this._subscriptionMessagesInput.unsubscribe();
}
if (this._subscriptionUrl) {
this._subscriptionUrl.unsubscribe();
}
if (this._subscriptionReload) {
this._subscriptionReload.unsubscribe();
}
}
}
<iframe
[style.width]="configuration.configuration['width'] || '100%'"
[style.height]="configuration.configuration['height'] || '800px'"
id="iframe"
#iframe
(load)="onLoad($event)"
></iframe>