From 0ea8edc55fe5259252b306d2647c3dc2f3e8ea68 Mon Sep 17 00:00:00 2001 From: Leonty Chudinov Date: Mon, 23 Mar 2020 16:16:45 +0500 Subject: [PATCH] Web Browser App: App2App communication - phase 1 Signed-off-by: Leonty Chudinov --- .../webClient/src/app/app.component.ts | 35 ++++++++++++-- .../address-bar/address-bar.component.ts | 19 ++++---- .../src/app/browser/browser.module.ts | 3 +- .../src/app/browser/services/index.ts | 1 + .../browser/services/navigation.service.ts | 7 ++- .../src/app/browser/services/proxy.service.ts | 6 +-- .../app/browser/services/settings.service.ts | 46 +++++++++++++++++++ .../src/app/browser/shared/launch-metadata.ts | 4 ++ 8 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 system-apps/web-browser-app/webClient/src/app/browser/services/settings.service.ts diff --git a/system-apps/web-browser-app/webClient/src/app/app.component.ts b/system-apps/web-browser-app/webClient/src/app/app.component.ts index f9fd9a3ce..f26c53daf 100644 --- a/system-apps/web-browser-app/webClient/src/app/app.component.ts +++ b/system-apps/web-browser-app/webClient/src/app/app.component.ts @@ -12,8 +12,9 @@ import { Component, Inject } from '@angular/core'; import { Angular2InjectionTokens } from 'pluginlib/inject-resources'; - import { LocaleService, TranslationService } from 'angular-l10n'; +import { WebBrowserLaunchMetadata, isLaunchMetadata } from './browser/shared'; +import { NavigationService, ProxyService, SettingsService } from './browser/services'; @Component({ selector: 'app-root', @@ -25,9 +26,10 @@ export class AppComponent { constructor( public locale: LocaleService, public translation: TranslationService, - @Inject(Angular2InjectionTokens.PLUGIN_DEFINITION) public pluginDefinition: ZLUX.ContainerPluginDefinition, @Inject(Angular2InjectionTokens.LOGGER) public log: ZLUX.ComponentLogger, - @Inject(Angular2InjectionTokens.LAUNCH_METADATA) public launchMetadata: any, + private navigation: NavigationService, + private proxy: ProxyService, + private settings: SettingsService, ) { } @@ -35,6 +37,33 @@ export class AppComponent { this.log.info(`web browser started`); } + provideZLUXDispatcherCallbacks(): ZLUX.ApplicationCallbacks { + return { + onMessage: (eventContext: any): Promise => this.zluxOnMessage(eventContext) + } + } + + private zluxOnMessage(eventContext: any): Promise { + if (isLaunchMetadata(eventContext)) { + this.handleLaunchMetadata(eventContext.data); + return Promise.resolve(); + } + return Promise.reject(`Event context missing or malformed`); + } + + private handleLaunchMetadata(launchMetaData: Partial): void { + const { enableProxy, hideControls, url } = launchMetaData; + if (typeof enableProxy === 'boolean' && this.proxy.isEnabled() !== enableProxy) { + this.proxy.toggle(); + } + if (typeof hideControls === 'boolean' && !this.settings.areControlsVisible() !== hideControls) { + this.settings.toggleControls(); + } + if (typeof url === 'string') { + this.navigation.navigate(url); + } + } + } diff --git a/system-apps/web-browser-app/webClient/src/app/browser/address-bar/address-bar.component.ts b/system-apps/web-browser-app/webClient/src/app/browser/address-bar/address-bar.component.ts index dae756143..5b3a909ac 100644 --- a/system-apps/web-browser-app/webClient/src/app/browser/address-bar/address-bar.component.ts +++ b/system-apps/web-browser-app/webClient/src/app/browser/address-bar/address-bar.component.ts @@ -8,13 +8,12 @@ Copyright Contributors to the Zowe Project. */ -import { Component, OnInit, OnDestroy, Optional, Inject, HostBinding } from '@angular/core'; +import { Component, OnInit, OnDestroy, HostBinding } from '@angular/core'; import { FormControl } from '@angular/forms'; import { Subscription } from 'rxjs'; import { throttleTime } from 'rxjs/operators'; import { NavigationService, ProxyService } from '../services'; -import { Angular2InjectionTokens } from 'pluginlib/inject-resources'; -import { LaunchMetadata } from '../shared'; +import { SettingsService } from '../services/settings.service'; @Component({ selector: 'app-address-bar', @@ -27,18 +26,12 @@ export class AddressBarComponent implements OnInit, OnDestroy { placeholder = 'URL'; proxyValueSubscription: Subscription; proxyErrorSubscription: Subscription; - @HostBinding('hidden') - isHidden: boolean; constructor( private navigation: NavigationService, private proxy: ProxyService, - @Optional() @Inject(Angular2InjectionTokens.LAUNCH_METADATA) - launchMetadata: Partial, + private settings: SettingsService, ) { - if (launchMetadata && launchMetadata.data && launchMetadata.data.hideControls) { - this.isHidden = true; - } this.urlControl = new FormControl(navigation.startURL); this.proxyControl = new FormControl(this.proxy.isEnabled()); this.proxyValueSubscription = this.proxyControl.valueChanges.pipe( @@ -49,6 +42,10 @@ export class AddressBarComponent implements OnInit, OnDestroy { ngOnInit() { } + + @HostBinding('hidden') get isHidden(): boolean { + return !this.settings.areControlsVisible(); + } navigate(): void { if (this.urlControl.value) { @@ -99,7 +96,7 @@ export class AddressBarComponent implements OnInit, OnDestroy { this.proxyErrorSubscription.unsubscribe(); } } - + } diff --git a/system-apps/web-browser-app/webClient/src/app/browser/browser.module.ts b/system-apps/web-browser-app/webClient/src/app/browser/browser.module.ts index aec994830..cf3eb846a 100644 --- a/system-apps/web-browser-app/webClient/src/app/browser/browser.module.ts +++ b/system-apps/web-browser-app/webClient/src/app/browser/browser.module.ts @@ -14,7 +14,7 @@ import { BrowserComponent } from './browser/browser.component'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { AddressBarComponent } from './address-bar/address-bar.component'; import { BrowserWindowComponent } from './browser-window/browser-window.component'; -import { NavigationService, ProxyService } from './services'; +import { NavigationService, ProxyService, SettingsService } from './services'; @NgModule({ imports: [ @@ -30,6 +30,7 @@ import { NavigationService, ProxyService } from './services'; providers: [ NavigationService, ProxyService, + SettingsService, ], exports: [ BrowserComponent diff --git a/system-apps/web-browser-app/webClient/src/app/browser/services/index.ts b/system-apps/web-browser-app/webClient/src/app/browser/services/index.ts index cdf3cc03f..3a111e15a 100644 --- a/system-apps/web-browser-app/webClient/src/app/browser/services/index.ts +++ b/system-apps/web-browser-app/webClient/src/app/browser/services/index.ts @@ -10,6 +10,7 @@ export * from './navigation.service'; export * from './proxy.service'; +export * from './settings.service'; /* This program and the accompanying materials are diff --git a/system-apps/web-browser-app/webClient/src/app/browser/services/navigation.service.ts b/system-apps/web-browser-app/webClient/src/app/browser/services/navigation.service.ts index 1ac1f990c..2d87f29b2 100644 --- a/system-apps/web-browser-app/webClient/src/app/browser/services/navigation.service.ts +++ b/system-apps/web-browser-app/webClient/src/app/browser/services/navigation.service.ts @@ -12,7 +12,7 @@ import { Injectable, Inject, Optional } from '@angular/core'; import { Observable, ReplaySubject } from 'rxjs'; import { ProxyService } from './proxy.service'; import { switchMap } from 'rxjs/operators'; -import { LaunchMetadata } from '../shared'; +import { isLaunchMetadata } from '../shared'; import { Angular2InjectionTokens } from 'pluginlib/inject-resources'; @Injectable() @@ -27,11 +27,10 @@ export class NavigationService { constructor( @Optional() @Inject(Angular2InjectionTokens.LAUNCH_METADATA) - launchMetadata: Partial, + launchMetadata: any, private proxy: ProxyService ) { - console.log(`navigation service got launch metadata:\n${JSON.stringify(launchMetadata, null, 2)}`); - if (launchMetadata && launchMetadata.data && typeof launchMetadata.data.url === 'string') { + if (isLaunchMetadata(launchMetadata) && typeof launchMetadata.data.url === 'string') { this.startURL = launchMetadata.data.url; } this.url$ = this.urlSubject.pipe(switchMap(url => this.proxy.process(url))); diff --git a/system-apps/web-browser-app/webClient/src/app/browser/services/proxy.service.ts b/system-apps/web-browser-app/webClient/src/app/browser/services/proxy.service.ts index faabbcf3c..5f3a1659e 100644 --- a/system-apps/web-browser-app/webClient/src/app/browser/services/proxy.service.ts +++ b/system-apps/web-browser-app/webClient/src/app/browser/services/proxy.service.ts @@ -13,7 +13,7 @@ import { HttpClient } from '@angular/common/http'; import { Observable, BehaviorSubject, of, Subject } from 'rxjs'; import { Angular2InjectionTokens } from 'pluginlib/inject-resources'; import { map, tap, catchError, switchMap } from 'rxjs/operators'; -import { LaunchMetadata } from '../shared'; +import { isLaunchMetadata } from '../shared'; export interface ProxyServerResult { @@ -33,10 +33,10 @@ export class ProxyService { @Inject(Angular2InjectionTokens.PLUGIN_DEFINITION) private pluginDefinition: ZLUX.ContainerPluginDefinition, @Optional() @Inject(Angular2InjectionTokens.LAUNCH_METADATA) - launchMetadata: Partial, + launchMetadata: any, ) { this.proxyServiceURL = ZoweZLUX.uriBroker.pluginRESTUri(this.pluginDefinition.getBasePlugin(), 'proxy', ''); - if (launchMetadata && launchMetadata.data && launchMetadata.data.enableProxy) { + if (isLaunchMetadata(launchMetadata) && launchMetadata.data.enableProxy) { this.enabled = true; } } diff --git a/system-apps/web-browser-app/webClient/src/app/browser/services/settings.service.ts b/system-apps/web-browser-app/webClient/src/app/browser/services/settings.service.ts new file mode 100644 index 000000000..816dfa8b4 --- /dev/null +++ b/system-apps/web-browser-app/webClient/src/app/browser/services/settings.service.ts @@ -0,0 +1,46 @@ +/* + This program and the accompanying materials are + made available under the terms of the Eclipse Public License v2.0 which accompanies + this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html + + SPDX-License-Identifier: EPL-2.0 + + Copyright Contributors to the Zowe Project. +*/ + +import { Injectable, Optional, Inject } from '@angular/core'; +import { Angular2InjectionTokens } from 'pluginlib/inject-resources'; +import { isLaunchMetadata } from '../shared'; + +@Injectable() +export class SettingsService { + private controlsVisible = true; + + constructor( + @Optional() @Inject(Angular2InjectionTokens.LAUNCH_METADATA) + launchMetadata: any, + ) { + if (isLaunchMetadata(launchMetadata) && launchMetadata.data.hideControls) { + this.controlsVisible = false; + } + } + + toggleControls(): void { + this.controlsVisible = !this.controlsVisible; + } + + areControlsVisible(): boolean { + return this.controlsVisible; + } +} + + +/* + This program and the accompanying materials are + made available under the terms of the Eclipse Public License v2.0 which accompanies + this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html + + SPDX-License-Identifier: EPL-2.0 + + Copyright Contributors to the Zowe Project. +*/ diff --git a/system-apps/web-browser-app/webClient/src/app/browser/shared/launch-metadata.ts b/system-apps/web-browser-app/webClient/src/app/browser/shared/launch-metadata.ts index d6e2127a7..183086a1d 100644 --- a/system-apps/web-browser-app/webClient/src/app/browser/shared/launch-metadata.ts +++ b/system-apps/web-browser-app/webClient/src/app/browser/shared/launch-metadata.ts @@ -18,6 +18,10 @@ export interface WebBrowserLaunchMetadata { enableProxy: boolean; } +export function isLaunchMetadata(data: any): data is LaunchMetadata { + return data && data.data != null && typeof data.data === 'object'; +} + /* This program and the accompanying materials are