Skip to content

Commit 678a060

Browse files
authored
Merge pull request #133 from SpringRoll/release/2.4.0
Release 2.4.0
2 parents 2145070 + 8b1eeb9 commit 678a060

File tree

10 files changed

+123
-24
lines changed

10 files changed

+123
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.4.0] unreleased
9+
10+
## Added
11+
12+
- Added `manageOwnVisibility` flag to the PausePlugin to make turning off visibility/focus management easier
813

914
## [2.3.4] 2022-06-09
1015

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ container.openPath('game.html');
115115

116116
`PausePlugin` sets a className of 'paused' or 'unpaused' on individual pause buttons.
117117

118+
The `PausePlugin` also has an optional flag
119+
`manageOwnVisibility`. This defaults to `true` and is intended to disable the visibility management of the plugin for environements
120+
that handle visibility/focus themselves. For most web based contexts this flag will not be needed.
121+
122+
```javascript
123+
import { PausePlugin, Container } from 'springroll-container';
124+
125+
const container = new Container("#game", {
126+
plugins: [
127+
// manageOwnVisibility is set to false which means the plugin will not send pause or unpause events if the app is no longer
128+
// focused or other similar states (switched tabs, etc)
129+
new PausePlugin('button#pause-button', false),
130+
]
131+
});
132+
container.openPath('game.html');
133+
```
134+
118135
### Captions
119136

120137
There are two plugins that interact with captions: `CaptionsTogglePlugin`, and `CaptionsStylePlugin`.

dist/SpringRoll-Container-umd.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/SpringRoll-Container-umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "springroll-container",
3-
"version": "2.3.4",
3+
"version": "2.4.0",
44
"description": "The iframe controller for interacting with SpringRoll applications",
55
"main": "./dist/index.js",
66
"license": "MIT",

src/plugins/PausePlugin.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import { Button } from '../ui-elements';
99
export class PausePlugin extends ButtonPlugin {
1010
/**
1111
* Creates an instance of PausePlugin.
12-
* @param {string | HTMLElement} pauseButton selector string or HTML Element for the input(s)
12+
* @param {string | HTMLElement} pauseButton selector string or HTML Element for the input(s)
13+
* @param {boolean} manageOwnVisibility whether the plugin should manage container's visibility or some other source will handle it
1314
* @memberof PausePlugin
1415
*/
15-
constructor(pauseButton) {
16+
constructor(pauseButton, manageOwnVisibility = true) {
1617
super('Pause-Button-plugin');
18+
this._manageOwnVisibility = manageOwnVisibility;
1719
this._appBlurred = false;
1820
this._containerBlurred = false;
1921
this._focusTimer = null;
@@ -35,7 +37,9 @@ export class PausePlugin extends ButtonPlugin {
3537
this.onContainerBlur.bind(this)
3638
);
3739

38-
if ( pauseButton instanceof HTMLElement ) {
40+
this.pageVisibility.enabled = this.manageOwnVisibility;
41+
42+
if (pauseButton instanceof HTMLElement) {
3943
this._pauseButton[0] = new Button({
4044
button: pauseButton,
4145
onClick: onPauseToggle,
@@ -81,10 +85,29 @@ export class PausePlugin extends ButtonPlugin {
8185
* @memberof PausePlugin
8286
* @returns {Boolean}
8387
*/
84-
get pause() {
88+
get pause() {
8589
return this._paused;
8690
}
8791

92+
/**
93+
* updates _manageOwnVisibility and also re-enables pageVisibility
94+
* @memberof PausePlugin
95+
* @param {Boolean} manageOwnVisibility
96+
*/
97+
set manageOwnVisibility(manageOwnVisibility) {
98+
this._manageOwnVisibility = manageOwnVisibility;
99+
100+
this.pageVisibility.enabled = this._manageOwnVisibility;
101+
}
102+
103+
/**
104+
* @memberof PausePlugin
105+
* @returns {Boolean}
106+
*/
107+
get manageOwnVisibility() {
108+
return this._manageOwnVisibility;
109+
}
110+
88111
/**
89112
* forces focus onto the iframe application window
90113
* @memberof PausePlugin
@@ -116,6 +139,9 @@ export class PausePlugin extends ButtonPlugin {
116139
* @memberof PausePlugin
117140
*/
118141
manageFocus() {
142+
if (!this.manageOwnVisibility) {
143+
return;
144+
}
119145
// Unfocus on the iframe
120146
if (this._keepFocus) {
121147
this.blurApp();
@@ -132,7 +158,7 @@ export class PausePlugin extends ButtonPlugin {
132158
// causing rapid toggling of the pause state and related issues,
133159
// especially in Internet Explorer
134160
this._focusTimer = setTimeout(
135-
function() {
161+
function () {
136162
this._focusTimer = null;
137163
// A manual pause cannot be overriden by focus events.
138164
// User must click the resume button.
@@ -215,9 +241,10 @@ export class PausePlugin extends ButtonPlugin {
215241
*/
216242
init({ iframe }) {
217243
this.iframe = iframe;
244+
218245
this.client.on(
219246
'features',
220-
function(features) {
247+
function (features) {
221248
if (features.disablePause) {
222249
this.pauseDisabled = true;
223250
}

src/plugins/PausePlugin.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,54 @@ describe('PausePlugin', () => {
4444
expect(pp.pauseButton[0].classList.contains('unpaused')).to.be.true;
4545
});
4646

47+
describe('manageOwnVisibility', () => {
48+
let disabledPlugin;
49+
before(() => {
50+
const button = document.createElement('button');
51+
document.body.innerHTML = '';
52+
button.id = 'ignore';
53+
document.body.appendChild(button);
54+
disabledPlugin = new PausePlugin('#ignore', false); // disable the focus management of the plugin
55+
disabledPlugin.preload({ client: new Bellhop() });
56+
});
57+
58+
it('should set manageOwnVisibility to false', () => {
59+
// control
60+
expect(pp.manageOwnVisibility).to.be.true;
61+
62+
expect(disabledPlugin.manageOwnVisibility).to.be.false;
63+
});
64+
65+
it('should set pageVisibility.enabled to false', () => {
66+
expect(disabledPlugin.pageVisibility.enabled).to.be.false;
67+
});
68+
69+
it('manageFocus() should not change pause state', async () => {
70+
expect(disabledPlugin.pause).to.be.false;
71+
72+
disabledPlugin._containerBlurred = true;
73+
disabledPlugin._appBlurred = true;
74+
75+
disabledPlugin.manageFocus();
76+
await sleep(150);
77+
expect(disabledPlugin.pause).to.be.false;
78+
});
79+
80+
it('setting flag to true should re-enable everything', async () => {
81+
disabledPlugin.manageOwnVisibility = true;
82+
83+
expect(disabledPlugin.pageVisibility.enabled).to.be.true;
84+
disabledPlugin._containerBlurred = true;
85+
disabledPlugin._appBlurred = true;
86+
87+
disabledPlugin.manageFocus();
88+
await sleep(150);
89+
expect(disabledPlugin.pause).to.be.true;
90+
});
91+
92+
93+
});
94+
4795
describe('manageFocus()', () => {
4896
it('should set pause to false if only app is blurred', async () => {
4997
pp.onPauseToggle(); //reset the manual pause state which was being set to true for some reason.

0 commit comments

Comments
 (0)