diff --git a/LICENSE b/LICENSE index 73f3cf5..f132062 100644 --- a/LICENSE +++ b/LICENSE @@ -2,7 +2,7 @@ BSD 2-Clause License OS.js - JavaScript Cloud/Web Desktop Platform -Copyright (c) 2011-2018, Anders Evenrud +Copyright (c) 2011-2019, Anders Evenrud All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/data/index.html b/data/index.html index 767aa03..8fee3ae 100644 --- a/data/index.html +++ b/data/index.html @@ -6,59 +6,33 @@

Hello World

- - -
+
+ +
+ diff --git a/index.js b/index.js index 4ba4f70..a5a30e7 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ /* * OS.js - JavaScript Cloud/Web Desktop Platform * - * Copyright (c) 2011-2018, Anders Evenrud + * Copyright (c) 2011-2019, Anders Evenrud * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,82 +27,47 @@ * @author Anders Evenrud * @licence Simplified BSD License */ +import osjs from 'osjs'; +import {name as applicationName} from './metadata.json'; -const createIframe = (bus, proc, win, cb) => { - const iframe = document.createElement('iframe'); - iframe.style.width = '100%'; - iframe.style.height = '100%'; - iframe.setAttribute('border', '0'); - - iframe.addEventListener('load', () => { - const ref = iframe.contentWindow; - - // This will proxy the window focus events to iframe - win.on('focus', () => ref.focus()); - win.on('blur', () => ref.blur()); - - // Create message sending wrapper - const sendMessage = msg => ref.postMessage(msg, window.location.href); - - // After connection is established, this handler will process - // all events coming from iframe. - proc.on('message', data => { - console.warn('[Application', 'Iframe sent', data); - bus.emit(data.method, sendMessage, ...data.args); - }); - - cb(sendMessage); - }); - - return iframe; -}; +osjs.register(applicationName, (core, args, options, metadata) => { + // Create a new Application instance + const proc = core.make('osjs/application', {args, options, metadata}); -// Creates the internal callback function when OS.js launches an application -// Note the first argument is the 'name' taken from your metadata.json file -OSjs.make('osjs/packages').register('MyIframeApplication', (core, args, options, metadata) => { + // Create a new IFrame Window + const win = proc.createIframeWindow(); - // Create a new Application instance - const proc = core.make('osjs/application', { - args, - options, - metadata + // Register our iframe events + win.on('iframe:init', context => { + console.log('iframe inited'); }); - // Create a new Window instance - proc.createWindow({ - id: 'MyIframeApplicationWindow', - title: metadata.title.en_EN, - dimension: {width: 400, height: 400}, - position: {left: 700, top: 200} - }) - .on('destroy', () => proc.destroy()) - .render(($content, win) => { - // Create a new bus for our messaging - const bus = core.make('osjs/event-handler', 'MyIframeApplicationWindow'); + win.on('iframe:message', (context, payload) => { + const {type} = payload; - // Get path to iframe content - const src = proc.resource('/data/index.html'); + switch (type) { + case 'ping': + console.log('IFrame says hello'); + context.send({type: 'pong'}); + break; - // Create DOM element - const iframe = createIframe(bus, proc, win, send => { - bus.on('yo', (send, args) => send({ - method: 'yo', - args: ['MyIframeApplication says hello'] - })); - - // Send the process ID to our iframe to establish communication - send({ - method: 'init', - args: [proc.pid] - }); + case 'create-dialog': + core.make('osjs/dialog', 'alert', {message: 'IFrame dialog'}, (btn, value) => { + context.respond({btn, value}); }); + break; + + default: + console.warn('Unknown iframe signal', payload); + break; + } + }); - // Finally set the source and attach - iframe.src = src; + // Make sure application is destroyed when window closes + win.on('destroy', () => proc.destroy()); - // Attach - $content.appendChild(iframe); - }); + // Opens the window iframe to a spesified location + win.open(proc.resource('/data/index.html')); return proc; }); diff --git a/package.json b/package.json index 2d5b5de..1dc0911 100644 --- a/package.json +++ b/package.json @@ -22,16 +22,16 @@ ], "dependencies": {}, "devDependencies": { - "@babel/core": "^7.0.0-beta.54", - "@babel/plugin-transform-runtime": "^7.0.0-beta.54", - "@babel/preset-env": "^7.0.0-beta.54", - "@babel/runtime": "^7.0.0-beta.54", - "babel-loader": "^8.0.0-beta.4", - "copy-webpack-plugin": "^4.5.2", - "eslint": "^5.1.0", - "hyperapp": "^1.2.0", - "webpack": "^4.16.2", - "webpack-cli": "^3.1.0" + "@babel/core": "^7.2.2", + "@babel/plugin-transform-runtime": "^7.2.0", + "@babel/preset-env": "^7.2.3", + "@babel/runtime": "^7.2.0", + "babel-loader": "^8.0.5", + "copy-webpack-plugin": "^4.6.0", + "eslint": "^5.12.0", + "hyperapp": "^1.2.9", + "webpack": "^4.28.3", + "webpack-cli": "^3.2.0" }, "author": "Anders Evenrud ", "license": "BSD-2-Clause", diff --git a/webpack.config.js b/webpack.config.js index 8d57b08..830a62f 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -9,6 +9,9 @@ module.exports = { entry: [ path.resolve(__dirname, 'index.js'), ], + externals: { + osjs: 'OSjs' + }, optimization: { minimize, },