Skip to content

Commit

Permalink
Merge pull request #174 from timgerstel/iframe/injectables
Browse files Browse the repository at this point in the history
[WIP] iFrame adapter: added support for plugin defintion, logger, and launch metadata
  • Loading branch information
1000TurquoisePogs committed Nov 1, 2019
2 parents 839388e + 6f4b4e9 commit 96c4cc2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
32 changes: 21 additions & 11 deletions bootstrap/web/iframe-adapter.js
Expand Up @@ -14,24 +14,32 @@
With ZLUX, there's a global called ZoweZLUX which holds useful tools. So, a site
Can determine what actions to take by knowing if it is or isnt embedded in ZLUX via IFrame.
*/

var responses = {};
var these = {}; //contains this'
var contextMenuActions = {};
var closeHandlers = {};
var pluginDef = undefined;
var launchMetadata = undefined;
var curResponseKey = 0;
var numUnresolved = 0;
var instanceId = -1;

let messageHandler = function(message) {
if(message.data.dispatchData){
if(instanceId === -1 && message.data.dispatchData.instanceId !== undefined){
instanceId = message.data.dispatchData.instanceId;
let data = message.data;
if(data.dispatchData){
if(instanceId === -1 && data.dispatchData.instanceId !== undefined){
instanceId = data.dispatchData.instanceId;
translateFunction('registerAdapterInstance', []);
} else if(instanceId !== -1 && data.dispatchData.instanceId !== undefined
&& data.dispatchData.instanceId !== instanceId){
console.warn('Desktop attempted to change instanceId for iframe instance=', instanceId, 'message=', message);
}
}
if(message.data.key === undefined || message.data.instanceId != instanceId) return;
let data = message.data;
if(data.key === undefined || data.instanceId != instanceId) return;
if(data.constructorData){
pluginDef = data.constructorData.pluginDef;
launchMetadata = data.constructorData.launchMetadata;
}
let key = data.key
if(responses[key]){
responses[key].resolve(data.value);
Expand Down Expand Up @@ -69,7 +77,7 @@ let messageHandler = function(message) {
console.log('restored')
return;
case 'windowEvents.moved':
console.log('moved')
//console.log('moved')
return;
case 'windowEvents.resized':
console.log('resized')
Expand All @@ -86,6 +94,8 @@ let messageHandler = function(message) {
}
}

window.addEventListener('message', messageHandler);

window.addEventListener("load", function () {
console.log('iFrame Adapter has loaded!');
window.top.postMessage('iframeload', '*');
Expand All @@ -95,7 +105,6 @@ window.addEventListener("unload", function () {
this.removeEventListener('message', messageHandler)
});

window.addEventListener('message', messageHandler);

function translateFunction(functionString, args){
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -247,9 +256,6 @@ var ZoweZLUX = {
invokeAction(action, eventContext, targetId){
return translateFunction('ZoweZLUX.dispatcher.invokeAction', [action, eventContext, targetId])
}
},
logger: {

},
registry: {

Expand Down Expand Up @@ -296,6 +302,10 @@ var ZoweZLUX = {
}
}

ZoweZLUX.logger = new exports.Logger();
ZoweZLUX.logger.addDestination(ZoweZLUX.logger.makeDefaultDestination(true, true, true, true, true))
var exports = (ZoweZLUX_tempExports) ? ZoweZLUX_tempExports : exports;

var windowActions = {
close(){
return translateFunction('windowActions.close', [])
Expand Down
Expand Up @@ -10,13 +10,15 @@
import { Injectable, Inject, Component, Optional } from '@angular/core';
import { Angular2InjectionTokens, Angular2PluginWindowActions, Angular2PluginWindowEvents, Angular2PluginViewportEvents } from '../../../../pluginlib/inject-resources';
import { SafeResourceUrl } from '@angular/platform-browser';
import { BaseLogger } from '../../../../app/shared/logger'

@Component({
templateUrl: './iframe-plugin.component.html'
})

@Injectable()
export class IFramePluginComponent {
private readonly logger: ZLUX.ComponentLogger = BaseLogger;
startingPage: SafeResourceUrl;
iframeId: string;
instanceId: number = -1;
Expand All @@ -27,7 +29,9 @@ export class IFramePluginComponent {
constructor(
@Optional() @Inject(Angular2InjectionTokens.WINDOW_ACTIONS) private windowActions: Angular2PluginWindowActions,
@Optional() @Inject(Angular2InjectionTokens.WINDOW_EVENTS) private windowEvents: Angular2PluginWindowEvents,
@Inject(Angular2InjectionTokens.VIEWPORT_EVENTS) private viewportEvents: Angular2PluginViewportEvents
@Inject(Angular2InjectionTokens.VIEWPORT_EVENTS) private viewportEvents: Angular2PluginViewportEvents,
@Inject(Angular2InjectionTokens.PLUGIN_DEFINITION) private pluginDefintion: ZLUX.ContainerPluginDefinition,
@Inject(Angular2InjectionTokens.LAUNCH_METADATA) private launchMetadata: any
){
addEventListener("message", this.postMessageListener.bind(this));
//The following references are to suppress typescript warnings
Expand All @@ -54,6 +58,27 @@ export class IFramePluginComponent {
if(split[0] === 'registerAdapterInstance' && this.instanceId == -1){
this.instanceId = data.request.instanceId;
this.frameSource = message.source;
try{
this.frameSource.postMessage({
key: -1,
constructorData: {
pluginDef: JSON.parse(JSON.stringify(this.pluginDefintion)),
launchMetadata: this.launchMetadata
},
instanceId: this.instanceId
}, '*')
}catch(e){
this.frameSource.postMessage({
key: -1,
constructorData: {
pluginDef: {},
launchMetadata: this.launchMetadata
},
instanceId: this.instanceId,
error: 'Unable to parse plugin definition'
}, '*');
this.logger.warn('Unable to parse plugin defintion. Error: ', e);
}
this.windowEvents.minimized.subscribe(() => {
this.postWindowEvent('windowEvents.minimized');
});
Expand Down Expand Up @@ -113,18 +138,23 @@ export class IFramePluginComponent {
}

private addActionsToContextMenu(key: number, source: any, itemsArray: Array<any>){
let copy = JSON.parse(JSON.stringify(itemsArray));
for(let i = 0; i < copy.length; i++){
copy[i].action = () => {
source.postMessage({
key: key,
originCall: 'windowActions.spawnContextMenu',
instanceId: this.instanceId,
contextMenuItemIndex: i
}, '*')
try{
let copy = JSON.parse(JSON.stringify(itemsArray));
for(let i = 0; i < copy.length; i++){
copy[i].action = () => {
source.postMessage({
key: key,
originCall: 'windowActions.spawnContextMenu',
instanceId: this.instanceId,
contextMenuItemIndex: i
}, '*')
}
}
return copy;
}catch(e){
this.logger.warn('Unable to parse context menu items. Error: ', e);
return undefined;
}
return copy;
}

private windowActionsHandler(fnSplit: Array<string>, args: Array<any>, message: any){
Expand Down Expand Up @@ -158,7 +188,6 @@ export class IFramePluginComponent {
let that = this;
args[0] = function(): Promise<void>{
return new Promise(function(resolve: any, reject: any){
console.log('close handler called from inside promise');
that.responses[message.data.key] = {
resolve: function(){
resolve();
Expand Down

0 comments on commit 96c4cc2

Please sign in to comment.