neo.mjs 5.13.10 → 5.14.1

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.
@@ -20,6 +20,17 @@ class App extends Base {
20
20
  * @protected
21
21
  */
22
22
  className: 'Neo.worker.App',
23
+ /**
24
+ * Remote method access for other workers
25
+ * @member {Object} remote
26
+ * @protected
27
+ */
28
+ remote: {
29
+ main: [
30
+ 'createNeoInstance',
31
+ 'destroyNeoInstance'
32
+ ]
33
+ },
23
34
  /**
24
35
  * @member {Boolean} singleton=true
25
36
  * @protected
@@ -60,7 +71,7 @@ class App extends Base {
60
71
 
61
72
  // convenience shortcuts
62
73
  Neo.applyDeltas = me.applyDeltas .bind(me);
63
- Neo.setCssVariable = me.setCssVariable.bind(me);
74
+ Neo.setCssVariable = me.setCssVariable.bind(me)
64
75
  }
65
76
 
66
77
  /**
@@ -69,11 +80,63 @@ class App extends Base {
69
80
  * @returns {Promise<*>}
70
81
  */
71
82
  applyDeltas(appName, deltas) {
72
- return this.promiseMessage('main', {
73
- action: 'updateDom',
74
- appName,
75
- deltas
76
- });
83
+ return this.promiseMessage('main', {action: 'updateDom', appName, deltas})
84
+ }
85
+
86
+ /**
87
+ * Remote method to use inside main threads for creating neo based class instances.
88
+ * Be aware that you can only pass configs which can get converted into pure JSON.
89
+ *
90
+ * Rendering a component into the document.body
91
+ * @example:
92
+ * Neo.worker.App.createNeoInstance({
93
+ * ntype : 'button',
94
+ * autoMount : true,
95
+ * autoRender: true
96
+ * text : 'Hi Nige!'
97
+ * }).then(id => console.log(id))
98
+ *
99
+ * Inserting a component into a container
100
+ * @example:
101
+ * Neo.worker.App.createNeoInstance({
102
+ * ntype : 'button',
103
+ * parentId : 'neo-container-3',
104
+ * parentIndex: 0
105
+ * text : 'Hi Nige!'
106
+ * }).then(id => console.log(id))
107
+ *
108
+ * @param {Object} config
109
+ * @param {String} [config.parentId] passing a parentId will put your instance into a container
110
+ * @param {Number} [config.parentIndex] if a parentId is passed, but no index, neo will use add()
111
+ * @returns {String} the instance id
112
+ */
113
+ createNeoInstance(config) {
114
+ let appName = Object.keys(Neo.apps)[0], // fallback in case no appName was provided
115
+ Container = Neo.container?.Base,
116
+ index, instance, parent;
117
+
118
+ config = {appName: appName, ...config};
119
+
120
+ if (config.parentId) {
121
+ parent = Neo.getComponent(config.parentId);
122
+
123
+ if (Container && parent && parent instanceof Container) {
124
+ index = config.parentIndex;
125
+
126
+ delete config.parentId;
127
+ delete config.parentIndex;
128
+
129
+ if (Neo.isNumber(index)) {
130
+ instance = parent.insert(index, config)
131
+ } else {
132
+ instance = parent.add(config)
133
+ }
134
+ }
135
+ } else {
136
+ instance = Neo[config.ntype ? 'ntype' : 'create'](config)
137
+ }
138
+
139
+ return instance.id
77
140
  }
78
141
 
79
142
  /**
@@ -82,7 +145,37 @@ class App extends Base {
82
145
  createThemeMap(data) {
83
146
  Neo.ns('Neo.cssMap.fileInfo', true);
84
147
  Neo.cssMap.fileInfo = data;
85
- this.resolveThemeFilesCache();
148
+ this.resolveThemeFilesCache()
149
+ }
150
+
151
+ /**
152
+ * Remote method to use inside main threads for destroying neo based class instances.
153
+ *
154
+ * @example:
155
+ * Neo.worker.App.destroyNeoInstance('neo-button-3').then(success => console.log(success))
156
+ *
157
+ * @param {String} id
158
+ * @returns {Boolean} returns true, in case the instance was found
159
+ */
160
+ destroyNeoInstance(id) {
161
+ let instance = Neo.get(id),
162
+ parent;
163
+
164
+ if (instance) {
165
+ if (instance.parentId) {
166
+ parent = Neo.getComponent(instance.parentId);
167
+
168
+ if (parent) {
169
+ parent.remove(instance);
170
+ return true
171
+ }
172
+ }
173
+
174
+ instance.destroy(true, true);
175
+ return true
176
+ }
177
+
178
+ return false
86
179
  }
87
180
 
88
181
  /**
@@ -92,8 +185,8 @@ class App extends Base {
92
185
  */
93
186
  fireMainViewsEvent(eventName, data) {
94
187
  this.ports.forEach(port => {
95
- Neo.apps[port.appName].mainViewInstance.fire(eventName, data);
96
- });
188
+ Neo.apps[port.appName].mainViewInstance.fire(eventName, data)
189
+ })
97
190
  }
