@merkur/core 0.37.0 → 0.39.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/README.md +1 -1
- package/lib/index.cjs +41 -85
- package/lib/index.js +41 -85
- package/lib/index.mjs +41 -85
- package/package.json +3 -4
- package/types.d.ts +1 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<a href="https://merkur.js.org/docs/getting-started" title="Getting started">
|
|
3
|
-
<img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-
|
|
3
|
+
<img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-logo.png" width="100px" height="100px" alt="Merkur illustration"/>
|
|
4
4
|
</a>
|
|
5
5
|
</p>
|
|
6
6
|
|
package/lib/index.cjs
CHANGED
|
@@ -14,66 +14,58 @@ function clone(object) {
|
|
|
14
14
|
return object;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
|
|
18
17
|
function setDefaultValueForUndefined(object, keys, value = {}) {
|
|
19
|
-
let objectClone = {
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
let objectClone = {
|
|
19
|
+
...object
|
|
20
|
+
};
|
|
21
|
+
keys.forEach(key => {
|
|
22
22
|
objectClone[key] = objectClone[key] || clone(value);
|
|
23
23
|
});
|
|
24
|
-
|
|
25
24
|
return objectClone;
|
|
26
25
|
}
|
|
27
|
-
|
|
28
26
|
function bindWidgetToFunctions(widget, target) {
|
|
29
27
|
target = target || widget;
|
|
30
|
-
Object.keys(target).forEach(
|
|
28
|
+
Object.keys(target).forEach(key => {
|
|
31
29
|
if (isFunction(target[key])) {
|
|
32
30
|
let originalFunction = target[key];
|
|
33
|
-
|
|
34
31
|
target[key] = createBoundedFunction(widget, originalFunction);
|
|
35
32
|
}
|
|
36
33
|
});
|
|
37
34
|
}
|
|
38
|
-
|
|
39
35
|
function hookMethod(widget, path, handler) {
|
|
40
|
-
const {
|
|
36
|
+
const {
|
|
37
|
+
target,
|
|
38
|
+
methodName
|
|
39
|
+
} = parsePath(widget, path);
|
|
41
40
|
const originalFunction = createBoundedFunction(widget, target[methodName]);
|
|
42
|
-
|
|
43
41
|
target[methodName] = function (widget, ...rest) {
|
|
44
42
|
return handler(widget, originalFunction, ...rest);
|
|
45
43
|
};
|
|
46
|
-
|
|
47
44
|
return originalFunction;
|
|
48
45
|
}
|
|
49
|
-
|
|
50
46
|
function parsePath(widget, path = '') {
|
|
51
47
|
const paths = path.split('.');
|
|
52
48
|
const methodName = paths.pop();
|
|
53
49
|
const target = paths.reduce((target, path) => target[path] || {}, widget);
|
|
54
|
-
|
|
55
50
|
if (!isFunction(target[methodName])) {
|
|
56
|
-
throw new Error(
|
|
57
|
-
`Defined path '${path}' is incorrect. Check your widget structure.`,
|
|
58
|
-
);
|
|
51
|
+
throw new Error(`Defined path '${path}' is incorrect. Check your widget structure.`);
|
|
59
52
|
}
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
return {
|
|
54
|
+
target,
|
|
55
|
+
methodName
|
|
56
|
+
};
|
|
62
57
|
}
|
|
63
|
-
|
|
64
58
|
function isFunction(value) {
|
|
65
59
|
return typeof value === 'function';
|
|
66
60
|
}
|
|
67
|
-
|
|
68
61
|
function assignMissingKeys(target, ...sources) {
|
|
69
|
-
sources.forEach(
|
|
70
|
-
Object.keys(source || {}).forEach(
|
|
62
|
+
sources.forEach(source => {
|
|
63
|
+
Object.keys(source || {}).forEach(key => {
|
|
71
64
|
if (!(key in target)) {
|
|
72
65
|
target[key] = source[key];
|
|
73
66
|
}
|
|
74
67
|
});
|
|
75
68
|
});
|
|
76
|
-
|
|
77
69
|
return target;
|
|
78
70
|
}
|
|
79
71
|
|
|
@@ -83,7 +75,6 @@ async function callPluginMethod(widget, method, args) {
|
|
|
83
75
|
widget = await plugin[method](widget, ...args);
|
|
84
76
|
}
|
|
85
77
|
}
|
|
86
|
-
|
|
87
78
|
return widget;
|
|
88
79
|
}
|
|
89
80
|
|
|
@@ -95,25 +86,12 @@ async function callPluginMethod(widget, method, args) {
|
|
|
95
86
|
function defineWidget(widgetDefinition) {
|
|
96
87
|
return widgetDefinition;
|
|
97
88
|
}
|
|
98
|
-
|
|
99
89
|
function setDefinitionDefaults(widgetDefinition) {
|
|
100
90
|
return {
|
|
101
91
|
...widgetDefinition,
|
|
102
|
-
...setDefaultValueForUndefined(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
null,
|
|
106
|
-
),
|
|
107
|
-
...setDefaultValueForUndefined(widgetDefinition, [
|
|
108
|
-
'slot',
|
|
109
|
-
'$dependencies',
|
|
110
|
-
'$external',
|
|
111
|
-
]),
|
|
112
|
-
...setDefaultValueForUndefined(
|
|
113
|
-
widgetDefinition,
|
|
114
|
-
['setup', 'create'],
|
|
115
|
-
(widget) => widget,
|
|
116
|
-
),
|
|
92
|
+
...setDefaultValueForUndefined(widgetDefinition, ['containerSelector'], null),
|
|
93
|
+
...setDefaultValueForUndefined(widgetDefinition, ['slot', '$dependencies', '$external']),
|
|
94
|
+
...setDefaultValueForUndefined(widgetDefinition, ['setup', 'create'], widget => widget)
|
|
117
95
|
};
|
|
118
96
|
}
|
|
119
97
|
|
|
@@ -122,22 +100,20 @@ function setDefinitionDefaults(widgetDefinition) {
|
|
|
122
100
|
*/
|
|
123
101
|
async function createMerkurWidget(widgetDefinition = {}) {
|
|
124
102
|
const definition = setDefinitionDefaults(widgetDefinition);
|
|
125
|
-
const {
|
|
126
|
-
|
|
103
|
+
const {
|
|
104
|
+
setup,
|
|
105
|
+
create
|
|
106
|
+
} = definition;
|
|
127
107
|
let widget = {
|
|
128
108
|
async setup(widget, ...rest) {
|
|
129
109
|
widget = await callPluginMethod(widget, 'setup', rest);
|
|
130
|
-
|
|
131
110
|
return setup(widget, ...rest);
|
|
132
111
|
},
|
|
133
112
|
async create(widget, ...rest) {
|
|
134
113
|
widget = await callPluginMethod(widget, 'create', rest);
|
|
135
|
-
|
|
136
114
|
return create(widget, ...rest);
|
|
137
115
|
},
|
|
138
|
-
$plugins: (definition.$plugins || []).map(
|
|
139
|
-
pluginFactory(),
|
|
140
|
-
),
|
|
116
|
+
$plugins: (definition.$plugins || []).map(pluginFactory => pluginFactory())
|
|
141
117
|
};
|
|
142
118
|
|
|
143
119
|
// TODO refactoring
|
|
@@ -146,95 +122,76 @@ async function createMerkurWidget(widgetDefinition = {}) {
|
|
|
146
122
|
widget.$dependencies = definition.$dependencies;
|
|
147
123
|
widget.$external = definition.$external;
|
|
148
124
|
widget.$in = {};
|
|
149
|
-
|
|
150
125
|
delete definition.name;
|
|
151
126
|
delete definition.version;
|
|
152
127
|
delete definition.$dependencies;
|
|
153
128
|
delete definition.$external;
|
|
154
129
|
delete definition.$plugins;
|
|
155
|
-
|
|
156
130
|
delete definition.setup;
|
|
157
131
|
delete definition.create;
|
|
158
|
-
|
|
159
132
|
widget = await widget.setup(widget, definition);
|
|
160
133
|
widget = await widget.create(widget, definition);
|
|
161
|
-
|
|
162
134
|
bindWidgetToFunctions(widget);
|
|
163
135
|
Object.seal(widget);
|
|
164
|
-
|
|
165
136
|
return widget;
|
|
166
137
|
}
|
|
167
138
|
|
|
168
|
-
function register({
|
|
139
|
+
function register({
|
|
140
|
+
name,
|
|
141
|
+
version,
|
|
142
|
+
createWidget
|
|
143
|
+
}) {
|
|
169
144
|
const merkur = getMerkur();
|
|
170
|
-
|
|
171
145
|
merkur.$in.widgetFactory[name + version] = createWidget;
|
|
172
146
|
}
|
|
173
|
-
|
|
174
147
|
function create(widgetProperties = {}) {
|
|
175
148
|
const merkur = getMerkur();
|
|
176
|
-
const {
|
|
149
|
+
const {
|
|
150
|
+
name,
|
|
151
|
+
version
|
|
152
|
+
} = widgetProperties;
|
|
177
153
|
const factory = merkur.$in.widgetFactory[name + version];
|
|
178
|
-
|
|
179
154
|
if (!isFunction(factory)) {
|
|
180
|
-
throw new Error(
|
|
181
|
-
`The widget with defined name and version "${
|
|
182
|
-
name + version
|
|
183
|
-
}" is not defined.`,
|
|
184
|
-
);
|
|
155
|
+
throw new Error(`The widget with defined name and version "${name + version}" is not defined.`);
|
|
185
156
|
}
|
|
186
|
-
|
|
187
157
|
return factory(widgetProperties);
|
|
188
158
|
}
|
|
189
|
-
|
|
190
|
-
|
|
159
|
+
function createMerkur({
|
|
160
|
+
$plugins = []
|
|
161
|
+
} = {}) {
|
|
191
162
|
const merkur = getMerkur();
|
|
192
|
-
|
|
193
|
-
$plugins.forEach((plugin) => {
|
|
163
|
+
$plugins.forEach(plugin => {
|
|
194
164
|
if (plugin && isFunction(plugin.setup)) {
|
|
195
165
|
plugin.setup(merkur);
|
|
196
166
|
}
|
|
197
167
|
});
|
|
198
|
-
|
|
199
168
|
return merkur;
|
|
200
169
|
}
|
|
201
|
-
|
|
202
170
|
function removeMerkur() {
|
|
203
171
|
const globalContext = getGlobalContext();
|
|
204
|
-
|
|
205
172
|
delete globalContext.__merkur__;
|
|
206
173
|
}
|
|
207
|
-
|
|
208
174
|
function getMerkur() {
|
|
209
175
|
const globalContext = getGlobalContext();
|
|
210
|
-
|
|
211
176
|
if (!globalContext.__merkur__) {
|
|
212
177
|
globalContext.__merkur__ = {
|
|
213
178
|
$in: {
|
|
214
179
|
widgets: [],
|
|
215
|
-
widgetFactory: {}
|
|
180
|
+
widgetFactory: {}
|
|
216
181
|
},
|
|
217
182
|
$external: {},
|
|
218
183
|
$dependencies: {},
|
|
219
184
|
isRegistered,
|
|
220
185
|
register,
|
|
221
|
-
create
|
|
186
|
+
create
|
|
222
187
|
};
|
|
223
188
|
}
|
|
224
|
-
|
|
225
189
|
return globalContext.__merkur__;
|
|
226
190
|
}
|
|
227
|
-
|
|
228
191
|
function isRegistered(name) {
|
|
229
192
|
const merkur = getMerkur();
|
|
230
|
-
|
|
231
|
-
return Boolean(
|
|
232
|
-
Object.keys(merkur.$in.widgetFactory).find((key) =>
|
|
233
|
-
new RegExp(`^${name}`).test(key),
|
|
234
|
-
),
|
|
235
|
-
);
|
|
193
|
+
return Boolean(Object.keys(merkur.$in.widgetFactory).find(key => new RegExp(`^${name}`).test(key)));
|
|
236
194
|
}
|
|
237
|
-
|
|
238
195
|
function getGlobalContext() {
|
|
239
196
|
if (typeof globalThis !== 'undefined') {
|
|
240
197
|
return globalThis;
|
|
@@ -248,7 +205,6 @@ function getGlobalContext() {
|
|
|
248
205
|
if (typeof global !== 'undefined') {
|
|
249
206
|
return global;
|
|
250
207
|
}
|
|
251
|
-
|
|
252
208
|
return {};
|
|
253
209
|
}
|
|
254
210
|
|
package/lib/index.js
CHANGED
|
@@ -14,66 +14,58 @@ function clone(object) {
|
|
|
14
14
|
return object;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
|
|
18
17
|
function setDefaultValueForUndefined(object, keys, value = {}) {
|
|
19
|
-
let objectClone = {
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
let objectClone = {
|
|
19
|
+
...object
|
|
20
|
+
};
|
|
21
|
+
keys.forEach(key => {
|
|
22
22
|
objectClone[key] = objectClone[key] || clone(value);
|
|
23
23
|
});
|
|
24
|
-
|
|
25
24
|
return objectClone;
|
|
26
25
|
}
|
|
27
|
-
|
|
28
26
|
function bindWidgetToFunctions(widget, target) {
|
|
29
27
|
target = target || widget;
|
|
30
|
-
Object.keys(target).forEach(
|
|
28
|
+
Object.keys(target).forEach(key => {
|
|
31
29
|
if (isFunction(target[key])) {
|
|
32
30
|
let originalFunction = target[key];
|
|
33
|
-
|
|
34
31
|
target[key] = createBoundedFunction(widget, originalFunction);
|
|
35
32
|
}
|
|
36
33
|
});
|
|
37
34
|
}
|
|
38
|
-
|
|
39
35
|
function hookMethod(widget, path, handler) {
|
|
40
|
-
const {
|
|
36
|
+
const {
|
|
37
|
+
target,
|
|
38
|
+
methodName
|
|
39
|
+
} = parsePath(widget, path);
|
|
41
40
|
const originalFunction = createBoundedFunction(widget, target[methodName]);
|
|
42
|
-
|
|
43
41
|
target[methodName] = function (widget, ...rest) {
|
|
44
42
|
return handler(widget, originalFunction, ...rest);
|
|
45
43
|
};
|
|
46
|
-
|
|
47
44
|
return originalFunction;
|
|
48
45
|
}
|
|
49
|
-
|
|
50
46
|
function parsePath(widget, path = '') {
|
|
51
47
|
const paths = path.split('.');
|
|
52
48
|
const methodName = paths.pop();
|
|
53
49
|
const target = paths.reduce((target, path) => target[path] || {}, widget);
|
|
54
|
-
|
|
55
50
|
if (!isFunction(target[methodName])) {
|
|
56
|
-
throw new Error(
|
|
57
|
-
`Defined path '${path}' is incorrect. Check your widget structure.`,
|
|
58
|
-
);
|
|
51
|
+
throw new Error(`Defined path '${path}' is incorrect. Check your widget structure.`);
|
|
59
52
|
}
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
return {
|
|
54
|
+
target,
|
|
55
|
+
methodName
|
|
56
|
+
};
|
|
62
57
|
}
|
|
63
|
-
|
|
64
58
|
function isFunction(value) {
|
|
65
59
|
return typeof value === 'function';
|
|
66
60
|
}
|
|
67
|
-
|
|
68
61
|
function assignMissingKeys(target, ...sources) {
|
|
69
|
-
sources.forEach(
|
|
70
|
-
Object.keys(source || {}).forEach(
|
|
62
|
+
sources.forEach(source => {
|
|
63
|
+
Object.keys(source || {}).forEach(key => {
|
|
71
64
|
if (!(key in target)) {
|
|
72
65
|
target[key] = source[key];
|
|
73
66
|
}
|
|
74
67
|
});
|
|
75
68
|
});
|
|
76
|
-
|
|
77
69
|
return target;
|
|
78
70
|
}
|
|
79
71
|
|
|
@@ -83,7 +75,6 @@ async function callPluginMethod(widget, method, args) {
|
|
|
83
75
|
widget = await plugin[method](widget, ...args);
|
|
84
76
|
}
|
|
85
77
|
}
|
|
86
|
-
|
|
87
78
|
return widget;
|
|
88
79
|
}
|
|
89
80
|
|
|
@@ -95,25 +86,12 @@ async function callPluginMethod(widget, method, args) {
|
|
|
95
86
|
function defineWidget(widgetDefinition) {
|
|
96
87
|
return widgetDefinition;
|
|
97
88
|
}
|
|
98
|
-
|
|
99
89
|
function setDefinitionDefaults(widgetDefinition) {
|
|
100
90
|
return {
|
|
101
91
|
...widgetDefinition,
|
|
102
|
-
...setDefaultValueForUndefined(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
null,
|
|
106
|
-
),
|
|
107
|
-
...setDefaultValueForUndefined(widgetDefinition, [
|
|
108
|
-
'slot',
|
|
109
|
-
'$dependencies',
|
|
110
|
-
'$external',
|
|
111
|
-
]),
|
|
112
|
-
...setDefaultValueForUndefined(
|
|
113
|
-
widgetDefinition,
|
|
114
|
-
['setup', 'create'],
|
|
115
|
-
(widget) => widget,
|
|
116
|
-
),
|
|
92
|
+
...setDefaultValueForUndefined(widgetDefinition, ['containerSelector'], null),
|
|
93
|
+
...setDefaultValueForUndefined(widgetDefinition, ['slot', '$dependencies', '$external']),
|
|
94
|
+
...setDefaultValueForUndefined(widgetDefinition, ['setup', 'create'], widget => widget)
|
|
117
95
|
};
|
|
118
96
|
}
|
|
119
97
|
|
|
@@ -122,22 +100,20 @@ function setDefinitionDefaults(widgetDefinition) {
|
|
|
122
100
|
*/
|
|
123
101
|
async function createMerkurWidget(widgetDefinition = {}) {
|
|
124
102
|
const definition = setDefinitionDefaults(widgetDefinition);
|
|
125
|
-
const {
|
|
126
|
-
|
|
103
|
+
const {
|
|
104
|
+
setup,
|
|
105
|
+
create
|
|
106
|
+
} = definition;
|
|
127
107
|
let widget = {
|
|
128
108
|
async setup(widget, ...rest) {
|
|
129
109
|
widget = await callPluginMethod(widget, 'setup', rest);
|
|
130
|
-
|
|
131
110
|
return setup(widget, ...rest);
|
|
132
111
|
},
|
|
133
112
|
async create(widget, ...rest) {
|
|
134
113
|
widget = await callPluginMethod(widget, 'create', rest);
|
|
135
|
-
|
|
136
114
|
return create(widget, ...rest);
|
|
137
115
|
},
|
|
138
|
-
$plugins: (definition.$plugins || []).map(
|
|
139
|
-
pluginFactory(),
|
|
140
|
-
),
|
|
116
|
+
$plugins: (definition.$plugins || []).map(pluginFactory => pluginFactory())
|
|
141
117
|
};
|
|
142
118
|
|
|
143
119
|
// TODO refactoring
|
|
@@ -146,95 +122,76 @@ async function createMerkurWidget(widgetDefinition = {}) {
|
|
|
146
122
|
widget.$dependencies = definition.$dependencies;
|
|
147
123
|
widget.$external = definition.$external;
|
|
148
124
|
widget.$in = {};
|
|
149
|
-
|
|
150
125
|
delete definition.name;
|
|
151
126
|
delete definition.version;
|
|
152
127
|
delete definition.$dependencies;
|
|
153
128
|
delete definition.$external;
|
|
154
129
|
delete definition.$plugins;
|
|
155
|
-
|
|
156
130
|
delete definition.setup;
|
|
157
131
|
delete definition.create;
|
|
158
|
-
|
|
159
132
|
widget = await widget.setup(widget, definition);
|
|
160
133
|
widget = await widget.create(widget, definition);
|
|
161
|
-
|
|
162
134
|
bindWidgetToFunctions(widget);
|
|
163
135
|
Object.seal(widget);
|
|
164
|
-
|
|
165
136
|
return widget;
|
|
166
137
|
}
|
|
167
138
|
|
|
168
|
-
function register({
|
|
139
|
+
function register({
|
|
140
|
+
name,
|
|
141
|
+
version,
|
|
142
|
+
createWidget
|
|
143
|
+
}) {
|
|
169
144
|
const merkur = getMerkur();
|
|
170
|
-
|
|
171
145
|
merkur.$in.widgetFactory[name + version] = createWidget;
|
|
172
146
|
}
|
|
173
|
-
|
|
174
147
|
function create(widgetProperties = {}) {
|
|
175
148
|
const merkur = getMerkur();
|
|
176
|
-
const {
|
|
149
|
+
const {
|
|
150
|
+
name,
|
|
151
|
+
version
|
|
152
|
+
} = widgetProperties;
|
|
177
153
|
const factory = merkur.$in.widgetFactory[name + version];
|
|
178
|
-
|
|
179
154
|
if (!isFunction(factory)) {
|
|
180
|
-
throw new Error(
|
|
181
|
-
`The widget with defined name and version "${
|
|
182
|
-
name + version
|
|
183
|
-
}" is not defined.`,
|
|
184
|
-
);
|
|
155
|
+
throw new Error(`The widget with defined name and version "${name + version}" is not defined.`);
|
|
185
156
|
}
|
|
186
|
-
|
|
187
157
|
return factory(widgetProperties);
|
|
188
158
|
}
|
|
189
|
-
|
|
190
|
-
|
|
159
|
+
function createMerkur({
|
|
160
|
+
$plugins = []
|
|
161
|
+
} = {}) {
|
|
191
162
|
const merkur = getMerkur();
|
|
192
|
-
|
|
193
|
-
$plugins.forEach((plugin) => {
|
|
163
|
+
$plugins.forEach(plugin => {
|
|
194
164
|
if (plugin && isFunction(plugin.setup)) {
|
|
195
165
|
plugin.setup(merkur);
|
|
196
166
|
}
|
|
197
167
|
});
|
|
198
|
-
|
|
199
168
|
return merkur;
|
|
200
169
|
}
|
|
201
|
-
|
|
202
170
|
function removeMerkur() {
|
|
203
171
|
const globalContext = getGlobalContext();
|
|
204
|
-
|
|
205
172
|
delete globalContext.__merkur__;
|
|
206
173
|
}
|
|
207
|
-
|
|
208
174
|
function getMerkur() {
|
|
209
175
|
const globalContext = getGlobalContext();
|
|
210
|
-
|
|
211
176
|
if (!globalContext.__merkur__) {
|
|
212
177
|
globalContext.__merkur__ = {
|
|
213
178
|
$in: {
|
|
214
179
|
widgets: [],
|
|
215
|
-
widgetFactory: {}
|
|
180
|
+
widgetFactory: {}
|
|
216
181
|
},
|
|
217
182
|
$external: {},
|
|
218
183
|
$dependencies: {},
|
|
219
184
|
isRegistered,
|
|
220
185
|
register,
|
|
221
|
-
create
|
|
186
|
+
create
|
|
222
187
|
};
|
|
223
188
|
}
|
|
224
|
-
|
|
225
189
|
return globalContext.__merkur__;
|
|
226
190
|
}
|
|
227
|
-
|
|
228
191
|
function isRegistered(name) {
|
|
229
192
|
const merkur = getMerkur();
|
|
230
|
-
|
|
231
|
-
return Boolean(
|
|
232
|
-
Object.keys(merkur.$in.widgetFactory).find((key) =>
|
|
233
|
-
new RegExp(`^${name}`).test(key),
|
|
234
|
-
),
|
|
235
|
-
);
|
|
193
|
+
return Boolean(Object.keys(merkur.$in.widgetFactory).find(key => new RegExp(`^${name}`).test(key)));
|
|
236
194
|
}
|
|
237
|
-
|
|
238
195
|
function getGlobalContext() {
|
|
239
196
|
if (typeof globalThis !== 'undefined') {
|
|
240
197
|
return globalThis;
|
|
@@ -248,7 +205,6 @@ function getGlobalContext() {
|
|
|
248
205
|
if (typeof global !== 'undefined') {
|
|
249
206
|
return global;
|
|
250
207
|
}
|
|
251
|
-
|
|
252
208
|
return {};
|
|
253
209
|
}
|
|
254
210
|
|
package/lib/index.mjs
CHANGED
|
@@ -12,66 +12,58 @@ function clone(object) {
|
|
|
12
12
|
return object;
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
|
|
16
15
|
function setDefaultValueForUndefined(object, keys, value = {}) {
|
|
17
|
-
let objectClone = {
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
let objectClone = {
|
|
17
|
+
...object
|
|
18
|
+
};
|
|
19
|
+
keys.forEach(key => {
|
|
20
20
|
objectClone[key] = objectClone[key] || clone(value);
|
|
21
21
|
});
|
|
22
|
-
|
|
23
22
|
return objectClone;
|
|
24
23
|
}
|
|
25
|
-
|
|
26
24
|
function bindWidgetToFunctions(widget, target) {
|
|
27
25
|
target = target || widget;
|
|
28
|
-
Object.keys(target).forEach(
|
|
26
|
+
Object.keys(target).forEach(key => {
|
|
29
27
|
if (isFunction(target[key])) {
|
|
30
28
|
let originalFunction = target[key];
|
|
31
|
-
|
|
32
29
|
target[key] = createBoundedFunction(widget, originalFunction);
|
|
33
30
|
}
|
|
34
31
|
});
|
|
35
32
|
}
|
|
36
|
-
|
|
37
33
|
function hookMethod(widget, path, handler) {
|
|
38
|
-
const {
|
|
34
|
+
const {
|
|
35
|
+
target,
|
|
36
|
+
methodName
|
|
37
|
+
} = parsePath(widget, path);
|
|
39
38
|
const originalFunction = createBoundedFunction(widget, target[methodName]);
|
|
40
|
-
|
|
41
39
|
target[methodName] = function (widget, ...rest) {
|
|
42
40
|
return handler(widget, originalFunction, ...rest);
|
|
43
41
|
};
|
|
44
|
-
|
|
45
42
|
return originalFunction;
|
|
46
43
|
}
|
|
47
|
-
|
|
48
44
|
function parsePath(widget, path = '') {
|
|
49
45
|
const paths = path.split('.');
|
|
50
46
|
const methodName = paths.pop();
|
|
51
47
|
const target = paths.reduce((target, path) => target[path] || {}, widget);
|
|
52
|
-
|
|
53
48
|
if (!isFunction(target[methodName])) {
|
|
54
|
-
throw new Error(
|
|
55
|
-
`Defined path '${path}' is incorrect. Check your widget structure.`,
|
|
56
|
-
);
|
|
49
|
+
throw new Error(`Defined path '${path}' is incorrect. Check your widget structure.`);
|
|
57
50
|
}
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
return {
|
|
52
|
+
target,
|
|
53
|
+
methodName
|
|
54
|
+
};
|
|
60
55
|
}
|
|
61
|
-
|
|
62
56
|
function isFunction(value) {
|
|
63
57
|
return typeof value === 'function';
|
|
64
58
|
}
|
|
65
|
-
|
|
66
59
|
function assignMissingKeys(target, ...sources) {
|
|
67
|
-
sources.forEach(
|
|
68
|
-
Object.keys(source || {}).forEach(
|
|
60
|
+
sources.forEach(source => {
|
|
61
|
+
Object.keys(source || {}).forEach(key => {
|
|
69
62
|
if (!(key in target)) {
|
|
70
63
|
target[key] = source[key];
|
|
71
64
|
}
|
|
72
65
|
});
|
|
73
66
|
});
|
|
74
|
-
|
|
75
67
|
return target;
|
|
76
68
|
}
|
|
77
69
|
|
|
@@ -81,7 +73,6 @@ async function callPluginMethod(widget, method, args) {
|
|
|
81
73
|
widget = await plugin[method](widget, ...args);
|
|
82
74
|
}
|
|
83
75
|
}
|
|
84
|
-
|
|
85
76
|
return widget;
|
|
86
77
|
}
|
|
87
78
|
|
|
@@ -93,25 +84,12 @@ async function callPluginMethod(widget, method, args) {
|
|
|
93
84
|
function defineWidget(widgetDefinition) {
|
|
94
85
|
return widgetDefinition;
|
|
95
86
|
}
|
|
96
|
-
|
|
97
87
|
function setDefinitionDefaults(widgetDefinition) {
|
|
98
88
|
return {
|
|
99
89
|
...widgetDefinition,
|
|
100
|
-
...setDefaultValueForUndefined(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
null,
|
|
104
|
-
),
|
|
105
|
-
...setDefaultValueForUndefined(widgetDefinition, [
|
|
106
|
-
'slot',
|
|
107
|
-
'$dependencies',
|
|
108
|
-
'$external',
|
|
109
|
-
]),
|
|
110
|
-
...setDefaultValueForUndefined(
|
|
111
|
-
widgetDefinition,
|
|
112
|
-
['setup', 'create'],
|
|
113
|
-
(widget) => widget,
|
|
114
|
-
),
|
|
90
|
+
...setDefaultValueForUndefined(widgetDefinition, ['containerSelector'], null),
|
|
91
|
+
...setDefaultValueForUndefined(widgetDefinition, ['slot', '$dependencies', '$external']),
|
|
92
|
+
...setDefaultValueForUndefined(widgetDefinition, ['setup', 'create'], widget => widget)
|
|
115
93
|
};
|
|
116
94
|
}
|
|
117
95
|
|
|
@@ -120,22 +98,20 @@ function setDefinitionDefaults(widgetDefinition) {
|
|
|
120
98
|
*/
|
|
121
99
|
async function createMerkurWidget(widgetDefinition = {}) {
|
|
122
100
|
const definition = setDefinitionDefaults(widgetDefinition);
|
|
123
|
-
const {
|
|
124
|
-
|
|
101
|
+
const {
|
|
102
|
+
setup,
|
|
103
|
+
create
|
|
104
|
+
} = definition;
|
|
125
105
|
let widget = {
|
|
126
106
|
async setup(widget, ...rest) {
|
|
127
107
|
widget = await callPluginMethod(widget, 'setup', rest);
|
|
128
|
-
|
|
129
108
|
return setup(widget, ...rest);
|
|
130
109
|
},
|
|
131
110
|
async create(widget, ...rest) {
|
|
132
111
|
widget = await callPluginMethod(widget, 'create', rest);
|
|
133
|
-
|
|
134
112
|
return create(widget, ...rest);
|
|
135
113
|
},
|
|
136
|
-
$plugins: (definition.$plugins || []).map(
|
|
137
|
-
pluginFactory(),
|
|
138
|
-
),
|
|
114
|
+
$plugins: (definition.$plugins || []).map(pluginFactory => pluginFactory())
|
|
139
115
|
};
|
|
140
116
|
|
|
141
117
|
// TODO refactoring
|
|
@@ -144,95 +120,76 @@ async function createMerkurWidget(widgetDefinition = {}) {
|
|
|
144
120
|
widget.$dependencies = definition.$dependencies;
|
|
145
121
|
widget.$external = definition.$external;
|
|
146
122
|
widget.$in = {};
|
|
147
|
-
|
|
148
123
|
delete definition.name;
|
|
149
124
|
delete definition.version;
|
|
150
125
|
delete definition.$dependencies;
|
|
151
126
|
delete definition.$external;
|
|
152
127
|
delete definition.$plugins;
|
|
153
|
-
|
|
154
128
|
delete definition.setup;
|
|
155
129
|
delete definition.create;
|
|
156
|
-
|
|
157
130
|
widget = await widget.setup(widget, definition);
|
|
158
131
|
widget = await widget.create(widget, definition);
|
|
159
|
-
|
|
160
132
|
bindWidgetToFunctions(widget);
|
|
161
133
|
Object.seal(widget);
|
|
162
|
-
|
|
163
134
|
return widget;
|
|
164
135
|
}
|
|
165
136
|
|
|
166
|
-
function register({
|
|
137
|
+
function register({
|
|
138
|
+
name,
|
|
139
|
+
version,
|
|
140
|
+
createWidget
|
|
141
|
+
}) {
|
|
167
142
|
const merkur = getMerkur();
|
|
168
|
-
|
|
169
143
|
merkur.$in.widgetFactory[name + version] = createWidget;
|
|
170
144
|
}
|
|
171
|
-
|
|
172
145
|
function create(widgetProperties = {}) {
|
|
173
146
|
const merkur = getMerkur();
|
|
174
|
-
const {
|
|
147
|
+
const {
|
|
148
|
+
name,
|
|
149
|
+
version
|
|
150
|
+
} = widgetProperties;
|
|
175
151
|
const factory = merkur.$in.widgetFactory[name + version];
|
|
176
|
-
|
|
177
152
|
if (!isFunction(factory)) {
|
|
178
|
-
throw new Error(
|
|
179
|
-
`The widget with defined name and version "${
|
|
180
|
-
name + version
|
|
181
|
-
}" is not defined.`,
|
|
182
|
-
);
|
|
153
|
+
throw new Error(`The widget with defined name and version "${name + version}" is not defined.`);
|
|
183
154
|
}
|
|
184
|
-
|
|
185
155
|
return factory(widgetProperties);
|
|
186
156
|
}
|
|
187
|
-
|
|
188
|
-
|
|
157
|
+
function createMerkur({
|
|
158
|
+
$plugins = []
|
|
159
|
+
} = {}) {
|
|
189
160
|
const merkur = getMerkur();
|
|
190
|
-
|
|
191
|
-
$plugins.forEach((plugin) => {
|
|
161
|
+
$plugins.forEach(plugin => {
|
|
192
162
|
if (plugin && isFunction(plugin.setup)) {
|
|
193
163
|
plugin.setup(merkur);
|
|
194
164
|
}
|
|
195
165
|
});
|
|
196
|
-
|
|
197
166
|
return merkur;
|
|
198
167
|
}
|
|
199
|
-
|
|
200
168
|
function removeMerkur() {
|
|
201
169
|
const globalContext = getGlobalContext();
|
|
202
|
-
|
|
203
170
|
delete globalContext.__merkur__;
|
|
204
171
|
}
|
|
205
|
-
|
|
206
172
|
function getMerkur() {
|
|
207
173
|
const globalContext = getGlobalContext();
|
|
208
|
-
|
|
209
174
|
if (!globalContext.__merkur__) {
|
|
210
175
|
globalContext.__merkur__ = {
|
|
211
176
|
$in: {
|
|
212
177
|
widgets: [],
|
|
213
|
-
widgetFactory: {}
|
|
178
|
+
widgetFactory: {}
|
|
214
179
|
},
|
|
215
180
|
$external: {},
|
|
216
181
|
$dependencies: {},
|
|
217
182
|
isRegistered,
|
|
218
183
|
register,
|
|
219
|
-
create
|
|
184
|
+
create
|
|
220
185
|
};
|
|
221
186
|
}
|
|
222
|
-
|
|
223
187
|
return globalContext.__merkur__;
|
|
224
188
|
}
|
|
225
|
-
|
|
226
189
|
function isRegistered(name) {
|
|
227
190
|
const merkur = getMerkur();
|
|
228
|
-
|
|
229
|
-
return Boolean(
|
|
230
|
-
Object.keys(merkur.$in.widgetFactory).find((key) =>
|
|
231
|
-
new RegExp(`^${name}`).test(key),
|
|
232
|
-
),
|
|
233
|
-
);
|
|
191
|
+
return Boolean(Object.keys(merkur.$in.widgetFactory).find(key => new RegExp(`^${name}`).test(key)));
|
|
234
192
|
}
|
|
235
|
-
|
|
236
193
|
function getGlobalContext() {
|
|
237
194
|
if (typeof globalThis !== 'undefined') {
|
|
238
195
|
return globalThis;
|
|
@@ -246,7 +203,6 @@ function getGlobalContext() {
|
|
|
246
203
|
if (typeof global !== 'undefined') {
|
|
247
204
|
return global;
|
|
248
205
|
}
|
|
249
|
-
|
|
250
206
|
return {};
|
|
251
207
|
}
|
|
252
208
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merkur/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.0",
|
|
4
4
|
"description": "Merkur is tiny and extensible library for creating front-end microservices.",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"module": "lib/index",
|
|
@@ -26,8 +26,7 @@
|
|
|
26
26
|
"preversion": "npm test",
|
|
27
27
|
"test": "jest --no-watchman -c ./jest.config.js",
|
|
28
28
|
"test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
|
|
29
|
-
"build": "rollup -c rollup.config.mjs"
|
|
30
|
-
"prepare": "npm run build"
|
|
29
|
+
"build": "rollup -c rollup.config.mjs"
|
|
31
30
|
},
|
|
32
31
|
"repository": {
|
|
33
32
|
"type": "git",
|
|
@@ -53,5 +52,5 @@
|
|
|
53
52
|
"access": "public"
|
|
54
53
|
},
|
|
55
54
|
"homepage": "https://merkur.js.org/",
|
|
56
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "8ad2c8b26246850ce6289502a8b05e882f80ce31"
|
|
57
56
|
}
|