Skip to content

Commit

Permalink
Merge pull request #3 from jaanauati/eslint-object-spread
Browse files Browse the repository at this point in the history
eslint fixes
  • Loading branch information
jaanauati committed May 21, 2016
2 parents 7e19641 + dc068a6 commit b0fe76a
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 130 deletions.
9 changes: 7 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
"beforeEach": true,
"afterEach": true
},
"ecmaFeatures": {
"experimentalObjectRestSpread": true
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
"rules": {
"react/prefer-es6-class": 0
}
}
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# React DFP
Inspired on jquery.dfp :-)... gpt/dfp components for react.
Gpt/dfp components that you can easily use in your isomorphic react apps. This package is inspired in the awesome library [jquery.dfp](https://github.com/coop182/jquery.dfp.js), and aims to provide its same ease of usage but, of course, taking into consideration the react concepts & lifecycle features.


## Install:
```
Expand Down
1 change: 1 addition & 0 deletions grunt/aliases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ default:
- 'dist'

dist:
- 'eslint'
- 'clean'
- 'babel:dist'

Expand Down
85 changes: 43 additions & 42 deletions js/adslot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import React from 'react';
import {DFPManager} from './manager';
import { DFPManager } from './manager';


export const AdSlot = React.createClass({
Expand All @@ -20,71 +20,65 @@ export const AdSlot = React.createClass({
shouldRefresh: React.PropTypes.func,
slotId: React.PropTypes.string,
},



getDefaultProps() {
return {
fetchNow: false,
};
},


getInitialState() {
return { ...this.props, slotId: this.generateSlotId() };
},

componentDidMount() {
this.registerSlot();
},

componentWillReceiveProps(nextProps) {
if (nextProps.hasOwnProperty('objectId')) {
const state = this.state;
state.slotId = this.generateSlotId();
this.unregisterSlot();
this.setState(state);
this.registerSlot();
}
},

componentWillUnmount() {
this.unregisterSlot();
},

getSlotId() {
return this.props.slotId || this.state.slotId;
},

generateSlotId() {
let slotId = this.props.slotId;
if (slotId === undefined) {
let seconds = (Date.now && Date.now() || new Date().getTime()) / 1000;
const seconds = (Date.now && Date.now() || new Date().getTime()) / 1000;
slotId = `adSlot-${seconds}`;
}
return slotId
},

getSlotId() {
return this.props.slotId || this.state.slotId;
return slotId;
},

registerSlot() {
DFPManager.registerSlot({ ...this.props, ...this.state, slotShouldRefresh: this.slotShouldRefresh });
DFPManager.registerSlot({ ...this.props, ...this.state,
slotShouldRefresh: this.slotShouldRefresh });
if (this.props.fetchNow === true) {
DFPManager.load(this.getSlotId());
}
DFPManager.attachSlotRenderEnded(this.slotRenderEnded);
},

unregisterSlot() {
DFPManager.unregisterSlot({ ...this.props, ...this.state });
DFPManager.detachSlotRenderEnded(this.slotRenderEnded);
},

getInitialState() {
return { ...this.props, slotId: this.generateSlotId() };
},

componentDidMount() {
this.registerSlot();
},

render() {
return (
<div className="adunitContainer"> <div id={this.getSlotId()} className="adBox" /> </div>
);
},

componentWillUnmount() {
this.unregisterSlot();
},

componentWillReceiveProps: function(nextProps) {
if (nextProps.hasOwnProperty('objectId')) {
let state = this.state;
state.slotId = this.generateSlotId();
this.unregisterSlot();
this.setState(state);
this.registerSlot();
}
},

slotRenderEnded(eventData) {
if (eventData.slotId === this.getSlotId()) {
if (this.props.onSlotRender !== undefined) {
if (this.props.onSlotRender !== undefined) {
this.props.onSlotRender(eventData);
}
}
Expand All @@ -93,8 +87,15 @@ export const AdSlot = React.createClass({
slotShouldRefresh() {
let r = true;
if (this.props.shouldRefresh !== undefined) {
r = this.props.shouldRefresh({...this.props, slotId: this.getSlotId()});
r = this.props.shouldRefresh({ ...this.props, slotId: this.getSlotId() });
}
return r;
},

render() {
return (
<div className="adunitContainer"> <div id={this.getSlotId()} className="adBox" /> </div>
);
},

});
15 changes: 8 additions & 7 deletions js/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@ export const DFPManager = Object.assign(new EventEmitter(), {
const slotsToRefresh = Object.keys(registeredSlots).map(
(k) => registeredSlots[k]
);
return slotsToRefresh.reduce( (val, slot) => {
const slots = {};
return slotsToRefresh.reduce((last, slot) => {
if (slot.slotShouldRefresh() === true) {
val[slot.slotId] = slot;
slots[slot.slotId] = slot;
}
return val;
}, {});
return slots;
}, slots);
},

refresh() {
if (loadAlreadyCalled === false) {
this.load();
Expand Down Expand Up @@ -153,11 +154,11 @@ export const DFPManager = Object.assign(new EventEmitter(), {
unregisterSlot({ slotId }) {
delete registeredSlots[slotId];
},

getRegisteredSlots() {
return registeredSlots;
},

attachSlotRenderEnded(cb) {
this.on('slotRenderEnded', cb);
},
Expand Down
14 changes: 0 additions & 14 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,3 @@ export function loadGPTScript() {
});
}

let docIsReady = null;
export function onDocumentReady() {
if (docIsReady === null) {
docIsReady = new Promise((resolve) => {
if (document.readyState !== 'loading') {
resolve();
} else {
document.addEventListener('DOMContentLoaded', () => resolve());
}
});
}
return docIsReady;
}

64 changes: 31 additions & 33 deletions lib/adslot.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ var AdSlot = exports.AdSlot = _react2.default.createClass({
fetchNow: false
};
},
getInitialState: function getInitialState() {
return _extends({}, this.props, { slotId: this.generateSlotId() });
},
componentDidMount: function componentDidMount() {
this.registerSlot();
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
if (nextProps.hasOwnProperty('objectId')) {
var state = this.state;
state.slotId = this.generateSlotId();
this.unregisterSlot();
this.setState(state);
this.registerSlot();
}
},
componentWillUnmount: function componentWillUnmount() {
this.unregisterSlot();
},
getSlotId: function getSlotId() {
return this.props.slotId || this.state.slotId;
},
generateSlotId: function generateSlotId() {
var slotId = this.props.slotId;
if (slotId === undefined) {
Expand All @@ -46,11 +67,9 @@ var AdSlot = exports.AdSlot = _react2.default.createClass({
}
return slotId;
},
getSlotId: function getSlotId() {
return this.props.slotId || this.state.slotId;
},
registerSlot: function registerSlot() {
_manager.DFPManager.registerSlot(_extends({}, this.props, this.state, { slotShouldRefresh: this.slotShouldRefresh }));
_manager.DFPManager.registerSlot(_extends({}, this.props, this.state, {
slotShouldRefresh: this.slotShouldRefresh }));
if (this.props.fetchNow === true) {
_manager.DFPManager.load(this.getSlotId());
}
Expand All @@ -60,35 +79,6 @@ var AdSlot = exports.AdSlot = _react2.default.createClass({
_manager.DFPManager.unregisterSlot(_extends({}, this.props, this.state));
_manager.DFPManager.detachSlotRenderEnded(this.slotRenderEnded);
},
getInitialState: function getInitialState() {
return _extends({}, this.props, { slotId: this.generateSlotId() });
},
componentDidMount: function componentDidMount() {
this.registerSlot();
},
render: function render() {
return _jsx('div', {
className: 'adunitContainer'
}, void 0, ' ', _jsx('div', {
id: this.getSlotId(),
className: 'adBox'
}), ' ');
},
componentWillUnmount: function componentWillUnmount() {
this.unregisterSlot();
},


componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
if (nextProps.hasOwnProperty('objectId')) {
var state = this.state;
state.slotId = this.generateSlotId();
this.unregisterSlot();
this.setState(state);
this.registerSlot();
}
},

slotRenderEnded: function slotRenderEnded(eventData) {
if (eventData.slotId === this.getSlotId()) {
if (this.props.onSlotRender !== undefined) {
Expand All @@ -102,5 +92,13 @@ var AdSlot = exports.AdSlot = _react2.default.createClass({
r = this.props.shouldRefresh(_extends({}, this.props, { slotId: this.getSlotId() }));
}
return r;
},
render: function render() {
return _jsx('div', {
className: 'adunitContainer'
}, void 0, ' ', _jsx('div', {
id: this.getSlotId(),
className: 'adBox'
}), ' ');
}
});
9 changes: 5 additions & 4 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ var DFPManager = exports.DFPManager = Object.assign(new _events.EventEmitter(),
var slotsToRefresh = Object.keys(registeredSlots).map(function (k) {
return registeredSlots[k];
});
return slotsToRefresh.reduce(function (val, slot) {
var slots = {};
return slotsToRefresh.reduce(function (last, slot) {
if (slot.slotShouldRefresh() === true) {
val[slot.slotId] = slot;
slots[slot.slotId] = slot;
}
return val;
}, {});
return slots;
}, slots);
},
refresh: function refresh() {
var _this3 = this;
Expand Down
17 changes: 0 additions & 17 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.loadGPTScript = loadGPTScript;
exports.onDocumentReady = onDocumentReady;
function doloadGPTScript(resolve, reject) {
window.googletag = window.googletag || {};
window.googletag.cmd = window.googletag.cmd || [];
Expand All @@ -27,19 +26,3 @@ function loadGPTScript() {
doloadGPTScript(resolve, reject);
});
}

var docIsReady = null;
function onDocumentReady() {
if (docIsReady === null) {
docIsReady = new Promise(function (resolve) {
if (document.readyState !== 'loading') {
resolve();
} else {
document.addEventListener('DOMContentLoaded', function () {
return resolve();
});
}
});
}
return docIsReady;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-dfp",
"version": "0.3.3",
"version": "0.3.4",
"author": {
"name": "Jonatan Alexis Anauati",
"email": "[email protected]"
Expand All @@ -14,6 +14,7 @@
"gpt",
"dfp",
"google dfp",
"google doubleclick for publishers",
"advertising",
"react-component"
],
Expand Down
Loading

0 comments on commit b0fe76a

Please sign in to comment.