98
191
 
99
192
  /**
@@ -102,7 +195,7 @@ class App extends Base {
102
195
  */
103
196
  importApp(path) {
104
197
  if (path.endsWith('.mjs')) {
105
- path = path.slice(0, -4);
198
+ path = path.slice(0, -4)
106
199
  }
107
200
 
108
201
  return import(
@@ -110,7 +203,7 @@ class App extends Base {
110
203
  /* webpackExclude: /[\\\/]node_modules/ */
111
204
  /* webpackMode: "lazy" */
112
205
  `../../${path}.mjs`
113
- );
206
+ )
114
207
  }
115
208
 
116
209
  /**
@@ -130,7 +223,7 @@ class App extends Base {
130
223
  classPath, classRoot, fileName, mapClassName, ns, themeFolders;
131
224
 
132
225
  if (!cssMap) {
133
- me.themeFilesCache.push([appName, proto]);
226
+ me.themeFilesCache.push([appName, proto])
134
227
  } else {
135
228
  // we need to modify app related class names
136
229
  if (!className.startsWith('Neo.')) {
@@ -140,12 +233,12 @@ class App extends Base {
140
233
  className[0] === 'view' && className.shift();
141
234
 
142
235
  mapClassName = `apps.${Neo.apps[appName].appThemeFolder || classRoot}.${className.join('.')}`;
143
- className = `apps.${lAppName}.${className.join('.')}`;
236
+ className = `apps.${lAppName}.${className.join('.')}`
144
237
  }
145
238
 
146
239
  if (parent && parent !== Neo.core.Base.prototype) {
147
240
  if (!Neo.ns(`${lAppName}.${parent.className}`, false, cssMap)) {
148
- me.insertThemeFiles(appName, parent);
241
+ me.insertThemeFiles(appName, parent)
149
242
  }
150
243
  }
151
244
 
@@ -163,7 +256,7 @@ class App extends Base {
163
256
  appName,
164
257
  className: mapClassName || className,
165
258
  folders : themeFolders
166
- });
259
+ })
167
260
  }
168
261
  }
169
262
  }
@@ -174,7 +267,7 @@ class App extends Base {
174
267
  * @param {Object} data useful event properties, differs for different event types. See Neo.main.DomEvents.
175
268
  */
176
269
  onDomEvent(data) {
177
- DomEventManager.fire(data);
270
+ DomEventManager.fire(data)
178
271
  }
179
272
 
180
273
  /**
@@ -182,7 +275,7 @@ class App extends Base {
182
275
  * @param {Object} data parsed key-value pairs for each hash value
183
276
  */
184
277
  onHashChange(data) {
185
- HashHistory.push(data.data);
278
+ HashHistory.push(data.data)
186
279
  }
187
280
 
