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