@soleil-se/app-util 1.2.2 → 2.0.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 +108 -108
- package/package.json +6 -8
- package/render-vue/{src/.eslintrc → .eslintrc} +3 -3
- package/render-vue/index.js +50 -43
- package/src/index.js +101 -99
- package/dist/index.js +0 -86
- package/render-vue/src/index.js +0 -42
package/README.md
CHANGED
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
# Webapp Util
|
|
2
|
-
Utility functions for Webapps.
|
|
3
|
-
## Install
|
|
4
|
-
`yarn add @soleil-se/webapp-util`
|
|
5
|
-
|
|
6
|
-
##
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
In `
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
```
|
|
59
|
-
### `getRouteUri(route)` ⇒ `String`
|
|
60
|
-
Get URI for a route.
|
|
61
|
-
|
|
62
|
-
**Returns**: `String` - URI for route.
|
|
63
|
-
|
|
64
|
-
| Param | Type | Description |
|
|
65
|
-
| --- | --- | --- |
|
|
66
|
-
| route | `String` | A route. |
|
|
67
|
-
|
|
68
|
-
#### Example
|
|
69
|
-
```javascript
|
|
70
|
-
const routeUri = getRouteUri('/my-route');
|
|
71
|
-
```
|
|
72
|
-
### `getResourceUri(resource)` ⇒ `String`
|
|
73
|
-
Get URI for a resource.
|
|
74
|
-
|
|
75
|
-
**Returns**: `String` - URI for a resource.
|
|
76
|
-
|
|
77
|
-
| Param | Type | Description |
|
|
78
|
-
| --- | --- | --- |
|
|
79
|
-
| resource | `String` | A resource. |
|
|
80
|
-
|
|
81
|
-
#### Example
|
|
82
|
-
```javascript
|
|
83
|
-
const resourceUri = getResourceUri('file/in/resource.png');
|
|
84
|
-
```
|
|
85
|
-
### `renderTemplate(template, [values])` ⇒ `String`
|
|
86
|
-
Renders a Underscore template and returns a string.
|
|
87
|
-
|
|
88
|
-
**Returns**: `String` - Rendered template
|
|
89
|
-
|
|
90
|
-
| Param | Type | Default | Description |
|
|
91
|
-
| --- | --- | --- | --- |
|
|
92
|
-
| template | `String` | | Underscore template. |
|
|
93
|
-
| [values] | `Object` | `{}` | Values. |
|
|
94
|
-
|
|
95
|
-
#### Example
|
|
96
|
-
```javascript
|
|
97
|
-
const string = renderTemplate('<div><%= foo %></div>', {
|
|
98
|
-
foo: 'bar',
|
|
99
|
-
});
|
|
100
|
-
```
|
|
101
|
-
### `isOffline`
|
|
102
|
-
If the webapp is running in offline mode or not.
|
|
103
|
-
|
|
104
|
-
```javascript
|
|
105
|
-
if(isOffline) {
|
|
106
|
-
// Do something
|
|
107
|
-
}
|
|
108
|
-
```
|
|
1
|
+
# Webapp Util
|
|
2
|
+
Utility functions for Webapps.
|
|
3
|
+
## Install
|
|
4
|
+
`yarn add @soleil-se/webapp-util`
|
|
5
|
+
|
|
6
|
+
## Migration 1.x.x to 2.x.x
|
|
7
|
+
|
|
8
|
+
Now using `currentScript` to reference the script element the app is running in to pass data.
|
|
9
|
+
|
|
10
|
+
Remove appName from the `renderApp` call in `./app_src/server/index.js`.
|
|
11
|
+
Remove export of `App` and `render` and replace with a call to `render(App)` in `./app_src/client/index.js`.
|
|
12
|
+
|
|
13
|
+
See [example](#example).
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
```javascript
|
|
17
|
+
import { renderApp, getRouteUri, getResourceUri, renderTemplate, isOffline } from '@soleil-se/webapp-util';
|
|
18
|
+
```
|
|
19
|
+
### `renderApp([config], [settings])` ⇒ `String`
|
|
20
|
+
Get HTML string for rendering an application.
|
|
21
|
+
Can be used together with `@soleil-se/webapp-util/render-vue` or a custom render function in the client code.
|
|
22
|
+
|
|
23
|
+
**Returns**: <code>String</code> - HTML for rendering an application.
|
|
24
|
+
|
|
25
|
+
| Param | Type | Default | Description |
|
|
26
|
+
| --- | --- | --- | --- |
|
|
27
|
+
| [config] | <code>Object</code> | <code>{}</code> | Server config or data that will be available in the attribute `data-app` on currentScript. In Vue it will also be available in the `$options` object. |
|
|
28
|
+
| [settings] | <code>Object</code> | <code>{}</code> | Settings object. |
|
|
29
|
+
| [settings.noScript] | <code>String</code> | <code>''</code> | HTML that will be rendered when JavaScript is not available. |
|
|
30
|
+
| [settings.selector] | <code>String</code> | <code>`[data-portlet-id="${portletId}"]`</code> | Query selector for where the app should be mounted. |
|
|
31
|
+
| [settings.async] | <code>Boolean</code> | <code>false</code> | If the app script should be loaded asynchronously. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
32
|
+
| [settings.defer] | <code>Boolean</code> | <code>true</code> | If the app script should be loaded after DOM is ready. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
33
|
+
|
|
34
|
+
#### Example
|
|
35
|
+
In `app_src/server/index.js`.
|
|
36
|
+
```javascript
|
|
37
|
+
router.get('/', (req, res) => {
|
|
38
|
+
const data = { foo: 'bar' };
|
|
39
|
+
// Most common usage.
|
|
40
|
+
res.send(renderApp(data));
|
|
41
|
+
|
|
42
|
+
// With all settings.
|
|
43
|
+
res.send(renderApp(data, {
|
|
44
|
+
noScript: 'You need JS for this!',
|
|
45
|
+
selector: '#mount_me_here',
|
|
46
|
+
async: false,
|
|
47
|
+
defer: true,
|
|
48
|
+
}));
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
##### **Vue**
|
|
52
|
+
In `app_src/client/index.js`.
|
|
53
|
+
```javascript
|
|
54
|
+
import render from '@soleil-se/webapp-util/render-vue';
|
|
55
|
+
import App from './App.vue';
|
|
56
|
+
|
|
57
|
+
render(App);
|
|
58
|
+
```
|
|
59
|
+
### `getRouteUri(route)` ⇒ `String`
|
|
60
|
+
Get URI for a route.
|
|
61
|
+
|
|
62
|
+
**Returns**: `String` - URI for route.
|
|
63
|
+
|
|
64
|
+
| Param | Type | Description |
|
|
65
|
+
| --- | --- | --- |
|
|
66
|
+
| route | `String` | A route. |
|
|
67
|
+
|
|
68
|
+
#### Example
|
|
69
|
+
```javascript
|
|
70
|
+
const routeUri = getRouteUri('/my-route');
|
|
71
|
+
```
|
|
72
|
+
### `getResourceUri(resource)` ⇒ `String`
|
|
73
|
+
Get URI for a resource.
|
|
74
|
+
|
|
75
|
+
**Returns**: `String` - URI for a resource.
|
|
76
|
+
|
|
77
|
+
| Param | Type | Description |
|
|
78
|
+
| --- | --- | --- |
|
|
79
|
+
| resource | `String` | A resource. |
|
|
80
|
+
|
|
81
|
+
#### Example
|
|
82
|
+
```javascript
|
|
83
|
+
const resourceUri = getResourceUri('file/in/resource.png');
|
|
84
|
+
```
|
|
85
|
+
### `renderTemplate(template, [values])` ⇒ `String`
|
|
86
|
+
Renders a Underscore template and returns a string.
|
|
87
|
+
|
|
88
|
+
**Returns**: `String` - Rendered template
|
|
89
|
+
|
|
90
|
+
| Param | Type | Default | Description |
|
|
91
|
+
| --- | --- | --- | --- |
|
|
92
|
+
| template | `String` | | Underscore template. |
|
|
93
|
+
| [values] | `Object` | `{}` | Values. |
|
|
94
|
+
|
|
95
|
+
#### Example
|
|
96
|
+
```javascript
|
|
97
|
+
const string = renderTemplate('<div><%= foo %></div>', {
|
|
98
|
+
foo: 'bar',
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
### `isOffline`
|
|
102
|
+
If the webapp is running in offline mode or not.
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
if(isOffline) {
|
|
106
|
+
// Do something
|
|
107
|
+
}
|
|
108
|
+
```
|
package/package.json
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soleil-se/app-util",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Utility functions for Webapps.",
|
|
5
|
-
"main": "./
|
|
5
|
+
"main": "./src/index.js",
|
|
6
6
|
"author": "Soleil AB",
|
|
7
7
|
"contributors": [
|
|
8
|
-
"Kimmy Monassar"
|
|
8
|
+
"Kimmy Monassar",
|
|
9
|
+
"Jonas Gällman"
|
|
9
10
|
],
|
|
10
11
|
"license": "UNLICENSED",
|
|
11
12
|
"private": false,
|
|
12
13
|
"homepage": "https://github.com/soleilit/server-monorepo/tree/master/packages/app-util",
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"build-server": "npx buble ./src/index.js --output ./dist/index.js --no modules,arrow --objectAssign=Object.assign",
|
|
16
|
-
"build-render-vue": "npx buble ./render-vue/src/index.js --output ./render-vue/index.js --no modules --objectAssign=Object.assign"
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"current-script-polyfill": "^1.0.0"
|
|
17
16
|
},
|
|
18
|
-
"dependencies": {},
|
|
19
17
|
"devDependencies": {}
|
|
20
18
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "@soleil/eslint-config-sitevision/client"
|
|
3
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"extends": "@soleil/eslint-config-sitevision/client"
|
|
3
|
+
}
|
package/render-vue/index.js
CHANGED
|
@@ -1,43 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
import Vue from 'vue';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
1
|
+
import 'current-script-polyfill';
|
|
2
|
+
import Vue from 'vue';
|
|
3
|
+
|
|
4
|
+
const { currentScript } = document;
|
|
5
|
+
|
|
6
|
+
const decodeAttribute = (attribute) => {
|
|
7
|
+
const encoded = currentScript.getAttribute(attribute);
|
|
8
|
+
if (encoded) {
|
|
9
|
+
return decodeURIComponent(encoded);
|
|
10
|
+
}
|
|
11
|
+
return undefined;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const parseAttribute = (attribute) => JSON.parse(decodeAttribute(attribute));
|
|
15
|
+
|
|
16
|
+
const offlineModeMixin = {
|
|
17
|
+
mounted() {
|
|
18
|
+
if (this.$options.isOffline) {
|
|
19
|
+
const $anchors = this.$el.querySelectorAll('a');
|
|
20
|
+
$anchors.forEach(($anchor) => {
|
|
21
|
+
$anchor.addEventListener('click', (e) => {
|
|
22
|
+
e.preventDefault();
|
|
23
|
+
window.$svjq($anchor).trigger('click');
|
|
24
|
+
}, false);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Render a Vue application
|
|
31
|
+
* @param {Vue} App Vue application to be started.
|
|
32
|
+
*/
|
|
33
|
+
export default function render(App) {
|
|
34
|
+
const selector = decodeAttribute('data-selector');
|
|
35
|
+
const options = parseAttribute('data-app');
|
|
36
|
+
|
|
37
|
+
Object.assign(App, options, {
|
|
38
|
+
mixins: [offlineModeMixin],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const mountElementExists = !!document.querySelector(selector);
|
|
42
|
+
|
|
43
|
+
if (mountElementExists) {
|
|
44
|
+
new Vue(App).$mount(selector);
|
|
45
|
+
} else {
|
|
46
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
47
|
+
new Vue(App).$mount(selector);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,99 +1,101 @@
|
|
|
1
|
-
import PortletContextUtil from 'PortletContextUtil';
|
|
2
|
-
import VersionUtil from 'VersionUtil';
|
|
3
|
-
import appInfo from 'appInfo';
|
|
4
|
-
/* Underscore is provided by SiteVision */
|
|
5
|
-
/* eslint-disable-next-line import/no-extraneous-dependencies */
|
|
6
|
-
import _ from 'underscore';
|
|
7
|
-
|
|
8
|
-
/** If the webapp is running in offline mode or not. */
|
|
9
|
-
export const isOffline = VersionUtil.getCurrentVersion() === VersionUtil.OFFLINE_VERSION;
|
|
10
|
-
|
|
11
|
-
const portletId = PortletContextUtil.getCurrentPortlet().getIdentifier().replace('.', '_');
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Get URI for a resource.
|
|
15
|
-
* @param {String} resource A resource.
|
|
16
|
-
* @returns {String} URI for a resource.
|
|
17
|
-
*/
|
|
18
|
-
export function getResourceUri(resource) {
|
|
19
|
-
return `/webapp-files/${appInfo.appIdentifier}/${appInfo.appVersion}/${resource}`;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Renders a Underscore template and returns a string.
|
|
24
|
-
* @param {String} template Underscore template.
|
|
25
|
-
* @param {Object} [values={}] Values.
|
|
26
|
-
* @returns {String} Rendered template
|
|
27
|
-
*/
|
|
28
|
-
export function renderTemplate(template, values = {}) {
|
|
29
|
-
if (typeof template === 'function') {
|
|
30
|
-
return template(values);
|
|
31
|
-
}
|
|
32
|
-
return _.template(template)(values);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Get HTML string for rendering an application.
|
|
37
|
-
* @param {
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* @param {
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* [
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
*
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
1
|
+
import PortletContextUtil from 'PortletContextUtil';
|
|
2
|
+
import VersionUtil from 'VersionUtil';
|
|
3
|
+
import appInfo from 'appInfo';
|
|
4
|
+
/* Underscore is provided by SiteVision */
|
|
5
|
+
/* eslint-disable-next-line import/no-extraneous-dependencies */
|
|
6
|
+
import _ from 'underscore';
|
|
7
|
+
|
|
8
|
+
/** If the webapp is running in offline mode or not. */
|
|
9
|
+
export const isOffline = VersionUtil.getCurrentVersion() === VersionUtil.OFFLINE_VERSION;
|
|
10
|
+
|
|
11
|
+
const portletId = PortletContextUtil.getCurrentPortlet().getIdentifier().replace('.', '_');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get URI for a resource.
|
|
15
|
+
* @param {String} resource A resource.
|
|
16
|
+
* @returns {String} URI for a resource.
|
|
17
|
+
*/
|
|
18
|
+
export function getResourceUri(resource) {
|
|
19
|
+
return `/webapp-files/${appInfo.appIdentifier}/${appInfo.appVersion}/${resource}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Renders a Underscore template and returns a string.
|
|
24
|
+
* @param {String} template Underscore template.
|
|
25
|
+
* @param {Object} [values={}] Values.
|
|
26
|
+
* @returns {String} Rendered template
|
|
27
|
+
*/
|
|
28
|
+
export function renderTemplate(template, values = {}) {
|
|
29
|
+
if (typeof template === 'function') {
|
|
30
|
+
return template(values);
|
|
31
|
+
}
|
|
32
|
+
return _.template(template)(values);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Get a HTML string for rendering an application.
|
|
37
|
+
* @param {Object} [data={}] Server config or data that will be available in the attribute
|
|
38
|
+
* `data-app` on currentScript. In Vue it will also be available in the `$options` object.
|
|
39
|
+
* @param {Object} [settings={}] Settings object.
|
|
40
|
+
* @param {String} [settings.noScript=''] HTML that will be rendered when JavaScript
|
|
41
|
+
* is not available.
|
|
42
|
+
* @param {String} [settings.selector=`[data-portlet-id="${portletId}"]`] Query selector for
|
|
43
|
+
* where the app should be mounted.
|
|
44
|
+
* @param {Boolean} [settings.async=false] If the app script should be loaded asynchronously.
|
|
45
|
+
* [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
|
|
46
|
+
* @param {Boolean} [settings.defer=true] If the app script should be loaded after DOM is ready.
|
|
47
|
+
* [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
|
|
48
|
+
* @returns {String} HTML for rendering an application.
|
|
49
|
+
*/
|
|
50
|
+
export function renderApp(data, {
|
|
51
|
+
noScript = '',
|
|
52
|
+
selector = `[data-portlet-id="${portletId}"]`,
|
|
53
|
+
async = false,
|
|
54
|
+
defer = true,
|
|
55
|
+
} = {}) {
|
|
56
|
+
if (typeof data === 'string') {
|
|
57
|
+
throw new Error('As of version 2 renderApp no longer needs the application name.');
|
|
58
|
+
}
|
|
59
|
+
if (isOffline) {
|
|
60
|
+
return `
|
|
61
|
+
<div data-portlet-id="${portletId}">${noScript}</div>
|
|
62
|
+
<script>
|
|
63
|
+
(function(){
|
|
64
|
+
var targetElement = document.querySelector('[data-portlet-id="${portletId}"]');
|
|
65
|
+
var script = document.createElement('script');
|
|
66
|
+
script.src = '${getResourceUri('client/index.js')}?${portletId.replace('_', '')}';
|
|
67
|
+
script.setAttribute('data-app', '${encodeURIComponent(JSON.stringify({ ...data, isOffline }))}');
|
|
68
|
+
script.setAttribute('data-selector', '${encodeURIComponent(selector)}');
|
|
69
|
+
targetElement.parentNode.insertBefore(script, targetElement.nextSibling);
|
|
70
|
+
})();
|
|
71
|
+
</script>`;
|
|
72
|
+
}
|
|
73
|
+
return `
|
|
74
|
+
<div data-portlet-id="${portletId}">${noScript}</div>
|
|
75
|
+
<script
|
|
76
|
+
src="${getResourceUri('client/index.js')}?${appInfo.appImportDate}${portletId.replace('_', '')}"
|
|
77
|
+
data-app="${encodeURIComponent(JSON.stringify({ ...data, isOffline }))}"
|
|
78
|
+
data-selector="${encodeURIComponent(selector)}"
|
|
79
|
+
${async && !defer ? 'async' : ''}
|
|
80
|
+
${defer ? 'defer' : ''}>
|
|
81
|
+
</script>`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get URI for a route.
|
|
86
|
+
* @param {String} route A route.
|
|
87
|
+
* @returns {String} URI for route.
|
|
88
|
+
*/
|
|
89
|
+
export function getRouteUri(route) {
|
|
90
|
+
const currentPageId = PortletContextUtil.getCurrentPage().getIdentifier().replace('_sitePage', '');
|
|
91
|
+
const currentPortletId = PortletContextUtil.getCurrentPortlet().getIdentifier();
|
|
92
|
+
return `/appresource/${currentPageId}/${currentPortletId}/${route}`.replace(/\/\//g, '/');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export default {
|
|
96
|
+
getRouteUri,
|
|
97
|
+
getResourceUri,
|
|
98
|
+
renderApp,
|
|
99
|
+
renderTemplate,
|
|
100
|
+
isOffline,
|
|
101
|
+
};
|
package/dist/index.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import PortletContextUtil from 'PortletContextUtil';
|
|
2
|
-
import VersionUtil from 'VersionUtil';
|
|
3
|
-
import appInfo from 'appInfo';
|
|
4
|
-
/* Underscore is provided by SiteVision */
|
|
5
|
-
/* eslint-disable-next-line import/no-extraneous-dependencies */
|
|
6
|
-
import _ from 'underscore';
|
|
7
|
-
|
|
8
|
-
/** If the webapp is running in offline mode or not. */
|
|
9
|
-
export var isOffline = VersionUtil.getCurrentVersion() === VersionUtil.OFFLINE_VERSION;
|
|
10
|
-
|
|
11
|
-
var portletId = PortletContextUtil.getCurrentPortlet().getIdentifier().replace('.', '_');
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Get URI for a resource.
|
|
15
|
-
* @param {String} resource A resource.
|
|
16
|
-
* @returns {String} URI for a resource.
|
|
17
|
-
*/
|
|
18
|
-
export function getResourceUri(resource) {
|
|
19
|
-
return ("/webapp-files/" + (appInfo.appIdentifier) + "/" + (appInfo.appVersion) + "/" + resource);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Renders a Underscore template and returns a string.
|
|
24
|
-
* @param {String} template Underscore template.
|
|
25
|
-
* @param {Object} [values={}] Values.
|
|
26
|
-
* @returns {String} Rendered template
|
|
27
|
-
*/
|
|
28
|
-
export function renderTemplate(template, values) {
|
|
29
|
-
if ( values === void 0 ) values = {};
|
|
30
|
-
|
|
31
|
-
if (typeof template === 'function') {
|
|
32
|
-
return template(values);
|
|
33
|
-
}
|
|
34
|
-
return _.template(template)(values);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Get HTML string for rendering an application.
|
|
39
|
-
* @param {String} name Name of the app, has to match the name of the application.
|
|
40
|
-
* @param {Object} [data={}] Server data that will be available in the app.
|
|
41
|
-
* In Vue it will be available in the `$options` object.
|
|
42
|
-
* @param {Object} [settings={}] Settings object.
|
|
43
|
-
* @param {String} [settings.noScript=''] HTML that will be rendered when JavaScript
|
|
44
|
-
* is not available.
|
|
45
|
-
* @param {String} [settings.selector=`[data-portlet-id="${portletId}"]`] Query selector for
|
|
46
|
-
* where the app should be mounted.
|
|
47
|
-
* @param {Boolean} [settings.async=true] If the app script should be loaded asynchronously.
|
|
48
|
-
* [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
|
|
49
|
-
* @param {Boolean} [settings.defer=false] If the app script should be loaded after DOM is ready.
|
|
50
|
-
* [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
|
|
51
|
-
* @returns {String} HTML for rendering an application.
|
|
52
|
-
*/
|
|
53
|
-
export function renderApp(name, data, ref) {
|
|
54
|
-
if ( ref === void 0 ) ref = {};
|
|
55
|
-
var noScript = ref.noScript; if ( noScript === void 0 ) noScript = '';
|
|
56
|
-
var selector = ref.selector; if ( selector === void 0 ) selector = "[data-portlet-id=\"" + portletId + "\"]";
|
|
57
|
-
var async = ref.async; if ( async === void 0 ) async = true;
|
|
58
|
-
var defer = ref.defer; if ( defer === void 0 ) defer = false;
|
|
59
|
-
|
|
60
|
-
var options = Object.assign({}, data, {isOffline: isOffline});
|
|
61
|
-
var clientUri = getResourceUri('client/index.js');
|
|
62
|
-
|
|
63
|
-
if (isOffline) {
|
|
64
|
-
return ("\n<!-- Generated by WebappUtil.renderApp() -->\n<div data-portlet-id=\"" + portletId + "\">" + noScript + "</div>\n<script src=\"" + clientUri + "?" + (appInfo.appImportDate) + "\"></script>\n<script> \n Soleil.webapps['" + name + "'].render('" + name + "', '" + selector + "', " + (JSON.stringify(options)) + ");\n</script>\n");
|
|
65
|
-
}
|
|
66
|
-
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 + "?" + (appInfo.appImportDate) + "\" " + (async && !defer ? 'async' : '') + " " + (defer ? 'defer' : '') + " onload=\"renderApp" + portletId + "();\"></script>\n");
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Get URI for a route.
|
|
71
|
-
* @param {String} route A route.
|
|
72
|
-
* @returns {String} URI for route.
|
|
73
|
-
*/
|
|
74
|
-
export function getRouteUri(route) {
|
|
75
|
-
var currentPageId = PortletContextUtil.getCurrentPage().getIdentifier().replace('_sitePage', '');
|
|
76
|
-
var currentPortletId = PortletContextUtil.getCurrentPortlet().getIdentifier();
|
|
77
|
-
return ("/appresource/" + currentPageId + "/" + currentPortletId + "/" + route).replace(/\/\//g, '/');
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export default {
|
|
81
|
-
getRouteUri: getRouteUri,
|
|
82
|
-
getResourceUri: getResourceUri,
|
|
83
|
-
renderApp: renderApp,
|
|
84
|
-
renderTemplate: renderTemplate,
|
|
85
|
-
isOffline: isOffline,
|
|
86
|
-
};
|
package/render-vue/src/index.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/* eslint-disable-next-line import/no-unresolved */
|
|
2
|
-
import Vue from 'vue';
|
|
3
|
-
|
|
4
|
-
window.Soleil = window.Soleil || {};
|
|
5
|
-
window.Soleil.webapps = window.Soleil.webapps || {};
|
|
6
|
-
|
|
7
|
-
const offlineModeMixin = {
|
|
8
|
-
mounted() {
|
|
9
|
-
if (this.$options.isOffline) {
|
|
10
|
-
const $anchors = this.$el.querySelectorAll('a');
|
|
11
|
-
$anchors.forEach(($anchor) => {
|
|
12
|
-
$anchor.addEventListener('click', (e) => {
|
|
13
|
-
e.preventDefault();
|
|
14
|
-
window.$svjq($anchor).trigger('click');
|
|
15
|
-
}, false);
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Render a Vue application
|
|
22
|
-
* @param {String} name Name of the application, has to match both on server and client.
|
|
23
|
-
* @param {String} selector Selector for element where the app should be mounted.
|
|
24
|
-
* @param {Object} [options={}] Options for `$options`.
|
|
25
|
-
*/
|
|
26
|
-
export default function render(name, selector, options = {}) {
|
|
27
|
-
const {
|
|
28
|
-
App,
|
|
29
|
-
} = window.Soleil.webapps[name];
|
|
30
|
-
Object.assign(App, options, {
|
|
31
|
-
mixins: [offlineModeMixin],
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const mountElementExists = !!document.querySelector(selector);
|
|
35
|
-
if (mountElementExists) {
|
|
36
|
-
new Vue(App).$mount(selector);
|
|
37
|
-
} else {
|
|
38
|
-
document.addEventListener('DOMContentLoaded', () => {
|
|
39
|
-
new Vue(App).$mount(selector);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|