UNPKG

react-prebid-adunit

Version:

React Component to wrap AdUnit elements using a Prebid.js container

1 lines 3.57 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _extends=Object.assign||function(a){for(var b,c=1;c<arguments.length;c++)for(var d in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,d)&&(a[d]=b[d]);return a},_createClass=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),_prebid=require("./_prebid"),_prebid2=_interopRequireDefault(_prebid),_propTypes=require("prop-types"),_propTypes2=_interopRequireDefault(_propTypes);function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _possibleConstructorReturn(a,b){if(!a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return b&&("object"==typeof b||"function"==typeof b)?b:a}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function, not "+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)}var propTypes={slot:_propTypes2.default.string.isRequired,adNetwork:_propTypes2.default.string.isRequired,targeting:_propTypes2.default.object,afterAdLoaded:_propTypes2.default.func,flexible:_propTypes2.default.bool},defaultProps={targeting:{},afterAdLoaded:function(){}},DFPPrebidContainer=function(a){function b(a){_classCallCheck(this,b);var c=_possibleConstructorReturn(this,(b.__proto__||Object.getPrototypeOf(b)).call(this,a));return c.slotRendered=c.slotRendered.bind(c),c}return _inherits(b,a),_createClass(b,null,[{key:"propTypes",get:function(){return _extends({},_prebid2.default.propTypes,propTypes)}},{key:"defaultProps",get:function(){return _extends({},_prebid2.default.defaultProps,defaultProps)}}]),_createClass(b,[{key:"adServerInit",value:function(){window.googletag||(window.googletag={cmd:[function(){return window.googletag.pubads().disableInitialLoad()}]})}},{key:"adServerType",value:function(){return"dfp"}},{key:"adServerURL",value:function(){return"https://www.googletagservices.com/tag/js/gpt.js"}},{key:"adServerRequest",value:function(){var a=this,b=window,c=b.googletag;c.cmd.push(function(){a.afterAdServerRequest().then(function(){c.pubads().refresh()})})}},{key:"adServerSlot",value:function(a,b){var c=this,d=window,e=d.googletag,f=this.props,g=f.adNetwork,h=f.slot,i=f.dimensions,j=f.targeting,k=f.domID;e.cmd.push(function(){var d=e.pubads(),f=e.defineSlot("/"+g+"/"+h,i,k);Object.keys(j).forEach(function(a){return f.setTargeting(a,j[a])}),f.addService(d),d.addEventListener("slotRenderEnded",c.slotRendered),a&&(d.enableSingleRequest(),e.enableServices(),b())})}},{key:"slotRendered",value:function(a){var b=this.props,c=b.flexible,d=b.domID,e=document.getElementById(d).querySelector("iframe"),f=a.size||[];if(a.slot.getSlotId().getDomId()===d&&("1x1"===f.join("x")&&(f=[e.width,e.height]),this.props.afterAdLoaded(!a.isEmpty,f),c&&!a.isEmpty)){var g=e.parentNode,h=e.contentDocument;g.style.width="100%",g.style.height="auto",e.width="100%";var i=function(){e.height=h.body.offsetHeight},j=[].slice.call(h.querySelectorAll("img"),0);j.forEach(function(a){return a.addEventListener("load",i)}),i()}}}]),b}(_prebid2.default);exports.default=DFPPrebidContainer,module.exports=exports["default"];