/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { Component, Inject, forwardRef, ChangeDetectorRef, OnInit, ViewChild, ElementRef } from '@angular/core'; import { Webview } from 'vs/workbench/parts/html/browser/webview'; import { Parts } from 'vs/workbench/services/part/common/partService'; import Event, { Emitter } from 'vs/base/common/event'; import { IDisposable } from 'vs/base/common/lifecycle'; import { memoize } from 'vs/base/common/decorators'; import { DashboardWidget, IDashboardWidget, WidgetConfig, WIDGET_CONFIG } from 'sql/parts/dashboard/common/dashboardWidget'; import { DashboardServiceInterface } from 'sql/parts/dashboard/services/dashboardServiceInterface.service'; import { CommonServiceInterface } from 'sql/services/common/commonServiceInterface.service'; import { IDashboardWebview } from 'sql/services/dashboard/common/dashboardViewService'; import * as sqlops from 'sqlops'; interface IWebviewWidgetConfig { id: string; } const selector = 'webview-widget'; @Component({ selector: selector, template: '
' }) export class WebviewWidget extends DashboardWidget implements IDashboardWidget, OnInit, IDashboardWebview { private _id: string; private _webview: Webview; private _html: string; private _onMessage = new Emitter(); public readonly onMessage: Event = this._onMessage.event; private _onMessageDisposable: IDisposable; private _dashboardService: DashboardServiceInterface; constructor( @Inject(forwardRef(() => CommonServiceInterface)) private commonService: CommonServiceInterface, @Inject(forwardRef(() => ChangeDetectorRef)) private _changeRef: ChangeDetectorRef, @Inject(WIDGET_CONFIG) protected _config: WidgetConfig, @Inject(forwardRef(() => ElementRef)) private _el: ElementRef ) { super(); this._id = (_config.widget[selector] as IWebviewWidgetConfig).id; this._dashboardService = commonService as DashboardServiceInterface; } ngOnInit() { this._dashboardService.dashboardViewService.registerWebview(this); this._createWebview(); } public get id(): string { return this._id; } public setHtml(html: string): void { this._html = html; if (this._webview) { this._webview.contents = html; this._webview.layout(); } } @memoize public get connection(): sqlops.connection.Connection { let currentConnection = this._dashboardService.connectionManagementService.connectionInfo.connectionProfile; let connection: sqlops.connection.Connection = { providerName: currentConnection.providerName, connectionId: currentConnection.id, options: currentConnection.options }; return connection; } @memoize public get serverInfo(): sqlops.ServerInfo { return this._dashboardService.connectionManagementService.connectionInfo.serverInfo; } public layout(): void { this._webview.layout(); } public sendMessage(message: string): void { if (this._webview) { this._webview.sendMessage(message); } } private _createWebview(): void { if (this._webview) { this._webview.dispose(); } if (this._onMessageDisposable) { this._onMessageDisposable.dispose(); } this._webview = new Webview( this._el.nativeElement, this._dashboardService.partService.getContainer(Parts.EDITOR_PART), this._dashboardService.themeService, this._dashboardService.environmentService, this._dashboardService.contextViewService, undefined, undefined, { allowScripts: true, enableWrappedPostMessage: true } ); this._onMessageDisposable = this._webview.onMessage(e => { this._onMessage.fire(e); }); this._webview.style(this._dashboardService.themeService.getTheme()); if (this._html) { this._webview.contents = this._html; } this._webview.layout(); } }