Skip to content

Commit b426690

Browse files
committed
Updated example
1 parent 4c5a9a9 commit b426690

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

index.js

+48-32
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,30 @@
2727
* @author Anders Evenrud <andersevenrud@gmail.com>
2828
* @licence Simplified BSD License
2929
*/
30+
import osjs from 'osjs';
31+
import {name as applicationName} from './metadata.json';
3032

31-
const createIframe = (bus, proc, win, cb) => {
33+
const createSimpleIframe = (core, proc) => ($content, win) => {
34+
const iframe = document.createElement('iframe');
35+
iframe.style.width = '100%';
36+
iframe.style.height = '100%';
37+
iframe.setAttribute('border', '0');
38+
39+
const src = proc.resource('/data/index.html');
40+
iframe.src = src;
41+
42+
$content.appendChild(iframe);
43+
};
44+
45+
const createIframe = (core, proc) => ($content, win) => {
46+
47+
// Create a new bus for our messaging
48+
const bus = core.make('osjs/event-handler', 'MyIframeApplicationWindow');
49+
50+
// Get path to iframe content
51+
const src = proc.resource('/data/index.html');
52+
53+
// Create DOM element
3254
const iframe = document.createElement('iframe');
3355
iframe.style.width = '100%';
3456
iframe.style.height = '100%';
@@ -51,15 +73,28 @@ const createIframe = (bus, proc, win, cb) => {
5173
bus.emit(data.method, sendMessage, ...data.args);
5274
});
5375

54-
cb(sendMessage);
76+
bus.on('yo', (send, args) => sendMessage({
77+
method: 'yo',
78+
args: ['MyIframeApplication says hello']
79+
}));
80+
81+
// Send the process ID to our iframe to establish communication
82+
sendMessage({
83+
method: 'init',
84+
args: [proc.pid]
85+
});
5586
});
5687

57-
return iframe;
88+
// Finally set the source and attach
89+
iframe.src = src;
90+
91+
// Attach
92+
$content.appendChild(iframe);
5893
};
5994

6095
// Creates the internal callback function when OS.js launches an application
6196
// Note the first argument is the 'name' taken from your metadata.json file
62-
OSjs.make('osjs/packages').register('MyIframeApplication', (core, args, options, metadata) => {
97+
const register = (core, args, options, metadata) => {
6398

6499
// Create a new Application instance
65100
const proc = core.make('osjs/application', {
@@ -76,33 +111,14 @@ OSjs.make('osjs/packages').register('MyIframeApplication', (core, args, options,
76111
position: {left: 700, top: 200}
77112
})
78113
.on('destroy', () => proc.destroy())
79-
.render(($content, win) => {
80-
// Create a new bus for our messaging
81-
const bus = core.make('osjs/event-handler', 'MyIframeApplicationWindow');
82-
83-
// Get path to iframe content
84-
const src = proc.resource('/data/index.html');
85-
86-
// Create DOM element
87-
const iframe = createIframe(bus, proc, win, send => {
88-
bus.on('yo', (send, args) => send({
89-
method: 'yo',
90-
args: ['MyIframeApplication says hello']
91-
}));
92-
93-
// Send the process ID to our iframe to establish communication
94-
send({
95-
method: 'init',
96-
args: [proc.pid]
97-
});
98-
});
99-
100-
// Finally set the source and attach
101-
iframe.src = src;
102-
103-
// Attach
104-
$content.appendChild(iframe);
105-
});
114+
115+
// Create an iframe with messaging etc
116+
.render(createIframe(core, proc));
117+
118+
// Or just a simple standard iframe
119+
// .render(createIframe(core, proc));
106120

107121
return proc;
108-
});
122+
};
123+
124+
osjs.register(applicationName, register);

webpack.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ module.exports = {
1212
optimization: {
1313
minimize,
1414
},
15+
externals: {
16+
osjs: 'OSjs'
17+
},
1518
plugins: [
1619
new CopyWebpackPlugin([
1720
{from: 'data', to: 'data'}

0 commit comments

Comments
 (0)