Skip to content

Commit 1fddc27

Browse files
authored
Added unload listener and trigger (#18)
* Added unload listener and trigger Added tests for load function Adjusted changelog and contributors * Removed target param and added to readme * Updated tests to remove target from unmount * Refactored frame to allow unload event to initiate data-status update * Cleaned code * Updated Version number * Took url out of unload trigger * Updated changelog
1 parent 97d52c2 commit 1fddc27

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Next Release
22
-------------
3+
* Added trigger for frame unloading. #18
4+
35
1.3.2
46
------
57
* Updated default height calculation to max to account for dropdowns.

CONTRIBUTORS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ Cerner Corporation
66
- Elliott Hoffman [@slor]
77
- Greg Howdeshell [@poloka]
88
- Matt Schile [@mschile]
9+
- Sam Milligan [@grneggandsam]
910

1011
[@mhemesath]: https://github.com/mhemesath
1112
[@kafkahw]: https://github.com/kafkahw
1213
[@cmgurba]: https://github.com/cmgurba
1314
[@slor]: https://github.com/slor
1415
[@poloka]: https://github.com/poloka
1516
[@mschile]: https://github.com/mschile
17+
[@grneggandsam]: https://github.com/grneggandsam

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Application lifecycles go through 3 stages as they load:
9696
1. ```mounted``` The application frame has been appended to the DOM and is loading the remote application site.
9797
2. ```launched``` The application frame has loaded and the embedded application has begun authorization sequence. At this time the app is loaded, but is hidden to prevent clickjacking.
9898
3. ```authorized``` The application has approved authorization and is now visible.
99+
4. ```unload``` The application frame is about to unload due to redirect or other causes.
99100

100101
These statuses are communicated to the consumer application environment in 2 ways.
101102

@@ -137,6 +138,11 @@ frame.on('xfc.launched', function() {
137138
frame.on('xfc.authorized', function(detail) {
138139
console.log('authorized', detail);
139140
})
141+
142+
// Listen for a container to trigger an unload event
143+
frame.on('xfc.unload', function(detail) {
144+
console.log('unloading', detail);
145+
})
140146
```
141147

142148
### Fullscreen Events

src/consumer/frame.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class Frame extends EventEmitter {
5353
return Promise.resolve();
5454
},
5555

56+
unload(detail = {}) {
57+
self.wrapper.setAttribute('data-status', 'unloaded');
58+
self.emit('xfc.unload', detail);
59+
return Promise.resolve();
60+
},
61+
5662
resize(height = null, width = null) {
5763
if (typeof resizeConfig.customCalculationMethod === 'function') {
5864
resizeConfig.customCalculationMethod.call(self.iframe);

src/provider/application.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Application extends EventEmitter {
1919
this.authorizeConsumer = this.authorizeConsumer.bind(this);
2020
this.verifyChallenge = this.verifyChallenge.bind(this);
2121
this.emitError = this.emitError.bind(this);
22+
this.unload = this.unload.bind(this);
2223

2324
// If the document referer (parent frame) origin is trusted, default that
2425
// to the active ACL;
@@ -115,8 +116,9 @@ class Application extends EventEmitter {
115116
*/
116117
launch() {
117118
if (window.self !== window.top) {
118-
// 1: Setup listeners for all incoming communication
119+
// 1: Setup listeners for all incoming communication and beforeunload
119120
window.addEventListener('message', this.handleConsumerMessage);
121+
window.addEventListener('beforeunload', this.unload);
120122

121123
// 2: Begin launch and authorization sequence
122124
this.JSONRPC.notification('launch');
@@ -231,6 +233,11 @@ class Application extends EventEmitter {
231233
emitError(error) {
232234
this.emit('xfc.error', error);
233235
}
236+
237+
unload() {
238+
this.JSONRPC.notification('unload');
239+
this.trigger('xfc.unload');
240+
}
234241
}
235242

236243
export default Application;

test/application.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ describe('Application', () => {
227227
expect(emittedError).to.equal(testErr);
228228
});
229229
});
230+
231+
describe("#unload()", () => {
232+
it("calls this.trigger with event 'xfc.unload'", sinon.test(function() {
233+
const trigger = this.stub(application, 'trigger');
234+
application.unload();
235+
236+
sinon.assert.calledWith(trigger, 'xfc.unload');
237+
}));
238+
});
230239
});
231240

232241
describe('#launch()', () => {

0 commit comments

Comments
 (0)