@merkur/integration-react 0.29.3 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +46 -125
- package/lib/index.es5.js +1 -1
- package/lib/index.es9.cjs +46 -143
- package/lib/index.es9.mjs +46 -138
- package/lib/index.js +46 -125
- package/lib/index.mjs +46 -125
- package/package.json +6 -6
package/lib/index.es9.mjs
CHANGED
|
@@ -1,29 +1,25 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { getMerkur } from '@merkur/core';
|
|
3
3
|
import { loadStyleAssets, loadScriptAssets } from '@merkur/integration';
|
|
4
|
-
|
|
5
4
|
function _extends() {
|
|
6
|
-
_extends = Object.assign
|
|
5
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
7
6
|
for (var i = 1; i < arguments.length; i++) {
|
|
8
7
|
var source = arguments[i];
|
|
9
|
-
|
|
10
8
|
for (var key in source) {
|
|
11
9
|
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
12
10
|
target[key] = source[key];
|
|
13
11
|
}
|
|
14
12
|
}
|
|
15
13
|
}
|
|
16
|
-
|
|
17
14
|
return target;
|
|
18
15
|
};
|
|
19
|
-
|
|
20
16
|
return _extends.apply(this, arguments);
|
|
21
17
|
}
|
|
22
|
-
|
|
23
18
|
const SelectorIdentifierMap = Object.freeze({
|
|
24
19
|
'#': 'id',
|
|
25
20
|
'.': 'className'
|
|
26
21
|
});
|
|
22
|
+
|
|
27
23
|
/**
|
|
28
24
|
* Parses container selector into element property. Currently
|
|
29
25
|
* only works with IDs and classnames.
|
|
@@ -31,28 +27,24 @@ const SelectorIdentifierMap = Object.freeze({
|
|
|
31
27
|
* @param {string} [containerSelector='']
|
|
32
28
|
* @return {{ string: string }} Property name and value tuple.
|
|
33
29
|
*/
|
|
34
|
-
|
|
35
30
|
function selectorToAttribute(containerSelector = '') {
|
|
36
31
|
if (typeof containerSelector !== 'string' || containerSelector.length === 0) {
|
|
37
32
|
return {};
|
|
38
33
|
}
|
|
39
|
-
|
|
40
34
|
let lastIdentifierIndex = -1;
|
|
41
|
-
|
|
42
35
|
for (let identifierKey in SelectorIdentifierMap) {
|
|
43
36
|
let curLastIndex = containerSelector.lastIndexOf(identifierKey);
|
|
44
|
-
|
|
45
37
|
if (lastIdentifierIndex < curLastIndex) {
|
|
46
38
|
lastIdentifierIndex = curLastIndex;
|
|
47
39
|
}
|
|
48
40
|
}
|
|
49
|
-
|
|
50
41
|
const identifier = containerSelector[lastIdentifierIndex];
|
|
51
42
|
const selectorName = containerSelector.substr(lastIdentifierIndex + 1);
|
|
52
43
|
return {
|
|
53
44
|
[SelectorIdentifierMap[identifier]]: selectorName
|
|
54
45
|
};
|
|
55
46
|
}
|
|
47
|
+
|
|
56
48
|
/**
|
|
57
49
|
* Returns empty <div> with SSR html as it's contents (if provided) and
|
|
58
50
|
* container selector defined, which is used as merkur widget wrapper.
|
|
@@ -60,8 +52,6 @@ function selectorToAttribute(containerSelector = '') {
|
|
|
60
52
|
* @param {{ html, containerSelector, children }}
|
|
61
53
|
* @return {import('react').ReactElement}
|
|
62
54
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
55
|
function WidgetWrapper({
|
|
66
56
|
html,
|
|
67
57
|
containerSelector,
|
|
@@ -74,9 +64,7 @@ function WidgetWrapper({
|
|
|
74
64
|
}
|
|
75
65
|
})) : /*#__PURE__*/React.createElement("div", selector, children);
|
|
76
66
|
}
|
|
77
|
-
|
|
78
67
|
var WidgetWrapper$1 = /*#__PURE__*/React.memo(WidgetWrapper);
|
|
79
|
-
|
|
80
68
|
class AbstractMerkurWidget extends React.Component {
|
|
81
69
|
/**
|
|
82
70
|
* Checks if widget has changed, e.g has different name or version.
|
|
@@ -88,51 +76,45 @@ class AbstractMerkurWidget extends React.Component {
|
|
|
88
76
|
static hasWidgetChanged(props, nextProps) {
|
|
89
77
|
return !!(props && props.name && props.version && nextProps && nextProps.name && nextProps.version && (props.version !== nextProps.version || props.name !== nextProps.name));
|
|
90
78
|
}
|
|
79
|
+
|
|
91
80
|
/**
|
|
92
81
|
* Validates widget properties, widget properties should not be an empty object.
|
|
93
82
|
*
|
|
94
83
|
* @param {object} widgetProperties
|
|
95
84
|
* @return {boolean} validation result.
|
|
96
85
|
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
86
|
static validateProperties(widgetProperties) {
|
|
100
87
|
return !!(widgetProperties && widgetProperties.name && widgetProperties.version);
|
|
101
88
|
}
|
|
89
|
+
|
|
102
90
|
/**
|
|
103
91
|
* Widget SSR rendered html (from server response).
|
|
104
92
|
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
93
|
get html() {
|
|
108
94
|
throw new Error('The html getter is abstract and must be overridden');
|
|
109
95
|
}
|
|
96
|
+
|
|
110
97
|
/**
|
|
111
98
|
* Widget container element.
|
|
112
99
|
*/
|
|
113
|
-
|
|
114
|
-
|
|
115
100
|
get container() {
|
|
116
101
|
throw new Error('The container getter is abstract and must be overridden');
|
|
117
102
|
}
|
|
118
|
-
|
|
119
103
|
constructor(props) {
|
|
120
104
|
super(props);
|
|
121
105
|
this._isMounted = false;
|
|
122
106
|
this._html = null;
|
|
123
107
|
}
|
|
124
|
-
|
|
125
108
|
componentDidMount() {
|
|
126
109
|
this._isMounted = true;
|
|
127
110
|
}
|
|
111
|
+
|
|
128
112
|
/**
|
|
129
113
|
* Renders fallback (children) when widget is already mounted
|
|
130
114
|
* but not ready or any error has occurred.
|
|
131
115
|
*
|
|
132
116
|
* @return {React.ReactElement|null}
|
|
133
117
|
*/
|
|
134
|
-
|
|
135
|
-
|
|
136
118
|
_renderFallback() {
|
|
137
119
|
const {
|
|
138
120
|
children
|
|
@@ -140,7 +122,6 @@ class AbstractMerkurWidget extends React.Component {
|
|
|
140
122
|
const {
|
|
141
123
|
encounteredError = null
|
|
142
124
|
} = this.state || {};
|
|
143
|
-
|
|
144
125
|
if (typeof children === 'function') {
|
|
145
126
|
return children({
|
|
146
127
|
error: encounteredError
|
|
@@ -148,90 +129,76 @@ class AbstractMerkurWidget extends React.Component {
|
|
|
148
129
|
} else if ( /*#__PURE__*/React.isValidElement(children)) {
|
|
149
130
|
return children;
|
|
150
131
|
}
|
|
151
|
-
|
|
152
132
|
return null;
|
|
153
133
|
}
|
|
134
|
+
|
|
154
135
|
/**
|
|
155
136
|
* Clears cached SSR rendered html.
|
|
156
137
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
159
138
|
_clearCachedHtml() {
|
|
160
139
|
this._html = null;
|
|
161
140
|
}
|
|
141
|
+
|
|
162
142
|
/**
|
|
163
143
|
* @return {boolean} true in browser environment.
|
|
164
144
|
*/
|
|
165
|
-
|
|
166
|
-
|
|
167
145
|
_isClient() {
|
|
168
146
|
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
169
147
|
}
|
|
148
|
+
|
|
170
149
|
/**
|
|
171
150
|
* @return {string} SSR rendered HTML, html from widgetProperties or ''.
|
|
172
151
|
*/
|
|
173
|
-
|
|
174
|
-
|
|
175
152
|
_getWidgetHTML() {
|
|
176
153
|
if (this._html !== null) {
|
|
177
154
|
return this._html;
|
|
178
155
|
}
|
|
179
|
-
|
|
180
156
|
this._html = this.html || this._getSSRHTML();
|
|
181
157
|
return this._html;
|
|
182
158
|
}
|
|
159
|
+
|
|
183
160
|
/**
|
|
184
161
|
* Return server-side rendered html, if its the first render on client
|
|
185
162
|
* after SSR.
|
|
186
163
|
*
|
|
187
164
|
* @return {string} server-side rendered html, if it's not available, return empty string.
|
|
188
165
|
*/
|
|
189
|
-
|
|
190
|
-
|
|
191
166
|
_getSSRHTML() {
|
|
192
167
|
if (!this._isMounted && this._isClient()) {
|
|
193
168
|
var _this$container, _this$container$child, _this$container$child2;
|
|
194
|
-
|
|
195
169
|
return ((_this$container = this.container) === null || _this$container === void 0 ? void 0 : (_this$container$child = _this$container.children) === null || _this$container$child === void 0 ? void 0 : (_this$container$child2 = _this$container$child[0]) === null || _this$container$child2 === void 0 ? void 0 : _this$container$child2.outerHTML) || '';
|
|
196
170
|
}
|
|
197
|
-
|
|
198
171
|
return '';
|
|
199
172
|
}
|
|
173
|
+
|
|
200
174
|
/**
|
|
201
175
|
* Checks if it's the first render after SSR.
|
|
202
176
|
*
|
|
203
177
|
* @return {boolean} true in case of a first render after SSR, otherwise false.
|
|
204
178
|
*/
|
|
205
|
-
|
|
206
|
-
|
|
207
179
|
_isSSRHydrate() {
|
|
208
180
|
return this._getSSRHTML().length > 0;
|
|
209
181
|
}
|
|
210
|
-
|
|
211
182
|
}
|
|
212
183
|
|
|
184
|
+
// error event name from @merkur/plugin-error
|
|
213
185
|
const MERKUR_ERROR_EVENT_NAME = '@merkur/plugin-error.error';
|
|
214
|
-
|
|
215
186
|
class MerkurWidget extends AbstractMerkurWidget {
|
|
216
187
|
/**
|
|
217
188
|
* @inheritdoc
|
|
218
189
|
*/
|
|
219
190
|
get html() {
|
|
220
191
|
var _this$props$widgetPro;
|
|
221
|
-
|
|
222
192
|
return ((_this$props$widgetPro = this.props.widgetProperties) === null || _this$props$widgetPro === void 0 ? void 0 : _this$props$widgetPro.html) || null;
|
|
223
193
|
}
|
|
194
|
+
|
|
224
195
|
/**
|
|
225
196
|
* @inheritdoc
|
|
226
197
|
*/
|
|
227
|
-
|
|
228
|
-
|
|
229
198
|
get container() {
|
|
230
199
|
var _document, _this$props$widgetPro2;
|
|
231
|
-
|
|
232
200
|
return this._isClient() && ((_document = document) === null || _document === void 0 ? void 0 : _document.querySelector((_this$props$widgetPro2 = this.props.widgetProperties) === null || _this$props$widgetPro2 === void 0 ? void 0 : _this$props$widgetPro2.containerSelector)) || null;
|
|
233
201
|
}
|
|
234
|
-
|
|
235
202
|
constructor(props) {
|
|
236
203
|
super(props);
|
|
237
204
|
this._widget = null;
|
|
@@ -242,6 +209,7 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
242
209
|
cachedWidgetMeta: null
|
|
243
210
|
};
|
|
244
211
|
}
|
|
212
|
+
|
|
245
213
|
/**
|
|
246
214
|
* In case widget props change to new widget, we need to reset
|
|
247
215
|
* state before next render. This enables us to immediately render
|
|
@@ -250,15 +218,14 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
250
218
|
* @param {object} nextProps
|
|
251
219
|
* @param {object} prevState
|
|
252
220
|
*/
|
|
253
|
-
|
|
254
|
-
|
|
255
221
|
static getDerivedStateFromProps(nextProps, prevState) {
|
|
256
222
|
if (prevState && nextProps && AbstractMerkurWidget.validateProperties(nextProps.widgetProperties)) {
|
|
257
223
|
const {
|
|
258
224
|
version,
|
|
259
225
|
name
|
|
260
|
-
} = nextProps.widgetProperties;
|
|
226
|
+
} = nextProps.widgetProperties;
|
|
261
227
|
|
|
228
|
+
// Cache widget meta data (name & version)
|
|
262
229
|
if (!prevState.cachedWidgetMeta) {
|
|
263
230
|
return {
|
|
264
231
|
cachedWidgetMeta: {
|
|
@@ -266,9 +233,9 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
266
233
|
version
|
|
267
234
|
}
|
|
268
235
|
};
|
|
269
|
-
}
|
|
270
|
-
|
|
236
|
+
}
|
|
271
237
|
|
|
238
|
+
// Replace cached widget meta data with new ones and reset state
|
|
272
239
|
if (AbstractMerkurWidget.hasWidgetChanged(prevState.cachedWidgetMeta, nextProps.widgetProperties)) {
|
|
273
240
|
return {
|
|
274
241
|
encounteredError: false,
|
|
@@ -280,9 +247,9 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
280
247
|
};
|
|
281
248
|
}
|
|
282
249
|
}
|
|
283
|
-
|
|
284
250
|
return null;
|
|
285
251
|
}
|
|
252
|
+
|
|
286
253
|
/**
|
|
287
254
|
* Component should be updated only in these cases:
|
|
288
255
|
* 1) State of MerkurWidget has changed (excluding { @code this.state.cachedWidgetMeta }).
|
|
@@ -296,13 +263,10 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
296
263
|
* @param {object} nextProps
|
|
297
264
|
* @param {object} nextState
|
|
298
265
|
*/
|
|
299
|
-
|
|
300
|
-
|
|
301
266
|
shouldComponentUpdate(nextProps, nextState) {
|
|
302
267
|
if (this.state.assetsLoaded !== nextState.assetsLoaded || this.state.encounteredError !== nextState.encounteredError || !AbstractMerkurWidget.validateProperties(this.props.widgetProperties) || AbstractMerkurWidget.hasWidgetChanged(this.props.widgetProperties, nextProps.widgetProperties)) {
|
|
303
268
|
return true;
|
|
304
269
|
}
|
|
305
|
-
|
|
306
270
|
if (this._widget && nextProps.widgetProperties && nextProps.widgetProperties.props) {
|
|
307
271
|
if (Object.keys(nextProps.widgetProperties.props).length !== Object.keys(this.props.widgetProperties.props).length) {
|
|
308
272
|
/**
|
|
@@ -311,7 +275,6 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
311
275
|
* comparison below would not work.
|
|
312
276
|
*/
|
|
313
277
|
this._widget.setProps(nextProps.widgetProperties.props);
|
|
314
|
-
|
|
315
278
|
return false;
|
|
316
279
|
} else {
|
|
317
280
|
/**
|
|
@@ -321,43 +284,37 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
321
284
|
for (let key in nextProps.widgetProperties.props) {
|
|
322
285
|
if (!this.props.widgetProperties || !this.props.widgetProperties.props || nextProps.widgetProperties.props[key] !== this.props.widgetProperties.props[key]) {
|
|
323
286
|
this._widget.setProps(nextProps.widgetProperties.props);
|
|
324
|
-
|
|
325
287
|
return false;
|
|
326
288
|
}
|
|
327
289
|
}
|
|
328
290
|
}
|
|
329
291
|
}
|
|
330
|
-
|
|
331
292
|
if (this._widget && nextProps.widgetProperties && nextProps.widgetProperties.state) {
|
|
332
293
|
if (Object.keys(nextProps.widgetProperties.state).length !== Object.keys(this.props.widgetProperties.state).length) {
|
|
333
294
|
this._widget.setState(nextProps.widgetProperties.state);
|
|
334
|
-
|
|
335
295
|
return false;
|
|
336
296
|
} else {
|
|
337
297
|
for (let key in nextProps.widgetProperties.state) {
|
|
338
298
|
if (!this.props.widgetProperties || !this.props.widgetProperties.state || nextProps.widgetProperties.state[key] !== this.props.widgetProperties.state[key]) {
|
|
339
299
|
this._widget.setState(nextProps.widgetProperties.state);
|
|
340
|
-
|
|
341
300
|
return false;
|
|
342
301
|
}
|
|
343
302
|
}
|
|
344
303
|
}
|
|
345
304
|
}
|
|
346
|
-
|
|
347
305
|
return false;
|
|
348
306
|
}
|
|
307
|
+
|
|
349
308
|
/**
|
|
350
309
|
* In case of mounting the component, we always try to first load
|
|
351
310
|
* the widget assets. Be it first mount after SSR or mount after
|
|
352
311
|
* first render on client.
|
|
353
312
|
*/
|
|
354
|
-
|
|
355
|
-
|
|
356
313
|
componentDidMount() {
|
|
357
314
|
super.componentDidMount();
|
|
358
|
-
|
|
359
315
|
this._loadWidgetAssets();
|
|
360
316
|
}
|
|
317
|
+
|
|
361
318
|
/**
|
|
362
319
|
* After the component has been updated, we still need to handle few situations.
|
|
363
320
|
* 1) In case assets have been loaded, we need to mount the widget.
|
|
@@ -366,24 +323,22 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
366
323
|
* @param {object} prevProps
|
|
367
324
|
* @param {object} prevState
|
|
368
325
|
*/
|
|
369
|
-
|
|
370
|
-
|
|
371
326
|
componentDidUpdate(prevProps, prevState) {
|
|
372
327
|
const {
|
|
373
328
|
widgetProperties: currentWidgetProperties
|
|
374
329
|
} = this.props;
|
|
375
330
|
const {
|
|
376
331
|
widgetProperties: prevWidgetProperties
|
|
377
|
-
} = prevProps;
|
|
332
|
+
} = prevProps;
|
|
378
333
|
|
|
334
|
+
// 1) Assets have been loaded => mount the widget
|
|
379
335
|
if (this.state.assetsLoaded && prevState.assetsLoaded !== this.state.assetsLoaded) {
|
|
380
336
|
return this._mountWidget();
|
|
381
|
-
}
|
|
382
|
-
|
|
337
|
+
}
|
|
383
338
|
|
|
339
|
+
// 2.1) In case we receive empty new properties, we need to cleanup.
|
|
384
340
|
if (!AbstractMerkurWidget.validateProperties(currentWidgetProperties) && AbstractMerkurWidget.validateProperties(prevWidgetProperties)) {
|
|
385
341
|
this._removeWidget();
|
|
386
|
-
|
|
387
342
|
this.setState({
|
|
388
343
|
encounteredError: false,
|
|
389
344
|
assetsLoaded: false,
|
|
@@ -391,39 +346,35 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
391
346
|
});
|
|
392
347
|
return;
|
|
393
348
|
}
|
|
349
|
+
|
|
394
350
|
/**
|
|
395
351
|
* 2.2) In case there were no widget properties before, we try to
|
|
396
352
|
* initialize widget first by doing the same as if it first mounted
|
|
397
353
|
* (loading assets into the DOM).
|
|
398
354
|
*/
|
|
399
|
-
|
|
400
|
-
|
|
401
355
|
if (AbstractMerkurWidget.validateProperties(currentWidgetProperties) && !AbstractMerkurWidget.validateProperties(prevWidgetProperties)) {
|
|
402
356
|
return this._loadWidgetAssets();
|
|
403
357
|
}
|
|
358
|
+
|
|
404
359
|
/**
|
|
405
360
|
* 2.3) In case widget has changed, first we need to cleanup (remove previous widget),
|
|
406
361
|
* and then we again try to initialize the new widget same way as
|
|
407
362
|
* if it has mounted for the first time (if there are any new widget properties).
|
|
408
363
|
*/
|
|
409
|
-
|
|
410
|
-
|
|
411
364
|
if (AbstractMerkurWidget.hasWidgetChanged(currentWidgetProperties, prevWidgetProperties)) {
|
|
412
365
|
this._removeWidget();
|
|
413
|
-
|
|
414
366
|
this._loadWidgetAssets();
|
|
415
|
-
|
|
416
367
|
return;
|
|
417
368
|
}
|
|
418
369
|
}
|
|
370
|
+
|
|
419
371
|
/**
|
|
420
372
|
* In case of unmounting we only really need to do the cleanup.
|
|
421
373
|
*/
|
|
422
|
-
|
|
423
|
-
|
|
424
374
|
componentWillUnmount() {
|
|
425
375
|
this._removeWidget();
|
|
426
376
|
}
|
|
377
|
+
|
|
427
378
|
/**
|
|
428
379
|
* There are two possible outputs from the render method:
|
|
429
380
|
* 1) Fallback is rendered only, when assets are not yet loaded
|
|
@@ -434,8 +385,6 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
434
385
|
*
|
|
435
386
|
* @return {React.ReactElement|null}
|
|
436
387
|
*/
|
|
437
|
-
|
|
438
|
-
|
|
439
388
|
render() {
|
|
440
389
|
const {
|
|
441
390
|
widgetProperties
|
|
@@ -444,30 +393,25 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
444
393
|
encounteredError,
|
|
445
394
|
assetsLoaded
|
|
446
395
|
} = this.state;
|
|
447
|
-
|
|
448
396
|
if (!AbstractMerkurWidget.validateProperties(widgetProperties) || encounteredError || this._isClient() && !this._isSSRHydrate() && !assetsLoaded) {
|
|
449
397
|
return this._renderFallback();
|
|
450
398
|
}
|
|
451
|
-
|
|
452
399
|
if (!widgetProperties.containerSelector) {
|
|
453
400
|
throw new Error(`The widgetProperties.containerSelector is not defined`);
|
|
454
401
|
}
|
|
455
|
-
|
|
456
402
|
const html = this._getWidgetHTML();
|
|
457
|
-
|
|
458
403
|
return /*#__PURE__*/React.createElement(React.Fragment, null, (!this._isClient() || this._isSSRHydrate()) && this._renderStyleAssets(), /*#__PURE__*/React.createElement(WidgetWrapper$1, {
|
|
459
404
|
containerSelector: widgetProperties.containerSelector,
|
|
460
405
|
html: html
|
|
461
406
|
}));
|
|
462
407
|
}
|
|
408
|
+
|
|
463
409
|
/**
|
|
464
410
|
* Renders widget style assets inline. This is used on SSR
|
|
465
411
|
* and in first render after SSR hydration.
|
|
466
412
|
*
|
|
467
413
|
* @return {[React.ReactElement]}
|
|
468
414
|
*/
|
|
469
|
-
|
|
470
|
-
|
|
471
415
|
_renderStyleAssets() {
|
|
472
416
|
const {
|
|
473
417
|
widgetProperties
|
|
@@ -481,7 +425,6 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
481
425
|
href: asset.source,
|
|
482
426
|
key: key
|
|
483
427
|
});
|
|
484
|
-
|
|
485
428
|
case 'inlineStyle':
|
|
486
429
|
return /*#__PURE__*/React.createElement("style", {
|
|
487
430
|
key: key,
|
|
@@ -492,56 +435,46 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
492
435
|
}
|
|
493
436
|
});
|
|
494
437
|
}
|
|
495
|
-
|
|
496
438
|
_handleError(error) {
|
|
497
|
-
if (typeof this.props.onError === 'function') {
|
|
498
|
-
|
|
439
|
+
if (typeof this.props.onError === 'function' && this.props.onError(error)) {
|
|
440
|
+
return;
|
|
499
441
|
}
|
|
500
|
-
|
|
501
442
|
this.setState({
|
|
502
443
|
encounteredError: error
|
|
503
444
|
});
|
|
504
445
|
}
|
|
446
|
+
|
|
505
447
|
/**
|
|
506
448
|
* Handles widget unmounting and removal, while removing
|
|
507
449
|
* event listeners, calling unmount on props and widget.
|
|
508
450
|
*/
|
|
509
|
-
|
|
510
|
-
|
|
511
451
|
_removeWidget() {
|
|
512
452
|
if (!this._widget) {
|
|
513
453
|
return;
|
|
514
454
|
}
|
|
515
|
-
|
|
516
455
|
if (this.props.onWidgetUnmounting) {
|
|
517
456
|
this.props.onWidgetUnmounting(this._widget);
|
|
518
|
-
}
|
|
519
|
-
|
|
457
|
+
}
|
|
520
458
|
|
|
459
|
+
// widget might not be using @merkur/plugin-event-emitter
|
|
521
460
|
if (typeof this._widget.off === 'function') {
|
|
522
461
|
this._widget.off(MERKUR_ERROR_EVENT_NAME, this._handleClientError);
|
|
523
462
|
}
|
|
524
|
-
|
|
525
463
|
this._widget.unmount();
|
|
526
|
-
|
|
527
464
|
this._widget = null;
|
|
528
|
-
|
|
529
465
|
this._clearCachedHtml();
|
|
530
466
|
}
|
|
467
|
+
|
|
531
468
|
/**
|
|
532
469
|
* Loads widget assets into page.
|
|
533
470
|
*/
|
|
534
|
-
|
|
535
|
-
|
|
536
471
|
_loadWidgetAssets() {
|
|
537
472
|
const {
|
|
538
473
|
widgetProperties
|
|
539
474
|
} = this.props;
|
|
540
|
-
|
|
541
475
|
if (!AbstractMerkurWidget.validateProperties(widgetProperties) || this._widget) {
|
|
542
476
|
return;
|
|
543
477
|
}
|
|
544
|
-
|
|
545
478
|
return Promise.all([loadStyleAssets(widgetProperties.assets), loadScriptAssets(widgetProperties.assets)]).then(() => new Promise(resolve => {
|
|
546
479
|
this.setState({
|
|
547
480
|
assetsLoaded: true
|
|
@@ -550,32 +483,27 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
550
483
|
});
|
|
551
484
|
})).catch(error => this._handleError(error));
|
|
552
485
|
}
|
|
486
|
+
|
|
553
487
|
/**
|
|
554
488
|
* Creates and mounts widget instance after all resource loaded.
|
|
555
489
|
*/
|
|
556
|
-
|
|
557
|
-
|
|
558
490
|
async _mountWidget() {
|
|
559
491
|
const {
|
|
560
492
|
widgetProperties,
|
|
561
493
|
onWidgetMounted,
|
|
562
494
|
debug = false
|
|
563
495
|
} = this.props;
|
|
564
|
-
|
|
565
496
|
if (!AbstractMerkurWidget.validateProperties(widgetProperties) || this._widget) {
|
|
566
497
|
return;
|
|
567
498
|
}
|
|
568
|
-
|
|
569
499
|
try {
|
|
570
500
|
const merkur = getMerkur();
|
|
571
501
|
this._widget = await merkur.create(widgetProperties);
|
|
572
502
|
await this._widget.mount();
|
|
573
|
-
|
|
574
503
|
if (typeof this._widget.on === 'function') {
|
|
575
504
|
// widget might not be using @merkur/plugin-event-emitter
|
|
576
505
|
this._widget.on(MERKUR_ERROR_EVENT_NAME, this._handleClientError);
|
|
577
506
|
}
|
|
578
|
-
|
|
579
507
|
if (typeof onWidgetMounted === 'function') {
|
|
580
508
|
onWidgetMounted(this._widget);
|
|
581
509
|
}
|
|
@@ -585,9 +513,7 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
585
513
|
}
|
|
586
514
|
}
|
|
587
515
|
}
|
|
588
|
-
|
|
589
516
|
}
|
|
590
|
-
|
|
591
517
|
class MerkurSlot extends AbstractMerkurWidget {
|
|
592
518
|
/**
|
|
593
519
|
* Returns access to current slot properties, based on it's name
|
|
@@ -595,33 +521,29 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
595
521
|
*/
|
|
596
522
|
get slot() {
|
|
597
523
|
var _widgetProperties$slo, _widgetProperties$slo2;
|
|
598
|
-
|
|
599
524
|
const {
|
|
600
525
|
widgetProperties,
|
|
601
526
|
slotName
|
|
602
527
|
} = this.props;
|
|
603
528
|
return (_widgetProperties$slo = widgetProperties === null || widgetProperties === void 0 ? void 0 : (_widgetProperties$slo2 = widgetProperties.slot) === null || _widgetProperties$slo2 === void 0 ? void 0 : _widgetProperties$slo2[slotName]) !== null && _widgetProperties$slo !== void 0 ? _widgetProperties$slo : null;
|
|
604
529
|
}
|
|
530
|
+
|
|
605
531
|
/**
|
|
606
532
|
* @inheritdoc
|
|
607
533
|
*/
|
|
608
|
-
|
|
609
|
-
|
|
610
534
|
get html() {
|
|
611
535
|
var _this$slot;
|
|
612
|
-
|
|
613
536
|
return ((_this$slot = this.slot) === null || _this$slot === void 0 ? void 0 : _this$slot.html) || null;
|
|
614
537
|
}
|
|
538
|
+
|
|
615
539
|
/**
|
|
616
540
|
* @inheritdoc
|
|
617
541
|
*/
|
|
618
|
-
|
|
619
|
-
|
|
620
542
|
get container() {
|
|
621
543
|
var _document2, _this$slot2;
|
|
622
|
-
|
|
623
544
|
return this._isClient() && ((_document2 = document) === null || _document2 === void 0 ? void 0 : _document2.querySelector((_this$slot2 = this.slot) === null || _this$slot2 === void 0 ? void 0 : _this$slot2.containerSelector)) || null;
|
|
624
545
|
}
|
|
546
|
+
|
|
625
547
|
/**
|
|
626
548
|
* The component should update only in following cases:
|
|
627
549
|
* 1) Component has no props.widgetProperties.
|
|
@@ -629,23 +551,19 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
629
551
|
*
|
|
630
552
|
* @param {object} nextProps
|
|
631
553
|
*/
|
|
632
|
-
|
|
633
|
-
|
|
634
554
|
shouldComponentUpdate(nextProps) {
|
|
635
555
|
if (!AbstractMerkurWidget.validateProperties(this.props.widgetProperties) || !AbstractMerkurWidget.validateProperties(nextProps.widgetProperties) || AbstractMerkurWidget.hasWidgetChanged(this.props.widgetProperties, nextProps.widgetProperties)) {
|
|
636
556
|
return true;
|
|
637
557
|
}
|
|
638
|
-
|
|
639
558
|
return false;
|
|
640
559
|
}
|
|
560
|
+
|
|
641
561
|
/**
|
|
642
562
|
* Cleanup when we receive empty widget properties.
|
|
643
563
|
*
|
|
644
564
|
* @param {object} prevProps
|
|
645
565
|
* @param {object} prevState
|
|
646
566
|
*/
|
|
647
|
-
|
|
648
|
-
|
|
649
567
|
componentDidUpdate(prevProps) {
|
|
650
568
|
const {
|
|
651
569
|
widgetProperties: currentWidgetProperties
|
|
@@ -653,21 +571,19 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
653
571
|
const {
|
|
654
572
|
widgetProperties: prevWidgetProperties
|
|
655
573
|
} = prevProps;
|
|
656
|
-
|
|
657
574
|
if (!AbstractMerkurWidget.validateProperties(currentWidgetProperties) && AbstractMerkurWidget.validateProperties(prevWidgetProperties)) {
|
|
658
575
|
this._removeSlot();
|
|
659
|
-
|
|
660
576
|
return;
|
|
661
577
|
}
|
|
662
578
|
}
|
|
579
|
+
|
|
663
580
|
/**
|
|
664
581
|
* Cleanup when unmounting
|
|
665
582
|
*/
|
|
666
|
-
|
|
667
|
-
|
|
668
583
|
componentWillUnmount() {
|
|
669
584
|
this._removeSlot();
|
|
670
585
|
}
|
|
586
|
+
|
|
671
587
|
/**
|
|
672
588
|
* There are two possible outputs from the render method:
|
|
673
589
|
* 1) Fallback is rendered only, when there are no widget properties.
|
|
@@ -677,20 +593,17 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
677
593
|
*
|
|
678
594
|
* @return {React.ReactElement|null}
|
|
679
595
|
*/
|
|
680
|
-
|
|
681
|
-
|
|
682
596
|
render() {
|
|
683
597
|
const {
|
|
684
598
|
widgetProperties
|
|
685
599
|
} = this.props;
|
|
686
|
-
|
|
687
600
|
if (!AbstractMerkurWidget.validateProperties(widgetProperties) || !this.slot) {
|
|
688
601
|
return this._renderFallback();
|
|
689
602
|
}
|
|
690
|
-
|
|
691
603
|
if (!this.slot['containerSelector']) {
|
|
692
604
|
throw new Error(`The ${this.slot.name}.containerSelector is not defined`);
|
|
693
605
|
}
|
|
606
|
+
|
|
694
607
|
/**
|
|
695
608
|
* In case of SPA rendering, we render fallback (which can also display
|
|
696
609
|
* loading placeholders) inside the component wrapper, until the widget
|
|
@@ -698,8 +611,6 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
698
611
|
* of the wrapper (containing fallback) with slot markup. In case of SPA
|
|
699
612
|
* we also don't want to render html to prevent FOUC.
|
|
700
613
|
*/
|
|
701
|
-
|
|
702
|
-
|
|
703
614
|
const isInitialSPARender = this._isClient() && !this._isSSRHydrate();
|
|
704
615
|
const html = isInitialSPARender ? '' : this._getWidgetHTML();
|
|
705
616
|
return /*#__PURE__*/React.createElement(WidgetWrapper$1, {
|
|
@@ -707,15 +618,12 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
707
618
|
html: html
|
|
708
619
|
}, isInitialSPARender && this._renderFallback());
|
|
709
620
|
}
|
|
621
|
+
|
|
710
622
|
/**
|
|
711
623
|
* Cleanup after slot removal.
|
|
712
624
|
*/
|
|
713
|
-
|
|
714
|
-
|
|
715
625
|
_removeSlot() {
|
|
716
626
|
this._clearCachedHtml();
|
|
717
627
|
}
|
|
718
|
-
|
|
719
628
|
}
|
|
720
|
-
|
|
721
629
|
export { MerkurSlot, MerkurWidget };
|