188
281
  /**
@@ -192,25 +285,25 @@ class App extends Base {
192
285
  onLoadApplication(data) {
193
286
  let me = this,
194
287
  config = Neo.config,
195
- path;
288
+ app, path;
196
289
 
197
290
  if (data) {
198
291
  me.data = data;
199
- config.resourcesPath = data.resourcesPath;
292
+ config.resourcesPath = data.resourcesPath
200
293
  }
201
294
 
202
295
  path = me.data.path;
203
296
 
204
297
  if (config.environment !== 'development') {
205
- path = path.startsWith('/') ? path.substring(1) : path;
298
+ path = path.startsWith('/') ? path.substring(1) : path
206
299
  }
207
300
 
208
301
  me.importApp(path).then(module => {
209
- module.onStart();
302
+ app = module.onStart();
210
303
 
211
304
  // short delay to ensure Component Controllers are ready
212
- config.hash && setTimeout(() => HashHistory.push(config.hash), 5);
213
- });
305
+ config.hash && setTimeout(() => HashHistory.push(config.hash), 5)
306
+ })
214
307
  }
215
308
 
216
309
  /**
@@ -223,15 +316,15 @@ class App extends Base {
223
316
  url = `resources/theme-map${config.useCssVars ? '' : '-no-vars'}.json`;
224
317
 
225
318
  if (config.environment === 'development') {
226
- url = `../../${url}`;
319
+ url = `../../${url}`
227
320
  }
228
321
 
229
322
  if (config.workerBasePath?.includes('node_modules')) {
230
- url = `../../${url}`;
323
+ url = `../../${url}`
231
324
  }
232
325
 
233
326
  if (url[0] !== '.') {
234
- url = `./${url}`;
327
+ url = `./${url}`
235
328
  }
236
329
 
237
330
  fetch(url)
@@ -239,7 +332,7 @@ class App extends Base {
239
332
  .then(data => {this.createThemeMap(data)});
240
333
 
241
334
  config.remotesApiUrl && import('../remotes/Api.mjs').then(module => module.default.load());
242
- !config.useVdomWorker && import('../vdom/Helper.mjs');
335
+ !config.useVdomWorker && import('../vdom/Helper.mjs')
243
336
  }
244
337
 
245
338
  /**
@@ -251,14 +344,14 @@ class App extends Base {
251
344
 
252
345
  port.onmessage = me.onMessage.bind(me);
253
346
 
254
- me.channelPorts[msg.origin] = port;
347
+ me.channelPorts[msg.origin] = port
255
348
  }
256
349
 
257
350
  /**
258
351
  * @param {Object} data
259
352
  */
260
353
  onWindowPositionChange(data) {
261
- this.fireMainViewsEvent('windowPositionChange', data.data);
354
+ this.fireMainViewsEvent('windowPositionChange', data.data)
262
355
  }
263
356
 
264
357
  /**
@@ -268,11 +361,7 @@ class App extends Base {
268
361
  registerApp(appName) {
269
362
  // register the name as fast as possible
270
363
  this.onRegisterApp({ appName });
271
-
272
- this.sendMessage('main', {
273
- action: 'registerAppName',
274
- appName
275
- });
364
+ this.sendMessage('main', {action: 'registerAppName', appName})
276
365
  }
277
366
 
278
367
  /**
@@ -281,7 +370,7 @@ class App extends Base {
281
370
  * @param {String} appName
282
371
  */
283
372
  removeAppFromThemeMap(appName) {
284
- delete Neo.cssMap[appName.toLowerCase()];
373
+ delete Neo.cssMap[appName.toLowerCase()]
285
374
  }
286
375
 
287
376
  /**
@@ -291,10 +380,10 @@ class App extends Base {
291
380
  let me = this;
292
381
 
293
382
  me.themeFilesCache.forEach(item => {
294
- me.insertThemeFiles(...item);
383
+ me.insertThemeFiles(...item)
295
384
  });
296
385
 
297
- me.themeFilesCache = [];
386
+ me.themeFilesCache = []
298
387
  }
299
388
 
300
389
  /**
@@ -310,13 +399,13 @@ class App extends Base {
310
399
  theme = Neo.config.themes?.[0];
311
400
 
312
401
  if (!addon) {
313
- return Promise.reject('Neo.main.addon.Stylesheet not imported');
402
+ return Promise.reject('Neo.main.addon.Stylesheet not imported')
314
403
  } else {
315
404
  if (theme.startsWith('neo-')) {
316
405
  theme = theme.substring(4);
317
406
  }
318
407
 
319
- return addon.setCssVariable({theme, ...data});
408
+ return addon.setCssVariable({theme, ...data})
320
409
  }
321
410
  }
322
411
  }