Skip to content

Commit 27c5bfe

Browse files
committed
Merge pull request #6 from georgeOsdDev/develop
Release v0.1.2
2 parents 94785b4 + 216e569 commit 27c5bfe

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## Change Log
22

3-
### Ver 0.1.2 (Next Release)
3+
### Ver 0.1.3 (Next Release)
4+
5+
### Ver 0.1.2
6+
* [#5 Add option `disableActiveWindow`](https://github.com/georgeOsdDev/react-web-notification/issues/5)
47

58
### Ver 0.1.1
69
* [#2 Compile before publishing](https://github.com/georgeOsdDev/react-web-notification/issues/2)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ React component which wrap web-notification.
2424
```javascript
2525
Notification.propTypes = {
2626
ignore: React.PropTypes.bool,
27+
disableActiveWindow: React.PropTypes.bool,
2728
notSupported: React.PropTypes.func,
2829
onPermissionGranted: React.PropTypes.func,
2930
onPermissionDenied: React.PropTypes.func,
@@ -40,6 +41,8 @@ Notification.propTypes = {
4041

4142
* `ignore` : if true, nothing will be happen
4243

44+
* `disableActiveWindow` : if true, nothing will be happen when window is active
45+
4346
* `notSupported()` : Called when [HTML5 Web Notification API](https://developer.mozilla.org/en/docs/Web/API/notification) is not supported.
4447

4548
* `onPermissionGranted()` : Called when permission granted.

components/Notification.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,30 @@ class Notification extends React.Component {
3333
};
3434
// Do not save Notification instance in state
3535
this.notifications = {};
36+
this.windowFocus = true;
37+
this.onWindowFocus = this._onWindowFocus.bind(this);
38+
this.onWindowBlur = this._onWindowBlur.bind(this);
39+
}
40+
41+
_onWindowFocus(){
42+
this.windowFocus = true;
43+
}
44+
45+
_onWindowBlur(){
46+
this.windowFocus = false;
3647
}
3748

3849
componentDidMount(){
50+
if (this.props.disableActiveWindow) {
51+
if (window.addEventListener){
52+
window.addEventListener('focus', this.onWindowFocus);
53+
window.addEventListener('blur', this.onWindowBlur);
54+
} else if (window.attachEvent){
55+
window.attachEvent('focus', this.onWindowFocus);
56+
window.attachEvent('blur', this.onWindowBlur);
57+
}
58+
}
59+
3960
if (!this.state.supported) {
4061
this.props.notSupported();
4162
} else {
@@ -58,8 +79,21 @@ class Notification extends React.Component {
5879
}
5980
}
6081

82+
componentWillUnmount(){
83+
if (this.props.disableActiveWindow) {
84+
if (window.removeEventListner){
85+
window.removeEventListener('focus', this.onWindowFocus);
86+
window.removeEventListener('blur', this.onWindowBlur);
87+
} else if (window.detachEvent){
88+
window.detachEvent('focus', this.onWindowFocus);
89+
window.detachEvent('blur', this.onWindowBlur);
90+
}
91+
}
92+
}
93+
6194
render() {
62-
if (!this.props.ignore && this.props.title && this.state.supported && this.state.granted) {
95+
let doNotShowOnActiveWindow = this.props.disableActiveWindow && this.windowFocus;
96+
if (!this.props.ignore && this.props.title && this.state.supported && this.state.granted && !doNotShowOnActiveWindow) {
6397

6498
let opt = this.props.options;
6599
if (typeof opt.tag !== 'string') {
@@ -103,6 +137,7 @@ class Notification extends React.Component {
103137

104138
Notification.propTypes = {
105139
ignore: React.PropTypes.bool,
140+
disableActiveWindow: React.PropTypes.bool,
106141
notSupported: React.PropTypes.func,
107142
onPermissionGranted: React.PropTypes.func,
108143
onPermissionDenied: React.PropTypes.func,
@@ -117,6 +152,7 @@ Notification.propTypes = {
117152

118153
Notification.defaultProps = {
119154
ignore: false,
155+
disableActiveWindow: false,
120156
notSupported: () => {},
121157
onPermissionGranted: () => {},
122158
onPermissionDenied: () => {},

example/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class App extends React.Component {
1010
constructor(props) {
1111
super(props);
1212
this.state = {
13-
ignore: true
13+
ignore: true,
14+
title: ''
1415
};
1516
}
1617

@@ -84,7 +85,7 @@ class App extends React.Component {
8485
<div>
8586
<button onClick={this.handleButtonClick.bind(this)}>Notif!</button>
8687
<Notification
87-
ignore={this.state.ignore && this.state.title}
88+
ignore={this.state.ignore && this.state.title !== ''}
8889
notSupported={this.handleNotSupported.bind(this)}
8990
onPermissionGranted={this.handlePermissionGranted.bind(this)}
9091
onPermissionDenied={this.handlePermissionDenied.bind(this)}

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"test:local": "karma start",
1111
"test": "./node_modules/.bin/karma start --browsers Firefox --single-run",
1212
"clean": "rimraf dist",
13-
"build": "clean & webpack"
13+
"build": "npm run clean & webpack"
1414
},
1515
"repository": {
1616
"type": "git",
@@ -19,9 +19,8 @@
1919
"keywords": [
2020
"react",
2121
"react-component",
22-
"url",
23-
"abchor",
24-
"link"
22+
"notification",
23+
"web noticifaction"
2524
],
2625
"author": "Takeharu.Oshida",
2726
"license": "MIT",

test/components/Notification_spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('Test of Notification', () => {
2222
it('should have default properties', function () {
2323
component = TestUtils.renderIntoDocument(<Notification title='test'/>);
2424
expect(component.props.ignore).to.be.eql(false);
25+
expect(component.props.disableActiveWindow).to.be.eql(false);
2526
expect(typeof component.props.notSupported).to.be.eql('function');
2627
expect(typeof component.props.onPermissionGranted).to.be.eql('function');
2728
expect(typeof component.props.onPermissionDenied).to.be.eql('function');

0 commit comments

Comments
 (0)