UNPKG

12.3 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
6
7var _react = require('react');
8
9var _react2 = _interopRequireDefault(_react);
10
11var _propTypes = require('prop-types');
12
13var _propTypes2 = _interopRequireDefault(_propTypes);
14
15var _reactProptypeConditionalRequire = require('react-proptype-conditional-require');
16
17var _reactProptypeConditionalRequire2 = _interopRequireDefault(_reactProptypeConditionalRequire);
18
19var _v = require('uuid/v4');
20
21var _v2 = _interopRequireDefault(_v);
22
23var _PSSnippet = require('./PSSnippet');
24
25var _PSSnippet2 = _interopRequireDefault(_PSSnippet);
26
27function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
29function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
30
31function _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; }
32
33function _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 */
34
35
36var PSClickWrap = function (_React$Component) {
37 _inherits(PSClickWrap, _React$Component);
38
39 function PSClickWrap(props) {
40 _classCallCheck(this, PSClickWrap);
41
42 var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
43
44 _this.createClickWrap = _this.createClickWrap.bind(_this);
45 _this.state = {
46 clickwrapGroupKey: null,
47 dynamicGroup: false
48 };
49 _this.propsEventMap = {
50 onAll: 'all',
51 onSent: 'sent',
52 onRetrieved: 'retrieved',
53 onSet: 'set',
54 onSetSignerId: 'set:signer_id',
55 onValid: 'valid',
56 onInvalid: 'invalid',
57 onRendered: 'rendered',
58 onDisplayed: 'displayed',
59 onScrolledContract: 'scrolled:contract',
60 onScrolled: 'scrolled',
61 onError: 'error'
62 };
63 var _this$props = _this.props,
64 psScriptUrl = _this$props.psScriptUrl,
65 backupScriptURL = _this$props.backupScriptURL,
66 accessId = _this$props.accessId,
67 testMode = _this$props.testMode,
68 disableSending = _this$props.disableSending,
69 dynamic = _this$props.dynamic,
70 signerId = _this$props.signerId,
71 debug = _this$props.debug;
72
73 if (!_PSSnippet2.default.isSnippetLoaded(psScriptUrl, backupScriptURL)) {
74 _PSSnippet2.default.injectSnippet(psScriptUrl, backupScriptURL);
75 }
76 _ps('create', accessId, {
77 test_mode: testMode,
78 disable_sending: disableSending,
79 dynamic: dynamic,
80 signer_id: signerId
81 });
82 if (debug) {
83 _ps.debug = true;
84 }
85 return _this;
86 }
87
88 PSClickWrap.prototype.componentDidMount = function componentDidMount() {
89 this.createClickWrap();
90 };
91
92 PSClickWrap.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
93 var _props = this.props,
94 clickWrapStyle = _props.clickWrapStyle,
95 renderData = _props.renderData,
96 filter = _props.filter,
97 groupKey = _props.groupKey;
98 var _state = this.state,
99 clickwrapGroupKey = _state.clickwrapGroupKey,
100 dynamicGroup = _state.dynamicGroup;
101
102 if (clickWrapStyle !== prevProps.clickWrapStyle && !dynamicGroup) {
103 _ps.getByKey(clickwrapGroupKey).site.set('style', clickWrapStyle);
104 _ps.getByKey(clickwrapGroupKey).retrieveHTML();
105 }
106 if (renderData !== prevProps.renderData) {
107 _ps(clickwrapGroupKey + ':retrieveHTML', renderData);
108 }
109 if (clickWrapStyle !== prevProps.clickWrapStyle && dynamicGroup) {
110 this.createClickWrap();
111 }
112 if (filter !== prevProps.filter && dynamicGroup) {
113 this.createClickWrap();
114 }
115 if (groupKey !== prevProps.groupKey && !dynamicGroup) {
116 this.createClickWrap();
117 _ps.getByKey(clickwrapGroupKey).retrieveHTML();
118 }
119 };
120
121 PSClickWrap.prototype.componentWillUnmount = function componentWillUnmount() {
122 var groupKey = this.props.groupKey;
123
124 if (_ps && _ps.getByKey(groupKey) && _ps.getByKey(groupKey).rendered) {
125 _ps.getByKey(groupKey).rendered = false;
126 }
127 this.unregisterEventListeners();
128 };
129
130 PSClickWrap.prototype.registerEventListener = function registerEventListener(eventProp, groupKey) {
131 var _this2 = this;
132
133 var eventCallbackFn = function eventCallbackFn() {
134 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
135 args[_key] = arguments[_key];
136 }
137
138 var shouldFireListener = false;
139 args.forEach(function (arg) {
140 // We need to check the context variable and see if it matches the groupKey, if it does -> fire the event (context argument position varies)
141 if (arg.get && arg.get('key') && arg.get('key') === groupKey) {
142 shouldFireListener = true;
143 // Else we should check if the context is for the entire site, and as such the context variable is a Site object.
144 } else if (arg.toString() === '[object Site]') {
145 shouldFireListener = true;
146 }
147 });
148 if (shouldFireListener) {
149 var _props2;
150
151 (_props2 = _this2.props)[eventProp].apply(_props2, args);
152 }
153 };
154 // In order to handle unregistration of event listeners, we override the toString function to identify the
155 // function by a UUID rather than the default toString of a function.
156 var newEventListenerID = (0, _v2.default)();
157 eventCallbackFn.toString = function () {
158 return newEventListenerID;
159 };
160 _ps.on(this.propsEventMap[eventProp], eventCallbackFn);
161 return eventCallbackFn.toString();
162 };
163
164 PSClickWrap.prototype.registerEventListeners = function registerEventListeners(groupKey) {
165 var _this3 = this;
166
167 var eventListeners = {};
168 Object.keys(this.propsEventMap).forEach(function (eventProp) {
169 if (_this3.props[eventProp]) {
170 eventListeners[_this3.propsEventMap[eventProp]] = _this3.registerEventListener(eventProp, groupKey);
171 }
172 });
173 // Store event listeners in state so we can unregister them later on unmount
174 this.setState({ eventListeners: eventListeners });
175 };
176
177 PSClickWrap.prototype.unregisterEventListeners = function unregisterEventListeners() {
178 var _this4 = this;
179
180 Object.keys(this.state.eventListeners).forEach(function (event) {
181 var eventUUID = _this4.state.eventListeners[event];
182 // In order to unregister the event, we must create a fake function (typeof passed to _ps.off must be a function),
183 // that returns the UUID we want to unregister.
184 var fakeEventListener = function fakeEventListener() {
185 return eventUUID;
186 };
187 fakeEventListener.toString = function () {
188 return eventUUID;
189 };
190 _ps.off(event, fakeEventListener);
191 });
192 };
193
194 PSClickWrap.prototype.createClickWrap = function createClickWrap() {
195 var _this5 = this;
196
197 var _props3 = this.props,
198 filter = _props3.filter,
199 containerId = _props3.containerId,
200 signerIdSelector = _props3.signerIdSelector,
201 clickWrapStyle = _props3.clickWrapStyle,
202 displayAll = _props3.displayAll,
203 renderData = _props3.renderData,
204 displayImmediately = _props3.displayImmediately,
205 forceScroll = _props3.forceScroll,
206 groupKey = _props3.groupKey,
207 confirmationEmail = _props3.confirmationEmail;
208
209 var options = { filter: filter, container_selector: containerId, confirmation_email: confirmationEmail, signer_id_selector: signerIdSelector, style: clickWrapStyle, display_all: displayAll, render_data: renderData, auto_run: displayImmediately, force_scroll: forceScroll };
210
211 if (groupKey) this.setState({ clickwrapGroupKey: groupKey, dynamicGroup: false });
212 var isDynamic = !groupKey;
213
214 var eventCallback = function eventCallback(err, group) {
215 if (group) {
216 var key = groupKey || group.get('key');
217
218 var state = { clickwrapGroupKey: key };
219 if (isDynamic) state.dynamicGroup = true;
220 _this5.setState(state);
221
222 if (!isDynamic) group.render();
223 _this5.registerEventListeners(key);
224 }
225 };
226
227 if (groupKey) _ps('load', groupKey, _extends({}, options, { event_callback: eventCallback }));else _ps('load', _extends({}, options, { event_callback: eventCallback }));
228 };
229
230 PSClickWrap.prototype.render = function render() {
231 var containerId = this.props.containerId;
232
233 return _react2.default.createElement('div', { id: containerId });
234 };
235
236 return PSClickWrap;
237}(_react2.default.Component);
238
239PSClickWrap.FILTER_OR_GROUPKEY_REQUIRED_ERROR_MESSAGE = 'PSClickWrap Error: You must provide either a groupKey or filter prop in order to use the PactSafe ClickWrap component!';
240PSClickWrap.MUST_PROVIDE_RENDER_DATA_ERROR_MESSAGE = 'PSClickWrap Error: You must provide a renderData prop when passing down the dynamic prop';
241PSClickWrap.MUST_PROVIDE_SIGNER_ID_OR_SIGNER_ID_SELECTOR = 'PSClickWrap Error: You must provide either a signer ID or a signer ID selector';
242
243PSClickWrap.propTypes = process.env.NODE_ENV !== "production" ? {
244 accessId: _propTypes2.default.string.isRequired,
245 clickWrapStyle: _propTypes2.default.oneOf(['full', 'scroll', 'checkbox', 'combined', 'embedded']),
246 confirmationEmail: _propTypes2.default.bool,
247 disableSending: _propTypes2.default.bool,
248 displayAll: _propTypes2.default.bool,
249 displayImmediately: _propTypes2.default.bool,
250 dynamic: _propTypes2.default.bool,
251 containerId: _propTypes2.default.string,
252 filter: (0, _reactProptypeConditionalRequire2.default)(_propTypes2.default.string, function (props) {
253 return !props.hasOwnProperty('groupKey');
254 }, PSClickWrap.FILTER_OR_GROUPKEY_REQUIRED_ERROR_MESSAGE),
255 forceScroll: _propTypes2.default.bool,
256 groupKey: (0, _reactProptypeConditionalRequire2.default)(_propTypes2.default.string, function (props) {
257 return !props.hasOwnProperty('filter');
258 }, PSClickWrap.FILTER_OR_GROUPKEY_REQUIRED_ERROR_MESSAGE),
259 psScriptUrl: _propTypes2.default.string,
260 backupScriptURL: _propTypes2.default.string,
261 renderData: (0, _reactProptypeConditionalRequire2.default)(_propTypes2.default.object, function (props) {
262 return props.hasOwnProperty('dynamic') && props.dynamic === true;
263 }, PSClickWrap.MUST_PROVIDE_RENDER_DATA_ERROR_MESSAGE),
264 signerIdSelector: (0, _reactProptypeConditionalRequire2.default)(_propTypes2.default.string, function (props) {
265 return !props.hasOwnProperty('signerId');
266 }, PSClickWrap.MUST_PROVIDE_SIGNER_ID_OR_SIGNER_ID_SELECTOR),
267 signerId: (0, _reactProptypeConditionalRequire2.default)(_propTypes2.default.string, function (props) {
268 return !props.hasOwnProperty('signerIdSelector');
269 }, PSClickWrap.MUST_PROVIDE_SIGNER_ID_OR_SIGNER_ID_SELECTOR),
270 testMode: _propTypes2.default.bool,
271 debug: _propTypes2.default.bool,
272 onAll: _propTypes2.default.func,
273 onSent: _propTypes2.default.func,
274 onRetrieved: _propTypes2.default.func,
275 onSet: _propTypes2.default.func,
276 onSetSignerId: _propTypes2.default.func,
277 onValid: _propTypes2.default.func,
278 onInvalid: _propTypes2.default.func,
279 onRendered: _propTypes2.default.func,
280 onDisplayed: _propTypes2.default.func,
281 onScrolledContract: _propTypes2.default.func,
282 onScrolled: _propTypes2.default.func,
283 onError: _propTypes2.default.func
284} : {};
285
286PSClickWrap.defaultProps = {
287 psScriptUrl: '//vault.pactsafe.io/ps.min.js',
288 backupScriptURL: '//d3l1mqnl5xpsuc.cloudfront.net/ps.min.js',
289 containerId: 'ps-clickwrap',
290 displayImmediately: true,
291 disableSending: false,
292 displayAll: true,
293 dynamic: false,
294 testMode: false
295};
296
297exports.default = PSClickWrap;
298module.exports = exports['default'];
\No newline at end of file