UNPKG

4.6 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _react = require('react');
6
7var _react2 = _interopRequireDefault(_react);
8
9var _propTypes = require('prop-types');
10
11var _propTypes2 = _interopRequireDefault(_propTypes);
12
13var _reactProptypeConditionalRequire = require('react-proptype-conditional-require');
14
15var _reactProptypeConditionalRequire2 = _interopRequireDefault(_reactProptypeConditionalRequire);
16
17var _PSSnippet = require('./PSSnippet');
18
19var _PSSnippet2 = _interopRequireDefault(_PSSnippet);
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
24
25function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
26
27function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* global _ps */
28
29
30var PSBrowseWrap = function (_React$Component) {
31 _inherits(PSBrowseWrap, _React$Component);
32
33 function PSBrowseWrap(props) {
34 _classCallCheck(this, PSBrowseWrap);
35
36 var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
37
38 var _this$props = _this.props,
39 psScriptUrl = _this$props.psScriptUrl,
40 groupKey = _this$props.groupKey,
41 accessId = _this$props.accessId;
42
43 if (!_PSSnippet2.default.isSnippetLoaded(psScriptUrl)) {
44 _PSSnippet2.default.injectSnippet(psScriptUrl);
45 }
46 _this.targetSelector = 'psbw-' + groupKey;
47 _ps('create', accessId);
48 return _this;
49 }
50
51 PSBrowseWrap.prototype.componentDidMount = function componentDidMount() {
52 var _props = this.props,
53 groupKey = _props.groupKey,
54 position = _props.position,
55 badgeText = _props.badgeText,
56 alwaysVisible = _props.alwaysVisible,
57 openLegalCenter = _props.openLegalCenter;
58
59 _ps('load', groupKey, {
60 target_selector: this.targetSelector,
61 position: position,
62 badge_text: badgeText,
63 always_visible: alwaysVisible,
64 open_legal_center: openLegalCenter
65 });
66 };
67
68 PSBrowseWrap.prototype.componentWillUnmount = function componentWillUnmount() {
69 var groupKey = this.props.groupKey;
70
71 _ps.getByKey(groupKey).rendered = false;
72 };
73
74 PSBrowseWrap.prototype.isSnippetLoaded = function isSnippetLoaded() {
75 var psScriptUrl = this.props.psScriptUrl;
76
77 var scripts = document.getElementsByTagName('script');
78 for (var i = 0; i < scripts.length; i += 1) {
79 if (scripts[i].src.indexOf(psScriptUrl) !== -1) return true;
80 }
81 return false;
82 };
83
84 PSBrowseWrap.prototype.render = function render() {
85 var _props2 = this.props,
86 link = _props2.link,
87 linkText = _props2.linkText;
88
89 return _react2.default.createElement(
90 'a',
91 { href: link, id: this.targetSelector },
92 linkText
93 );
94 };
95
96 return PSBrowseWrap;
97}(_react2.default.Component);
98
99PSBrowseWrap.MUST_PROVIDE_LINK_IF_OPEN_LEGAL_CENTER_FALSE = 'PSBrowseWrap Error: You must provide a link prop if openLegalCenter is passed false';
100
101PSBrowseWrap.propTypes = process.env.NODE_ENV !== "production" ? {
102 accessId: _propTypes2.default.string.isRequired,
103 alwaysVisible: _propTypes2.default.bool,
104 badgeText: _propTypes2.default.string,
105 groupKey: _propTypes2.default.string.isRequired,
106 link: (0, _reactProptypeConditionalRequire2.default)(_propTypes2.default.string, function (props) {
107 return props.hasOwnProperty('openLegalCenter') && props.openLegalCenter === false;
108 }, PSBrowseWrap.MUST_PROVIDE_LINK_IF_OPEN_LEGAL_CENTER_FALSE),
109 linkText: _propTypes2.default.string.isRequired,
110 openLegalCenter: _propTypes2.default.bool,
111 position: _propTypes2.default.oneOf(['middle', 'left', 'right', 'auto']),
112 psScriptUrl: _propTypes2.default.string
113} : {};
114
115PSBrowseWrap.defaultProps = {
116 psScriptUrl: '//vault.pactsafe.io/ps.min.js',
117 position: 'auto',
118 link: '#',
119 openLegalCenter: true
120};
121
122exports.default = PSBrowseWrap;
123module.exports = exports['default'];
\No newline at end of file