1 | "use strict";
|
2 |
|
3 | require("core-js/modules/es.symbol");
|
4 |
|
5 | require("core-js/modules/es.symbol.description");
|
6 |
|
7 | require("core-js/modules/es.symbol.iterator");
|
8 |
|
9 | require("core-js/modules/es.array.iterator");
|
10 |
|
11 | require("core-js/modules/es.function.name");
|
12 |
|
13 | require("core-js/modules/es.object.assign");
|
14 |
|
15 | require("core-js/modules/es.object.create");
|
16 |
|
17 | require("core-js/modules/es.object.define-property");
|
18 |
|
19 | require("core-js/modules/es.object.get-own-property-descriptor");
|
20 |
|
21 | require("core-js/modules/es.object.get-prototype-of");
|
22 |
|
23 | require("core-js/modules/es.object.set-prototype-of");
|
24 |
|
25 | require("core-js/modules/es.object.to-string");
|
26 |
|
27 | require("core-js/modules/es.string.iterator");
|
28 |
|
29 | require("core-js/modules/web.dom-collections.iterator");
|
30 |
|
31 | Object.defineProperty(exports, "__esModule", {
|
32 | value: true
|
33 | });
|
34 | exports["default"] = withTrackingComponentDecorator;
|
35 | exports.ReactTrackingContext = exports.TrackingContextType = void 0;
|
36 |
|
37 | var _react = _interopRequireWildcard(require("react"));
|
38 |
|
39 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
40 |
|
41 | var _deepmerge = _interopRequireDefault(require("deepmerge"));
|
42 |
|
43 | var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
|
44 |
|
45 | var _dispatchTrackingEvent = _interopRequireDefault(require("./dispatchTrackingEvent"));
|
46 |
|
47 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
48 |
|
49 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
|
50 |
|
51 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
52 |
|
53 | function _extends() { _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; }; return _extends.apply(this, arguments); }
|
54 |
|
55 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
56 |
|
57 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
58 |
|
59 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
60 |
|
61 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
62 |
|
63 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
64 |
|
65 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
66 |
|
67 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
68 |
|
69 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
70 |
|
71 | var TrackingContextType = _propTypes["default"].shape({
|
72 | data: _propTypes["default"].object,
|
73 | dispatch: _propTypes["default"].func,
|
74 | process: _propTypes["default"].func
|
75 | });
|
76 |
|
77 | exports.TrackingContextType = TrackingContextType;
|
78 |
|
79 | var ReactTrackingContext = _react["default"].createContext({});
|
80 |
|
81 | exports.ReactTrackingContext = ReactTrackingContext;
|
82 |
|
83 | function withTrackingComponentDecorator() {
|
84 | var trackingData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
85 |
|
86 | var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
87 | _ref$dispatch = _ref.dispatch,
|
88 | dispatch = _ref$dispatch === void 0 ? _dispatchTrackingEvent["default"] : _ref$dispatch,
|
89 | _ref$dispatchOnMount = _ref.dispatchOnMount,
|
90 | dispatchOnMount = _ref$dispatchOnMount === void 0 ? false : _ref$dispatchOnMount,
|
91 | process = _ref.process;
|
92 |
|
93 | return function (DecoratedComponent) {
|
94 | var decoratedComponentName = DecoratedComponent.displayName || DecoratedComponent.name || 'Component';
|
95 |
|
96 | var WithTracking =
|
97 |
|
98 | function (_Component) {
|
99 | _inherits(WithTracking, _Component);
|
100 |
|
101 | function WithTracking(props, context) {
|
102 | var _this;
|
103 |
|
104 | _classCallCheck(this, WithTracking);
|
105 |
|
106 | _this = _possibleConstructorReturn(this, _getPrototypeOf(WithTracking).call(this, props, context));
|
107 |
|
108 | _this.getProcessFn = function () {
|
109 | var tracking = _this.context.tracking;
|
110 | return tracking && tracking.process;
|
111 | };
|
112 |
|
113 | _this.getOwnTrackingData = function () {
|
114 | var ownTrackingData = typeof trackingData === 'function' ? trackingData(_this.props) : trackingData;
|
115 | return ownTrackingData || {};
|
116 | };
|
117 |
|
118 | _this.getTrackingDataFn = function () {
|
119 | var tracking = _this.context.tracking;
|
120 | var contextGetTrackingData = tracking && tracking.getTrackingData || _this.getOwnTrackingData;
|
121 | return function () {
|
122 | return contextGetTrackingData === _this.getOwnTrackingData ? _this.getOwnTrackingData() : (0, _deepmerge["default"])(contextGetTrackingData(), _this.getOwnTrackingData());
|
123 | };
|
124 | };
|
125 |
|
126 | _this.getTrackingDispatcher = function () {
|
127 | var tracking = _this.context.tracking;
|
128 | var contextDispatch = tracking && tracking.dispatch || dispatch;
|
129 | return function (data) {
|
130 | return contextDispatch((0, _deepmerge["default"])(_this.getOwnTrackingData(), data || {}));
|
131 | };
|
132 | };
|
133 |
|
134 | _this.trackEvent = function () {
|
135 | var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
136 |
|
137 | _this.contextValueForProvider.tracking.dispatch(data);
|
138 | };
|
139 |
|
140 | if (_this.getProcessFn() && process) {
|
141 |
|
142 | console.error('[react-tracking] options.process should be defined once on a top-level component');
|
143 | }
|
144 |
|
145 | _this.tracking = {
|
146 | trackEvent: _this.trackEvent,
|
147 | getTrackingData: _this.getTrackingDataFn()
|
148 | };
|
149 | _this.contextValueForProvider = {
|
150 | tracking: {
|
151 | dispatch: _this.getTrackingDispatcher(),
|
152 | getTrackingData: _this.getTrackingDataFn(),
|
153 | process: _this.getProcessFn() || process
|
154 | }
|
155 | };
|
156 | return _this;
|
157 | }
|
158 |
|
159 | _createClass(WithTracking, [{
|
160 | key: "componentDidMount",
|
161 | value: function componentDidMount() {
|
162 | var contextProcess = this.getProcessFn();
|
163 |
|
164 | if (typeof contextProcess === 'function' && typeof dispatchOnMount === 'function') {
|
165 | this.trackEvent((0, _deepmerge["default"])(contextProcess(this.getOwnTrackingData()) || {}, dispatchOnMount(this.tracking.getTrackingData()) || {}));
|
166 | } else if (typeof contextProcess === 'function') {
|
167 | var processed = contextProcess(this.getOwnTrackingData());
|
168 |
|
169 | if (processed || dispatchOnMount === true) {
|
170 | this.trackEvent(processed);
|
171 | }
|
172 | } else if (typeof dispatchOnMount === 'function') {
|
173 | this.trackEvent(dispatchOnMount(this.tracking.getTrackingData()));
|
174 | } else if (dispatchOnMount === true) {
|
175 | this.trackEvent();
|
176 | }
|
177 | }
|
178 | }, {
|
179 | key: "render",
|
180 | value: function render() {
|
181 | return _react["default"].createElement(ReactTrackingContext.Provider, {
|
182 | value: this.contextValueForProvider
|
183 | }, _react["default"].createElement(DecoratedComponent, _extends({}, this.props, {
|
184 | tracking: this.tracking
|
185 | })));
|
186 | }
|
187 | }]);
|
188 |
|
189 | return WithTracking;
|
190 | }(_react.Component);
|
191 |
|
192 | WithTracking.displayName = "WithTracking(".concat(decoratedComponentName, ")");
|
193 | WithTracking.contextType = ReactTrackingContext;
|
194 | (0, _hoistNonReactStatics["default"])(WithTracking, DecoratedComponent);
|
195 | return WithTracking;
|
196 | };
|
197 | } |
\ | No newline at end of file |