Skip to content

Commit

Permalink
Added unload listener and trigger (#18)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
grneggandsam committed Mar 22, 2018
1 parent 97d52c2 commit 1fddc27
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Next Release
-------------
* Added trigger for frame unloading. #18

1.3.2
------
* Updated default height calculation to max to account for dropdowns.
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Cerner Corporation
- Elliott Hoffman [@slor]
- Greg Howdeshell [@poloka]
- Matt Schile [@mschile]
- Sam Milligan [@grneggandsam]

[@mhemesath]: https://github.com/mhemesath
[@kafkahw]: https://github.com/kafkahw
[@cmgurba]: https://github.com/cmgurba
[@slor]: https://github.com/slor
[@poloka]: https://github.com/poloka
[@mschile]: https://github.com/mschile
[@grneggandsam]: https://github.com/grneggandsam
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Application lifecycles go through 3 stages as they load:
1. ```mounted``` The application frame has been appended to the DOM and is loading the remote application site.
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.
3. ```authorized``` The application has approved authorization and is now visible.
4. ```unload``` The application frame is about to unload due to redirect or other causes.

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

Expand Down Expand Up @@ -137,6 +138,11 @@ frame.on('xfc.launched', function() {
frame.on('xfc.authorized', function(detail) {
console.log('authorized', detail);
})

// Listen for a container to trigger an unload event
frame.on('xfc.unload', function(detail) {
console.log('unloading', detail);
})
```

### Fullscreen Events
Expand Down
6 changes: 6 additions & 0 deletions src/consumer/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class Frame extends EventEmitter {
return Promise.resolve();
},

unload(detail = {}) {
self.wrapper.setAttribute('data-status', 'unloaded');
self.emit('xfc.unload', detail);
return Promise.resolve();
},

resize(height = null, width = null) {
if (typeof resizeConfig.customCalculationMethod === 'function') {
resizeConfig.customCalculationMethod.call(self.iframe);
Expand Down
9 changes: 8 additions & 1 deletion src/provider/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Application extends EventEmitter {
this.authorizeConsumer = this.authorizeConsumer.bind(this);
this.verifyChallenge = this.verifyChallenge.bind(this);
this.emitError = this.emitError.bind(this);
this.unload = this.unload.bind(this);

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

// 2: Begin launch and authorization sequence
this.JSONRPC.notification('launch');
Expand Down Expand Up @@ -231,6 +233,11 @@ class Application extends EventEmitter {
emitError(error) {
this.emit('xfc.error', error);
}

unload() {
this.JSONRPC.notification('unload');
this.trigger('xfc.unload');
}
}

export default Application;
9 changes: 9 additions & 0 deletions test/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ describe('Application', () => {
expect(emittedError).to.equal(testErr);
});
});

describe("#unload()", () => {
it("calls this.trigger with event 'xfc.unload'", sinon.test(function() {
const trigger = this.stub(application, 'trigger');
application.unload();

sinon.assert.calledWith(trigger, 'xfc.unload');
}));
});
});

describe('#launch()', () => {
Expand Down

0 comments on commit 1fddc27

Please sign in to comment.