@soleil-se/app-util 1.1.2 → 1.1.3
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 +12 -10
- package/dist/index.js +12 -11
- package/package.json +1 -1
- package/render-vue/index.js +3 -2
- package/render-vue/src/index.js +3 -2
- package/src/index.js +19 -18
package/README.md
CHANGED
|
@@ -4,20 +4,21 @@
|
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
```javascript
|
|
7
|
-
import {
|
|
7
|
+
import { renderApp, getRouteUri, getResourceUri, renderTemplate, isOffline } from '@soleil-se/webapp-util';
|
|
8
8
|
```
|
|
9
|
-
### `
|
|
10
|
-
Get HTML for rendering
|
|
11
|
-
To be used together with `@soleil-se/webapp-util/render-vue` in the client code.
|
|
12
|
-
|
|
9
|
+
### `renderApp(name, data, [noScript])` ⇒ `String`
|
|
10
|
+
Get HTML string for rendering an application.
|
|
11
|
+
To be used together with `@soleil-se/webapp-util/render-vue` or a custom render function in the client code.
|
|
12
|
+
|
|
13
|
+
**Returns**: <code>String</code> - HTML for rendering an application.
|
|
13
14
|
|
|
14
15
|
| Param | Type | Default | Description |
|
|
15
16
|
| --- | --- | --- | --- |
|
|
16
|
-
| name | <code>String</code> | | Name of the app, has to match the name of the
|
|
17
|
-
| data | <code>Object</code> | |
|
|
17
|
+
| name | <code>String</code> | | Name of the app, has to match the name of the application. |
|
|
18
|
+
| data | <code>Object</code> | | Server data that will be available in the app. In Vue it will be available in the `$options` object. |
|
|
18
19
|
| settings | <code>Object</code> | | Settings object. |
|
|
19
20
|
| [settings.noScript] | <code>String</code> | <code>''</code> | HTML that will be rendered when JavaScript is not available. |
|
|
20
|
-
| [settings.selector] | <code>String</code> | <code>`[data-
|
|
21
|
+
| [settings.selector] | <code>String</code> | <code>`[data-portlet-id="${portletId}"]`</code> | Query selector for where the app should be mounted. |
|
|
21
22
|
| [settings.async] | <code>Boolean</code> | <code>true</code> | If the app script should be loaded asynchronously. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
22
23
|
| [settings.defer] | <code>Boolean</code> | <code>false</code> | If the app script should be loaded after DOM is ready. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
23
24
|
|
|
@@ -26,10 +27,10 @@ In `app_src/server/index.js`.
|
|
|
26
27
|
```javascript
|
|
27
28
|
router.get('/', (req, res) => {
|
|
28
29
|
// Most common usage.
|
|
29
|
-
res.send(
|
|
30
|
+
res.send(renderApp('AppName', data));
|
|
30
31
|
|
|
31
32
|
// With all settings.
|
|
32
|
-
res.send(
|
|
33
|
+
res.send(renderApp('AppName', data, {
|
|
33
34
|
noScript: 'You need JS for this!',
|
|
34
35
|
selector: '#mount_me_here',
|
|
35
36
|
async: false,
|
|
@@ -37,6 +38,7 @@ router.get('/', (req, res) => {
|
|
|
37
38
|
}));
|
|
38
39
|
});
|
|
39
40
|
```
|
|
41
|
+
Vue example.
|
|
40
42
|
In `app_src/vue/index.js`.
|
|
41
43
|
```javascript
|
|
42
44
|
import render from '@soleil-se/webapp-util/render-vue';
|
package/dist/index.js
CHANGED
|
@@ -36,34 +36,35 @@ export function renderTemplate(template, values) {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
* Get HTML for rendering
|
|
40
|
-
* @param {String} name Name of the app, has to match the name of the
|
|
41
|
-
* @param {Object} data
|
|
39
|
+
* Get HTML string for rendering an application.
|
|
40
|
+
* @param {String} name Name of the app, has to match the name of the application.
|
|
41
|
+
* @param {Object} data Server data that will be available in the app.
|
|
42
|
+
* In Vue it will be available in the `$options` object.
|
|
42
43
|
* @param {Object} settings Settings object.
|
|
43
44
|
* @param {String} [settings.noScript=''] HTML that will be rendered when JavaScript
|
|
44
45
|
* is not available.
|
|
45
|
-
* @param {String} [settings.selector=`[data-
|
|
46
|
+
* @param {String} [settings.selector=`[data-portlet-id="${portletId}"]`] Query selector for
|
|
46
47
|
* where the app should be mounted.
|
|
47
48
|
* @param {Boolean} [settings.async=true] If the app script should be loaded asynchronously.
|
|
48
49
|
* [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
|
|
49
50
|
* @param {Boolean} [settings.defer=false] If the app script should be loaded after DOM is ready.
|
|
50
51
|
* [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
|
|
51
|
-
* @returns {String} HTML for rendering
|
|
52
|
+
* @returns {String} HTML for rendering an application.
|
|
52
53
|
*/
|
|
53
|
-
export function
|
|
54
|
+
export function renderApp(name, data, ref) {
|
|
54
55
|
if ( ref === void 0 ) ref = {};
|
|
55
56
|
var noScript = ref.noScript; if ( noScript === void 0 ) noScript = '';
|
|
56
|
-
var selector = ref.selector; if ( selector === void 0 ) selector = "[data-
|
|
57
|
+
var selector = ref.selector; if ( selector === void 0 ) selector = "[data-portlet-id=\"" + portletId + "\"]";
|
|
57
58
|
var async = ref.async; if ( async === void 0 ) async = true;
|
|
58
59
|
var defer = ref.defer; if ( defer === void 0 ) defer = false;
|
|
59
60
|
|
|
60
61
|
var options = Object.assign({}, {webapp: app, isOffline: isOffline}, data);
|
|
61
|
-
var
|
|
62
|
+
var clientUri = getResourceUri('client/index.js');
|
|
62
63
|
|
|
63
64
|
if (isOffline) {
|
|
64
|
-
return ("\n<!-- Generated by WebappUtil.
|
|
65
|
+
return ("\n<!-- Generated by WebappUtil.renderApp() -->\n<div data-portlet-id=\"" + portletId + "\">" + noScript + "</div>\n<script src=\"" + clientUri + "\"></script>\n<script> \n Soleil.webapps['" + name + "'].render('" + name + "', '" + selector + "', " + (JSON.stringify(options)) + ");\n</script>\n");
|
|
65
66
|
}
|
|
66
|
-
return ("\n<!-- Generated by WebappUtil.
|
|
67
|
+
return ("\n<!-- Generated by WebappUtil.renderApp() -->\n<div data-portlet-id=\"" + portletId + "\">" + noScript + "</div>\n<script>\n function renderApp" + portletId + "() {\n Soleil.webapps['" + name + "'].render('" + name + "', '" + selector + "', " + (JSON.stringify(options)) + ");\n } \n</script>\n<script src=\"" + clientUri + "\" " + (async && !defer ? 'async' : '') + " " + (defer ? 'defer' : '') + " onload=\"renderApp" + portletId + "();\"></script>\n");
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
/**
|
|
@@ -84,7 +85,7 @@ export function getRouteUri(route) {
|
|
|
84
85
|
export default {
|
|
85
86
|
getRouteUri: getRouteUri,
|
|
86
87
|
getResourceUri: getResourceUri,
|
|
87
|
-
|
|
88
|
+
renderApp: renderApp,
|
|
88
89
|
renderTemplate: renderTemplate,
|
|
89
90
|
isOffline: isOffline,
|
|
90
91
|
};
|
package/package.json
CHANGED
package/render-vue/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* eslint-disable-next-line import/no-unresolved */
|
|
2
2
|
import Vue from 'vue';
|
|
3
3
|
|
|
4
|
-
window.
|
|
4
|
+
window.Soleil = window.Soleil || {};
|
|
5
|
+
window.Soleil.webapps = window.Soleil.webapps || {};
|
|
5
6
|
|
|
6
7
|
var offlineModeMixin = {
|
|
7
8
|
mounted: function mounted() {
|
|
@@ -25,7 +26,7 @@ var offlineModeMixin = {
|
|
|
25
26
|
export default function render(name, selector, options) {
|
|
26
27
|
if ( options === void 0 ) options = {};
|
|
27
28
|
|
|
28
|
-
var ref = window.
|
|
29
|
+
var ref = window.Soleil.webapps[name];
|
|
29
30
|
var App = ref.App;
|
|
30
31
|
Object.assign(App, options, {
|
|
31
32
|
mixins: [offlineModeMixin],
|
package/render-vue/src/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* eslint-disable-next-line import/no-unresolved */
|
|
2
2
|
import Vue from 'vue';
|
|
3
3
|
|
|
4
|
-
window.
|
|
4
|
+
window.Soleil = window.Soleil || {};
|
|
5
|
+
window.Soleil.webapps = window.Soleil.webapps || {};
|
|
5
6
|
|
|
6
7
|
const offlineModeMixin = {
|
|
7
8
|
mounted() {
|
|
@@ -25,7 +26,7 @@ const offlineModeMixin = {
|
|
|
25
26
|
export default function render(name, selector, options = {}) {
|
|
26
27
|
const {
|
|
27
28
|
App,
|
|
28
|
-
} = window.
|
|
29
|
+
} = window.Soleil.webapps[name];
|
|
29
30
|
Object.assign(App, options, {
|
|
30
31
|
mixins: [offlineModeMixin],
|
|
31
32
|
});
|
package/src/index.js
CHANGED
|
@@ -34,48 +34,49 @@ export function renderTemplate(template, values = {}) {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
* Get HTML for rendering
|
|
38
|
-
* @param {String} name Name of the app, has to match the name of the
|
|
39
|
-
* @param {Object} data
|
|
37
|
+
* Get HTML string for rendering an application.
|
|
38
|
+
* @param {String} name Name of the app, has to match the name of the application.
|
|
39
|
+
* @param {Object} data Server data that will be available in the app.
|
|
40
|
+
* In Vue it will be available in the `$options` object.
|
|
40
41
|
* @param {Object} settings Settings object.
|
|
41
42
|
* @param {String} [settings.noScript=''] HTML that will be rendered when JavaScript
|
|
42
43
|
* is not available.
|
|
43
|
-
* @param {String} [settings.selector=`[data-
|
|
44
|
+
* @param {String} [settings.selector=`[data-portlet-id="${portletId}"]`] Query selector for
|
|
44
45
|
* where the app should be mounted.
|
|
45
46
|
* @param {Boolean} [settings.async=true] If the app script should be loaded asynchronously.
|
|
46
47
|
* [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
|
|
47
48
|
* @param {Boolean} [settings.defer=false] If the app script should be loaded after DOM is ready.
|
|
48
49
|
* [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
|
|
49
|
-
* @returns {String} HTML for rendering
|
|
50
|
+
* @returns {String} HTML for rendering an application.
|
|
50
51
|
*/
|
|
51
|
-
export function
|
|
52
|
+
export function renderApp(name, data, {
|
|
52
53
|
noScript = '',
|
|
53
|
-
selector = `[data-
|
|
54
|
+
selector = `[data-portlet-id="${portletId}"]`,
|
|
54
55
|
async = true,
|
|
55
56
|
defer = false,
|
|
56
57
|
} = {}) {
|
|
57
58
|
const options = { webapp: app, isOffline, ...data };
|
|
58
|
-
const
|
|
59
|
+
const clientUri = getResourceUri('client/index.js');
|
|
59
60
|
|
|
60
61
|
if (isOffline) {
|
|
61
62
|
return `
|
|
62
|
-
<!-- Generated by WebappUtil.
|
|
63
|
-
<div data-
|
|
64
|
-
<script src="${
|
|
63
|
+
<!-- Generated by WebappUtil.${this.name}() -->
|
|
64
|
+
<div data-portlet-id="${portletId}">${noScript}</div>
|
|
65
|
+
<script src="${clientUri}"></script>
|
|
65
66
|
<script>
|
|
66
|
-
|
|
67
|
+
Soleil.webapps['${name}'].render('${name}', '${selector}', ${JSON.stringify(options)});
|
|
67
68
|
</script>
|
|
68
69
|
`;
|
|
69
70
|
}
|
|
70
71
|
return `
|
|
71
|
-
<!-- Generated by WebappUtil.
|
|
72
|
-
<div data-
|
|
72
|
+
<!-- Generated by WebappUtil.${this.name}() -->
|
|
73
|
+
<div data-portlet-id="${portletId}">${noScript}</div>
|
|
73
74
|
<script>
|
|
74
|
-
function
|
|
75
|
-
|
|
75
|
+
function renderApp${portletId}() {
|
|
76
|
+
Soleil.webapps['${name}'].render('${name}', '${selector}', ${JSON.stringify(options)});
|
|
76
77
|
}
|
|
77
78
|
</script>
|
|
78
|
-
<script src="${
|
|
79
|
+
<script src="${clientUri}" ${async && !defer ? 'async' : ''} ${defer ? 'defer' : ''} onload="renderApp${portletId}();"></script>
|
|
79
80
|
`;
|
|
80
81
|
}
|
|
81
82
|
|
|
@@ -97,7 +98,7 @@ export function getRouteUri(route) {
|
|
|
97
98
|
export default {
|
|
98
99
|
getRouteUri,
|
|
99
100
|
getResourceUri,
|
|
100
|
-
|
|
101
|
+
renderApp,
|
|
101
102
|
renderTemplate,
|
|
102
103
|
isOffline,
|
|
103
104
|
};
|